-
Notifications
You must be signed in to change notification settings - Fork 0
/
testServer.py
32 lines (27 loc) · 928 Bytes
/
testServer.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
import socket
import json
HOST = '127.0.0.1'
PORT = 1239
def updateLeaderboard(scores):
#if new score < lowest score, return original leaderboard
if scores['newScore']['score'] < scores['scoreboard'][4]['score']:
return scores['scoreboard']
#appends new score to leaderboard, sorts, and returns sorted list
scores['scoreboard'].append(scores['newScore'])
newList = sorted(scores['scoreboard'], key=lambda sc: sc['score'])
newList.reverse()
newList.pop()
return newList
def startServer():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST, PORT))
s.listen()
conn, addr = s.accept()
data = conn.recv(1024)
clientData = json.loads(data.decode('utf-8'))
updatedBoard = updateLeaderboard(clientData)
print(updatedBoard)
response = json.dumps(updatedBoard).encode('utf-8')
conn.sendall(response)
print('sent server response')
startServer()