Cleaned up Session args

This commit is contained in:
Eric Froemling 2020-05-20 00:06:36 -07:00
parent 1ef716a2f2
commit acb38415eb
9 changed files with 57 additions and 71 deletions

View File

@ -30,8 +30,8 @@ if TYPE_CHECKING:
from typing import Any, List, Dict, Optional, Callable, Sequence
import ba
TEAM_COLORS = ((0.2, 0.4, 1.6), )
TEAM_NAMES = ('Good Guys', )
TEAM_COLORS = [(0.2, 0.4, 1.6)]
TEAM_NAMES = ['Good Guys']
class CoopSession(Session):
@ -43,6 +43,9 @@ class CoopSession(Session):
the computer and include functionality such as
high score lists.
"""
use_teams = True
use_team_colors = False
allow_mid_activity_joins = False
def __init__(self) -> None:
"""Instantiate a co-op mode session."""
@ -51,7 +54,6 @@ class CoopSession(Session):
from bastd.activity.coopjoin import CoopJoinActivity
_ba.increment_analytics_count('Co-op session start')
app = _ba.app
# If they passed in explicit min/max, honor that.
@ -63,14 +65,7 @@ class CoopSession(Session):
if 'max_players' in app.coop_session_args:
max_players = app.coop_session_args['max_players']
else:
try:
max_players = app.config['Coop Game Max Players']
except Exception:
# Old pref value.
try:
max_players = app.config['Challenge Game Max Players']
except Exception:
max_players = 4
max_players = app.config.get('Coop Game Max Players', 4)
# print('FIXME: COOP SESSION WOULD CALC DEPS.')
depsets: Sequence[ba.DependencySet] = []
@ -78,10 +73,8 @@ class CoopSession(Session):
super().__init__(depsets,
team_names=TEAM_NAMES,
team_colors=TEAM_COLORS,
use_team_colors=False,
min_players=min_players,
max_players=max_players,
allow_mid_activity_joins=False)
max_players=max_players)
# Tournament-ID if we correspond to a co-op tournament (otherwise None)
self.tournament_id = (app.coop_session_args['tournament_id']

View File

@ -24,18 +24,19 @@ from __future__ import annotations
from typing import TYPE_CHECKING
import _ba
from ba import _multiteamsession
from ba._multiteamsession import MultiTeamSession
if TYPE_CHECKING:
import ba
class DualTeamSession(_multiteamsession.MultiTeamSession):
class DualTeamSession(MultiTeamSession):
"""ba.Session type for teams mode games.
Category: Gameplay Classes
"""
_use_teams = True
use_teams = True
use_team_colors = True
_playlist_selection_var = 'Team Tournament Playlist Selection'
_playlist_randomize_var = 'Team Tournament Playlist Randomize'
_playlists_var = 'Team Tournament Playlists'

View File

@ -37,7 +37,8 @@ class FreeForAllSession(MultiTeamSession):
Category: Gameplay Classes
"""
_use_teams = False
use_teams = False
use_team_colors = True
_playlist_selection_var = 'Free-for-All Playlist Selection'
_playlist_randomize_var = 'Free-for-All Playlist Randomize'
_playlists_var = 'Free-for-All Playlists'

View File

@ -889,18 +889,16 @@ class Lobby:
player.reset()
def __init__(self) -> None:
from ba import _team as bs_team
from ba import _coopsession
from ba._team import SessionTeam
from ba._coopsession import CoopSession
session = _ba.getsession()
teams = session.teams if session.use_teams else None
self._use_team_colors = session.use_team_colors
if teams is not None:
self._teams = [weakref.ref(team) for team in teams]
if session.use_teams:
self._teams = [weakref.ref(team) for team in session.teams]
else:
self._dummy_teams = bs_team.SessionTeam()
self._dummy_teams = SessionTeam()
self._teams = [weakref.ref(self._dummy_teams)]
v_offset = (-150
if isinstance(session, _coopsession.CoopSession) else -50)
v_offset = (-150 if isinstance(session, CoopSession) else -50)
self.choosers: List[Chooser] = []
self.base_v_offset = v_offset
self.update_positions()

View File

