-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
205 lines (122 loc) · 4.38 KB
/
app.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
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
import json
from flask import (Flask, render_template,
request, redirect, url_for,
session
)
from forms import PlayerForm
from model import GameSession, GameInfo
# not required for prod version
from flask_cors import CORS # for dev purposes
from helpers import (
vertical_moves,
horizontal_moves,
diagonal_right_cross,
diagonal_left_cross
)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
# app setup
app = Flask(__name__, template_folder="ui/build/", static_folder='ui/build/static')
CORS(app)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
def render(data):
return json.dumps(data)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
@app.route("/", methods=['POST', 'GET'])
def index():
formdata = {}
if request.data:
formdata = json.loads(request.data)
form = PlayerForm(**formdata)
if request.method == "POST":
if not form.validate():
out = {
"errors": {key: val[0] for key, val in form.errors.items() },
"status": -1
}
return render(out)
# set up the session for the game
mdl = GameInfo(
player_one=form.player_one.data,
player_two=form.player_two.data,
status=0
)
mdl.save()
# x makes the first move
out = {
"session": mdl.session,
"status": 0,
"current_player": "x"
}
return render(out)
return render_template("index.html", form=form)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
@app.route("/game/<player>/<sess>", methods=['GET', 'POST'])
def play_session(player, sess):
is_error = False
if request.method == 'POST':
if request.data:
# save a new play and return data back to the client
formdata = json.loads(request.data)
win = check_winnings(formdata['moves'], player)
draw_status = False
if check_plays(formdata['moves']) == 'draw':
draw_status = True
mdl = GameSession()
mdl.record_play(formdata['moves'], player, sess, winner=win, isdraw=draw_status)
else:
is_error = True
play_sess = GameSession()
out = {
"game_info": play_sess.get_game(sess),
"status": 0 if is_error == False else -1,
'errors': {"move": 'server error detected.' if is_error == True else None }
}
return render(out)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
def check_winnings(game_moves, player):
resp = vertical_moves(game_moves, player)
if resp == True:
return resp
resp = horizontal_moves(game_moves, player)
if resp == True:
return resp
resp = diagonal_right_cross(game_moves, player)
if resp == True:
return resp
resp = diagonal_left_cross(game_moves, player)
if resp == True:
return resp
return False
def check_plays(game_moves):
status = 'draw'
for row in game_moves:
for col in row:
if col == 0:
status = "gameon"
break
if status != "draw":
break
return status
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
@app.route("/gameover/<player>/<sess>")
def game_over(player, sess):
GameInfo(session=sess).end_game()
play_sess = GameSession()
play_sess.end_game(player, sess)
out = {
"game_info": play_sess.get_game(sess),
"status": 0
}
return render(out)
# +-------------------------+-------------------------+
# +-------------------------+-------------------------+
if __name__ == '__main__':
app.run(port=5000, debug=True)