73 lines
2.0 KiB
Python
73 lines
2.0 KiB
Python
import string
|
|
import random
|
|
import jwt
|
|
from datetime import datetime, timedelta
|
|
import truthseeker
|
|
|
|
# Map of all actively running games
|
|
# game_lists["game.id"]-> game info linked to that id
|
|
game_lists = {}
|
|
|
|
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
|
|
|
|
GameInfo.id : str, the game identifier of the game
|
|
"""
|
|
def __init__(self):
|
|
self.game_id = None
|
|
|
|
def gen_jwt(self, username, owner):
|
|
return jwt.encode(
|
|
payload={
|
|
"game_id": self.game_id,
|
|
"username": username,
|
|
"owner": owner,
|
|
"exp": datetime.utcnow() + timedelta(hours = 1) # handled automatically on jwt.decode
|
|
},
|
|
key=truthseeker.app.config["SECRET_KEY"],
|
|
algorithm="HS256"
|
|
)
|
|
|
|
|
|
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 game_id
|
|
: return type : GameInfo
|
|
"""
|
|
if game_id in game_lists:
|
|
return game_lists[game_id]
|
|
else:
|
|
return None |