-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket_server.py
84 lines (59 loc) · 2 KB
/
socket_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
import socket
import threading
import sqlite3
import config
PORT = config.SOCKET_PORT
SERVER = config.SOCKET_HOST
print(SERVER)
ADDR = (SERVER, PORT)
HEADER = 8
FORMAT = "utf-8"
DISCONNECT_MESSAGE = "!DISCONNECT"
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(ADDR)
def parse_old_elo(elo_list):
elo_string = ""
for elo in elo_list:
elo_string += f"{elo}/"
elo_string = elo_string[:-1]
return elo_string
def parse_new_stats(msg):
new_stats = {}
player_list = msg.split("/")
for player in player_list:
player_split = player.split("$")
new_stats[player_split[0]] = player_split[1]
for key in new_stats:
new_stats[key] = new_stats[key].split(":")
value_list = []
for value in new_stats[key]:
value_list.append(value)
value_list = [int(x) for x in value_list]
new_stats[key] = value_list
return new_stats
def handle_client(conn, addr):
connected = True
while connected:
msg_length = conn.recv(HEADER)
msg_length = int(msg_length)
msg = conn.recv(msg_length).decode(FORMAT)
print(msg)
steam_id_list = msg.split("/")
elo_list = retrieve_elo_from_database(steam_id_list) # ADD YOUR FUNCTION TO RETRIEVE THE ELO FROM THE DB
elo_string = parse_old_elo(elo_list)
conn.send(elo_string.encode(FORMAT))
msg2_length = conn.recv(HEADER)
msg2_length = int(msg2_length)
msg2 = conn.recv(msg2_length).decode(FORMAT)
new_stats = parse_new_stats(msg2)
print(new_stats)
store_info_in_database(new_stats) # ADD YOUR FUNCTION TO STORE THE NEW STATS IN THE DB
connected = False
def start():
server.listen()
print("Server started...")
while True:
conn, addr = server.accept()
thread = threading.Thread(target=handle_client, args=(conn, addr))
thread.start()
start()