fix quick custom game

This commit is contained in:
brostosjoined 2024-01-20 22:05:35 +03:00
parent c0fac8061a
commit 06d63d907a

View file

@ -21,17 +21,17 @@ if TYPE_CHECKING:
lang = bs.app.lang.language lang = bs.app.lang.language
if lang == 'Spanish': if lang == "Spanish":
custom_txt = 'personalizar...' custom_txt = "personalizar..."
else: else:
custom_txt = 'custom...' custom_txt = "custom..."
if 'quick_game_button' in babase.app.config: if "quick_game_button" in babase.app.config:
config = babase.app.config['quick_game_button'] config = babase.app.config["quick_game_button"]
else: else:
config = {'selected': None, 'config': None} config = {"selected": None, "config": None}
babase.app.config['quick_game_button'] = config babase.app.config["quick_game_button"] = config
babase.app.config.commit() babase.app.config.commit()
@ -43,7 +43,8 @@ def start_game(session: bs.Session, fadeout: bool = True):
bs.new_host_session(session) bs.new_host_session(session)
except Exception: except Exception:
from bascenev1lib import mainmenu from bascenev1lib import mainmenu
babase.print_exception('exception running session', session)
babase.print_exception("exception running session", session)
# Drop back into a main menu session. # Drop back into a main menu session.
bs.new_host_session(mainmenu.MainMenuSession) bs.new_host_session(mainmenu.MainMenuSession)
@ -56,33 +57,31 @@ def start_game(session: bs.Session, fadeout: bool = True):
class SimplePlaylist: class SimplePlaylist:
def __init__(self, settings: dict, gametype: type[bs.GameActivity]):
def __init__(self,
settings: dict,
gametype: type[bs.GameActivity]):
self.settings = settings self.settings = settings
self.gametype = gametype self.gametype = gametype
def pull_next(self) -> None: def pull_next(self) -> None:
if 'map' not in self.settings['settings']: if "map" not in self.settings["settings"]:
settings = dict( settings = dict(map=self.settings["map"], **self.settings["settings"])
map=self.settings['map'], **self.settings['settings'])
else: else:
settings = self.settings['settings'] settings = self.settings["settings"]
return dict(resolved_type=self.gametype, settings=settings) return dict(resolved_type=self.gametype, settings=settings)
class CustomSession(FreeForAllSession): class CustomSession(FreeForAllSession):
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
# pylint: disable=cyclic-import # pylint: disable=cyclic-import
self.use_teams = False self.use_teams = False
self._tutorial_activity_instance = None self._tutorial_activity_instance = None
bs.Session.__init__(self, depsets=[], bs.Session.__init__(
team_names=None, self,
team_colors=None, depsets=[],
min_players=1, team_names=None,
max_players=self.get_max_players()) team_colors=None,
min_players=1,
max_players=self.get_max_players(),
)
self._series_length = 1 self._series_length = 1
self._ffa_series_length = 1 self._ffa_series_length = 1
@ -90,15 +89,14 @@ class CustomSession(FreeForAllSession):
# Which game activity we're on. # Which game activity we're on.
self._game_number = 0 self._game_number = 0
self._playlist = SimplePlaylist(self._config, self._gametype) self._playlist = SimplePlaylist(self._config, self._gametype)
config['selected'] = self._gametype.__name__ config["selected"] = self._gametype.__name__
config['config'] = self._config config["config"] = self._config
babase.app.config.commit() babase.app.config.commit()
# Get a game on deck ready to go. # Get a game on deck ready to go.
self._current_game_spec: Optional[Dict[str, Any]] = None self._current_game_spec: Optional[Dict[str, Any]] = None
self._next_game_spec: Dict[str, Any] = self._playlist.pull_next() self._next_game_spec: Dict[str, Any] = self._playlist.pull_next()
self._next_game: Type[bs.GameActivity] = ( self._next_game: Type[bs.GameActivity] = self._next_game_spec["resolved_type"]
self._next_game_spec['resolved_type'])
# Go ahead and instantiate the next game we'll # Go ahead and instantiate the next game we'll
# use so it has lots of time to load. # use so it has lots of time to load.
@ -109,8 +107,7 @@ class CustomSession(FreeForAllSession):
class SelectGameWindow(PlaylistAddGameWindow): class SelectGameWindow(PlaylistAddGameWindow):
def __init__(self, transition: str = "in_right"):
def __init__(self, transition: str = 'in_right'):
class EditController: class EditController:
_sessiontype = bs.FreeForAllSession _sessiontype = bs.FreeForAllSession
@ -118,32 +115,44 @@ class SelectGameWindow(PlaylistAddGameWindow):
return self._sessiontype return self._sessiontype
self._editcontroller = EditController() self._editcontroller = EditController()
self._r = 'addGameWindow' self._r = "addGameWindow"
uiscale = bui.app.ui_v1.uiscale uiscale = bui.app.ui_v1.uiscale
self._width = 750 if uiscale is babase.UIScale.SMALL else 650 self._width = 750 if uiscale is babase.UIScale.SMALL else 650
x_inset = 50 if uiscale is babase.UIScale.SMALL else 0 x_inset = 50 if uiscale is babase.UIScale.SMALL else 0
self._height = (346 if uiscale is babase.UIScale.SMALL else self._height = (
380 if uiscale is babase.UIScale.MEDIUM else 440) 346
if uiscale is babase.UIScale.SMALL
else 380
if uiscale is babase.UIScale.MEDIUM
else 440
)
top_extra = 30 if uiscale is babase.UIScale.SMALL else 20 top_extra = 30 if uiscale is babase.UIScale.SMALL else 20
self._scroll_width = 210 self._scroll_width = 210
self._root_widget = bui.containerwidget( self._root_widget = bui.containerwidget(
size=(self._width, self._height + top_extra), size=(self._width, self._height + top_extra),
transition=transition, transition=transition,
scale=(2.17 if uiscale is babase.UIScale.SMALL else scale=(
1.5 if uiscale is babase.UIScale.MEDIUM else 1.0), 2.17
stack_offset=(0, 1) if uiscale is babase.UIScale.SMALL else (0, 0)) if uiscale is babase.UIScale.SMALL
else 1.5
if uiscale is babase.UIScale.MEDIUM
else 1.0
),
stack_offset=(0, 1) if uiscale is babase.UIScale.SMALL else (0, 0),
)
self._back_button = bui.buttonwidget(parent=self._root_widget, self._back_button = bui.buttonwidget(
position=(58 + x_inset, parent=self._root_widget,
self._height - 53), position=(58 + x_inset, self._height - 53),
size=(165, 70), size=(165, 70),
scale=0.75, scale=0.75,
text_scale=1.2, text_scale=1.2,
label=babase.Lstr(resource='backText'), label=babase.Lstr(resource="backText"),
autoselect=True, autoselect=True,
button_type='back', button_type="back",
on_activate_call=self._back) on_activate_call=self._back,
)
self._select_button = select_button = bui.buttonwidget( self._select_button = select_button = bui.buttonwidget(
parent=self._root_widget, parent=self._root_widget,
position=(self._width - (172 + x_inset), self._height - 50), position=(self._width - (172 + x_inset), self._height - 50),
@ -151,22 +160,26 @@ class SelectGameWindow(PlaylistAddGameWindow):
size=(160, 60), size=(160, 60),
scale=0.75, scale=0.75,
text_scale=1.2, text_scale=1.2,
label=babase.Lstr(resource='selectText'), label=babase.Lstr(resource="selectText"),
on_activate_call=self._add) on_activate_call=self._add,
)
if bui.app.ui_v1.use_toolbars: if bui.app.ui_v1.use_toolbars:
bui.widget(edit=select_button, bui.widget(
right_widget=bui.get_special_widget('party_button')) edit=select_button, right_widget=bui.get_special_widget("party_button")
)
bui.textwidget(parent=self._root_widget, bui.textwidget(
position=(self._width * 0.5, self._height - 28), parent=self._root_widget,
size=(0, 0), position=(self._width * 0.5, self._height - 28),
scale=1.0, size=(0, 0),
text=babase.Lstr(resource=self._r + '.titleText'), scale=1.0,
h_align='center', text=babase.Lstr(resource=self._r + ".titleText"),
color=bui.app.ui_v1.title_color, h_align="center",
maxwidth=250, color=bui.app.ui_v1.title_color,
v_align='center') maxwidth=250,
v_align="center",
)
v = self._height - 64 v = self._height - 64
self._selected_title_text = bui.textwidget( self._selected_title_text = bui.textwidget(
@ -176,8 +189,9 @@ class SelectGameWindow(PlaylistAddGameWindow):
scale=1.0, scale=1.0,
color=(0.7, 1.0, 0.7, 1.0), color=(0.7, 1.0, 0.7, 1.0),
maxwidth=self._width - self._scroll_width - 150 - x_inset * 2, maxwidth=self._width - self._scroll_width - 150 - x_inset * 2,
h_align='left', h_align="left",
v_align='center') v_align="center",
)
v -= 30 v -= 30
self._selected_description_text = bui.textwidget( self._selected_description_text = bui.textwidget(
@ -187,111 +201,115 @@ class SelectGameWindow(PlaylistAddGameWindow):
scale=0.7, scale=0.7,
color=(0.5, 0.8, 0.5, 1.0), color=(0.5, 0.8, 0.5, 1.0),
maxwidth=self._width - self._scroll_width - 150 - x_inset * 2, maxwidth=self._width - self._scroll_width - 150 - x_inset * 2,
h_align='left') h_align="left",
)
scroll_height = self._height - 100 scroll_height = self._height - 100
v = self._height - 60 v = self._height - 60
self._scrollwidget = bui.scrollwidget(parent=self._root_widget, self._scrollwidget = bui.scrollwidget(
position=(x_inset + 61, parent=self._root_widget,
v - scroll_height), position=(x_inset + 61, v - scroll_height),
size=(self._scroll_width, size=(self._scroll_width, scroll_height),
scroll_height), highlight=False,
highlight=False) )
bui.widget(edit=self._scrollwidget, bui.widget(
up_widget=self._back_button, edit=self._scrollwidget,
left_widget=self._back_button, up_widget=self._back_button,
right_widget=select_button) left_widget=self._back_button,
right_widget=select_button,
)
self._column: Optional[bui.Widget] = None self._column: Optional[bui.Widget] = None
v -= 35 v -= 35
bui.containerwidget(edit=self._root_widget, bui.containerwidget(
cancel_button=self._back_button, edit=self._root_widget,
start_button=select_button) cancel_button=self._back_button,
start_button=select_button,
)
self._selected_game_type: Optional[Type[bs.GameActivity]] = None self._selected_game_type: Optional[Type[bs.GameActivity]] = None
bui.containerwidget(edit=self._root_widget, bui.containerwidget(edit=self._root_widget, selected_child=self._scrollwidget)
selected_child=self._scrollwidget)
self._game_types: list[type[bs.GameActivity]] = [] self._game_types: list[type[bs.GameActivity]] = []
# Get actual games loading in the bg. # Get actual games loading in the bg.
babase.app.meta.load_exported_classes(bs.GameActivity, babase.app.meta.load_exported_classes(
self._on_game_types_loaded, bs.GameActivity, self._on_game_types_loaded, completion_cb_in_bg_thread=True
completion_cb_in_bg_thread=True) )
# Refresh with our initial empty list. We'll refresh again once # Refresh with our initial empty list. We'll refresh again once
# game loading is complete. # game loading is complete.
self._refresh() self._refresh()
if config['selected']: if config["selected"]:
for gt in self._game_types: for gt in self._game_types:
if gt.__name__ == config['selected']: if gt.__name__ == config["selected"]:
self._refresh(selected=gt) self._refresh(selected=gt)
self._set_selected_game_type(gt) self._set_selected_game_type(gt)
def _refresh(self, def _refresh(
select_get_more_games_button: bool = False, self, select_get_more_games_button: bool = False, selected: bool = None
selected: bool = None) -> None: ) -> None:
# from babase.internal import get_game_types # from babase.internal import get_game_types
if self._column is not None: if self._column is not None:
self._column.delete() self._column.delete()
self._column = bui.columnwidget(parent=self._scrollwidget, self._column = bui.columnwidget(parent=self._scrollwidget, border=2, margin=0)
border=2,
margin=0)
for i, gametype in enumerate(self._game_types): for i, gametype in enumerate(self._game_types):
def _doit() -> None: def _doit() -> None:
if self._select_button: if self._select_button:
bs.apptimer(0.1, bs.apptimer(0.1, self._select_button.activate)
self._select_button.activate)
txt = bui.textwidget(parent=self._column, txt = bui.textwidget(
position=(0, 0), parent=self._column,
size=(self._width - 88, 24), position=(0, 0),
text=gametype.get_display_string(), size=(self._width - 88, 24),
h_align='left', text=gametype.get_display_string(),
v_align='center', h_align="left",
color=(0.8, 0.8, 0.8, 1.0), v_align="center",
maxwidth=self._scroll_width * 0.8, color=(0.8, 0.8, 0.8, 1.0),
on_select_call=babase.Call( maxwidth=self._scroll_width * 0.8,
self._set_selected_game_type, gametype), on_select_call=babase.Call(self._set_selected_game_type, gametype),
always_highlight=True, always_highlight=True,
selectable=True, selectable=True,
on_activate_call=_doit) on_activate_call=_doit,
)
if i == 0: if i == 0:
bui.widget(edit=txt, up_widget=self._back_button) bui.widget(edit=txt, up_widget=self._back_button)
self._get_more_games_button = bui.buttonwidget( self._get_more_games_button = bui.buttonwidget(
parent=self._column, parent=self._column,
autoselect=True, autoselect=True,
label=babase.Lstr(resource=self._r + '.getMoreGamesText'), label=babase.Lstr(resource=self._r + ".getMoreGamesText"),
color=(0.54, 0.52, 0.67), color=(0.54, 0.52, 0.67),
textcolor=(0.7, 0.65, 0.7), textcolor=(0.7, 0.65, 0.7),
on_activate_call=self._on_get_more_games_press, on_activate_call=self._on_get_more_games_press,
size=(178, 50)) size=(178, 50),
)
if select_get_more_games_button: if select_get_more_games_button:
bui.containerwidget(edit=self._column, bui.containerwidget(
selected_child=self._get_more_games_button, edit=self._column,
visible_child=self._get_more_games_button) selected_child=self._get_more_games_button,
visible_child=self._get_more_games_button,
)
def _add(self) -> None: def _add(self) -> None:
_babase.lock_all_input() # Make sure no more commands happen. _babase.lock_all_input() # Make sure no more commands happen.
bs.apptimer(0.1, _babase.unlock_all_input) bs.apptimer(0.1, _babase.unlock_all_input)
gameconfig = {} gameconfig = {}
if config['selected'] == self._selected_game_type.__name__: if config["selected"] == self._selected_game_type.__name__:
if config['config']: if config["config"]:
gameconfig = config['config'] gameconfig = config["config"]
if 'map' in gameconfig: if "map" in gameconfig:
gameconfig['settings']['map'] = gameconfig.pop('map') gameconfig["settings"]["map"] = gameconfig.pop("map")
self._selected_game_type.create_settings_ui( self._selected_game_type.create_settings_ui(
self._editcontroller.get_session_type(), self._editcontroller.get_session_type(), gameconfig, self._edit_game_done
gameconfig, )
self._edit_game_done)
def _edit_game_done(self, config: Optional[Dict[str, Any]]) -> None: def _edit_game_done(self, config: Optional[Dict[str, Any]]) -> None:
if config: if config:
@ -299,14 +317,21 @@ class SelectGameWindow(PlaylistAddGameWindow):
CustomSession._gametype = self._selected_game_type CustomSession._gametype = self._selected_game_type
start_game(CustomSession) start_game(CustomSession)
else: else:
bui.app.ui_v1.clear_main_menu_window(transition='out_right') bui.app.ui_v1.clear_main_menu_window(transition="out_right")
bui.app.ui_v1.set_main_menu_window( bui.app.ui_v1.set_main_menu_window(
SelectGameWindow(transition='in_left').get_root_widget()) SelectGameWindow(transition="in_left").get_root_widget(),
from_window=None,
)
def _back(self) -> None: def _back(self) -> None:
bui.containerwidget(edit=self._root_widget, transition='out_right') if not self._root_widget or self._root_widget.transitioning_out:
return
bui.containerwidget(edit=self._root_widget, transition="out_right")
bui.app.ui_v1.set_main_menu_window( bui.app.ui_v1.set_main_menu_window(
PlayWindow(transition='in_left').get_root_widget()) PlayWindow(transition="in_left").get_root_widget(),
from_window=self._root_widget,
)
PlayWindow._old_init = PlayWindow.__init__ PlayWindow._old_init = PlayWindow.__init__
@ -319,10 +344,14 @@ def __init__(self, *args, **kwargs):
height = 550 height = 550
def do_quick_game() -> None: def do_quick_game() -> None:
if not self._root_widget or self._root_widget.transitioning_out:
return
self._save_state() self._save_state()
bui.containerwidget(edit=self._root_widget, transition='out_left') bui.containerwidget(edit=self._root_widget, transition="out_left")
bui.app.ui_v1.set_main_menu_window( bui.app.ui_v1.set_main_menu_window(
SelectGameWindow().get_root_widget()) SelectGameWindow().get_root_widget(), from_window=self._root_widget
)
self._quick_game_button = bui.buttonwidget( self._quick_game_button = bui.buttonwidget(
parent=self._root_widget, parent=self._root_widget,
@ -334,46 +363,44 @@ def __init__(self, *args, **kwargs):
label=custom_txt, label=custom_txt,
on_activate_call=do_quick_game, on_activate_call=do_quick_game,
color=(0.54, 0.52, 0.67), color=(0.54, 0.52, 0.67),
textcolor=(0.7, 0.65, 0.7)) textcolor=(0.7, 0.65, 0.7),
)
self._restore_state() self._restore_state()
def states(self) -> None: def states(self) -> None:
return { return {
'Team Games': self._teams_button, "Team Games": self._teams_button,
'Co-op Games': self._coop_button, "Co-op Games": self._coop_button,
'Free-for-All Games': self._free_for_all_button, "Free-for-All Games": self._free_for_all_button,
'Back': self._back_button, "Back": self._back_button,
'Quick Game': self._quick_game_button "Quick Game": self._quick_game_button,
} }
def _save_state(self) -> None: def _save_state(self) -> None:
swapped = {v: k for k, v in states(self).items()} swapped = {v: k for k, v in states(self).items()}
if self._root_widget.get_selected_child() in swapped: if self._root_widget.get_selected_child() in swapped:
bui.app.ui_v1.window_states[ bui.app.ui_v1.window_states[self.__class__.__name__] = swapped[
self.__class__.__name__] = swapped[ self._root_widget.get_selected_child()
self._root_widget.get_selected_child()] ]
else: else:
babase.print_exception(f'Error saving state for {self}.') babase.print_exception(f"Error saving state for {self}.")
def _restore_state(self) -> None: def _restore_state(self) -> None:
if not hasattr(self, '_quick_game_button'): if not hasattr(self, "_quick_game_button"):
return # ensure that our monkey patched init ran return # ensure that our monkey patched init ran
if self.__class__.__name__ not in bui.app.ui_v1.window_states: if self.__class__.__name__ not in bui.app.ui_v1.window_states:
bui.containerwidget(edit=self._root_widget, bui.containerwidget(edit=self._root_widget, selected_child=self._coop_button)
selected_child=self._coop_button)
return return
sel = states(self).get( sel = states(self).get(bui.app.ui_v1.window_states[self.__class__.__name__], None)
bui.app.ui_v1.window_states[self.__class__.__name__], None)
if sel: if sel:
bui.containerwidget(edit=self._root_widget, selected_child=sel) bui.containerwidget(edit=self._root_widget, selected_child=sel)
else: else:
bui.containerwidget(edit=self._root_widget, bui.containerwidget(edit=self._root_widget, selected_child=self._coop_button)
selected_child=self._coop_button) babase.print_exception(f"Error restoring state for {self}.")
babase.print_exception(f'Error restoring state for {self}.')
# ba_meta export plugin # ba_meta export plugin