def _make_game_from_db_row(row: dict) -> models.Game: game_result = models.GameResult(white_score=row[Games.c.white_score], black_score=row[Games.c.black_score]) return models.Game(game_id=row[Games.c.game_id], white_id=row[Games.c.white_id], black_id=row[Games.c.black_id], result=game_result)
def parse_game_finished_event_as_game(data: dict) -> models.Game: result = models.GameResult(data['white_score'], data['black_score']) return models.Game( game_id=data['game_id'], white_id=data['white_id'], black_id=data['black_id'], result=result)
async def show_game(request: web.Request) -> ta.DictOrResponse: white, black = await _find_two_random_functions_or_error( request.app['db_engine']) game = models.Game(game_id=uuid.uuid4().hex, white_id=white.function_id, black_id=black.function_id, result=models.UNKNOWN_RESULT) return { 'game': game, 'white': white, 'black': black, 'has_already_visited': (_COOKIE_VISITED in request.cookies), }
async def _get_game_or_error(request, data) -> models.Game: try: parsed_input = _PostGameInput.schema(dict(data)) except vol.Invalid: loggers.web.info('bad request for post_game', exc_info=True) raise web.HTTPBadRequest keys = _PostGameInput.Keys try: result = models.GameResult(white_score=parsed_input[keys.white_score], black_score=parsed_input[keys.black_score]) return models.Game(game_id=request.match_info['game_id'], white_id=parsed_input[keys.white_id], black_id=parsed_input[keys.black_id], result=result) except (models.ResultError, models.GameError): loggers.web.info('bad request for post_game', exc_info=True) raise web.HTTPBadRequest
def _make_post_game_data(white_id='905', black_id='1005', white_score='1', black_score='0', game_hash=None): if game_hash is None: # noinspection PyTypeChecker game = models.Game( game_id='some_game_id', white_id=white_id, black_id=black_id, # unknown result because we're only interested in .get_hash() result result=models.UNKNOWN_RESULT) salt = cfg.get_config().game_hash_salt game_hash = game.get_hash(salt) return { 'white_id': white_id, 'black_id': black_id, 'white_score': white_score, 'black_score': black_score, 'hash': game_hash, }
def _get_game(white_id=666, black_id=777, result=models.BLACK_WINS_RESULT): return models.Game(game_id='some_game_id', white_id=white_id, black_id=black_id, result=result)