-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
217 lines (165 loc) · 6.28 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
from flask import Flask, request, jsonify
from random import randint
import chess
import chess.pgn
board = chess.Board()
white = True
finished = False
games = {}
from werkzeug.exceptions import BadRequest
aip_http_port = "5000"
app = Flask(__name__)
@app.route("/health")
def handle_health():
return "I'm healthy!\n"
@app.route("/turn")
def handle_turn():
req = request.get_json(force=True)
resp = ""
if "game_name" not in req or "user_name" not in req:
raise BadRequest(description="game_name/username missing")
else:
game_name = req["game_name"]
if len(games[game_name]["players"]) < 2:
return "Waiting for opponent"
else:
return {"turn" : "white" if games[str(req["game_name"])]["white"] else "black", "white" : games[str(req["game_name"])][req["user_name"]], "moves" : games[str(req["game_name"])]["moves"]}
@app.route(
"/create_game",
methods=["POST"],
)
def create_game():
global games
req = request.get_json(force=True)
if "game_name" in req and "user_name" in req:
game_name = req["game_name"]
user_name = req["user_name"]
if game_name == "" or user_name == "":
raise BadRequest(description="username/gamename is empty")
games[str(game_name)] = {'board':chess.Board(), 'players':[user_name], 'white':True, 'moves':[]}
else :
raise BadRequest(description="username not found")
response = {"response": "game created successfully"}
return jsonify(response), 200, {"Content-Type": "application/json; charset=utf-8"}
@app.route(
"/join_game",
methods=["POST"],
)
def join_game():
req = request.get_json(force=True)
if "game_name" in req and "user_name" in req:
game_name = req["game_name"]
user_name = req["user_name"]
if game_name == "" or user_name == "":
raise BadRequest(description="username/gamename is empty")
if game_name in games:
if len(games[game_name]['players']) == 1:
games[game_name]['players'].append(user_name)
elif user_name in games[game_name]['players']:
response = {"response": "game rejoined successfully" , "white": games[game_name][user_name]}
return jsonify(response), 200, {"Content-Type": "application/json; charset=utf-8"}
else:
raise BadRequest(description="game is full")
else :
raise BadRequest(description="username/game not found")
#assign teams
white = randint(0,1)
games[str(req["game_name"])][req["user_name"]] = True if white == 0 else False
black = games[str(req["game_name"])]["players"][0]
games[str(req["game_name"])][black] = False if white == 0 else True
response = {"response": "game join successfully" , "white": True if white == 0 else False}
return jsonify(response), 200, {"Content-Type": "application/json; charset=utf-8"}
@app.route(
"/move",
methods=["POST"],
)
def handle_move():
req = request.get_json(force=True)
(move,user_name,game_name) = validate_request(req)
print(move)
if(len(games[game_name]["players"]) == 1):
raise BadRequest(description="you have no opponent!")
response = {"ERROR": "wow"}
global white
global finished
if not finished:
resp = process_input(move,game_name)
response = {"response": str(resp)}
return jsonify(response), 200, {"Content-Type": "application/json; charset=utf-8"}
def validate_request(req):
if "user_name" in req and "game_name" in req and games[req["game_name"]]["white"] == games[req["game_name"]][req["user_name"]]:
return (req["move"],req["user_name"],req["game_name"])
elif "moves" in req["move"] or "fen" in req["move"] or "board" in req["move"] or "legal_moves" in req["move"] or "pgn" in req["move"]:
return (req["move"],req["user_name"],req["game_name"])
else:
raise BadRequest(description="Its not your turn")
@app.errorhandler(BadRequest)
def handle_bad_request(e):
response = {
"response": e.description
}
return jsonify(response), 400, {"Content-Type": "application/json; charset=utf-8"}
def process_input(input,game_name):
out = ""
global white
board = games[game_name]['board']
if input == "board":
return board
elif input == "legal_moves":
return board.legal_moves
elif input == "moves":
return games[game_name]["moves"]
elif input == "fen":
return board.fen()
elif input == "pgn":
return chess.pgn.Game.from_board(board)
else:
try:
board.push_san(input)
games[game_name]["moves"].append(input)
games[game_name]["white"] = not games[game_name]["white"]
except Exception as e:
raise BadRequest(description="invalid move")
out += check_check()
out += check_stalemate()
out += check_checkmate()
out += check_insufficient_material()
out += check_fivefold_repetition()
return out
def check_check():
if(board.is_check()):
if(white):
return "White is in check\n"
else:
return "Black is in check\n"
return ""
def check_stalemate():
global finished
if(board.is_stalemate()):
finished = True
return "Game has finished in Stalemate\n"
return ""
def check_checkmate():
global finished
if(board.is_checkmate()):
finished = True
if(white):
return ("Black wins\n")
else:
return ("White wins\n")
return ""
def check_insufficient_material():
global finished
if(board.is_insufficient_material()):
finished = True
return("Game has finished in Insufficient Material\n")
return ""
def check_fivefold_repetition():
global finished
if(board.is_fivefold_repetition()):
finished = True
return("Game has finished in Fivefold Repetition\n")
return ""
if __name__ == "__main__":
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = True
app.run(host="0.0.0.0", port=aip_http_port)