-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
473 lines (429 loc) · 17.9 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
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
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
from http.server import HTTPServer, BaseHTTPRequestHandler
from urllib.parse import urlparse, parse_qs
from functools import partial
import json
import argparse
import time
import random
import pymongo
import math
import signal
MAX_USER_ID = 9999
USER_ID_LENGTH = 4 # 4 digits
WIN_COOLDOWN = 3600 # 1 hour
IDLE_TIMER = 600 # Kick an idle user after 10 minutes
SABOTAGE_DURATION = 30 # 30 seconds
COUNTS_AS_CLOSE = 0.02 # Totem is within range of Gage and vice versa
DEFAULT_SABTOKENS = 3 # New playes get 3 tokens starting off
WIN_SABTOKENS = 2 # Earn 2 tokens for winning a game
SCORE_BASE = 5000 # 5000 seconds (1.39 h) until the score goes to 0
SCORE_BONUS_MULT = 20
SCORE_BONUS_MAX = 1000
client = pymongo.MongoClient("mongodb+srv://De1-SoC:l2b-11_sabotage@sabotage."
"q6bsa.mongodb.net/Sabotage?retryWrites="
"true&w=majority")
db = client["Sabotage"]
userIDCluster = db["IDs"]
usersCluster = db["Users"]
sessionsCluster = db["Sessions"]
# Given a player location [lat, lon], return a list of nearby sessions.
def getNearby(playerLoc):
def howClose(lat0, lon0, lat1, lon1):
return math.sqrt((float(lat1) - float(lat0))**2
+ (float(lon1) - float(lon0))**2)
locations = sessionsCluster.find()
closeLocs = filter(
lambda loc:
howClose(loc["Coord"][0], loc["Coord"][1],
playerLoc[0], playerLoc[1]) < COUNTS_AS_CLOSE,
locations
)
nearby = [{"SessionID": loc["SessionID"],
"Coord": loc["Coord"],
"Metrics": loc["Metrics"]}
for loc in closeLocs]
return nearby
# Generate a unique user ID
def generateUserID():
ids = random.sample(range(1, MAX_USER_ID+1), MAX_USER_ID)
for id in ids:
yield id
def calcScore(start, end, mod):
score = max(SCORE_BASE - (end - start), 0)
if score > 0:
score += min(mod * SCORE_BONUS_MULT, SCORE_BONUS_MAX)
return int(score)
# Callback to remove the Sabotage from a session
def sabotageTimeoutHandler(sessionID, saboteurID, sabNum, signum, frame, ):
session = sessionsCluster.find_one({"SessionID": sessionID})
if session is not None:
if (math.isclose(session["Sabotage"]["Duration"],
time.time(), abs_tol=1)
and saboteurID == session["Sabotage"]["SaboteurID"]
and sabNum == session["Sabotage"]["SabotageID"]):
sessionsCluster.update_one({
"SessionID": sessionID
}, {"$unset": {"Sabotage": ""}})
# Wrapper function for setting up the timeout
def sabotageTimeout(sessionID, saboteurID, sabNum):
handler = partial(sabotageTimeoutHandler, sessionID, saboteurID, sabNum)
signal.signal(signal.SIGALRM, handler)
signal.alarm(SABOTAGE_DURATION)
class HTTPRequestHandler(BaseHTTPRequestHandler):
def __init__(self, idGen, *args, **kwargs):
self.ids = idGen
super().__init__(*args, **kwargs)
"""
Given a user ID, check if the user exists.
If yes, then return the given ID and False (not a new user).
If no, then return a newly generated ID and true (is a new user).
"""
def checkUser(self, messageID):
if (not messageID):
try:
userID = next(self.ids)
while userIDCluster.find_one({"ID": userID}) is not None:
print(userIDCluster.find({"ID": userID}))
userID = next(self.ids)
print(f"New User, ID: {userID}")
userIDCluster.insert_one({"ID": userID})
return str(userID).zfill(USER_ID_LENGTH), True
except StopIteration:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, no availble user IDs".encode())
return "", False
else:
userID = messageID
if userIDCluster.find_one({"ID": int(userID)}) is None:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, user not in database".encode())
return "", False
print(f"User ID: {userID}")
return str(userID).zfill(USER_ID_LENGTH), False
def do_POST_DE1(self, message):
sessionID = message["SessionID"]
print("Received POST request from De1-SoC: " + sessionID)
session = sessionsCluster.find_one({"SessionID": sessionID})
# Session IDs should only be added to the database manually
if session is None:
print("Session not found")
self.send_response(400)
self.end_headers()
self.wfile.write("Error finding session".encode())
return
if ("Coord" in message):
lat = float(message["Coord"]["lat"])
lon = float(message["Coord"]["lon"])
print("Received new GPS coordinates:\n" +
"Latitude: " + str(lat) + '\n' +
"Longitude: " + str(lon))
storedCoord = session["Coord"]
if (not math.isclose(float(storedCoord[0]), lat, abs_tol=0.00005)
or not
math.isclose(float(storedCoord[1]), lon, abs_tol=0.00005)):
print(f"De1-SoC {sessionID} has moved!")
if lat != 0.0 and lon != 0.0:
sessionsCluster.update_one(
{"SessionID": sessionID},
{"$set": {
"Coord": [str(lat), str(lon)]
}})
sessionsCluster.update_one(
{"SessionID": sessionID},
{"$set": {"Moved": True}})
if lat == 0.0 and lon == 0.0:
print("Received lat lon (0, 0). Using last known location")
# Update numPlayers when updating a user's
# CurrSession instead
if ("NumPlayers" in message):
print("NumPlayers from POST /de1 deprecated! Do nothing")
if ("Win" in message):
winner = message["Win"]
winningUser = usersCluster.find_one({"UserID": winner})
if winningUser is None:
print("Winner not in database, reject \"win\" condition")
else:
if winningUser["CurrSession"]["ID"] == sessionID:
if winningUser["CurrSession"]["Win"]:
print(f"Player {winner} re-entered their win,"
" do nothing")
else:
print(f"Player {winner} has just won a game!")
usersCluster.update_one({"UserID": winner}, [
{"$set": {
"CurrSession": {
"Timeout": time.time() + WIN_COOLDOWN,
"Win": True
}}
}
])
usersCluster.update_one(
{"UserID": winner},
{"$inc": {"SabTokens": WIN_SABTOKENS}}
)
score = calcScore(
winningUser["CurrSession"]["Start"],
time.time(),
int(session["Metrics"]["NumPlayers"])
)
sessionsCluster.update_one({
"SessionID": sessionID
}, {"$push": {"Leaderboard": {winner: score}}})
else:
self.send_response(200)
self.end_headers()
self.wfile.write(f"Warning, {winner} tried to win"
" a session he's not in".encode())
return
self.send_response(200)
self.end_headers()
def leaveSession(self, user, clear=False):
oldSession = sessionsCluster.find_one({
"SessionID": user["CurrSession"]["ID"]
})
if oldSession is not None:
sessionsCluster.update_one({
"SessionID": user["CurrSession"]["ID"]
}, {"$inc": {"Metrics.NumPlayers": -1}})
if clear:
usersCluster.update_one({
"UserID": user["UserID"]
}, {"$unset": {"CurrSession": ""}})
def do_POST_App(self, message):
print("Received POST request from mobile app")
userID = message["UserID"]
user = usersCluster.find_one({"UserID": userID})
if user is None:
print("User not found in database")
self.send_response(400)
self.end_headers()
self.wfile.write("User not found in database".encode())
return
if ("SessionID" in message):
sessionID = message["SessionID"]
# Need to check != session second in boolean
if ("CurrSession" not in user or
user["CurrSession"]["ID"] != sessionID):
if "CurrSession" in user:
# "-1" is a flag for leave current session
if sessionID == "-1":
print("User leaving session")
self.leaveSession(user, clear=True)
self.send_response(200)
self.end_headers()
return
elif user["CurrSession"]["ID"] != sessionID:
print("User leaving previous session for new one")
oldSession = sessionsCluster.find_one({
"SessionID": user["CurrSession"]["ID"]
})
if oldSession is not None:
sessionsCluster.update_one({
"SessionID": user["CurrSession"]["ID"]
}, {"$inc": {"Metrics.NumPlayers": -1}})
# Make sure someone's not starting a game from across the city
try:
next(filter(
lambda loc: loc["SessionID"] == sessionID,
user["NearbySessions"]
))
except StopIteration:
print("Not a nearby session")
self.send_response(400)
self.end_headers()
self.wfile.write("Not a nearby session".encode())
return
startTime = time.time()
print(f"User {userID} starting at: {startTime}")
usersCluster.update_one({"UserID": userID}, [
{"$set": {"CurrSession": {
"ID": sessionID,
"Start": time.time(),
"Timeout": time.time() + IDLE_TIMER,
"Win": False
}}}
])
sessionsCluster.update_one({
"SessionID": sessionID
}, {"$inc": {"Metrics.NumPlayers": 1}})
if ("Sabotage" in message):
sabNum = message["Sabotage"]
print("Attempting sabotage: " + sabNum)
tokens = user["SabTokens"]
if tokens > 0:
usersCluster.update_one(
{"UserID": userID},
{"$inc": {"SabTokens": -1}}
)
sessionsCluster.update_one(
{"SessionID": sessionID},
[{"$set": {
"Sabotage": {
"SaboteurID": userID,
"SabotageID": sabNum,
"Duration": time.time() + SABOTAGE_DURATION
}
}}])
sabotageTimeout(sessionID, userID, sabNum)
else:
print("Attempted sabotage with no tokens, do nothing")
self.send_response(200)
self.end_headers()
def do_POST(self):
print("Handling POST request.")
# Reject requests that are not JSON
if (self.headers["Content-Type"] != "application/json"):
self.send_response(400)
self.end_headers()
self.wfile.write("Error, POST request is not a JSON string"
.encode())
return
length = int(self.headers["Content-Length"])
message = json.loads(self.rfile.read(length))
# SessionID is required for De1-SoC POST requests.
# All others are optional.
route = self.path.split('?')[0]
if route == "/de1":
self.do_POST_DE1(message)
elif route == "/app":
self.do_POST_App(message)
else:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, POST request with invalid route!"
.encode())
def parse_GET(self):
params = parse_qs(urlparse(self.path).query)
if (not params):
self.send_response(400)
self.end_headers()
self.wfile.write("Error, GET request missing parameters!".encode())
return "", ""
# Parse URL for ID parameter
userID, newUser = self.checkUser(int(params["UserID"][0]))
if (not userID):
return "", ""
if (newUser):
usersCluster.insert_one({
"UserID": userID,
"Timeout": time.time() + IDLE_TIMER,
"SabTokens": DEFAULT_SABTOKENS
})
return userID, params
def do_GET_sessions(self):
userID, params = self.parse_GET()
if userID == "" and params == "":
return # Headers handled in parse_GET
playerLoc = params["PlayerLoc"]
nearbySessions = getNearby(playerLoc)
usersCluster.update_one({"UserID": userID}, {
"$set": {"NearbySessions": nearbySessions}
})
responseMSG = {
"UserID": userID,
"Sessions": nearbySessions
}
print(responseMSG)
responseMSG = json.dumps(responseMSG)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(responseMSG.encode())
def do_GET_Lobby_Info(self):
userID, params = self.parse_GET()
if userID == "" and params == "":
return # Header handled in parse_GET
sessionID = params["SessionID"][0]
print(f"Getting session info for {sessionID}")
session = sessionsCluster.find_one({"SessionID": sessionID})
if session is None:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, session not found".encode())
return
if "Sabotage" in session:
# sabotage = session["Sabotage"]
sabotage = {
"SaboteurID": session["Sabotage"]["SaboteurID"],
"Sabotage": session["Sabotage"]["SabotageID"],
"Duration": session["Sabotage"]["Duration"]
}
else:
sabotage = None
if "NumPlayers" in session["Metrics"]:
numPlayers = str(session["Metrics"]["NumPlayers"])
else:
numPlayers = "0"
winner = usersCluster.find_one({"UserID": userID})
if winner is None:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, winner not found in database".encode())
return
sabTokens = winner["SabTokens"]
if "CurrSession" in winner:
win = winner["CurrSession"]["Win"]
else:
win = False
if "Leaderboard" in session:
leaderboard = session["Leaderboard"]
# It's a list of dicts with one key-value pair, which
# we sort by value
leaderboard.sort(
key=lambda x: list(x.values())[0],
reverse=True
)
else:
leaderboard = []
responseMSG = {} if sabotage is None else sabotage
responseMSG["NumPlayers"] = numPlayers
responseMSG["SabTokens"] = sabTokens
responseMSG["Win"] = win
responseMSG["Leaderboard"] = leaderboard
print(responseMSG)
responseMSG = json.dumps(responseMSG)
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
self.wfile.write(responseMSG.encode())
def do_GET(self):
print("Handling GET request")
route = self.path.split('?')[0]
if route == "/sessions":
self.do_GET_sessions()
elif route == "/lobby":
self.do_GET_Lobby_Info()
else:
self.send_response(400)
self.end_headers()
self.wfile.write("Error, GET request has invalid route!".encode())
def main():
parser = argparse.ArgumentParser(
description="Remote server that processes HTTP requests"
"from the De1-SoC and mobile app")
parser.add_argument("-i",
"--ip",
nargs="?",
metavar="IP_ADDRESS",
type=str,
help="IP address to run the server on",
default="" # Default is localhost
)
parser.add_argument("-p",
"--port",
metavar="SERVER_PORT",
type=int,
help="Port to run the server on",
required=True
)
args = parser.parse_args()
print("Serving at port: " + str(args.port))
print("With IP bound to: " + ("localhost" if args.ip == "" else args.ip))
userIDGenerator = generateUserID()
handler = partial(HTTPRequestHandler, userIDGenerator)
httpd = HTTPServer((args.ip, args.port), handler)
httpd.serve_forever()
if __name__ == "__main__":
main()