-
Notifications
You must be signed in to change notification settings - Fork 203
/
Copy pathserver.py
200 lines (157 loc) · 5.73 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
import socket
from _thread import *
from board import Board
import pickle
import time
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "localhost"
port = 5555
server_ip = socket.gethostbyname(server)
try:
s.bind((server, port))
except socket.error as e:
print(str(e))
s.listen()
print("[START] Waiting for a connection")
connections = 0
games = {0:Board(8, 8)}
spectartor_ids = []
specs = 0
def read_specs():
global spectartor_ids
spectartor_ids = []
try:
with open("specs.txt", "r") as f:
for line in f:
spectartor_ids.append(line.strip())
except:
print("[ERROR] No specs.txt file found, creating one...")
open("specs.txt", "w")
def threaded_client(conn, game, spec=False):
global pos, games, currentId, connections, specs
if not spec:
name = None
bo = games[game]
if connections % 2 == 0:
currentId = "w"
else:
currentId = "b"
bo.start_user = currentId
# Pickle the object and send it to the server
data_string = pickle.dumps(bo)
if currentId == "b":
bo.ready = True
bo.startTime = time.time()
conn.send(data_string)
connections += 1
while True:
if game not in games:
break
try:
d = conn.recv(8192 * 3)
data = d.decode("utf-8")
if not d:
break
else:
if data.count("select") > 0:
all = data.split(" ")
col = int(all[1])
row = int(all[2])
color = all[3]
bo.select(col, row, color)
if data == "winner b":
bo.winner = "b"
print("[GAME] Player b won in game", game)
if data == "winner w":
bo.winner = "w"
print("[GAME] Player w won in game", game)
if data == "update moves":
bo.update_moves()
if data.count("name") == 1:
name = data.split(" ")[1]
if currentId == "b":
bo.p2Name = name
elif currentId == "w":
bo.p1Name = name
#print("Recieved board from", currentId, "in game", game)
if bo.ready:
if bo.turn == "w":
bo.time1 = 900 - (time.time() - bo.startTime) - bo.storedTime1
else:
bo.time2 = 900 - (time.time() - bo.startTime) - bo.storedTime2
sendData = pickle.dumps(bo)
#print("Sending board to player", currentId, "in game", game)
conn.sendall(sendData)
except Exception as e:
print(e)
connections -= 1
try:
del games[game]
print("[GAME] Game", game, "ended")
except:
pass
print("[DISCONNECT] Player", name, "left game", game)
conn.close()
else:
available_games = list(games.keys())
game_ind = 0
bo = games[available_games[game_ind]]
bo.start_user = "s"
data_string = pickle.dumps(bo)
conn.send(data_string)
while True:
available_games = list(games.keys())
bo = games[available_games[game_ind]]
try:
d = conn.recv(128)
data = d.decode("utf-8")
if not d:
break
else:
try:
if data == "forward":
print("[SPECTATOR] Moved Games forward")
game_ind += 1
if game_ind >= len(available_games):
game_ind = 0
elif data == "back":
print("[SPECTATOR] Moved Games back")
game_ind -= 1
if game_ind < 0:
game_ind = len(available_games) -1
bo = games[available_games[game_ind]]
except:
print("[ERROR] Invalid Game Recieved from Spectator")
sendData = pickle.dumps(bo)
conn.sendall(sendData)
except Exception as e:
print(e)
print("[DISCONNECT] Spectator left game", game)
specs -= 1
conn.close()
while True:
read_specs()
if connections < 6:
conn, addr = s.accept()
spec = False
g = -1
print("[CONNECT] New connection")
for game in games.keys():
if games[game].ready == False:
g=game
if g == -1:
try:
g = list(games.keys())[-1]+1
games[g] = Board(8,8)
except:
g = 0
games[g] = Board(8,8)
'''if addr[0] in spectartor_ids and specs == 0:
spec = True
print("[SPECTATOR DATA] Games to view: ")
print("[SPECTATOR DATA]", games.keys())
g = 0
specs += 1'''
print("[DATA] Number of Connections:", connections+1)
print("[DATA] Number of Games:", len(games))
start_new_thread(threaded_client, (conn,g,spec))