-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
54 lines (37 loc) · 1.27 KB
/
server.py
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
from quart import Quart, render_template, request
from constants import ORANGE, BLUE
from ginkgopolis import GameController
from player import RandomPlayer, WebPlayer
from server_helpers import json_response
app = Quart(__name__, static_url_path='/static')
PLAYERS = [RandomPlayer('John', ORANGE), WebPlayer('Lisa', BLUE)]
GAME_CONTROLLER = GameController(PLAYERS)
@app.route("/")
def index():
return render_template('index.html')
@app.route("/board")
def board():
return json_response(GAME_CONTROLLER.board, app)
@app.route("/play")
async def play():
planned = await GAME_CONTROLLER.play_round()
return json_response(planned, app)
@app.route("/make_move/<player>", methods=['POST'])
async def make_move(player):
move = await request.get_json()
for p in PLAYERS:
if player == p.name and isinstance(p, WebPlayer):
await p.received_move(move)
return "Move received", 200
return "Player not found", 404
@app.route("/players")
async def players():
return json_response([p.name for p in PLAYERS], app)
@app.route("/players/<player>")
async def player(player):
for p in PLAYERS:
if player == p.name:
return json_response(p, app)
return "Player not found", 404
if __name__ == '__main__':
app.run()