def signout(): """ Request: { "auth_token": "auth_token" } Response [422] (unauthenticated): { "errors": { "UnauthorizedAccess": [ "Attempted to access data without an authenticated player" ] } } Response [422] (signout failure - save error): { "errors": { "SignoutError": [ "Unable to sign out player" ] } } Response [200] (success): { "Success": "Player signed out" } """ if get_current_user() and get_current_user().logout(): return render_view('players/show', 200, player={'Success': 'Player signed out'}) else: return render_view('422', 422, errors=UNABLE_TO_SIGNOUT_ERROR)
def create(): # Get the input validator inputs = CreateInputs(get_inputs()) # Verify the match creation inputs if inputs.validate_on_submit(): # Ensure we have a valid game game = GamesService.get_instance().get(inputs.game_id.data) if game: # If an opponent is specified, match with that opponent if inputs.opponent_id.data: # Ensure that the opponent is a valid user opponent = PlayersService.get_instance().get(inputs.opponent_id.data) if not opponent: return render_view('422', 422, errors=OPPONENT_NOT_FOUND_ERROR, inputs=inputs.serialized()) # First attempt to find a match already requested by the desired opponent match = MatchesService.get_instance().get_opponent_match( game.get_id(), get_current_user(), opponent.get_id() ) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is assigned to the desired opponent, # but leave it in the waiting state else: match = Match(game, get_current_user()) match.add_player(opponent, should_start=False) # Otherwise, match with a random opponent else: # First, attempt to find a match that is looking for an opponent match = MatchesService.get_instance().get_random_match(game.get_id(), get_current_user()) # If the match is found, add the player to it and start the match if it's full if match: match.add_player(get_current_user()) # If no match is found, create one that is waiting for an opponent else: match = Match(game, get_current_user()) try: match.save() return render_view('matches/show', 201, match=match.serialized) except Exception as e: return render_view('422', 422, errors={e.__class__.__name__: [e.message]}, inputs=inputs.serialized()) return render_view('422', 422, errors=GAME_NOT_FOUND_ERROR, inputs=inputs.serialized()) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())
def show(match_id): # Get the current turn turn, errors = TurnsService.get_instance().get_player_turn(match_id, get_current_user().get_id()) if not errors: turn_player = TurnPlayersService.get_instance().get_for_turn_by_player( turn.get_id(), get_current_user().get_id() ) # Get the turn_definition_fillers for this turn turn_definition_fillers = TurnDefinitionFillersService.get_instance().get_list_by_turn(turn.get_id()) # If this player is the selector for this turn if turn_player.get_is_selector(): # TODO Test SQL for turn.get_match().get_game().get_definition_filler_count() vs turn.match.game.get_definition_filler_count() if turn.get_match().get_game().get_definition_filler_count() > len(turn_definition_fillers): pass return render_view('matches/show', 200, match=match.serialized) return render_view('422', 422, errors=errors, inputs={'id': match_id})
def index(): # Get the input validator inputs = ListInputs(get_inputs()) # Verify the list inputs if inputs.validate(): matches = MatchesService.get_instance().get_list_by_game_for_player( inputs.game_id.data, get_current_user().get_id(), inputs.limit.data, inputs.offset.data ) return render_view('matches/index', 200, matches={match.get_id(): match.serialized for match in matches}) return render_view('422', 422, errors=inputs.errors, inputs=inputs.serialized())