-
Notifications
You must be signed in to change notification settings - Fork 1
/
Server.py
50 lines (37 loc) · 1.25 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
from flask import Flask, request
import json
import sys
import os
from importlib import import_module
games = {}
app = Flask(__name__)
@app.route('/game_init', methods=['POST'])
def game_init():
state = request.get_json(force=True)
my_game = Strategy(state)
games[state.get('gameId', str(len(games)))] = my_game
setup = my_game.get_setup()
return json.dumps(setup)
@app.route('/turn', methods=['POST'])
def turn():
game_state = request.get_json(force=True)
game_id = game_state.get('gameId', 'game1')
if not game_id in games:
games[game_id] = Strategy(game_state)
my_game = games[game_id]
my_game.update_game(game_state)
decision = my_game.do_turn()
return json.dumps(decision)
@app.route('/game_over', methods=['POST'])
def game_over():
end_state = request.get_json(force=True)
game = games.pop(end_state.get('gameId', 'game'), None)
game.game_over(end_state.get('result', 'TIE'));
return json.dumps(end_state)
@app.route('/health', methods=['GET'])
def health():
return "200"
if __name__ == "__main__":
module = import_module(sys.argv[1]) # import the player strategy file
Strategy = getattr(module, 'Strategy')
app.run(host='0.0.0.0', port=int(os.environ['PORT']), debug=True)