-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.py
More file actions
394 lines (331 loc) · 12.7 KB
/
Main.py
File metadata and controls
394 lines (331 loc) · 12.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
import json
import pprint
import random
import os
import math
from flask import Flask, session, render_template, request, url_for, abort, session
app = Flask(__name__)
@app.route('/')
def index_page():
''' Index of the Page '''
return render_template('index.html', default_people=DATA.DEFAULT_SIZE)
@app.route('/checkGameOver', methods=["POST"])
def check_game_over():
''' Given a game ID, checks if the spy was discovered or not based on
the current. '''
game_id = request.get_json()
game = DATA.games[int(game_id)]
status = game.checkGameOver()
return json.dumps(status)
@app.route('/newgame', methods=["POST"])
def new_game():
''' Creates a new game '''
# make new game
# L[0] = numPlayers
# L[1] = isFancy
L = request.get_json()
isFancy = L[1]
try:
num_players = int(L[0])
assert(num_players >= 1)
except:
return json.dumps(["error", "That is not a valid number of users."])
location_type = L[2]
time_limit = L[3]
if time_limit == "":
return json.dumps(["error", "You must put in a time limit."])
elif time_limit < 0:
return json.dumps(["error", "Time must be positive."])
password = L[4]
if password == "":
password = None
if (len(DATA.games) < DATA.GAME_CAP):
if num_players > DATA.MAX_SIZE:
num_players = DATA.MAX_SIZE
# Initialize the game
newGame = Game(num_players, isFancy, location_type,
time_limit, get_location(), password)
# Find new game location (available ID)
indexValue = DATA.next_game_id()
# Add the game in the right ID location
if indexValue is None:
# If indexValue is None, we must add more slots
DATA.games.append(newGame)
indexValue = len(DATA.games) - 1
else:
# Otherwise, we can add it to the regular location
DATA.games[indexValue] = newGame
# Return the ID of the game to the player (to get URL)
return json.dumps(["ok", indexValue])
else:
return json.dumps(["error", "Cannot Create More Games"])
@app.route('/how-to-play')
def how_to_play_f():
return render_template("how-to-play.html")
@app.route('/about-us')
def how_to_play():
return render_template("about-us.html")
@app.route('/save_drawings', methods=["POST"])
def save_the_whales():
''' '''
session['drawing'] = request.get_json()
return json.dumps("")
@app.route('/game/<id_num>', methods = ["GET", "POST"])
def join_game(id_num):
''' Connects user to existing game - if possible '''
try:
id_num = int(id_num)
except:
abort(404, description="Not a Valid Game ID")
if (id_num >= len(DATA.games) or (DATA.games[id_num] is None)):
abort(404, description="Not a Valid Game")
else:
game = DATA.games[int(id_num)]
role, location = game.join_game(int(id_num))
if game.password != None:
if request.method == "GET":
return render_template("login.html", gameID=id_num, incorrect=False)
elif request.form["password"] != game.password:
return render_template("login.html", gameID=id_num, incorrect=True)
return render_template("game.html", gameID=id_num,
numPlayers=game.current_players,
role=role,
maxPlayers=game.num_people,
location=location,
time=game.time_limit, passw=game.password)
@app.errorhandler(404)
def page_not_found(e, description):
return render_template('404.html', desc=description)
def get_role(location, fancy=True):
''' Given a location, returns one of the possible roles from the location.
If fancy is enabled, this will also choose some color and an adjective '''
role = random.choice(DATA.locations_dict[location])
if (fancy):
color = random.choice(DATA.colors_list)
adjective = random.choice(DATA.adjectives_list)
return (role, color + " " + adjective[0].upper() + adjective[1:] + " " + role)
else:
return role
def get_location():
''' Returns the user's location from the set of possible values '''
return random.choice(list(DATA.locations_dict.keys()))
class Data:
def __init__(self):
''' Initializes all of the data storage objects for the
flask application '''
self.GAME_CAP = 10
self.DEFAULT_SIZE = 5
self.MAX_SIZE = 100
self.adjectives_list = []
self.colors_list = []
self.locations_dict = {}
self.distanceMatrix = []
self.numPlayers = 0
self.games = []
self.playerList = {}
self.playerID = 0
self.MAX_PLAYER_ID = 10000
def load_json_adjectives(self):
''' Loads all of the adjectives and colors from the external info.JSON
file. Should only be done once per session. '''
json_data = open("info.json").read()
data = json.loads(json_data)
self.adjectives_list = data["adjectives"]
self.colors_list = data["colors"]
def load_json_roles(self):
'''
Gets all of the location and role data from the JSON file
'''
json_data = open("locations.json").read()
self.locations_dict = json.loads(json_data)
def next_game_id(self):
''' Gets the index of the game array to insert the new game,
or returns None if it should be appended '''
for i in range(len(self.games)):
if self.games[i] == None:
return i
return None
class Game:
''' Each game will be an instance of this object '''
def __init__(self, num_people, fancy=True,
location_type="random", time_limit=0, location=None, password=None):
''' Initializer for a new game '''
self.num_people = num_people
self.current_players = 0
self.location = location
# When the game starts, all of the roles are available
self.available_roles = DATA.locations_dict[self.location]
self.player_dictionary = {}
self.fancy = fancy
self.password = password
# TODO: add support for more than one spy, and allow user to put
# in how many spies they would like to have
self.num_spies = 0
self.num_max_spies = 1
self.spy_id = -1
random.shuffle(self.available_roles)
self.pick_spy()
self.time_limit = time_limit
def pick_spy(self):
''' Decides which people will be the spies '''
self.spy_id = random.randint(0, self.num_people-1)
self.num_spies += 1
def update_size(self, num_people):
''' Change the number of people currently in the game '''
self.num_people = num_people
def join_game(self, gameid):
''' Allows a new person to join the game '''
if 'drawing' in session:
drawings = session['drawing']
else:
session['drawing'] = []
if 'gameid' in session and session['gameid'] == gameid: # person is already here
return (self.player_dictionary[session['userid']][0],
self.location)
session['gameid'] = gameid
session['userid'] = self.current_players
# When a new person joins a game, we need to increment the number of
# people in the game, and then give them a role that is available
if self.current_players < self.num_people:
if self.current_players == self.spy_id:
self.player_dictionary[self.current_players] = ["Spy", None]
self.current_players += 1
return ("Spy", "UNKNOWN")
else:
role = self.available_roles[self.current_players % len(self.available_roles)]
if (self.fancy):
role = self.fancify_role(role)
self.player_dictionary[self.current_players] = [role, None]
self.current_players += 1
return (role, self.location)
else:
abort(404, description="Too Many People in Game")
def fancify_role(self, role):
''' Takes a regular role and makes it fancies '''
color = random.choice(DATA.colors_list)
adjective = random.choice(DATA.adjectives_list)
return color + " " + adjective[0].upper() + adjective[1:] + " " + role
def set_custom_location(self, location, roles):
''' Allows the player to put in a custom location with associated
roles.
@params:
location - String
roles - List'''
pass
def make_guess(self, layer_guessing, guess):
'''
Allows a single user to make a guess for who they think the
spy is.
'''
self.player_dictionary[player_guessing][1] = guess
def checkGameOver(self):
'''
Checks if the game is over
'''
timesAccused = [0 for i in range(self.num_people)]
print(self.player_dictionary)
for player_id, player_tuple in self.player_dictionary:
timesAccused[player_tuple[1]] += 1
@app.route('/gpsdata', methods=["POST"])
def gpsdata():
''' Gets GPS coordinates from the user, assigns an ID, and
appends to the list'''
data = request.data
playerID = getID()
DATA.playerList[playerID] = {data}
print(playerID)
session['playerID'] = playerID
#newDistEntry = updatePlayerMatrix(playerID)
#DATA.distanceMatrix.append(newDistEntry)
return "Player is in the database"
def getID():
'''Takes the data variable playerID, assigns it to a user, and
then increments by 1 to give the player a unique id'''
playerID = DATA.playerID
if 'playerID' in session:
return (session['playerID'])
DATA.playerID += 1
updateNumPlayers()
return playerID
def updateNumPlayers():
DATA.numPlayers = len(DATA.playerList.keys())
return
''' def updatePlayerMatrix(playerID):
keyList = list(DATA.playerList.keys())
print(keyList)
if len(keyList) == 1:
keyList = []
else:
keyList.remove(playerID)
print(keyList)
if len(keyList) <= 0:
return
newPlayerMatrix = []
for i in range (DATA.numPlayers):
print(i)
newPlayerMatrix[i] = calculateDist(playerID,keyList[i])
return newPlayerMatrix
'''
def calculateDist(userID1,userID2):
'''Finds all the locations in the list that are within x miles of
user. Uses the formula from '''
# the data associated with a user ID is a weird set, this extracts
# the actual coordinates
csv1 = str(DATA.playerList[userID1])[3:-2]
csv2 = str(DATA.playerList[userID2])[3:-2]
gps1 = csv1.split(',')
gps2 = csv2.split(',')
gps1 = list(map(float,gps1))
gps2 = list(map(float,gps2))
lat1 = gps1[0]
lon1 = gps1[1]
lat2 = gps2[0]
lon2 = gps2[1]
distance = distFromLats(lat1,lon1,lat2,lon2)
print(distance)
return distance
def degreesToRadians(degrees):
'''degrees --> radians '''
return degrees * 3.14159265 / 180
def distFromLats(lat1,lon1,lat2,lon2):
'''formula for calculating distance between spherical coordinates, outputs distance'''
earthRadiusKm = 6371
dLat = degreesToRadians(lat2-lat1)
dLon = degreesToRadians(lon2-lon1)
lat1 = degreesToRadians(lat1)
lat2 = degreesToRadians(lat2)
a = math.sin(dLat/2) * math.sin(dLat/2) + math.sin(dLon/2) * math.sin(dLon/2) * math.cos(lat1) * math.cos(lat2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1-a))
return earthRadiusKm * c
@app.errorhandler(404)
def page_not_found(description):
return render_template('404.html', desc=description)
@app.route('/role_test')
def get_some_role():
return get_role(get_location())
def get_location():
''' Returns the user's location from the set of possible values '''
return random.choice(list(DATA.locations_dict.keys()))
def get_role(location, fancy=True):
''' Given a location, returns one of the possible roles from the location.
If fancy is enabled, this will also choose some color and an adjective '''
role = random.choice(DATA.locations_dict[location])
if (fancy):
color = random.choice(DATA.colors_list)
adjective = random.choice(DATA.adjectives_list)
return (role, color + " " + adjective[0].upper() + adjective[1:] + " " + role)
else:
return role
def testing():
''' A function used on startup if in debugging mode '''
print("In debugging mode")
print(get_role(get_location(), True))
if __name__ == "__main__":
app.secret_key = "12837198276348971263784961278364182736"
debugging = True
app.config['DEBUG'] = debugging
DATA = Data()
DATA.load_json_adjectives()
DATA.load_json_roles()
DATA.debugging = debugging
app.run(host='0.0.0.0')