@ -59,7 +59,7 @@ class MultiTeamSession(Session):
app = _ba.app
cfg = app.config
if self._use_teams:
if self.use_teams:
team_names = cfg.get('Custom Team Names', DEFAULT_TEAM_NAMES)
team_colors = cfg.get('Custom Team Colors', DEFAULT_TEAM_COLORS)
else:
@ -72,7 +72,6 @@ class MultiTeamSession(Session):
super().__init__(depsets,
team_names=team_names,
team_colors=team_colors,
use_team_colors=self._use_teams,
min_players=1,
max_players=self.get_max_players())
@ -107,7 +106,7 @@ class MultiTeamSession(Session):
# got it and we don't want that to affect our config.
playlist = copy.deepcopy(playlists[self._playlist_name])
else:
if self._use_teams:
if self.use_teams:
playlist = _playlist.get_default_teams_playlist()
else:
playlist = _playlist.get_default_free_for_all_playlist()
@ -161,7 +160,7 @@ class MultiTeamSession(Session):
def get_max_players(self) -> int:
"""Return max number of ba.Players allowed to join the game at once."""
if self._use_teams:
if self.use_teams:
return _ba.app.config.get('Team Game Max Players', 8)
return _ba.app.config.get('Free-for-All Max Players', 8)

View File

