-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
265 lines (235 loc) · 9.52 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
import time
import random
import socket
from select import select
import messages
MOVELEFT = 1
MOVERIGHT = 3
MOVEUP = 2
MOVEDOWN = 4
E_RIPPLE = 2
E_LENS = 3
E_LIQUID = 4
E_SHAKY = 5
E_TWIRL = 6
E_WAVES = 7
E_WIN = 9
class Player:
def __init__(self, cid, position):
self.cid = cid
self.position = position
self.direction = 1
self.velocity = 0
self.effect = 0
self.alive = True
class Server:
def __init__(self, address):
self.address = address
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.socket.setblocking(0)
self.socket.bind(self.address)
self.running = False
self.sockets = dict()
f = open('levels/level2.txt', "r")
lines = f.readlines()
f.close()
self.worldGrid = []
for index in range(len(lines)):
self.worldGrid.insert(0, list(lines[index].strip()))
self.start_points = dict()
for cid in ["A", "B", "C", "D"]:
self.start_points[cid] = self.getPlayerStartPosition(cid)
def descriptors(self):
yield self.socket
for descriptor in self.sockets:
yield descriptor
def generate_id(self):
cid = False
while not cid:
cid = chr(random.randint(65,68))
for descriptor in self.sockets:
player = self.sockets[descriptor]
if player.cid == cid:
cid = False
return cid
def run(self):
if self.running:
return
remove = set()
self.socket.listen(4)
self.running = True
self.start_time = False
while self.running:
start = time.time()
r, w, x = select(self.descriptors(), [], [], 0)
for descriptor in r:
if descriptor == self.socket:
descriptor, address = self.socket.accept()
if len(self.sockets) > 3:
descriptor.send(messages.pack_server_full())
descriptor.close()
continue
if self.start_time is False:
self.start_time = time.time() + 10
print "New game starts in 10 seconds"
cid = self.generate_id()
print "%s: connected" % cid
position = self.start_points[cid]
player = Player(cid, position)
self.sockets[descriptor] = player
try:
descriptor.send(messages.pack_cid(cid, position[0], position[1]))
except socket.error, err:
remove.add(descriptor)
elif descriptor in self.sockets:
player = self.sockets[descriptor]
mid, data = None, None
try:
mid = descriptor.recv(1)
if mid:
length, unpacker = messages.get_unpacker(mid)
if length and self.start_time == True:
data = descriptor.recv(length)
if not data:
remove.add(descriptor)
if ord(mid) == 5:
player.alive = False
elif ord(mid) == 6: #key up
player.velocity = 0
elif ord(mid) == 7: #key down
key = unpacker(data)
player.direction = key
player.velocity = 1
else:
remove.add(descriptor)
except socket.error, err:
remove.add(descriptor)
for descriptor in self.sockets:
if self.start_time not in [False, True]:
if time.time() > self.start_time:
self.start_time = True
start_msg = messages.pack_start(5)
for client in self.sockets:
try:
client.send(start_msg)
except socket.error, err:
remove.add(client)
print "New game started"
elif self.start_time == False:
continue
player = self.sockets[descriptor]
if player.velocity:
if self.isMoveLegal(player, player.direction):
x,y = player.position
if player.direction == MOVELEFT:
player.position = x-1, y
elif player.direction == MOVERIGHT:
player.position = x+1, y
elif player.direction == MOVEUP:
player.position = x, y+1
elif player.direction == MOVEDOWN:
player.position = x, y-1
self.checkTile(player)
pos_msg = messages.pack_position(player.cid,
player.direction,
player.position[0],
player.position[1])
for client in self.sockets:
try:
client.send(pos_msg)
except socket.error, err:
remove.add(client)
if player.effect:
eat_msg = messages.pack_eat(player.cid,
player.effect,
player.position[0],
player.position[1])
for client in self.sockets:
try:
client.send(eat_msg)
except socket.error, err:
remove.add(client)
player.effect = 0
if not player.alive:
remove.add(descriptor)
death_msg = messages.pack_death(player.cid,
player.position[0],
player.position[1])
for client in self.sockets:
try:
client.send(death_msg)
except socket.error, err:
remove.add(client)
for client in remove:
try:
player = self.sockets[client]
print "%s: disconnected" % player.cid
del self.sockets[client]
except KeyError:
pass
if self.start_time == True and len(self.sockets) <= 0:
self.start_time = False
print "Server ready for new game"
snooze = 1/20.0 - (time.time()-start)
if snooze > 0:
time.sleep(snooze)
def isMoveLegal(self, player, movedir):
x, y = player.position
if movedir == MOVELEFT:
if self.worldGrid[y][x-1] == "1" or x <= 0:
return False
else:
return True
elif movedir == MOVEUP:
try:
if self.worldGrid[y+1][x] == "1" or y >= len(self.worldGrid)-1:
return False
else:
return True
except IndexError:
return False
elif movedir == MOVERIGHT:
try:
if self.worldGrid[y][x+1] == "1" or x >= len(self.worldGrid[0])-1:
return False
else:
return True
except IndexError:
return False
elif movedir == MOVEDOWN:
if self.worldGrid[y-1][x] == "1" or y <= 0:
return False
else:
return True
else:
return False
def checkTile(self, player):
x,y = player.position
if self.worldGrid[y][x] != '0':
if self.worldGrid[y][x] == str(E_RIPPLE):
player.effect = E_RIPPLE
elif self.worldGrid[y][x] == str(E_LENS):
player.effect = E_LENS
elif self.worldGrid[y][x] == str(E_LIQUID):
player.effect = E_LIQUID
elif self.worldGrid[y][x] == str(E_SHAKY):
player.effect = E_SHAKY
elif self.worldGrid[y][x] == str(E_TWIRL):
player.effect = E_TWIRL
elif self.worldGrid[y][x] == str(E_WAVES):
player.effect = E_WAVES
elif self.worldGrid[y][x] == str(E_WIN):
player.effect = E_WIN
else:
player.effect = 0
else:
player.effect = 0
def getPlayerStartPosition(self, cid):
for index, row in enumerate(self.worldGrid):
for jindex, col in enumerate(row):
if col == cid:
return (jindex, index)
if __name__ == "__main__":
s = Server(("", 10066))
s.run()