def _duplicate_session(sio: ServerApp, session: GameSession, new_title: str): _verify_has_admin(sio, session.id, None) current_user = sio.get_current_user() _add_audit_entry(sio, session, f"Duplicated session as {new_title}") with database.db.atomic(): new_session: GameSession = GameSession.create( name=new_title, password=session.password, creator=current_user, layout_description_json=session.layout_description_json, seed_hash=session.seed_hash, dev_features=session.dev_features, ) for preset in session.presets: assert isinstance(preset, GameSessionPreset) GameSessionPreset.create( session=new_session, row=preset.row, preset=preset.preset, ) GameSessionMembership.create( user=current_user, session=new_session, row=None, admin=True, connection_state="Offline", ) GameSessionAudit.create( session=new_session, user=current_user, message=f"Duplicated from {session.name}", )
def _create_row(sio: ServerApp, session: GameSession, preset_json: dict): _verify_has_admin(sio, session.id, None) _verify_in_setup(session) _verify_no_layout_description(session) preset = _get_preset(preset_json) new_row_id = session.num_rows with database.db.atomic(): GameSessionPreset.create(session=session, row=new_row_id, preset=json.dumps(preset.as_json))
def _delete_row(sio: ServerApp, session: GameSession, row_id: int): _verify_has_admin(sio, session.id, None) _verify_in_setup(session) _verify_no_layout_description(session) if session.num_rows < 2: raise InvalidAction("Can't delete row when there's only one") if row_id != session.num_rows - 1: raise InvalidAction(f"Can only delete the last row") with database.db.atomic(): GameSessionPreset.delete().where( GameSessionPreset.session == session, GameSessionPreset.row == row_id).execute() GameSessionMembership.update(row=None).where( GameSessionMembership.session == session.id, GameSessionMembership.row == row_id, ).execute()
def create_game_session(sio: ServerApp, session_name: str): current_user = sio.get_current_user() with database.db.atomic(): new_session = GameSession.create( name=session_name, password=None, creator=current_user, ) GameSessionPreset.create( session=new_session, row=0, preset=json.dumps(PresetManager(None).default_preset.as_json)) membership = GameSessionMembership.create(user=sio.get_current_user(), session=new_session, row=0, admin=True) sio.join_game_session(membership) return new_session.create_session_entry()
def _change_row(sio: ServerApp, session: GameSession, arg: Tuple[int, dict]): if len(arg) != 2: raise InvalidAction("Missing arguments.") row_id, preset_json = arg _verify_has_admin(sio, session.id, sio.get_current_user().id) _verify_in_setup(session) _verify_no_layout_description(session) preset = _get_preset(preset_json) try: with database.db.atomic(): preset_row = GameSessionPreset.get( GameSessionPreset.session == session, GameSessionPreset.row == row_id) preset_row.preset = json.dumps(preset.as_json) preset_row.save() except peewee.DoesNotExist: raise InvalidAction(f"invalid row: {row_id}")
def _change_row(sio: ServerApp, session: GameSession, arg: Tuple[int, dict]): if len(arg) != 2: raise InvalidAction("Missing arguments.") row_id, preset_json = arg _verify_has_admin(sio, session.id, sio.get_current_user().id) _verify_in_setup(session) _verify_no_layout_description(session) preset = _get_preset(preset_json) # if preset.game != RandovaniaGame.PRIME2: # raise InvalidAction("Only Prime 2 presets allowed.") try: with database.db.atomic(): preset_row = GameSessionPreset.get( GameSessionPreset.session == session, GameSessionPreset.row == row_id) preset_row.preset = json.dumps(preset.as_json) logger().info(f"Session {session.id}: Changing row {row_id}.") preset_row.save() except peewee.DoesNotExist: raise InvalidAction(f"invalid row: {row_id}")