From e2301ec1e7dc0d89739a224fe56be6558a0a71db Mon Sep 17 00:00:00 2001 From: SIMAILA Djalim Date: Mon, 28 Nov 2022 15:35:59 +0100 Subject: [PATCH] refractor + some doc --- truthseeker/game_api.py | 37 ++++++++++++++++++++++++++++++++++++- truthseeker/routes_api.py | 17 +++++++++++++---- 2 files changed, 49 insertions(+), 5 deletions(-) diff --git a/truthseeker/game_api.py b/truthseeker/game_api.py index f22b26b..d685f3c 100644 --- a/truthseeker/game_api.py +++ b/truthseeker/game_api.py @@ -1,23 +1,58 @@ import string import random + +# Map of all actively running games +# game_lists["game.id"]-> game info linked to that id game_lists = {} -def random_string(length): +def random_string(length: int) ->str: + """ + This function create a random string as long as the lint passed as + parameter + + : param length: the lenght of the random string + : type length : int + : return : a random string + : return type : string + """ return "".join(random.choice(string.ascii_letters) for _ in range(length)) class GameInfo: + """ + The game info class stores all information linked to a active game + + Game.start_token : str, + Game.id : str, the game identifier of the game + """ def __init__(self): self.start_token = None def create_game(): + """ + This function creates a new game by creating a GameInfo object and stores + it into the game_lists dictionnary + + : return : a new GameInfo + : return type : GameInfo + """ game = GameInfo() game.id = random_string(6) game.start_token = random_string(64) game_lists[game.id] = game + #TODO ADD A WEBSOCKET IF THE GAME IS KNOWN TO BE MULTIPLAYER return game def get_game_info(game_id): + """ + This function retrieve a the GameInfo object linked to the game_id + passed as parametter + + : param game_id : the lenght of the random string + : type game_id : str + : return : The GameInfo Object linked to the gameId + : return type : GameInfo + """ if game_id in game_lists: return game_lists[game_id] else: diff --git a/truthseeker/routes_api.py b/truthseeker/routes_api.py index 20daa73..b04f1a5 100644 --- a/truthseeker/routes_api.py +++ b/truthseeker/routes_api.py @@ -6,16 +6,25 @@ api_routes = flask.Blueprint("api", __name__) @api_routes.route("/createGame") def create_game(): - return "Created game {}".format(game_api.create_game().id) + response = {} + response["status"] = "ok" + response["gameId"] = game_api.create_game().id + return response @api_routes.route("/getGameInfo") def get_game_info(): + response = {} gameid = flask.request.args.get("gameid") if gameid == None: - return "No 'gameid' argument" + response["status"] = "No 'gameid' argument" + return response game = game_api.get_game_info(gameid) if game == None: - return "Game {} does not exist".format(gameid) + response["status"] = "Game {} does not exist".format(gameid) + return response else: - return "Game {} start token : {}".format(gameid, game.start_token) + response["status"] = "ok" + response["gameid"] = gameid + response["token"] = game.start_token + return response