added getNpcImage static
This commit is contained in:
		
							parent
							
								
									3ce9fea232
								
							
						
					
					
						commit
						0e0e615414
					
				| @ -37,3 +37,13 @@ def getNpcRandomAnswer(npc, QA_TYPE) -> tables.Answer : | ||||
| def getRandomQuestion(QA_TYPE) -> tables.Answer : | ||||
|     answers = session.query(tables.Question).filter_by(QUESTION_TYPE=QA_TYPE).all() | ||||
|     return random.choice(answers) | ||||
| 
 | ||||
| def getTraitFromText(text): | ||||
|     trait_lid = session.query(tables.Locale).filter_by(TEXT=text).one().TEXT_ID | ||||
|     return session.query(tables.Trait).filter_by(NAME_LID=trait_lid).one().TRAIT_ID | ||||
| 
 | ||||
| def getTraits(lang): | ||||
|     traits = [] | ||||
|     for trait in session.query(tables.Trait).all(): | ||||
|         traits.append(getTextFromLid(lang,trait.NAME_LID)) | ||||
|     return traits | ||||
| @ -31,6 +31,7 @@ class Member: | ||||
|     def __init__(self, username): | ||||
|         self.username = username | ||||
|         self.socket = None | ||||
|         self.progress = 0 | ||||
| 
 | ||||
|     def __str__(self) -> str: | ||||
|         return "Member[username={}]".format(self.username) | ||||
| @ -81,6 +82,12 @@ class Game: | ||||
|         reaction_id = self.reaction_table[npc_id][int(reaction)] | ||||
|         return read_image(f"./truthseeker/static/images/npc/{npc_id}/{reaction_id}.png") | ||||
|      | ||||
| 
 | ||||
|     def has_finished(self): | ||||
|         for member in self.members: | ||||
|             if member.progress != 5 : return False | ||||
|         return True | ||||
| 
 | ||||
|     def __str__(self) -> str: | ||||
|         return "Game[game_id={}, owner={}, members={}]".format(self.game_id, self.owner, self.members) | ||||
| 
 | ||||
| @ -109,7 +116,11 @@ def get_game(game_id): | ||||
|         return None | ||||
| 
 | ||||
| def get_game_info(game_id): | ||||
|     """ | ||||
|     """    if not flask.session: | ||||
|         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 | ||||
| 
 | ||||
| @ -172,11 +183,17 @@ def generateGameData(LANG): | ||||
|     data["questions"] = {} | ||||
|     data["questions"]["QA_0"] = getTextFromLid("FR",getRandomQuestion(0).TEXT_LID) | ||||
|     data["questions"]["QA_1"] = getTextFromLid("FR",getRandomQuestion(1).TEXT_LID) | ||||
|     data["traits"] = getTraits(LANG) | ||||
|     return data, reactions_table | ||||
| 
 | ||||
| 
 | ||||
| def read_image(path:str): | ||||
|     try: | ||||
|         return open(path, "rb").read() | ||||
|     except: | ||||
|         return 1 | ||||
| 
 | ||||
| def getTraitIdFromString(trait): | ||||
|     return getTraitFromText(trait) | ||||
| 
 | ||||
| def get_npc_image(npc_id): | ||||
|     return read_image(f"./truthseeker/static/images/npc/{npc_id}/0.png") | ||||
| @ -81,6 +81,17 @@ def get_data(): | ||||
| 
 | ||||
| @routes_api.route("/getNpcImage", methods=["GET", "POST"]) | ||||
| def getNpcImage(): | ||||
|     npc_id = flask.request.values.get("npcid") | ||||
|     image = game_logic.get_npc_image(npc_id) | ||||
|      | ||||
|     response = flask.make_response(image) | ||||
|     response.headers.set('Content-Type', 'image/png') | ||||
|     response.headers.set( | ||||
|         'Content-Disposition', 'attachment', filename=f'0.png') | ||||
|     return response | ||||
| 
 | ||||
| @routes_api.route("/getNpcReaction", methods=["GET", "POST"]) | ||||
| def getNpcReaction(): | ||||
| 
 | ||||
|     if not flask.session: | ||||
|         return {"error": 1, "msg": "No session"} | ||||
| @ -100,3 +111,20 @@ def getNpcImage(): | ||||
|     response.headers.set( | ||||
|         'Content-Disposition', 'attachment', filename=f'{reactionid}.png') | ||||
|     return response | ||||
| 
 | ||||
| @routes_api.route("/gameProgress", methods=["GET", "POST"]) | ||||
| def gameProgress(): | ||||
|     if not flask.session: | ||||
|         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"} | ||||
|      | ||||
|     username = flask.session["username"] | ||||
|     game.get_member(username).progress += 1 | ||||
|      | ||||
|     APP.socketio_app.emit("gameprogress", [flask.session["username"]], room="game."+game.game_id) | ||||
|     if game.has_finished() : APP.socketio_app.emit("gamefinshed",room="game."+game.game_id) | ||||
|      | ||||
|     return {"error": 0} | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user