@ -75,6 +75,9 @@ class Session:
there is no associated Campaign.
"""
use_teams = False
use_team_colors = True
allow_mid_activity_joins = True
# Note: even though these are instance vars, we annotate them at the
# class level so that docs generation can access their types.
@ -89,10 +92,8 @@ class Session:
depsets: Sequence[ba.DependencySet],
team_names: Sequence[str] = None,
team_colors: Sequence[Sequence[float]] = None,
use_team_colors: bool = True,
min_players: int = 1,
max_players: int = 8,
allow_mid_activity_joins: bool = True):
max_players: int = 8):
"""Instantiate a session.
depsets should be a sequence of successfully resolved ba.DependencySet
@ -154,16 +155,12 @@ class Session:
self.campaign = None
self.campaign_state: Dict[str, str] = {}
self._use_teams = (team_names is not None)
self._use_team_colors = use_team_colors
self._in_set_activity = False
self._allow_mid_activity_joins = allow_mid_activity_joins
self.teams = []
self.players = []
self._in_set_activity = False
self._next_team_id = 0
self._activity_retained: Optional[ba.Activity] = None
self.launch_end_session_activity_time: Optional[float] = None
self._launch_end_session_activity_time: Optional[float] = None
self._activity_end_timer: Optional[ba.Timer] = None
self._activity_weak = empty_weakref(Activity)
if self._activity_weak() is not None:
@ -175,8 +172,8 @@ class Session:
self.min_players = min_players
self.max_players = max_players
# Create Teams.
if self._use_teams:
# Create static teams if we're using them.
if self.use_teams:
assert team_names is not None
assert team_colors is not None
for i, color in enumerate(team_colors):
@ -199,16 +196,6 @@ class Session:
# Instantiate our session globals node which will apply its settings.
sharedobj('globals')
@property
def use_teams(self) -> bool:
"""(internal)"""
return self._use_teams
@property
def use_team_colors(self) -> bool:
"""(internal)"""
return self._use_team_colors
def on_player_request(self, player: ba.SessionPlayer) -> bool:
"""Called when a new ba.Player wants to join the Session.
@ -284,7 +271,7 @@ class Session:
print('Player not found in Activity in on_player_leave.')
# If we're a non-team session, remove their team too.
if not self._use_teams:
if not self.use_teams:
# They should have been the only one on their team.
assert not sessionteam.players
@ -335,14 +322,14 @@ class Session:
curtime = _ba.time(TimeType.REAL)
if self._ending:
# Ignore repeats unless its been a while.
assert self.launch_end_session_activity_time is not None
since_last = (curtime - self.launch_end_session_activity_time)
assert self._launch_end_session_activity_time is not None
since_last = (curtime - self._launch_end_session_activity_time)
if since_last < 30.0:
return
print_error(
'launch_end_session_activity called twice (since_last=' +
str(since_last) + ')')
self.launch_end_session_activity_time = curtime
self._launch_end_session_activity_time = curtime
self.set_activity(_ba.new_activity(EndSessionActivity))
self.wants_to_end = False
self._ending = True # Prevent further actions.
@ -603,7 +590,7 @@ class Session:
# If we're not allowing mid-game joins, don't pass; just announce
# the arrival and say they'll partake next round.
if pass_to_activity:
if not self._allow_mid_activity_joins:
if not self.allow_mid_activity_joins:
pass_to_activity = False
with _ba.Context(self):
_ba.screenmessage(
@ -615,7 +602,7 @@ class Session:
# If we're a non-team session, each player gets their own team.
# (keeps mini-game coding simpler if we can always deal with teams).
if self._use_teams:
if self.use_teams:
sessionteam = chooser.get_team()
else:
our_team_id = self._next_team_id

View File

@ -34,8 +34,19 @@ if TYPE_CHECKING:
from typing import Any, Type, List, Dict, Tuple, Union, Sequence
class Player(ba.Player['Team']):
"""Our player type for this game."""
class Team(ba.Team[Player]):
"""Our team type for this game."""
def __init__(self) -> None:
pass
# ba_meta export game
class DeathMatchGame(ba.TeamGameActivity[ba.Player, ba.Team]):
class DeathMatchGame(ba.TeamGameActivity[Player, Team]):
"""A game type based on acquiring kills."""
name = 'Death Match'
@ -92,12 +103,15 @@ class DeathMatchGame(ba.TeamGameActivity[ba.Player, ba.Team]):
def __init__(self, settings: Dict[str, Any]):
from bastd.actor.scoreboard import Scoreboard
super().__init__(settings)
if self.settings_raw['Epic Mode']:
self.slow_motion = True
self._scoreboard = Scoreboard()
self._score_to_win = None
self._dingsound = ba.getsound('dingSmall')
self._epic_mode = bool(settings['Epic Mode'])
# Base class overrides.
self.slow_motion = self._epic_mode
self.default_music = (ba.MusicType.EPIC if self._epic_mode else
ba.MusicType.TO_THE_DEATH)
def get_instance_description(self) -> Union[str, Sequence]:
return 'Crush ${ARG1} of your enemies.', self._score_to_win
@ -105,13 +119,7 @@ class DeathMatchGame(ba.TeamGameActivity[ba.Player, ba.Team]):
def get_instance_description_short(self) -> Union[str, Sequence]:
return 'kill ${ARG1} enemies', self._score_to_win
def on_transition_in(self) -> None:
self.default_music = (ba.MusicType.EPIC
if self.settings_raw['Epic Mode'] else
ba.MusicType.TO_THE_DEATH)
super().on_transition_in()
def on_team_join(self, team: ba.Team) -> None:
def on_team_join(self, team: Team) -> None:
team.gamedata['score'] = 0
if self.has_begun():
self._update_scoreboard()

View File

@ -242,7 +242,6 @@ class MeteorShowerGame(ba.TeamGameActivity[Player, Team]):
# (these per-player scores are only meaningful in team-games)
for team in self.teams:
for player in team.players:
survived = False
# Throw an extra fudge factor in so teams that

View File

@ -1,5 +1,5 @@
<!-- THIS FILE IS AUTO GENERATED; DO NOT EDIT BY HAND -->
<h4><em>last updated on 2020-05-19 for Ballistica version 1.5.0 build 20023</em></h4>
<h4><em>last updated on 2020-05-20 for Ballistica version 1.5.0 build 20023</em></h4>
<p>This page documents the Python classes and functions in the 'ba' module,
which are the ones most relevant to modding in Ballistica. If you come across something you feel should be included here or could be better explained, please <a href="mailto:support@froemling.net">let me know</a>. Happy modding!</p>
<hr>
@ -4145,7 +4145,7 @@ list in <a href="#class_ba_Activity">ba.Activity</a>; not this.</p>
<h5><a href="#method_ba_Session____init__">&lt;constructor&gt;</a>, <a href="#method_ba_Session__begin_next_activity">begin_next_activity()</a>, <a href="#method_ba_Session__end">end()</a>, <a href="#method_ba_Session__end_activity">end_activity()</a>, <a href="#method_ba_Session__get_custom_menu_entries">get_custom_menu_entries()</a>, <a href="#method_ba_Session__getactivity">getactivity()</a>, <a href="#method_ba_Session__handlemessage">handlemessage()</a>, <a href="#method_ba_Session__on_activity_end">on_activity_end()</a>, <a href="#method_ba_Session__on_player_leave">on_player_leave()</a>, <a href="#method_ba_Session__on_player_request">on_player_request()</a>, <a href="#method_ba_Session__on_team_join">on_team_join()</a>, <a href="#method_ba_Session__on_team_leave">on_team_leave()</a>, <a href="#method_ba_Session__set_activity">set_activity()</a></h5>
<dl>
<dt><h4><a name="method_ba_Session____init__">&lt;constructor&gt;</a></dt></h4><dd>
<p><span>ba.Session(depsets: Sequence[<a href="#class_ba_DependencySet">ba.DependencySet</a>], team_names: Sequence[str] = None, team_colors: Sequence[Sequence[float]] = None, use_team_colors: bool = True, min_players: int = 1, max_players: int = 8, allow_mid_activity_joins: bool = True)</span></p>
<p><span>ba.Session(depsets: Sequence[<a href="#class_ba_DependencySet">ba.DependencySet</a>], team_names: Sequence[str] = None, team_colors: Sequence[Sequence[float]] = None, min_players: int = 1, max_players: int = 8)</span></p>
<p>Instantiate a session.</p>