-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
460 lines (362 loc) · 16.3 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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
from dataclasses import dataclass
from threading import Thread
import unicodedata as ud
import json
import math
import time
from datetime import datetime
from random import randint
import eventlet
from flask_socketio import SocketIO
from flask import Flask, send_from_directory, render_template
from ip import ip_address, port
async_mode = None
app = Flask(__name__, static_url_path='')
socketio = SocketIO(app, async_mode=async_mode)
latin_letters = {}
SPAWN_R = 400
# TODO: rewrite this :D
def is_latin(uchr):
try:
return latin_letters[uchr]
except KeyError:
return latin_letters.setdefault(uchr, 'LATIN' in ud.name(uchr))
def only_roman_chars(unistr):
return all(is_latin(uchr)
for uchr in unistr
if uchr.isalpha())
@dataclass
class Point3d:
x: float
y: float
z: float
# Vector structure for collisions
class Point:
def __init__(self, a, b):
if type(a) == Point and type(b) == Point:
self.x = b.x - a.x
self.y = b.y - a.y
else:
self.x = a
self.y = b
def dp(self, other):
return self.x * other.x + other.y * self.y
def cp(self, other):
return self.x * other.y - other.x * self.y
# Player structure
@dataclass
class Player:
x: float
y: float
z: float
player_name: str
speed: float
heading: float
last_heading: float
x_trail: list
y_trail: list
z_trail: list
pressed_keys: set
booster: int = 0
score: int = 0
dead: bool = False
trail_size: int = 0
rotation: float = 0
boost_time: int = 0
toggle_controls_rotation: bool = True
reset: bool = True
max_turn_angle: float = 0
last_collision_check = None
last_seen = None
# Main class for all game functions
class Game:
def __init__(self) -> None:
self.AllPlayers = dict()
self.LastTrail = dict()
self.boosters = list()
self.LastTime = int(time.time() * 1000) # Current time in milliseconds
self.LastBoosters = int(time.time() * 1000)
self.TurnSpeed = 0.005
self.TurnMultiplier = 0.2
self.Speed = 0.07
self.StartPositions = [0, 180, 90, 270, 45, 225, 135, 315, 0, 200, 110, 290, 340, 160, 70, 250, 225, 320]
self.UsersNum = 0
self.MaxBoosters = 5
self.Winner = ''
self.GameStart = True
def player_reset(self):
if self.UsersNum == 0:
text = "Disintegrated"
socketio.emit("display_winner", json.dumps(text))
else:
text = "Winner:"
socketio.emit('display_winner', json.dumps(text + " " + self.Winner))
time.sleep(2)
self.GameStart = True
socketio.emit("clear")
TheGrid.UsersNum = 0
self.Winner = ""
for key in self.AllPlayers.keys():
elem = self.AllPlayers[key]
elem.last_collision_check = None
elem.x_trail = []
elem.y_trail = []
elem.z_trail = []
elem.trail_size = 0
elem.rotation = 0
elem.dead = False
elem.booster = 0
elem.speed = 0
elem.boost_time = 0
angle = TheGrid.StartPositions[TheGrid.UsersNum] * math.pi / 180
a = Point(SPAWN_R * math.cos(angle), SPAWN_R * math.sin(angle))
b = Point(0, 800)
elem.heading = math.atan2(a.cp(b), a.dp(b)) - math.pi
elem.y = 0
elem.x = SPAWN_R * math.cos(angle)
elem.z = SPAWN_R * math.sin(angle)
TheGrid.UsersNum += 1
# Collision check
def collision_check(self):
# TODO: Asymptotic of this algorithm seems very bad :(
global player
for player_key in self.AllPlayers.keys(): # Bike which we check
for enemy_key in self.AllPlayers.keys(): # Bike for collisions
player = self.AllPlayers[player_key]
enemy = self.AllPlayers[enemy_key]
for poly in range(self.AllPlayers[enemy_key].trail_size - 1):
try:
if player.last_collision_check is not None:
a = Point(player.last_collision_check.x, player.last_collision_check.y) # Bike coords
b = Point(player.x + 6 * math.sin(player.heading),
player.z + 6 * math.cos(player.heading)) # Second point
c = Point(enemy.x_trail[poly], enemy.z_trail[poly]) # Trail part 1
d = Point(enemy.x_trail[poly + 1], enemy.z_trail[poly + 1]) # Trail part 2
line1 = (Point(c, b).cp(Point(c, d)) > 0) == (Point(c, d).cp(Point(c, a)) > 0)
line2 = (Point(a, c).cp(Point(a, b)) > 0) == (Point(a, b).cp(Point(a, d)) > 0)
if line1 and line2:
parallel1 = max(a.x, b.x) >= min(c.x, d.x) and min(a.x, b.x) <= max(c.x, d.x)
parallel2 = max(a.y, b.y) >= min(c.y, d.y) and min(a.y, b.y) <= max(c.y, d.y)
if parallel1 and parallel2 and not player.dead:
player.dead = True
if player_key != enemy_key:
enemy.score += 1
self.UsersNum -= 1
if TheGrid.UsersNum <= 1:
TheGrid.Winner = enemy_key
TheGrid.player_reset()
except:
pass
player.last_collision_check = Point(player.x + 1 * math.sin(player.heading),
player.z + 1 * math.cos(player.heading))
for bike_key in self.AllPlayers.keys():
try:
dx = abs(self.LastTrail[bike_key].x - self.AllPlayers[bike_key].x)
dz = abs(self.LastTrail[bike_key].z - self.AllPlayers[bike_key].z)
if dx * dx + dz * dz > 50:
self.AllPlayers[bike_key].x_trail.append(self.AllPlayers[bike_key].x)
self.AllPlayers[bike_key].y_trail.append(self.AllPlayers[bike_key].y)
self.AllPlayers[bike_key].z_trail.append(self.AllPlayers[bike_key].z)
self.AllPlayers[bike_key].trail_size += 1
self.LastTrail[bike_key].x = self.AllPlayers[bike_key].x
self.LastTrail[bike_key].z = self.AllPlayers[bike_key].z
except:
pass
# Collision with boosters
for bike in self.AllPlayers.keys():
for boosterInd in range(len(self.boosters)):
dx = self.boosters[boosterInd].x - self.AllPlayers[bike].x
dz = self.boosters[boosterInd].z - self.AllPlayers[bike].z
if math.sqrt(dx * dx + dz * dz) <= 8 and self.AllPlayers[bike].booster <= 8:
if self.AllPlayers[bike].booster < TheGrid.MaxBoosters:
self.AllPlayers[bike].booster += 1
self.boosters.pop(boosterInd)
converted = []
for booster in self.boosters:
converted.append({"x": booster.x, "y": booster.y, "z": booster.z })
socketio.emit('booster', json.dumps(converted))
break
# Compute movements of all bikes since last calculation
def update(self):
current_time = int(time.time() * 1000) # Current time in milliseconds
if self.GameStart and current_time - self.LastTime < 450: return
self.GameStart = False
for bike_key in self.AllPlayers.keys(): # Iterate over all players
# Out of borders
if abs(self.AllPlayers[bike_key].x) > 500 or abs(self.AllPlayers[bike_key].z) > 800:
self.AllPlayers[bike_key].dead = True
self.UsersNum -= 1
if self.UsersNum <= 1:
self.player_reset()
if self.AllPlayers[bike_key].dead:
continue
if self.AllPlayers[bike_key].boost_time <= 0:
# Reset player speed to normal
self.AllPlayers[bike_key].speed = min(TheGrid.Speed, self.AllPlayers[bike_key].speed + 0.01)
else:
# Update boost time
self.AllPlayers[bike_key].boost_time -= (current_time - self.LastTime)
self.AllPlayers[bike_key].speed = min(TheGrid.Speed * 3, self.AllPlayers[bike_key].speed + 0.01)
# Bike vertical rotation
if self.AllPlayers[bike_key].reset:
# Reset to vertical state
if self.AllPlayers[bike_key].rotation > 0:
self.AllPlayers[bike_key].rotation = max(self.AllPlayers[bike_key].rotation - 0.03,
self.AllPlayers[bike_key].max_turn_angle)
else:
self.AllPlayers[bike_key].rotation = min(self.AllPlayers[bike_key].rotation + 0.03,
self.AllPlayers[bike_key].max_turn_angle)
else:
# Slowly turn
if self.AllPlayers[bike_key].max_turn_angle > 0:
# Right turn
self.AllPlayers[bike_key].rotation = min(self.AllPlayers[bike_key].rotation + 0.02,
self.AllPlayers[bike_key].max_turn_angle)
else:
# Left turn
self.AllPlayers[bike_key].rotation = max(self.AllPlayers[bike_key].rotation - 0.02,
self.AllPlayers[bike_key].max_turn_angle)
# Update heading (heading is updated through bike.rotation)
self.AllPlayers[bike_key].heading += (current_time - self.LastTime) * self.AllPlayers[
bike_key].rotation * self.TurnSpeed
speed = (current_time - self.LastTime) * self.AllPlayers[bike_key].speed
self.AllPlayers[bike_key].speed = max(0, self.AllPlayers[bike_key].speed - abs(self.AllPlayers[bike_key].heading - self.AllPlayers[bike_key].last_heading) * self.TurnMultiplier)
self.AllPlayers[bike_key].x += speed * math.sin(self.AllPlayers[bike_key].heading)
self.AllPlayers[bike_key].z += speed * math.cos(self.AllPlayers[bike_key].heading)
self.AllPlayers[bike_key].last_heading = self.AllPlayers[bike_key].heading
# Create boosters
if current_time - self.LastBoosters > (10000) and len(self.boosters) < 10 and self.UsersNum:
for i in range(min(3, 10 - len(self.boosters))):
# Field sizes
rx = 500
ry = 800
self.boosters.append(Point3d(randint(-rx, rx), 1, randint(-ry, ry)))
converted = []
for i in self.boosters:
converted.append({"x": i.x, "y": i.y, "z": i.z })
socketio.emit('booster', json.dumps(converted))
self.LastBoosters = current_time
self.LastTime = current_time
# Main page
@app.route('/')
def root():
return render_template('main.html')
# Get files from server (e.g. libs)
@app.route('/js/<path:path>')
def send_js(path):
return send_from_directory('js', path)
# Used for checking a name entered by the user
@app.route('/check/<username>')
def check(username):
if not only_roman_chars(username):
return '{"status": "", "error": "true"}'
return ['{"status": "false", "error": "false"}', '{"status": "true", "error": "false"}'][
username in TheGrid.AllPlayers.keys()]
@socketio.on('pingserver')
def ping(name):
if name in TheGrid.AllPlayers.keys():
TheGrid.AllPlayers[name].last_seen = int(time.time() * 1000)
@socketio.on('keyup')
def up(data):
username = data['user']
if data['key'] in TheGrid.AllPlayers[username].pressed_keys:
TheGrid.AllPlayers[username].pressed_keys.remove(data['key'])
try:
if (data['key'] == 65 and 68 not in TheGrid.AllPlayers[username].pressed_keys) or (data['key'] == 68 and 65 not in TheGrid.AllPlayers[username].pressed_keys): # A or D
TheGrid.AllPlayers[username].max_turn_angle = 0
TheGrid.AllPlayers[username].reset = True
except:
pass
@socketio.on('keydown')
def down(data):
username = data['user']
TheGrid.AllPlayers[username].pressed_keys.add(data['key'])
try:
if data['key'] == 65: # A
TheGrid.AllPlayers[username].max_turn_angle = 0.7
TheGrid.AllPlayers[username].reset = False
elif data['key'] == 68: # D
TheGrid.AllPlayers[username].max_turn_angle = -0.7
TheGrid.AllPlayers[username].reset = False
elif data['key'] == 16: # Shift
TheGrid.AllPlayers[username].speed = TheGrid.Speed * 3
TheGrid.AllPlayers[username].boost_time = 2000 * TheGrid.AllPlayers[username].booster
TheGrid.AllPlayers[username].booster = 0
elif data['key'] == 67: # C
TheGrid.AllPlayers[username].toggle_controls_rotation = not TheGrid.AllPlayers[
username].toggle_controls_rotation
except Exception as e:
print(e)
@socketio.on('message')
def handle_message(data):
pass
# When user chooses a name he submits his final name and we add him to the table
@socketio.on('add_user')
def add(username, mobile):
print(datetime.now(), "add_user")
if username not in TheGrid.AllPlayers.keys():
if len(TheGrid.AllPlayers) == 1:
TheGrid.player_reset()
TheGrid.LastTrail[username] = Point3d(0, 0, 0)
angle = TheGrid.StartPositions[TheGrid.UsersNum] * math.pi / 180
a = Point(SPAWN_R * math.cos(angle), SPAWN_R * math.sin(angle))
b = Point(0, 800)
heading = math.atan2(a.cp(b), a.dp(b)) - math.pi
TheGrid.AllPlayers[username] = Player(SPAWN_R * math.cos(angle), 0, SPAWN_R * math.sin(angle),
username, TheGrid.Speed, heading, heading, [], [], [], set())
TheGrid.UsersNum += 1
converted = []
for booster in TheGrid.boosters:
converted.append({"x": booster.x, "y": booster.y, "z": booster.z })
socketio.emit('booster', json.dumps(converted))
@socketio.on('remove_user')
def remove_user(username):
print('remove_user')
try:
del TheGrid.AllPlayers[username]
except:
pass
# We start a parallel thread for game logics
def game_loop(name):
while True:
TheGrid.update()
# Convert to JSON
converted = dict()
for player in TheGrid.AllPlayers.items():
converted[player[0]] = {'x': player[1].x,
'y': player[1].y,
'z': player[1].z,
'heading': player[1].heading,
'controls': player[1].toggle_controls_rotation,
'rotation': player[1].rotation,
'status': player[1].dead,
'boosters': player[1].booster,
'score': player[1].score}
if len(TheGrid.AllPlayers) != 0:
socketio.emit('update', json.dumps(converted))
time.sleep(0.01) # Default 0.01
# Second parallel thread for collision checks (They are much less frequent)
def collisions(name):
while True:
TheGrid.collision_check()
time.sleep(0.1)
def send():
while True:
for user in TheGrid.AllPlayers:
if TheGrid.AllPlayers[user].last_seen != None and int(time.time() * 1000) - TheGrid.AllPlayers[user].last_seen >= 20000:
del TheGrid.AllPlayers[user]
print(f"User {user} deleted due to inactivity")
break
socketio.emit('pingclient')
time.sleep(5)
if __name__ == "__main__":
TheGrid = Game()
eventlet.monkey_patch()
x = Thread(target=game_loop, args=(1,))
x.start()
y = Thread(target=collisions, args=(1,))
y.start()
z = Thread(target=send)
z.start()
print(f'Listening on http://{ip_address}:{port}')
socketio.run(app, host=ip_address, port=port)