Merge pull request #19 from ThomasRubini/game_logic
server side game logic
This commit is contained in:
		
						commit
						8720dc39f3
					
				| @ -1,3 +1,5 @@ | ||||
| Flask==2.2.2 | ||||
| pyjwt==2.6.0 | ||||
| Flask-SocketIO==5.3.2 | ||||
| SQLAlchemy==1.4.20 | ||||
| pymysql==1.0.2 | ||||
							
								
								
									
										39
									
								
								truthseeker/logic/data_persistance/data_access.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										39
									
								
								truthseeker/logic/data_persistance/data_access.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,39 @@ | ||||
| from sqlalchemy import create_engine | ||||
| from sqlalchemy.orm import Session | ||||
| from sqlalchemy import engine as eg | ||||
| import random | ||||
| import truthseeker.logic.data_persistance.tables as tables | ||||
| from truthseeker.logic.data_persistance.secret import HOST, USER, PASS | ||||
| 
 | ||||
| url_object = eg.URL.create( | ||||
|     "mariadb+pymysql", | ||||
|     username=USER, | ||||
|     password=PASS, | ||||
|     host=HOST, | ||||
|     port=6776, | ||||
|     database="truthInquiry", | ||||
| ) | ||||
| engine = create_engine(url_object) | ||||
| session = Session(engine) | ||||
| 
 | ||||
| def getTextFromLid(lang,lid) -> str: | ||||
|     return session.query(tables.Locale).filter_by(LANG=lang, TEXT_ID=lid).one().TEXT | ||||
| 
 | ||||
| def getRandomPlace() -> tables.Place: | ||||
|     return random.choice(session.query(tables.Place).all()) | ||||
| 
 | ||||
| def getRandomNpc() -> tables.Npc : | ||||
|     return random.choice(session.query(tables.Npc).all()) | ||||
| 
 | ||||
| def getNpcRandomTraitId(npc) -> int: | ||||
|     reactions = session.query(tables.Reaction).filter_by(NPC_ID=npc.NPC_ID).all() | ||||
|     reaction = random.choice(reactions) | ||||
|     return reaction.TRAIT_ID | ||||
| 
 | ||||
| def getNpcRandomAnswer(npc, QA_TYPE) -> tables.Answer : | ||||
|     answers = session.query(tables.Answer).filter_by(QA_TYPE=QA_TYPE,NPC_ID=npc.NPC_ID).all() | ||||
|     return random.choice(answers) | ||||
| 
 | ||||
| def getRandomQuestion(QA_TYPE) -> tables.Answer : | ||||
|     answers = session.query(tables.Question).filter_by(QUESTION_TYPE=QA_TYPE).all() | ||||
|     return random.choice(answers) | ||||
							
								
								
									
										3
									
								
								truthseeker/logic/data_persistance/secret.py
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								truthseeker/logic/data_persistance/secret.py
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,3 @@ | ||||
| HOST = "mariadb.simailadjalim.fr" | ||||
| USER = "truthInquiry" | ||||
| PASS = "truthInquiry" | ||||
| @ -1,5 +1,6 @@ | ||||
| import string | ||||
| import random | ||||
| from truthseeker.logic.data_persistance.data_access import * | ||||
| from datetime import datetime, timedelta | ||||
| from truthseeker import APP | ||||
| 
 | ||||
