-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
330 lines (274 loc) · 10.7 KB
/
app.py
File metadata and controls
330 lines (274 loc) · 10.7 KB
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import json
from flask import Flask, request
import string
import random
from game import Game
app = Flask(__name__)
#shift everything to the middle of the screen more
#because it looks ew rn and imma forget to do it if i dont write it down
#we gotta make it so you can show/reroll your dice until the next player bets
#militsa
#dudo button
#says who clicked it
#exacto button
#only available to a few people
#^^^^YOU WON or YOU LOST
#play again button (keep track of data from sessions)
#refresh actions
#home page
#michelle
#display dice and reroll dice **buggy
#convert to ones button?
#we have to get rid of dice
#losing -> gray
#display who bet last
#game id make smaller text
#fix bug with rerolling dice
#peter
#make the server available online
#cheeky round!
#disable increasing face num
#when you get out change the order
#future
#make a computer player
#Uncaught TypeError: Cannot set property 'onmouseenter' of null at perudo_game.js:126
#make it more apparent when the turn changes
#resultion make a scrolling
#make reroll more obvious
#if she selected display dice, reset selection after display dice
#each die should only reroll once
##show which dice are displayed
#last dice doesnt select
#when you bet with 1's probability calculates with relation to 1's
#___________________________________Information on all players_________________________________________
all_games = {} # key is session ID, value is Game instance
possible_chars = string.ascii_uppercase + string.digits
SESSION_ID_LEN = 4
all_session_ids = []
session_cookies = {} # dictionary where key is session, value is list of cookies associated with that session
def update_session_cookies():
pass
#___________________________________GET Requests_________________________________________
@app.route('/')
def index():
return app.send_static_file('home.html')
@app.route('/getNewSession', methods=['GET'])
def generate_session_id():
session_id = ''.join(random.choice(possible_chars) for i in range(SESSION_ID_LEN))
while session_id in all_session_ids:
session_id = ''.join(random.choice(possible_chars) for i in range(SESSION_ID_LEN))
all_session_ids.append(session_id)
all_games[session_id] = Game()
return session_id, 200
@app.route('/dudo', methods=['GET'])
def dudo():
cup = request.args.get('cup')
text = "the dudo was done by cup " + str(cup)
return text
#GET the playerinfo dice dictionary
@app.route('/info', methods=['GET'])
def getInfo():
game_id = request.args.get('id')
return json.dumps({"success": True, "allInfo": all_games[game_id].playerInfo}), 200
#GET and return player positions
@app.route("/getPos", methods=["GET"])
def getPos():
game_id = request.args.get('id')
return json.dumps({"success": True, "data": all_games[game_id].playerInfo}), 200
#GET and return player's dice and displayed dice
@app.route("/getDisplayed", methods=["GET"])
def getDisplayedDice():
game_id = request.args.get('id')
playerInfo = all_games[game_id].playerInfo
data = {
"red":
{
"dice": playerInfo["red"]["dice_nums"],
"disp": playerInfo["red"]["displayed_dice"],
"dice_left": playerInfo["red"]["num_dice_left"]
},
"orange":
{
"dice": playerInfo["orange"][ "dice_nums"],
"disp": playerInfo["orange"]["displayed_dice"],
"dice_left": playerInfo["orange"]["num_dice_left"]
},
"yellow":
{
"dice": playerInfo["yellow"][ "dice_nums"],
"disp": playerInfo["yellow"]["displayed_dice"],
"dice_left": playerInfo["yellow"]["num_dice_left"]
},
"green":
{
"dice": playerInfo["green"][ "dice_nums"],
"disp": playerInfo["green"]["displayed_dice"],
"dice_left": playerInfo["green"]["num_dice_left"]
},
"blue":
{
"dice": playerInfo["blue"][ "dice_nums"],
"disp": playerInfo["blue"]["displayed_dice"],
"dice_left": playerInfo["blue"]["num_dice_left"]
},
"black":
{
"dice": playerInfo["black"][ "dice_nums"],
"disp": playerInfo["black"]["displayed_dice"],
"dice_left": playerInfo["black"]["num_dice_left"]
}
}
return json.dumps({"success": True, "data": data}), 200
@app.route("/getDoubt", methods=["GET"])
def getDoubt():
game_id = request.args.get('id')
return json.dumps({"success": True, "data": all_games[game_id].doubt}), 200
@app.route("/getTurnOrder", methods=["GET"])
def getTurnOrder():
game_id = request.args.get('id')
return json.dumps({"success": True, "data": all_games[game_id].turnOrder}), 200
#returns the index of the color whos turn it currently is
@app.route("/getCurrentTurnColor", methods=["GET"])
def getCurrentTurnColor():
game_id = request.args.get('id')
game = all_games[game_id]
return json.dumps({"success": True, "current_turn_color": game.turnOrder[game.current_turn_idx]}), 200
@app.route("/getStartSession", methods=["GET"])
def getStartSession():
game_id = request.args.get('id')
game = all_games[game_id]
return json.dumps({"success": True, "start_session": game.start_session}), 200
@app.route("/getHost", methods=["GET"])
def getPlayersInSession():
game_id = request.args.get('id')
game = all_games[game_id]
if(len(game.playerCodes) != 0):
return json.dumps({"success": True, "host": game.playerCodes[0]}), 200
else:
return json.dumps({"success": True, "host": "No Host"}), 404
@app.route("/getPreviousBet", methods=["GET"])
def getPreviousBet():
game_id = request.args.get('id')
game = all_games[game_id]
return json.dumps({"success": True, "prev_bet_count": game.last_bet[0], "prev_bet_face": game.last_bet[1]}), 200
#___________________________________POST Requests_________________________________________
@app.route('/gamePage', methods=['POST'])
def enter():
print('----------------from server: ' + request.args.get('id'))
return app.send_static_file('index.html'), 200
#POST the numbers of the player's dice to the server
@app.route("/postNums", methods = ["POST"])
def postNums():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
color_player = body["color"]
dice_nums = body["dice_nums"]
game.playerInfo[game.colors[color_player]]["dice_nums"] = dice_nums
return json.dumps({"success": True, "data": body}), 201
#POST the dice which are displayed
@app.route("/postDisplayed", methods = ["POST"])
def postDisplayed():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
color_player = body["color"]
displayed = body["displayed"]
game.playerInfo[game.colors[color_player]]["displayed_dice"] = displayed
return json.dumps({"success": True, "data": body}), 201
#POST the dice which are displayed
@app.route("/gameStart", methods = ["POST"])
def gameStart():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
for key in game.playerInfo:
game.playerInfo[key]["gameStart"] = True
color_player = body["color"]
game.playerInfo[game.colors[color_player]]["rank"] = game.playerCount
# print("color of player =" + str(color_player) + "rank = " + str(playerInfo[colors[color_player]]["rank"]))
game.playerCount +=1
return json.dumps({"success": True, "data": body}), 201
#POST the dice positions
@app.route("/postPos", methods = ["POST"])
def postPos():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
# print(str(body))
game.playerInfo["red"]["rank"] = body["redRank"]
game.playerInfo["orange"]["rank"] = body["orangeRank"]
game.playerInfo["yellow"]["rank"] = body["yellowRank"]
game.playerInfo["green"]["rank"] = body["greenRank"]
game.playerInfo["blue"]["rank"] = body["blueRank"]
game.playerInfo["black"]["rank"] = body["blackRank"]
return json.dumps({"success": True, "data": body}), 201
#for each die, make it's pos = the html pos
#prior to clicking start game, you can see the host, and cannot click the host.
@app.route("/postUsername", methods = ["POST"])
def postUsername():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
cup_color = body["color"]
game.playerInfo[cup_color]["username"] = body["username"]
return json.dumps({"success": True, "data": body}), 201
@app.route("/postDoubt", methods = ["POST"])
def postDoubt():
game_id = request.args.get('id')
game = all_games[game_id]
game.doubt = True
return json.dumps({"success": True}), 201
@app.route("/joinGame", methods=["POST"])
def join_game():
session_id = request.args.get('id').upper()
if session_id in all_session_ids:
update_session_cookies()
return session_id, 200
else:
return "That game ID does not exist.", 404
@app.route("/postTurnOrder", methods = ["POST"])
def postTurnOrder():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
game.turnOrder = body["turn_order"]
return json.dumps({"success": True}), 201
#increments turn_idx to represent who is playing currently
#also posts the new latest bet from the player
@app.route("/postNextTurn", methods = ["POST"])
def postNextTurn():
game_id = request.args.get('id')
game = all_games[game_id]
if (game.current_turn_idx < len(game.turnOrder) -1):
game.current_turn_idx += 1
else:
game.current_turn_idx = 0
body = json.loads(request.data)
game.last_bet[0] = body["bet_count"]
game.last_bet[1] = body["bet_face"]
return json.dumps({"success": True}), 201
@app.route("/postStartSession", methods = ["POST"])
def postStartSession():
game_id = request.args.get('id')
game = all_games[game_id]
game.start_session = True
return json.dumps({"success": True}), 201
@app.route("/postPlayerCode", methods = ["POST"])
def postPlayerCode():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
game.playerCodes.append(body["code"])
return json.dumps({"success": True}), 201
@app.route("/postLostDie", methods = ["POST"])
def postLostDie():
game_id = request.args.get('id')
game = all_games[game_id]
body = json.loads(request.data)
color_player = body["color"]
game.playerInfo[game.colors[color_player]]["num_dice_left"] -= 1
return json.dumps({"success": True}), 201
#___________________________________Run the server_________________________________________
if __name__ == "__main__":
app.run(threaded=True, port=5000, debug=True)