forked from chazkii/man.battleships
-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_server.py
50 lines (33 loc) · 1.24 KB
/
game_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
import os
from flask import Flask, jsonify
from flask_cors import CORS
from play_game import play_match
from web.formatter import format_match_output
app = Flask(__name__)
CORS(app)
@app.route("/")
def landing_page():
return "Welcome to Battleships!"
@app.route("/bot_names/")
def get_bot_names():
# TODO: This is a horrible hack. Fix it
bots_path = str(os.sep.join(os.path.realpath(__file__).split(os.sep)[:-1])) + os.sep + 'bots'
bot_names = [bot.split('.')[0] for bot in os.listdir(bots_path) if bot not in ['__init__.py', '__pycache__']]
return jsonify(bot_names)
@app.route("/play_game/<player_1>/<player_2>")
def do_play_game(player_1: str, player_2: str):
return jsonify(format_match_output(play_match(player_1, player_2, 1)))
@app.route("/play_match/<player_1>/<player_2>")
def do_play_match(player_1: str, player_2: str):
"""
Plays a match of games between player_1 and player_2, and returns a json document summarising the games for visualisation
:param player_1: str
:param player_2: str
:param games: int
:return:
"""
return jsonify(play_match(player_1, player_2))
def run_server():
app.run("0.0.0.0", port=5678, threaded=False, debug=True)
if __name__ == "__main__":
run_server()