-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
70 lines (62 loc) · 1.64 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
import socket
from _thread import start_new_thread
from game import Game
import pickle
IP=socket.gethostbyname(socket.gethostname())
PORT=1234
ADDR=(IP,PORT)
FORMAT="utf-8"
server=socket.socket(socket.AF_INET,socket.SOCK_STREAM)
try:
server.bind(ADDR)
except Exception as e:
str(e)
server.listen()
print(f"\n[LISTENING] Listening For Connections...\n")
connected=set()
games={}
idCount=0
def handle_clients(client,p,gameId):
global idCount
client.send(str(p).encode(FORMAT))
while True:
try:
data=client.recv(2048*2).decode(FORMAT)
if gameId in games:
game=games[gameId]
if not data:
break
else:
if data=="reset":
game.reset()
elif data!="get":
game.play(p,data)
client.sendall(pickle.dumps(game))
else:
break
except:
break
try:
del games[gameId]
print(f"\n[LOST CONNECTION] Closing Game: {gameId}\n")
except:
pass
idCount-=1
client.close()
def run_server():
global idCount
while True:
client,address=server.accept()
print(f"\n[NEW CONNECTION] Connected To {address}\n")
idCount+=1
gameId=(idCount-1)//2
p=0
if idCount%2==1:
games[gameId]=Game(gameId)
print(f"\n[NEW GAME] Creating A New Game\n")
if idCount%2==0:
games[gameId].ready=True
p=1
start_new_thread(handle_clients,(client,p,gameId))
if __name__ == "__main__":
run_server()