def test_query_equals_insert_players(): """ Check that inserted record is the same as record catched by query. """ with api.app.app_context(): result = models.get_player(TestValues.PLAYER_ID) assert result.game_id == TestValues.GAME_ID assert result.state == cfg.STATE
def classify(): """ Classify endpoint for continuous guesses. """ game_state = "Playing" # Check if image submitted correctly if "image" not in request.files: raise excp.BadRequest("No image submitted") # Retrieve the image and check if it satisfies constraints image = request.files["image"] allowed_file(image) # use player_id submitted by player to find game player_id = request.values["player_id"] # Get time from POST request time_left = float(request.values["time"]) # Get label for game session player = models.get_player(player_id) clientRound = request.values.get("client_round_num", None) game = models.get_game(player.game_id) server_round = game.session_num if clientRound is not None and int(clientRound) < game.session_num: raise excp.BadRequest( "Server-round number larger than request/client. Probably a request processed out of order" ) labels = json.loads(game.labels) label = labels[game.session_num - 1] certainty, best_guess = classifier.predict_image_by_post(image) best_certainty = certainty[best_guess] # The player has won if the game is completed within the time limit has_won = (time_left > 0 and best_guess == label and best_certainty >= setup.CERTAINTY_THRESHOLD) # End game if player win or loose if has_won or time_left <= 0: # Update session_num in game and state for player models.update_game_for_player(player.game_id, player_id, 1, "Done") # save image storage.save_image(image, label, best_certainty) # Update game state to be done game_state = "Done" # translate labels into norwegian translation = models.get_translation_dict() certainty_translated = dict([(translation[label], probability) for label, probability in certainty.items()]) data = { "certainty": certainty_translated, "guess": translation[best_guess], "correctLabel": translation[label], "hasWon": has_won, "gameState": game_state, "serverRound": server_round, } return json.dumps(data), 200
def test_white_image_data_done(client): """ Test if the white_image_data function returns the correct data and that state is "done" when time_left parameter is zero. """ res = client.get("/startGame") player_id = json.loads(res.data)["player_id"] game_id = models.get_player(player_id).game_id label = "" data, code = api.white_image_data(label, 0, game_id, player_id) json_data = json.loads(data) assert (json_data["gameState"] == "Done") assert (json_data["correctLabel"] == label) assert (json_data["hasWon"] is False) assert (json_data["certainty"] == 1.0) assert (json_data["guess"] == setup.WHITE_IMAGE_GUESS)
def get_label(): """ Provides the client with a new word. """ player_id = request.values["player_id"] player = models.get_player(player_id) game = models.get_game(player.game_id) # Check if game complete if game.session_num > setup.NUM_GAMES: raise excp.BadRequest("Number of games exceeded") labels = json.loads(game.labels) label = labels[game.session_num - 1] norwegian_label = models.to_norwegian(label) data = {"label": norwegian_label} return json.dumps(data), 200
def end_game(): """ Endpoint for ending game consisting of NUM_GAMES sessions. """ player_id = request.values["player_id"] name = request.values["name"] score = float(request.values["score"]) player = models.get_player(player_id) game = models.get_game(player.game_id) if game.session_num != setup.NUM_GAMES + 1: raise excp.BadRequest("Game not finished") today = datetime.date.today() models.insert_into_scores(name, score, today) # Clean database for unnecessary data models.delete_session_from_game(player.game_id) models.delete_old_games() return json.dumps({"success": "OK"}), 200