def update_current_id_player(game_id): """This function update 'current_id_player' of the game game_id.""" list_id_players = db_get_value("games", game_id, "players") current_ind_player = list_id_players.index(db_get_value("games", game_id, "current_id_player")) next_ind_player = (current_ind_player + 1) % len(list_id_players) db_update_value("games", game_id, "current_id_player", list_id_players[next_ind_player])
def post_mp3(game_id): """This function creates the mp3file depending on roles of players. - method: GET - route: /<game_id>/mp3 - payload example: - response example: response.mpga """ # find role of each player name_roles = "-".join(sorted( [db_get_value("players", player_id, "role") for player_id in db_get_value("games", game_id, "players") \ if db_get_value("players", player_id, "role") not in ("blue", "merlin", "perceval", "red")] )) return send_file("resources/_{}.mp3".format(name_roles), attachment_filename="roles.mp3", mimetype="audio/mpeg")
def quest_unsend(game_id): """This function sends new quest of the game <game_id>. - method: POST - route: /<game_id>/quest_unsend - payload example: None - response example: board """ if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] < 4: update_current_id_player(game_id) db_update_value("games", game_id, "nb_quest_unsend", db_get_value("games", game_id, "nb_quest_unsend") + 1) return game_get(game_id) if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] == 4: db_update_value("games", game_id, "nb_quest_unsend", db_get_value("games", game_id, "nb_quest_unsend") + 1) r.RethinkDB().table("games").get(game_id).update( {"result": {"status": False}}, return_changes=True)["changes"][0]["new_val"].run() return game_get(game_id) if r.RethinkDB().table("games").get(game_id).run()["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400)
def quest_put(game_id, quest_number): game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) quest = r.RethinkDB().table("quests").get(game["quests"][quest_number]).run() if game["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400) if "result" in game: return make_response("Game is over !", 400) if game["current_quest"] != quest_number: return make_response("Vote number {} is already established !".format(quest_number), 400) if ("status" in quest or "votes" in quest) and quest["status"] != None: return make_response("Only vote number {} is allowed !".format(game["current_quest"]), 400) if game["nb_quest_unsend"] == 5: return make_response("Game is over because 5 consecutive laps have been passed : Red team won !", 400) for player_id in request.json: if player_id not in game["players"]: return make_response("Player {} is not in this game !".format(player_id), 400) id_quest_number = game["quests"][quest_number] nb_players_to_send = db_get_value("quests", id_quest_number, "nb_players_to_send") if len(request.json) != nb_players_to_send: return make_response( "Quest number {} needs {} votes !".format(quest_number, nb_players_to_send), 400 ) if "status" in quest: r.RethinkDB().table("quests").get(id_quest_number).update({"votes": None}).run() return jsonify( r.RethinkDB().table("quests").get(id_quest_number).update( { "votes": {player_id: None for player_id in request.json}, "status": None }, return_changes=True )["changes"][0]["new_val"].run() )
def guess_merlin(game_id): """ """ if len(request.json) != 1: return make_response("Only 1 vote required ('assassin') !", 400) game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) if game["nb_quest_unsend"] == 5: return make_response( "Game is over because 5 consecutive laps have been passed : Red team won !", 400) assassin_id = list(request.json)[0] vote_assassin = request.json[assassin_id] if assassin_id not in game["players"]: return make_response( "Player {} is not in this game !".format(assassin_id), 400) if "assassin" not in r.RethinkDB().table("players").get(assassin_id).run(): return make_response( "Player {} is not 'assassin' !".format(assassin_id), 400) if vote_assassin not in game["players"]: return make_response( "Player {} is not in this game !".format(vote_assassin), 400) game = r.RethinkDB().table("games").get(game_id).run() if not game: return make_response("Game's id {} does not exist !".format(game_id), 400) result = game.get("result") if not result: return make_response("Game's status is not established !", 400) if not result["status"]: return make_response( "Games's status should be 'true' (ie blue team won) !", 400) if "guess_merlin_id" in result: return make_response("Merlin already chosen !", 400) result["guess_merlin_id"] = vote_assassin if db_get_value("players", vote_assassin, "role") == "merlin": result["status"] = False updated_game = r.RethinkDB().table("games").get(game_id).update( {"result": result}, return_changes=True)["changes"][0]["new_val"].run() updated_game.update({ "players": resolve_key_id(table="players", list_id=updated_game["players"]), "quests": resolve_key_id(table="quests", list_id=updated_game["quests"]) }) return jsonify(updated_game)
def quest_delete(game_id, quest_number): id_quest_number = db_get_value("games", game_id, "quests")[quest_number] return r.RethinkDB().table("quests").get(id_quest_number).replace( r.RethinkDB().row.without("votes", "status"), return_changes=True)["changes"][0]["new_val"].run()