-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
88 lines (65 loc) · 1.97 KB
/
server.py
File metadata and controls
88 lines (65 loc) · 1.97 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
import socket
from _thread import *
import sys
server = "127.0.0.1"
port = 5555
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.bind((server, port))
except socket.error as e:
str(e)
s.listen()
print("Waiting for a connection, Server Started")
def read_pos(str):
str = str.split(",")
return (int(str[0]), int(str[1]), str[2], int(str[3]), int(str[4]), float(str[5]), int(str[6]), int(str[7]))
def make_pos(tup):
return str(tup[0]) + "," + str(tup[1]) + "," + tup[2] + "," + str(tup[3]) + "," + str(tup[4]) + "," + str(tup[5]) + "," + str(tup[6]) + "," + str(tup[7])
class Game:
def __init__(self, id):
self.pos = [(96 ,800-96, "right", 0, 0, 0.0, 0, 0),(96, 800-96, "right", 0, 0, 0.0, 0, 0)]
self.id = id
games = {}
idCounts = 0
def threaded_client(conn, player, gameId):
global idCounts
game = games[gameId]
conn.send(str.encode(make_pos(game.pos[player])))
reply = ""
while True:
try:
data = read_pos(conn.recv(2048).decode())
game.pos[player] = data
if not data:
print("Disconnected")
break
else:
if player == 1:
reply = game.pos[0]
else:
reply = game.pos[1]
print("Received: ", data)
print("Sending: ", reply)
conn.sendall(str.encode(make_pos(reply)))
except:
break
print("Lost connection")
try:
del games[gameId]
print("Closing Game", gameId)
except:
pass
idCounts -= 1
conn.close()
while True:
conn, addr = s.accept()
print("Connected to:", addr)
idCounts += 1
currentPlayer = 0
gameId = (idCounts - 1) // 2
if idCounts % 2 == 1:
games[gameId] = Game(gameId)
print("Creating a new game...")
else:
currentPlayer = 1
start_new_thread(threaded_client, (conn, currentPlayer, gameId ))