async def add_round(request, persistence): """Add a round to the game.""" game_id = request.match_info['game_id'] json = await request.json(loads=loads_or_empty) try: round_name = json['round_name'] except KeyError: return json_response({'error': 'Must specify the name.'}, status=400) if len(round_name) < 1: return json_response({'error': 'The name must not be empty.'}, status=400) user_session = await get_session(request) if not client_owns_game(game_id, user_session, persistence): return json_response({'error': 'The user is not the moderator of this game.'}, status=403) try: persistence.add_round(game_id, round_name) except RoundExists: return json_response({'error': 'Round with this name already exists.'}, status=409) # No point to catch NoSuchGame because we cannot sensibly handle situation when there is a game # in a session but not in the storage. Let's better 500. return json_response({'game': persistence.serialize_game(game_id)})
async def add_game(request, persistence): """ Create a new game. The user will become the moderator of the game. """ json = await request.json(loads=loads_or_empty) try: available_cards = json['cards'] except KeyError: return json_response({'error': 'No card set provided.'}, status=400) moderator_name = json.get('moderator_name', '') if moderator_name == '': return json_response({'error': 'Moderator name not provided.'}, status=400) if len(available_cards) < 2: return json_response({'error': 'Cannot play with less than 2 cards.'}, status=400) moderator_session = await get_session(request) # Get or assign the moderator id: moderator_id = get_or_assign_id(moderator_session) game_id = get_random_id() persistence.add_game(game_id, moderator_id, moderator_name, coerce_cards(available_cards)) return json_response({'game_id': game_id, 'game': persistence.serialize_game(game_id)})
async def add_poll(request, persistence): """Add a poll to a round.""" game_id = request.match_info['game_id'] round_name = request.match_info['round_name'] user_session = await get_session(request) if not client_owns_game(game_id, user_session, persistence): return json_response({'error': 'The user is not the moderator of this game.'}, status=403) try: persistence.add_poll(game_id, round_name) except NoSuchRound: return json_response({'error': 'Round does not exist.'}, status=404) except RoundFinalized: return json_response({'error': 'This round is finalized.'}, status=409) return json_response({'game': persistence.serialize_game(game_id)})
def test_json_response_argument_passing(): """Check if ``json_response`` passes keyword args to ``aiohttp.web.Response``.""" status = 400 reason = 'I have a bad day' response = json_response({'food': 'tortilla'}, status=status, reason=reason) assert response.status == status assert response.reason == reason
async def cast_vote(request, persistence): """Vote in the active poll in the round.""" game_id = request.match_info['game_id'] round_name = request.match_info['round_name'] json = await request.json(loads=loads_or_empty) player_session = await get_session(request) player_id = get_id(player_session) try: vote_str = json['vote'] except KeyError: return json_response({'error': 'Must provide an estimation.'}, status=400) vote = coerce_card(vote_str) try: persistence.cast_vote(game_id, round_name, player_id, vote) except NoSuchGame: return json_response({'error': 'There is no such game.'}, status=404) except NoSuchRound: return json_response({'error': 'There is no such round in the game.'}, status=404) except RoundFinalized: return json_response({'error': 'The round is finalized.'}, status=409) except IllegalEstimation: return json_response({'error': 'The estimation voted for is invalid.'}, status=400) except PlayerNotInGame: return json_response({'error': 'Cannot vote until the name is provided.'}, status=401) return json_response({'game': persistence.serialize_game(game_id)})
async def finalize_round(request, persistence): """Finalize an owned round.""" game_id = request.match_info['game_id'] round_name = request.match_info['round_name'] user_session = await get_session(request) if not client_owns_game(game_id, user_session, persistence): return json_response({'error': 'The user is not the moderator of this game.'}, status=403) try: persistence.finalize_round(game_id, round_name) except NoSuchRound: return json_response({'error': 'Round does not exist.'}, status=404) except NoActivePoll: return json_response({'error': 'There is no active poll in this round.'}, status=404) except RoundFinalized: return json_response({'error': 'This round has already been finalized.'}, status=409) return json_response({'game': persistence.serialize_game(game_id)})
async def join_game(request, persistence): """Join a game and provide a name.""" game_id = request.match_info['game_id'] json = await request.json(loads=loads_or_empty) try: player_name = json['name'] except KeyError: return json_response({'error': 'Must provide a name.'}, status=400) if len(player_name) < 1: return json_response({'error': 'The name must not be empty.'}, status=400) player_session = await get_session(request) player_id = get_or_assign_id(player_session) try: persistence.add_player(game_id, player_id, player_name) except NoSuchGame: return json_response({'error': 'There is no such game.'}, status=404) except PlayerNameTaken: return json_response({'error': 'There is already a player with such name in the game.'}, status=409) except PlayerAlreadyRegistered: return json_response({'error': 'The client is already registered in this game.'}, status=409) return json_response({'game': persistence.serialize_game(game_id)})
def get_status(request, persistence): """Respond with OK and the number of games in response body.""" return json_response({'games_count': persistence.games_count})
def test_json_response_encoding(native_data, resulting_bytes): """Check if ``json_response`` encodes compatible Python data structures to JSON bytes.""" response = json_response(native_data) assert isinstance(response.body, bytes) assert response.body == resulting_bytes