-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
57 lines (41 loc) · 1.3 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
55
56
57
import json
from flask import Flask, request
from game import Game
app = Flask(__name__)
only_game = Game()
@app.route('/game/<int:game_id>', methods=["GET"])
def find_game(game_id):
msg = f"Game {game_id} doesn't exist yet!!"
return msg
@app.route('/state', methods=["GET"])
def game_state():
return json.dumps(only_game.get_state())
@app.route('/move', methods=["POST"])
def move():
data = request.json
unit_count = _check_valid_unit_count(data['unit_count'])
node_index_from = _check_valid_node(data["from"])
node_index_to = _check_valid_node(data["to"])
try:
only_game.move(unit_count, node_index_from, node_index_to)
except Exception as e:
return str(e), 400
return json.dumps(only_game.get_state())
@app.route('/end_turn', methods=["POST"])
def end_turn():
only_game.next_turn()
return 'ended.'
def _check_valid_unit_count(stuff):
return int(stuff)
def _check_valid_node(stuff):
return int(stuff)
@app.route('/add', methods=["POST"])
def reinforce():
data = request.json
unit_count = _check_valid_unit_count(data['unit_count'])
node_index = _check_valid_node(data["to"])
try:
only_game.reinforce(unit_count, node_index)
except Exception as e:
return str(e), 400
return json.dumps(only_game.get_state())