/
gameconnection.py
634 lines (531 loc) · 20.8 KB
/
gameconnection.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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
import asyncio
import contextlib
from server.db import FAFDatabase
from sqlalchemy import or_, select, text
from .abc.base_game import GameConnectionState
from .config import TRACE
from .db.models import (
coop_leaderboard, coop_map, login, moderation_report, reported_user,
teamkills
)
from .decorators import with_logger
from .game_service import GameService
from .games.game import Game, GameError, GameState, ValidityState, Victory
from .player_service import PlayerService
from .players import Player, PlayerState
from .protocol import (
DisconnectedError, GpgNetServerProtocol, QDataStreamProtocol
)
@with_logger
class GameConnection(GpgNetServerProtocol):
"""
Responsible for connections to the game, using the GPGNet protocol
"""
def __init__(
self,
database: FAFDatabase,
game: Game,
player: Player,
protocol: QDataStreamProtocol,
player_service: PlayerService,
games: GameService,
state: GameConnectionState = GameConnectionState.INITIALIZING
):
"""
Construct a new GameConnection
"""
super().__init__()
self._db = database
self._logger.debug('GameConnection initializing')
self.protocol = protocol
self._state = state
self.game_service = games
self.player_service = player_service
self._player = player
player.game_connection = self # Set up weak reference to self
self._game = game
self.finished_sim = False
@property
def state(self) -> GameConnectionState:
return self._state
@property
def game(self) -> Game:
return self._game
@game.setter
def game(self, val: Game):
self._game = val
@property
def player(self) -> Player:
return self._player
@player.setter
def player(self, val: Player):
self._player = val
def is_host(self) -> bool:
if not self.game or not self.player:
return False
return (
self.player.state == PlayerState.HOSTING and
self.player == self.game.host
)
async def send(self, message):
"""
Send a game message to the client.
:raises: DisconnectedError
NOTE: When calling this on a connection other than `self` make sure to
handle `DisconnectedError`, otherwise failure to send the message will
cause the caller to be disconnected as well.
"""
message['target'] = "game"
self._logger.log(TRACE, ">> %s: %s", self.player.login, message)
await self.protocol.send_message(message)
async def _handle_idle_state(self):
"""
This message is sent by FA when it doesn't know what to do.
:return: None
"""
assert self.game
state = self.player.state
if state == PlayerState.HOSTING:
self.game.state = GameState.LOBBY
self._state = GameConnectionState.CONNECTED_TO_HOST
self.game.add_game_connection(self)
self.game.host = self.player
elif state == PlayerState.JOINING:
return
else:
self._logger.error("Unknown PlayerState: %s", state)
await self.abort()
async def _handle_lobby_state(self):
"""
The game has told us it is ready and listening on
self.player.game_port for UDP.
We determine the connectivity of the peer and respond
appropriately
"""
player_state = self.player.state
if player_state == PlayerState.HOSTING:
await self.send_HostGame(self.game.map_folder_name)
self.game.set_hosted()
# If the player is joining, we connect him to host
# followed by the rest of the players.
elif player_state == PlayerState.JOINING:
await self.connect_to_host(self.game.host.game_connection)
if self._state is GameConnectionState.ENDED:
# We aborted while trying to connect
return
self._state = GameConnectionState.CONNECTED_TO_HOST
try:
self.game.add_game_connection(self)
except GameError as e:
await self.abort(f"GameError while joining {self.game.id}: {e}")
return
tasks = []
for peer in self.game.connections:
if peer != self and peer.player != self.game.host:
self._logger.debug("%s connecting to %s", self.player, peer)
tasks.append(self.connect_to_peer(peer))
await asyncio.gather(*tasks)
async def connect_to_host(self, peer: "GameConnection"):
"""
Connect self to a given peer (host)
:return:
"""
if not peer or peer.player.state != PlayerState.HOSTING:
await self.abort("The host left the lobby")
return
await self.send_JoinGame(peer.player.login, peer.player.id)
if not peer:
await self.abort("The host left the lobby")
return
await peer.send_ConnectToPeer(
player_name=self.player.login,
player_uid=self.player.id,
offer=True
)
async def connect_to_peer(self, peer: "GameConnection"):
"""
Connect two peers
:return: None
"""
if peer is not None:
await self.send_ConnectToPeer(
player_name=peer.player.login,
player_uid=peer.player.id,
offer=True
)
if peer is not None:
with contextlib.suppress(DisconnectedError):
await peer.send_ConnectToPeer(
player_name=self.player.login,
player_uid=self.player.id,
offer=False
)
async def handle_action(self, command, args):
"""
Handle GpgNetSend messages, wrapped in the JSON protocol
:param command: command type
:param args: command arguments
:return: None
"""
try:
await COMMAND_HANDLERS[command](self, *args)
except KeyError:
self._logger.warning(
"Unrecognized command %s: %s from player %s",
command, args, self.player
)
except (TypeError, ValueError):
self._logger.exception("Bad command arguments")
except ConnectionError as e:
raise e
except Exception: # pragma: no cover
self._logger.exception("Something awful happened in a game thread!")
await self.abort()
async def handle_desync(self, *_args): # pragma: no cover
self.game.desyncs += 1
async def handle_game_option(self, key, value):
if not self.is_host():
return
if key == "Victory":
self.game.gameOptions["Victory"] = Victory.from_gpgnet_string(value)
else:
self.game.gameOptions[key] = value
if key == "Slots":
self.game.max_players = int(value)
elif key == "ScenarioFile":
raw = repr(value)
self.game.map_scenario_path = \
raw.replace("\\", "/").replace("//", "/").replace("'", "")
self.game.map_file_path = 'maps/{}.zip'.format(
self.game.map_scenario_path.split("/")[2].lower()
)
elif key == "Title":
self.game.name = self.game.sanitize_name(value)
self._mark_dirty()
async def handle_game_mods(self, mode, args):
if not self.is_host():
return
if mode == "activated":
# In this case args is the number of mods
if int(args) == 0:
self.game.mods = {}
elif mode == "uids":
uids = str(args).split()
self.game.mods = {uid: "Unknown sim mod" for uid in uids}
async with self._db.acquire() as conn:
result = await conn.execute(
text("SELECT `uid`, `name` from `table_mod` WHERE `uid` in :ids"),
ids=tuple(uids))
async for row in result:
self.game.mods[row["uid"]] = row["name"]
else:
self._logger.warning("Ignoring game mod: %s, %s", mode, args)
return
self._mark_dirty()
async def handle_player_option(self, player_id, command, value):
if not self.is_host():
return
self.game.set_player_option(int(player_id), command, value)
self._mark_dirty()
async def handle_ai_option(self, name, key, value):
if not self.is_host():
return
self.game.set_ai_option(str(name), key, value)
self._mark_dirty()
async def handle_clear_slot(self, slot):
if not self.is_host():
return
self.game.clear_slot(int(slot))
self._mark_dirty()
async def handle_game_result(self, army, result):
army = int(army)
result = str(result).lower()
try:
label, score = result.split(" ")
await self.game.add_result(self.player.id, army, label, int(score))
except (KeyError, ValueError): # pragma: no cover
self._logger.warning("Invalid result for %s reported: %s", army, result)
async def handle_operation_complete(self, army, secondary, delta):
if not int(army) == 1:
return
if self.game.validity != ValidityState.COOP_NOT_RANKED:
return
secondary, delta = int(secondary), str(delta)
async with self._db.acquire() as conn:
# FIXME: Resolve used map earlier than this
result = await conn.execute(
select([coop_map.c.id]).where(
coop_map.c.filename == self.game.map_file_path
)
)
row = await result.fetchone()
if not row:
self._logger.debug("can't find coop map: %s", self.game.map_file_path)
return
mission = row["id"]
await conn.execute(
coop_leaderboard.insert().values(
mission=mission,
gameuid=self.game.id,
secondary=secondary,
time=delta,
player_count=len(self.game.players),
)
)
async def handle_json_stats(self, stats):
self.game.report_army_stats(stats)
async def handle_enforce_rating(self):
self.game.enforce_rating = True
async def handle_teamkill_report(self, gametime, reporter_id, reporter_name, teamkiller_id, teamkiller_name):
"""
Sent when a player is teamkilled and clicks the 'Report' button.
:param gametime: seconds of gametime when kill happened
:param reporter_id: reporter id
:param reporter_name: reporter nickname
:param teamkiller_id: teamkiller id
:param teamkiller_name: teamkiller nickname - Used as a failsafe in case ID is wrong
"""
async with self._db.acquire() as conn:
# Sometime the game sends a wrong ID - but a correct player name
# We need to make sure the player ID is correct before pursuing
check = await conn.execute(select([login.c.id]).where(
or_(
login.c.id == teamkiller_id,
login.c.login == teamkiller_name
)
))
row = await check.fetchone()
if not row:
self._logger.debug(
"Discarded teamkill report with unknown reported player %s[%s]",
teamkiller_id, teamkiller_name
)
return
verified_teamkiller_id = row[login.c.id]
# The reporter's ID also needs to be checked the exact same way
# for the same reasons
check = await conn.execute(select([login.c.id]).where(
or_(
login.c.id == reporter_id,
login.c.login == reporter_name
)
))
row = await check.fetchone()
if not row:
self._logger.debug(
"Discarded teamkill report with unknown reporter %s[%s]",
reporter_id, reporter_name
)
return
verified_reporter_id = row[login.c.id]
insert = moderation_report.insert().values(
reporter_id=verified_reporter_id,
game_id=self.game.id,
game_incident_timecode=gametime,
report_description=f"Auto-generated teamkill report from {reporter_name}",
)
result = await conn.execute(insert)
await conn.execute(
reported_user.insert().values(
player_id=verified_teamkiller_id,
report_id=result.lastrowid
)
)
async def handle_teamkill_happened(self, gametime, victim_id, victim_name, teamkiller_id, teamkiller_name):
"""
Send automatically by the game whenever a teamkill happens. Takes
the same parameters as TeamkillReport.
:param gametime: seconds of gametime when kill happened
:param victim_id: victim id
:param victim_name: victim nickname (for debug purpose only)
:param teamkiller_id: teamkiller id
:param teamkiller_name: teamkiller nickname (for debug purpose only)
"""
victim_id = int(victim_id)
teamkiller_id = int(teamkiller_id)
if 0 in (victim_id, teamkiller_id):
self._logger.debug("Ignoring teamkill for AI player")
return
async with self._db.acquire() as conn:
await conn.execute(
teamkills.insert().values(
teamkiller=teamkiller_id,
victim=victim_id,
game_id=self.game.id,
gametime=gametime,
)
)
async def handle_ice_message(self, receiver_id, ice_msg):
receiver_id = int(receiver_id)
peer = self.player_service.get_player(receiver_id)
if not peer:
self._logger.debug(
"Ignoring ICE message for unknown player: %s", receiver_id
)
return
game_connection = peer.game_connection
if not game_connection:
self._logger.debug(
"Ignoring ICE message for player without game connection: %s", receiver_id
)
return
try:
await game_connection.send({
"command": "IceMsg",
"args": [int(self.player.id), ice_msg]
})
except DisconnectedError:
self._logger.debug(
"Failed to send ICE message to player due to a disconnect: %s",
receiver_id
)
async def handle_game_state(self, state):
"""
Changes in game state
:param state: new state
:return: None
"""
if state == 'Idle':
await self._handle_idle_state()
# Don't mark as dirty
return
elif state == 'Lobby':
# TODO: Do we still need to schedule with `ensure_future`?
#
# We do not yield from the task, since we
# need to keep processing other commands while it runs
await self._handle_lobby_state()
elif state == 'Launching':
if self.player.state != PlayerState.HOSTING:
return
self._logger.info(
"Launching game %s in state %s",
self.game,
self.game.state
)
await self.game.launch()
if len(self.game.mods.keys()) > 0:
async with self._db.acquire() as conn:
uids = list(self.game.mods.keys())
await conn.execute(text(
""" UPDATE mod_stats s JOIN mod_version v ON v.mod_id = s.mod_id
SET s.times_played = s.times_played + 1 WHERE v.uid in :ids"""),
ids=tuple(uids)
)
elif state == 'Ended':
await self.on_connection_lost()
self._mark_dirty()
async def handle_game_ended(self, *args):
"""
Signals that the simulation has ended.
"""
self.finished_sim = True
await self.game.check_sim_end()
if self.game.ended:
await self.game.on_game_end()
async def handle_rehost(self, *args):
"""
Signals that the user has rehosted the game. This is currently unused but
included for documentation purposes.
"""
pass
async def handle_bottleneck(self, *args):
"""
Not sure what this command means. This is currently unused but
included for documentation purposes.
"""
pass
async def handle_bottleneck_cleared(self, *args):
"""
Not sure what this command means. This is currently unused but
included for documentation purposes.
"""
pass
async def handle_disconnected(self, *args):
"""
Not sure what this command means. This is currently unused but
included for documentation purposes.
"""
pass
async def handle_chat(self, message: str):
"""
Whenever the player sends a chat message during the game lobby.
"""
pass
async def handle_game_full(self):
"""
Sent when all game slots are full
"""
pass
def _mark_dirty(self):
if self.game:
self.game_service.mark_dirty(self.game)
async def abort(self, log_message: str = ''):
"""
Abort the connection
Removes the GameConnection object from the any associated Game object,
and deletes references to Player and Game held by this object.
"""
try:
if self._state is GameConnectionState.ENDED:
return
self._logger.debug("%s.abort(%s)", self, log_message)
if self.game.state is GameState.LOBBY:
await self.disconnect_all_peers()
self._state = GameConnectionState.ENDED
await self.game.remove_game_connection(self)
self._mark_dirty()
self.player.state = PlayerState.IDLE
if self.player.lobby_connection:
self.player.lobby_connection.game_connection = None
del self.player.game
del self.player.game_connection
except Exception as ex: # pragma: no cover
self._logger.debug("Exception in abort(): %s", ex)
async def disconnect_all_peers(self):
tasks = []
for peer in self.game.connections:
if peer == self:
continue
tasks.append(peer.send_DisconnectFromPeer(self.player.id))
for fut in asyncio.as_completed(tasks):
try:
await fut
except Exception:
self._logger.debug(
"peer_sendDisconnectFromPeer failed for player %i",
self.player.id,
exc_info=True
)
async def on_connection_lost(self):
try:
await self.game.remove_game_connection(self)
except Exception as e: # pragma: no cover
self._logger.exception(e)
finally:
await self.abort()
def __str__(self):
return "GameConnection({}, {})".format(self.player, self.game)
COMMAND_HANDLERS = {
"Desync": GameConnection.handle_desync,
"GameState": GameConnection.handle_game_state,
"GameOption": GameConnection.handle_game_option,
"GameMods": GameConnection.handle_game_mods,
"PlayerOption": GameConnection.handle_player_option,
"AIOption": GameConnection.handle_ai_option,
"ClearSlot": GameConnection.handle_clear_slot,
"GameResult": GameConnection.handle_game_result,
"OperationComplete": GameConnection.handle_operation_complete,
"JsonStats": GameConnection.handle_json_stats,
"EnforceRating": GameConnection.handle_enforce_rating,
"TeamkillReport": GameConnection.handle_teamkill_report,
"TeamkillHappened": GameConnection.handle_teamkill_happened,
"GameEnded": GameConnection.handle_game_ended,
"Rehost": GameConnection.handle_rehost,
"Bottleneck": GameConnection.handle_bottleneck,
"BottleneckCleared": GameConnection.handle_bottleneck_cleared,
"Disconnected": GameConnection.handle_disconnected,
"IceMsg": GameConnection.handle_ice_message,
"Chat": GameConnection.handle_chat,
"GameFull": GameConnection.handle_game_full
}