-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
275 lines (228 loc) · 7.14 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
import scipy
import socket
import numpy
import pickle
import curses
import random
import threading
import time
import queue
import sys
def nextkeygen(socket, id,np):
socket.setblocking(0)
global keychange,next_key,moveq, movearray,gameover
movearray = []
for i in range(np):
i = i
movearray.append("")
moveq = queue.Queue(maxsize=0)
gameover = True
while (gameover==True):
try:
data = socket.recv(1024*10)
data = pickle.loads(data)
if data in ["s","a","w","d"]:
next_key = data
keychange = True
# moveq.put((next_key,id))
movearray[id] = data
else:
next_key = -1
keychange = False
except OSError as err:
continue
s = socket.socket()
print ("Socket successfully created")
port = int(sys.argv[2])
s.bind((sys.argv[1], port))
print ("socket binded to =" ,port)
s.listen(5)
print ("socket is listening")
sh,sw = 0,0
counter = 0
nplayers = int(sys.argv[3])
playsoclist = [] #Sockets of Players
while len(playsoclist) < nplayers:
c, addr = s.accept()
print(c)
print ('Got connection from', addr)
playsoclist.append(c)
if len(playsoclist) > 0:
output = str(len(playsoclist))
c.send(pickle.dumps(output))
sh=35
sw=35
snk_x = sw//2
snk_y = sh//2
def gensnakes(tup):
if tup[2] ==1:
snk_y = tup[1]
snk_x = tup[0]
snake = [[snk_y, snk_x],[snk_y, snk_x-1],[snk_y, snk_x-2]]
return snake
else:
snk_y = tup[1]
snk_x = tup[0]
snake = [[snk_y, snk_x],[snk_y-1, snk_x],[snk_y-2, snk_x]]
return snake
def gameobjgen(x,y,nplayers):
headcord =[]
gameobjectlist = []
while True:
if len(headcord) == nplayers:
break
n_x = numpy.random.randint(5,x)
n_y = numpy.random.randint(5,y)
xory = numpy.random.randint(1,3)
if (n_x,n_y,xory) in headcord:
continue
else:
headcord.append((n_x,n_y,xory))
for item in headcord:
temp = gensnakes(item)
gameobjectlist.append(temp)
return gameobjectlist
# snake1 = [
# [snk_y, snk_x],
# [snk_y, snk_x-1],
# [snk_y, snk_x-2]
# ]
# snake2 = [
# [snk_y, snk_x-5],
# [snk_y, snk_x-6],
# [snk_y, snk_x-7]
# ]
# snake3 = [
# [snk_y+9, snk_x],
# [snk_y+10, snk_x],
# [snk_y+11, snk_x]
# ]
gameobjects = gameobjgen(23,23,nplayers)
# gameobjects.append(snake1)
# gameobjects.append(snake2)
# gameobjects.append(snake3)
heads = []
for i in range(len(gameobjects)):
heads.append([gameobjects[i][0][0],gameobjects[i][0][1]])
# heads.append([snake2[0][0],snake2[0][1]])
# heads.append([snake3[0][0],snake3[0][1]])
def collision(head,gameobjs,pid):
for i in range(len(gameobjs)):
if head in gameobjs[i][1:]:
if pid == i:
continue
return i
return -1
# Game loop
def Gameloop(key,playsoclist,pid,snake1,heads,gameobjects,np):
global keychange,next_key,moveq,headlist,snakeobj, movearray,rPlayers,gameover
gameover = True
rPlayers = np
movearray = []
for i in range(np):
movearray.append("")
headlist = heads
snakeobj = gameobjects
moveq = queue.Queue(maxsize=0)
keychange = False
next_key = -1
# mc = 0
while gameover==True:
if movearray[pid] != "":
key = movearray[pid]
movearray[pid] =""
print("Key = ", key )
new_head1 = [snake1[0][0],snake1[0][1]]
if key == "s":
new_head1[0] += 1
if key == "w":
new_head1[0] -= 1
if key == "a":
new_head1[1] -= 1
if key == "d":
new_head1[1] += 1
if key == "x":
for i in range(len(playsoclist)):
if playsoclist[i] != " ":
finallist= (str(pid+1),snake1,"Full")
# print(finallist)
playsoclist[i].send(pickle.dumps(finallist))
continue
if headlist[pid] == [-1,-1]:
break
snake1.insert(0, new_head1)
if snake1[0][0] in [0, sh] or snake1[0][1] in [0, sw] or new_head1 in headlist:
print("End")
fail = [[-1,-1]]
if playsoclist[pid] == " ":
break
playsoclist[pid].send(pickle.dumps((str(pid),fail,False)))
playsoclist[pid].close()
playsoclist[pid] = " "
rPlayers -=1
if new_head1 in headlist:
print("HEAD TO HEAD")
nPID = headlist.index(new_head1)
playsoclist[nPID].send(pickle.dumps((str(nPID),fail,False)))
playsoclist[nPID].close()
playsoclist[nPID] = " "
headlist[pid] = [-1,-1]
headlist[nPID] = [-1,-1]
rPlayers-=1
if rPlayers==0:
print("ALL LOSE")
gameover = False
quit()
if rPlayers == 1:
for plr in playsoclist:
if plr != " ":
finallist= (str(pid+1),snake1,True)
plr.send(pickle.dumps(finallist))
break
else:
print("Sending back move", snake1[0], "to ", pid)
headlist[pid] = new_head1
rpid = collision(new_head1, snakeobj,pid)
if rpid >= 0:
rPlayers-=1
fail = [[-1,-1]]
print("COLLISION\n\n\n\n")
print("Player ", pid+1, " collided with " , rpid+1)
headlist[rpid] = [-1,-1]
snakeobj[rpid] = [[headlist]]
if playsoclist[rpid] ==" ":
continue
playsoclist[rpid].send(pickle.dumps((str(rpid),fail,False)))
playsoclist[rpid].close()
playsoclist[rpid] = " "
time.sleep(0.3)
win = False
if rPlayers ==1:
win = True
for i in range(len(playsoclist)):
if playsoclist[i] != " ":
finallist= (str(pid+1),snake1,win)
playsoclist[i].send(pickle.dumps(finallist))
if win == True:
gameover = False
snake1.pop()
snakeobj[pid] = snake1
print("Start of game")
threadlist = []
gamestatelist = []
for i in range(nplayers):
t1 = threading.Thread(target=nextkeygen, args=(playsoclist[i],i,nplayers,))
threadlist.append(t1)
t2 = threading.Thread(target=Gameloop, args=("x",playsoclist,i,gameobjects[i],heads,gameobjects,nplayers,))
gamestatelist.append(t2)
for i in range(nplayers):
threadlist[i].start()
gamestatelist[i].start()
for i in range(nplayers):
threadlist[i].join()
gamestatelist[i].join()
print("Server out")
t1.join()
t2.join()
c.close()
s.close()