-
Notifications
You must be signed in to change notification settings - Fork 22
/
server.py
351 lines (259 loc) · 12 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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
from time import time
from math import radians, floor, ceil
from threading import Thread
import terrain, saves, network, mobs, items, render_interface
from colours import colour_str, TERM_YELLOW
from console import log
from data import timings
from player import MAX_PLAYER_HEALTH
def _log_event(event, args):
log(' Event:', colour_str(event, fg=TERM_YELLOW))
log(' Args:', args)
log()
def log_event_send(*args, label=None):
log('Sending', '- [{}]'.format(label) if label else None)
_log_event(*args)
def log_event_receive(*args, label=''):
log('Received', '- [{}]'.format(label) if label else None)
_log_event(*args)
def dt(last_tick):
dt = 0
t = time()
tick_period = 1 / timings['tps']
if t >= tick_period + last_tick:
dt = int((t - last_tick) // tick_period)
last_tick += dt * tick_period
return dt, last_tick
class Server:
def __init__(self, player, save, port, settings, local_interface):
self.current_players = {}
self.local_player = player
self.local_interface = local_interface
self.game = Game(save, settings)
self.default_port = port
self.serving = False
def _update_clients(self, message, exclude=None):
log_event_send(message['event'], message['args'], label='Server')
for name, sock in self.current_players.items():
if name != exclude:
network.send(sock, message)
if self.local_player != exclude:
self.local_interface.handle(message)
def _player_list(self):
return list(self.current_players.keys()) + [self.local_player]
def handle(self, sock, data):
log_event_receive(data['event'], data['args'], label='Server')
result = (
{'get_chunks': self.event_get_chunks,
'set_player': self.event_set_player,
'get_players': self.event_get_players,
'get_mobs': self.event_get_mobs,
'get_items': self.event_get_items,
'set_blocks': self.event_set_blocks,
'get_time': self.event_get_time,
'player_attack': self.event_player_attack,
'splash_damage': self.event_splash_damage,
'respawn': self.event_respawn,
'logout': lambda: self.event_logout(sock),
'login': lambda name: self.event_login(name, sock),
'unload_slices': self.event_unload_slices
}[data['event']](*data.get('args', []))
)
if result is not None:
log_event_send(result['event'], result['args'], label='Server')
return result
# Handler and local interface mathods
def event_login(self, name, sock):
if name not in self.current_players.keys() and not name == self.local_player:
log('Logging in: ' + name)
if not name == self.local_player:
# local_player already contains the local_player name
self.current_players[name] = sock
self._update_clients({'event': 'set_players', 'args': [{name: self.game.get_player(name)}]})
else:
log('Not Logging in: ' + name)
return {'event': 'error', 'args': [{'event': 'login', 'message': 'Username in use'}]}
def event_logout(self, sock=None):
# Re-add all players which aren't the sock
players = {}
for name, conn in self.current_players.items():
if conn == sock:
log('Logging out', name, sock)
self._update_clients({'event': 'remove_player', 'args': [name]}, name)
else:
players[name] = sock
self.current_players = players
def event_set_blocks(self, blocks):
self._update_clients({'event': 'set_blocks', 'args': [self.game.set_blocks(blocks)]})
def event_get_chunks(self, chunk_list):
return {'event': 'set_chunks', 'args': list(self.game.get_chunks(chunk_list))}
def event_set_player(self, name, player):
self.game.set_player(name, player)
self._update_clients({'event': 'set_players', 'args': [{name: player}]}, exclude=name)
def event_get_players(self):
return {'event': 'set_players', 'args': [self.game.get_players(self._player_list())]}
def event_get_mobs(self):
return {'event': 'set_mobs', 'args': [self.game.mobs]}
def event_get_items(self):
return {'event': 'add_items', 'args': [self.game.items]}
def event_unload_slices(self, name, edges):
player = self.game.get_player(name)
player['edges'] = edges
self.game.set_player(name, player)
self.game.reload_slices()
def event_get_time(self):
return {'event': 'set_time', 'args': [self.game.time]}
def event_respawn(self, name):
player = self.game.get_player(name)
self.game._meta['items'].update(
items.new_item(player['x'], player['y'], player['inv'], self.game._last_tick, ttl=5*60))
player['inv'] = []
player['x'], player['y'] = self.game.spawn
player['health'] = MAX_PLAYER_HEALTH
self._update_clients({'event': 'set_players', 'args': [{name: player}]})
def event_player_attack(self, name, x, y, radius, strength):
updated_players, updated_mobs = self.game.player_attack(name, x, y, radius, strength)
self._update_clients({'event': 'set_players', 'args': [updated_players]})
self._update_clients({'event': 'set_mobs', 'args': [updated_mobs]})
def event_splash_damage(self, x, y, radius, strength):
updated_players, updated_mobs = self.game.splash_damage(x, y, radius, strength)
self._update_clients({'event': 'set_players', 'args': [updated_players]})
self._update_clients({'event': 'set_mobs', 'args': [updated_mobs]})
# Methods for local interface only:
def local_interface_login(self):
self._update_clients({
'event': 'set_players',
'args': [{self.local_player: self.game.get_player(self.local_player)}]
})
def local_interface_init_server(self):
self.serving = True
self.port, self._stop_server = network.start(self.handle, self.default_port)
log('Server started on port', self.port)
def local_interface_kill_server(self):
log('Server stopped')
self.serving = False
self._update_clients({'event': 'logout', 'args': ['Server Closed']}, exclude=self.local_player)
self.current_players = {}
self._stop_server()
self.port, self._stop_server = None, None
def local_interface_pause(self, paused):
if not self.serving:
self.game.pause(paused)
def local_interface_map(self):
return self.game._map
def local_interface_slice_heights(self):
return self.game._slice_heights
def local_interface_mobs(self):
return self.game.mobs
def local_interface_dt(self):
dt, time = self.game.dt()
if dt and time % 100 == 0:
self._update_clients({'event': 'set_time', 'args': [time]})
return dt, time
def local_interface_update_mobs(self):
updated_players, new_items = self.game.update_mobs()
self._update_clients({'event': 'set_players', 'args': [updated_players]})
self._update_clients({'event': 'set_mobs', 'args': [self.game.mobs]})
self._update_clients({'event': 'add_items', 'args': [new_items]})
def local_interface_spawn_mobs(self, *args):
self.game.spawn_mobs(*args)
self._update_clients({'event': 'set_mobs', 'args': [self.game.mobs]})
def local_interface_update_items(self):
removed_items = self.game.update_items()
self._update_clients({'event': 'remove_items', 'args': [removed_items]})
def local_interface_items(self):
return self.game.items
class Game:
""" The game. """
def __init__(self, save, settings):
self._save = save
self._map = {}
self._slice_heights = {}
self._meta = saves.get_meta(save)
self._last_tick = time()
self._settings = settings
def get_chunks(self, chunk_list):
new_slices = {}
new_slice_heights = {}
log('loading chunks', chunk_list)
# Generates new terrain
for chunk_n in chunk_list:
chunk, chunk_slice_heights = saves.load_chunk(self._save, chunk_n)
if not chunk:
chunk, chunk_slice_heights = terrain.gen_chunk(chunk_n, self._meta)
saves.save_chunk(self._save, chunk_n, chunk, chunk_slice_heights)
new_slices.update(chunk)
new_slice_heights.update(chunk_slice_heights)
log('new slices', new_slices.keys())
self._map.update(new_slices)
self._slice_heights.update(new_slice_heights)
return {key: ''.join(value) for key, value in new_slices.items()}, new_slice_heights
def set_blocks(self, blocks):
self._map, new_slices = saves.set_blocks(self._map, blocks)
saves.save_slices(self._save, new_slices, self._slice_heights)
return blocks
def set_player(self, name, player):
self._meta['players'][name].update(player)
saves.save_meta(self._save, self._meta)
def get_player(self, name):
self._meta = saves.load_player(name, self._meta)
return self._meta['players'][name]
def get_players(self, players):
""" Returns player objects """
return {name: self._meta['players'][name] for name in players}
# TODO: pause and unpause implementation
def pause(self, paused):
pass
def dt(self):
self._dt, self._last_tick = dt(self._last_tick)
self.time += self._dt
return self._dt, self.time
def reload_slices(self):
new_map = {}
for x, slice_ in self._map.items():
if any(x in range(*player['edges']) for player in self._meta['players'].values() if player.get('edges')):
new_map[x] = slice_
self._map = new_map
def player_attack(self, name, ax, ay, radius, strength):
return mobs.calculate_player_attack(name, ax, ay, radius, strength, self._meta['players'], self._meta['mobs'])
def splash_damage(self, dx, dy, radius, strength):
return mobs.calculate_player_attack(None, dx, dy, radius, strength, self._meta['players'], self._meta['mobs'])
def update_mobs(self):
if not self._settings.get('mobs'):
self._meta['mobs'].clear()
return {}, {}
updated_players, new_items = mobs.update(self._meta['mobs'], self._meta['players'], self._map, self._last_tick)
self._meta['items'].update(new_items)
return updated_players, new_items
def spawn_mobs(self, n_mob_spawn_cycles, bk_objects, sky_colour, day, lights):
if self._settings.get('mobs') and n_mob_spawn_cycles != 0:
for player in self._meta['players'].values():
px, py = player['x'], player['y']
width = 2 * mobs.spawn_player_range
height = 2 * mobs.spawn_player_range # TODO: Do we want this to be the full map height?
x_start = px - int(width * 0.5)
y_start = py - int(height * 0.5)
x_end = x_start + width
y_end = y_start + height
render_interface.create_lighting_buffer(width, height, x_start, y_start, self._map, self._slice_heights, bk_objects, sky_colour, day, lights)
for i in range(n_mob_spawn_cycles):
mobs.spawn(self._meta['mobs'], self._meta['players'], self._map, x_start, y_start, x_end, y_end)
def update_items(self):
removed_items = items.pickup_items(self._meta['items'], self._meta['players'])
removed_items += items.despawn_items(self._meta['items'], self._last_tick)
return removed_items
@property
def mobs(self):
return self._meta['mobs']
@property
def items(self):
return self._meta['items']
@property
def spawn(self):
return self._meta['spawn'], 1
@property
def time(self):
return self._meta['tick']
@time.setter
def time(self, time):
self._meta['tick'] = time