refractor + some doc
This commit is contained in:
parent
d74f304330
commit
e2301ec1e7
@ -1,23 +1,58 @@
|
|||||||
import string
|
import string
|
||||||
import random
|
import random
|
||||||
|
|
||||||
|
|
||||||
|
# Map of all actively running games
|
||||||
|
# game_lists["game.id"]-> game info linked to that id
|
||||||
game_lists = {}
|
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))
|
return "".join(random.choice(string.ascii_letters) for _ in range(length))
|
||||||
|
|
||||||
class GameInfo:
|
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):
|
def __init__(self):
|
||||||
self.start_token = None
|
self.start_token = None
|
||||||
|
|
||||||
def create_game():
|
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 = GameInfo()
|
||||||
game.id = random_string(6)
|
game.id = random_string(6)
|
||||||
game.start_token = random_string(64)
|
game.start_token = random_string(64)
|
||||||
game_lists[game.id] = game
|
game_lists[game.id] = game
|
||||||
|
#TODO ADD A WEBSOCKET IF THE GAME IS KNOWN TO BE MULTIPLAYER
|
||||||
return game
|
return game
|
||||||
|
|
||||||
def get_game_info(game_id):
|
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:
|
if game_id in game_lists:
|
||||||
return game_lists[game_id]
|
return game_lists[game_id]
|
||||||
else:
|
else:
|
||||||
|
@ -6,16 +6,25 @@ api_routes = flask.Blueprint("api", __name__)
|
|||||||
|
|
||||||
@api_routes.route("/createGame")
|
@api_routes.route("/createGame")
|
||||||
def create_game():
|
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")
|
@api_routes.route("/getGameInfo")
|
||||||
def get_game_info():
|
def get_game_info():
|
||||||
|
response = {}
|
||||||
gameid = flask.request.args.get("gameid")
|
gameid = flask.request.args.get("gameid")
|
||||||
if gameid == None:
|
if gameid == None:
|
||||||
return "No 'gameid' argument"
|
response["status"] = "No 'gameid' argument"
|
||||||
|
return response
|
||||||
game = game_api.get_game_info(gameid)
|
game = game_api.get_game_info(gameid)
|
||||||
if game == None:
|
if game == None:
|
||||||
return "Game {} does not exist".format(gameid)
|
response["status"] = "Game {} does not exist".format(gameid)
|
||||||
|
return response
|
||||||
else:
|
else:
|
||||||
return "Game {} start token : {}".format(gameid, game.start_token)
|
response["status"] = "ok"
|
||||||
|
response["gameid"] = gameid
|
||||||
|
response["token"] = game.start_token
|
||||||
|
return response
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user