Start documentation in game_logic.py
This commit is contained in:
parent
e8c5288f00
commit
a8fae6b3fc
@ -3,20 +3,15 @@ import random
|
|||||||
from truthseeker.logic.data_persistance.data_access import *
|
from truthseeker.logic.data_persistance.data_access import *
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
from truthseeker import APP
|
from truthseeker import APP
|
||||||
|
from typing import Union, Optional
|
||||||
|
|
||||||
# Map of all actively running games
|
|
||||||
# games_list["game.game_id"]-> game info linked to that id
|
|
||||||
|
|
||||||
def random_string(length: int) ->str:
|
def random_string(length: int) ->str:
|
||||||
"""
|
"""
|
||||||
This function create a random string as long as the lint passed as
|
This function create a random string as long as the lint passed as
|
||||||
parameter
|
parameter
|
||||||
|
|
||||||
: param length: the lenght of the random string
|
:param length: the length of the random string to create
|
||||||
: type length : int
|
:return: a random string
|
||||||
: 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))
|
||||||
|
|
||||||
@ -24,13 +19,13 @@ class Member:
|
|||||||
"""
|
"""
|
||||||
stores information related to the member of a given game
|
stores information related to the member of a given game
|
||||||
|
|
||||||
Member.username : The username of this member
|
:attr str username: The username of this member
|
||||||
Member.socker : The reference to the socket to talk to this member
|
:attr TODO progress: TODO
|
||||||
|
:attr TODO results: TODO
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self, username):
|
def __init__(self, username):
|
||||||
self.username = username
|
self.username = username
|
||||||
self.socket = None
|
|
||||||
self.progress = 0
|
self.progress = 0
|
||||||
self.results = None
|
self.results = None
|
||||||
|
|
||||||
@ -44,10 +39,14 @@ class Game:
|
|||||||
"""
|
"""
|
||||||
The game info class stores all information linked to a active game
|
The game info class stores all information linked to a active game
|
||||||
|
|
||||||
Game.game_id : str, the game identifier of the game
|
:attr str game_id: str, the game identifier of the game
|
||||||
Game.owner : Member, the game identifier of the game
|
:attr owner Member: the player start created the game. It is also stored in self.members
|
||||||
Game.members : Member[], the members of the game
|
:attr Member[] members: the members of the game
|
||||||
|
:attr bool has_started: TODO
|
||||||
|
:attr TODO gamedata: TODO
|
||||||
|
:attr TODO reaction_table: TODO
|
||||||
"""
|
"""
|
||||||
|
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self.game_id = None
|
self.game_id = None
|
||||||
self.owner = None
|
self.owner = None
|
||||||
@ -56,12 +55,21 @@ class Game:
|
|||||||
self.gamedata = {}
|
self.gamedata = {}
|
||||||
self.reaction_table = {}
|
self.reaction_table = {}
|
||||||
|
|
||||||
def set_owner(self, username):
|
"""
|
||||||
|
Set the owner of the game
|
||||||
|
|
||||||
|
:param username: the username of the owner.
|
||||||
|
:return: the Member object created by this method
|
||||||
|
"""
|
||||||
|
def set_owner(self, username: str) -> Member:
|
||||||
self.owner = Member(username)
|
self.owner = Member(username)
|
||||||
self.members.append(self.owner)
|
self.members.append(self.owner)
|
||||||
return self.owner
|
return self.owner
|
||||||
|
|
||||||
def generateGameResults(self):
|
"""
|
||||||
|
TODO + TODO RET TYPE
|
||||||
|
"""
|
||||||
|
def generateGameResults(self) -> None:
|
||||||
data = {}
|
data = {}
|
||||||
npcs = data["npcs"] = {}
|
npcs = data["npcs"] = {}
|
||||||
for npc_id in self.gamedata["npcs"]:
|
for npc_id in self.gamedata["npcs"]:
|
||||||
@ -75,29 +83,50 @@ class Game:
|
|||||||
player_results[member.username] = member.results
|
player_results[member.username] = member.results
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def generate_data(self):
|
"""
|
||||||
|
TODO
|
||||||
|
"""
|
||||||
|
def generate_data(self) -> None:
|
||||||
#TODO Get language from player
|
#TODO Get language from player
|
||||||
self.gamedata, self.reaction_table = generateGameData("FR")
|
self.gamedata, self.reaction_table = generateGameData("FR")
|
||||||
|
|
||||||
def get_member(self, username):
|
"""
|
||||||
|
Get a Member object from a username
|
||||||
|
|
||||||
|
:param username: the username of the member to search for
|
||||||
|
:return the member corresponding to the username, or None if none if found:
|
||||||
|
"""
|
||||||
|
def get_member(self, username: str) -> Union[Member, None]:
|
||||||
for member in self.members:
|
for member in self.members:
|
||||||
if member.username == username:
|
if member.username == username:
|
||||||
return member
|
return member
|
||||||
|
|
||||||
def add_member(self, username):
|
"""
|
||||||
|
Add a Member to the game
|
||||||
|
|
||||||
|
:param username: the username of the member to add
|
||||||
|
:return: the Member created, or None if a Member with this username already exists in the game
|
||||||
|
"""
|
||||||
|
def add_member(self, username: str) -> Union[Member, None]:
|
||||||
if self.get_member(username):
|
if self.get_member(username):
|
||||||
return None
|
return None
|
||||||
member = Member(username)
|
member = Member(username)
|
||||||
self.members.append(member)
|
self.members.append(member)
|
||||||
return member
|
return member
|
||||||
|
|
||||||
def get_npc_reaction(self,npc_id,reaction):
|
"""
|
||||||
|
TODO + TODO TYPES
|
||||||
|
"""
|
||||||
|
def get_npc_reaction(self, npc_id, reaction) -> None:
|
||||||
if npc_id not in self.reaction_table.keys():
|
if npc_id not in self.reaction_table.keys():
|
||||||
return 0
|
return 0
|
||||||
reaction_id = self.reaction_table[npc_id][int(reaction)]
|
reaction_id = self.reaction_table[npc_id][int(reaction)]
|
||||||
return read_image(f"./truthseeker/static/images/npc/{npc_id}/{reaction_id}.png")
|
return read_image(f"./truthseeker/static/images/npc/{npc_id}/{reaction_id}.png")
|
||||||
|
|
||||||
def getPlayerResults(self,responses: dict):
|
"""
|
||||||
|
TODO + TODO RETTYPE
|
||||||
|
"""
|
||||||
|
def getPlayerResults(self, responses: dict) -> None:
|
||||||
results = {}
|
results = {}
|
||||||
try:
|
try:
|
||||||
for npc_id in responses:
|
for npc_id in responses:
|
||||||
@ -108,7 +137,12 @@ class Game:
|
|||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def has_finished(self):
|
"""
|
||||||
|
Checks if the game has finished by checking if every Member has submitted answers
|
||||||
|
|
||||||
|
:return: True if the game has finished, else False
|
||||||
|
"""
|
||||||
|
def has_finished(self) -> bool:
|
||||||
for member in self.members:
|
for member in self.members:
|
||||||
if member.results == None : return False
|
if member.results == None : return False
|
||||||
return True
|
return True
|
||||||
@ -119,13 +153,12 @@ class Game:
|
|||||||
def __repr__(self) -> str:
|
def __repr__(self) -> str:
|
||||||
return self.__str__()
|
return self.__str__()
|
||||||
|
|
||||||
def create_game(owner):
|
def create_game(owner: str) -> Game:
|
||||||
"""
|
"""
|
||||||
This function creates a new game by creating a Game object and stores
|
This function creates a new game by creating a Game object and stores
|
||||||
it into the games_list dictionnary
|
it into the games_list dictionnary
|
||||||
|
|
||||||
: return : a new Game
|
:return: a new Game
|
||||||
: return type : Game
|
|
||||||
"""
|
"""
|
||||||
game = Game()
|
game = Game()
|
||||||
game.owner = owner
|
game.owner = owner
|
||||||
@ -134,25 +167,12 @@ def create_game(owner):
|
|||||||
APP.games_list[game.game_id] = game
|
APP.games_list[game.game_id] = game
|
||||||
return game
|
return game
|
||||||
|
|
||||||
def get_game(game_id):
|
def get_game(game_id: str) -> Union[Game, None]:
|
||||||
if game_id in APP.games_list:
|
"""
|
||||||
return APP.games_list[game_id]
|
Get a game from its ID
|
||||||
else:
|
|
||||||
return None
|
|
||||||
|
|
||||||
def get_game_info(game_id):
|
:param game_id: the id of the game to search
|
||||||
""" if not flask.session:
|
:return: the Game object or None if not found
|
||||||
return {"error": 1, "msg": "No session"}
|
|
||||||
game = game_logic.get_game(flask.session["game_id"])
|
|
||||||
if game == None:
|
|
||||||
return {"error": 1, "msg": "this game doesn't exist"}
|
|
||||||
This function retrieve a the Game object linked to the game_id
|
|
||||||
passed as parametter
|
|
||||||
|
|
||||||
: param game_id : the lenght of the random string
|
|
||||||
: type game_id : str
|
|
||||||
: return : The Game Object linked to the game_id
|
|
||||||
: return type : Game
|
|
||||||
"""
|
"""
|
||||||
if game_id in APP.games_list:
|
if game_id in APP.games_list:
|
||||||
return APP.games_list[game_id]
|
return APP.games_list[game_id]
|
||||||
@ -178,10 +198,10 @@ def generateNpcText(npc: tables.Npc, lang: str) -> dict:
|
|||||||
data["QA_1"] = getTextFromLid(lang, getNpcRandomAnswer(npc,1).TEXT_LID)
|
data["QA_1"] = getTextFromLid(lang, getNpcRandomAnswer(npc,1).TEXT_LID)
|
||||||
return data
|
return data
|
||||||
|
|
||||||
def generateNpcReactions(npc : tables.Npc) ->list:
|
def generateNpcReactions(npc: tables.Npc) ->list:
|
||||||
return getNpcRandomTraitId(npc)
|
return getNpcRandomTraitId(npc)
|
||||||
|
|
||||||
def generatePlaceData(npcs :list, places: list, lang : str) -> dict:
|
def generatePlaceData(npcs: list, places: list, lang: str) -> dict:
|
||||||
data = {}
|
data = {}
|
||||||
random.shuffle(npcs)
|
random.shuffle(npcs)
|
||||||
for place in places:
|
for place in places:
|
||||||
|
Loading…
Reference in New Issue
Block a user