-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfastapi_server.py
More file actions
69 lines (57 loc) · 2.45 KB
/
fastapi_server.py
File metadata and controls
69 lines (57 loc) · 2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
from fastapi import FastAPI
from datetime import date
from player import Player
from game_engine import Altered_game_engine
gameengines_running = [] # List of running game engines
# a new game engine is appended to this list every time a new game is created
app = FastAPI()
@app.get("/")
async def root():
return {"message": "Altered TCG"}
@app.post("/game/create")
def create_game(player: Player):
gameengines_running.append(Altered_game_engine())
player_data = gameengines_running[-1].create_a_new_game(player)
return {"Success": True, "data": player_data}
@app.post("/game/get_all_running_games")
def get_all_running_games():
return {"Success": True, "data": [game.id for game in gameengines_running]}
@app.post("/game/join")
def create_game(player: Player):
game = get_game_engine(player.game_id) # retrieve the game_engine running for this player game
player_data = game.join_a_game(player)
return {"Success": True, "data": player_data}
@app.post("/game/start")
def start_game(player: Player):
game = get_game_engine(player.game_id) # retrieve the game_engine running for this player game
game.start()
return {"Success": True}
@app.post("/game/play_actions")
def play_actions(player: Player):
game = get_game_engine(player.game_id) # retrieve the game_engine running for this player game
player_data = game.play_actions(player)
return {"Success": True, "data": player_data}
@app.get("/game/get_player")
def get_player(player: Player):
game = get_game_engine(player.game_id) # retrieve the game_engine running for this player game
player_data = game.get_player(player)
if type(player_data) == dict: # error
return player_data
return {"Success": True, "data": player_data}
# region nested functions
def get_game_engine(game_id):
for game_engine in gameengines_running:
if game_engine.id == game_id:
return game_engine
raise 'Game not found'
# @app.get("/game/actions_available")
# def get_actions_available(game_id: str, player_game_id: int):
# json_path = os.path.join(GAMES_FOLDER, f"{game_id}.json")
# if not os.path.exists(json_path):
# return {"Success": False, "message": "Game not found"}
# with open(json_path, 'r') as json_file:
# game_dict = json.load(json_file)
# player_dict = game_dict[f"player{player_game_id}"]
# return {"Success": True, "actions": ["draw6cards"]}
# @app.post("/game/{id}/draw6cards")
# def create_game(id):