| @ -40,7 +41,7 @@ class Member: | ||||
| class Game: | ||||
|     """ | ||||
|     The game info class stores all information linked to a active game | ||||
|   | ||||
| 
 | ||||
|     Game.game_id : str, the game identifier of the game | ||||
|     Game.owner : Member, the game identifier of the game | ||||
|     Game.members : Member[], the members of the game | ||||
| @ -50,17 +51,21 @@ class Game: | ||||
|         self.owner = None | ||||
|         self.members = [] | ||||
|         self.has_started = False | ||||
|         self.gamedata = {} | ||||
| 
 | ||||
|     def set_owner(self, username): | ||||
|         self.owner = Member(username) | ||||
|         self.members.append(self.owner) | ||||
|         return self.owner | ||||
| 
 | ||||
|     def generate_data(self): | ||||
|         self.gamedata = generateGameData("FR") | ||||
| 
 | ||||
|     def get_member(self, username): | ||||
|         for member in self.members: | ||||
|             if member.username == username: | ||||
|                 return member | ||||
| 
 | ||||
|          | ||||
|     def add_member(self, username): | ||||
|         if self.get_member(username): | ||||
|             return None | ||||
| @ -87,7 +92,6 @@ def create_game(owner): | ||||
|     game.members.append(Member(owner)) | ||||
|     game.game_id = random_string(6) | ||||
|     APP.games_list[game.game_id] = game | ||||
|     #TODO ADD A WEBSOCKET IF THE GAME IS KNOWN TO BE MULTIPLAYER | ||||
|     return game | ||||
| 
 | ||||
| def get_game(game_id): | ||||
| @ -109,4 +113,49 @@ def get_game_info(game_id): | ||||
|     if game_id in APP.games_list: | ||||
|         return APP.games_list[game_id] | ||||
|     else: | ||||
|         return None | ||||
|         return None | ||||
| 
 | ||||
| def generateNpcData(npc: tables.Npc, lang: str) -> dict: | ||||
|     data = {} | ||||
|     data["name"] = getTextFromLid(lang, npc.NAME_LID) | ||||
|     data["QA_0"] = getTextFromLid(lang, getNpcRandomAnswer(npc,0).TEXT_LID) | ||||
|     data["QA_1"] = getTextFromLid(lang, getNpcRandomAnswer(npc,1).TEXT_LID) | ||||
|     data["R_0"]  = getNpcRandomTraitId(npc) | ||||
|     data["R_1"]  = getNpcRandomTraitId(npc) | ||||
|     return data | ||||
| 
 | ||||
| def generatePlaceData(npcs :list, places: list, lang : str) -> dict: | ||||
|     data = {} | ||||
|     random.shuffle(npcs) | ||||
|     for place in places: | ||||
|         placedata = data[str(place.PLACE_ID)] = {} | ||||
|         placedata["name"] = getTextFromLid(lang,place.NAME_LID) | ||||
|         placedata["npcs"] = [] | ||||
|         for _ in npcs: | ||||
|             placedata["npcs"].append(npcs.pop().NPC_ID) | ||||
|             if len(placedata["npcs"]) == 2: break | ||||
|     return data | ||||
| 
 | ||||
| 
 | ||||
| def generateGameData(LANG): | ||||
|     data = {} | ||||
|     data["npcs"] = {} | ||||
|     npcs = [] | ||||
|     while len(npcs) != 5: | ||||
|         npc = getRandomNpc() | ||||
|         if npc not in npcs : | ||||
|             npcs.append(npc) | ||||
|     for npc in npcs: | ||||
|         data["npcs"][str(npc.NPC_ID)] = generateNpcData(npc,LANG) | ||||
| 
 | ||||
|     places = [] | ||||
|     while len(places) != 3: | ||||
|         place = getRandomPlace() | ||||
|         if place not in places: | ||||
|             places.append(place) | ||||
| 
 | ||||
|     data["rooms"] = generatePlaceData(npcs,places,LANG) | ||||
|     data["questions"] = {} | ||||
|     data["questions"]["QA_0"] = getTextFromLid("FR",getRandomQuestion(0).TEXT_LID) | ||||
|     data["questions"]["QA_1"] = getTextFromLid("FR",getRandomQuestion(1).TEXT_LID) | ||||
|     return data | ||||
|  | ||||
| @ -56,18 +56,25 @@ def start_game(): | ||||
|         return {"error": 1, "msg": "No session"} | ||||
|     if not flask.session["is_owner"]: | ||||
|         return {"error": 1, "msg": "you are not the owner of this game"} | ||||
|      | ||||
|     game = game_logic.get_game(flask.session["game_id"]) | ||||
| 
 | ||||
|     if game == None: | ||||
|         return {"error": 1, "msg": "this game doesn't exist"} | ||||
|     print(game.has_started) | ||||
|     if game.has_started: | ||||
|         return {"error": 1, "msg": "this game is already started"} | ||||
| 
 | ||||
|     game.generate_data() | ||||
|     game.has_started = True | ||||
| 
 | ||||
|     APP.socketio_app.emit("gamestart", {}, room="game."+game.game_id) | ||||
|      | ||||
|      | ||||
|     return {"error": 0} | ||||
| 
 | ||||
| @routes_api.route("/getGameData", methods=["GET", "POST"]) | ||||
| def get_data(): | ||||
|     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"} | ||||
|      | ||||
|     response = {} | ||||
|     response["error"] = 0 | ||||
|     response["gamedata"] = game.gamedata | ||||
|     return response | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user