Skip to content

Commit bc4e2a2

Browse files
Brutus5000Christian SchmidtSheikah45BlackYps
authored
Log every attempt to join a game (#1048)
* Log every attempt to join a game * Fix player id reference and add test * format * fix sort * remove unused variable * Revert unnecessary whitespace changes * Use correct db version * Improve test --------- Co-authored-by: Christian Schmidt <[email protected]> Co-authored-by: Sheikah45 <[email protected]> Co-authored-by: BlackYps <[email protected]>
1 parent cbc55f3 commit bc4e2a2

File tree

4 files changed

+81
-6
lines changed

4 files changed

+81
-6
lines changed

.github/workflows/test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ on:
1111
- cron: '0 0 * * *'
1212

1313
env:
14-
FAF_DB_VERSION: v136
14+
FAF_DB_VERSION: v138
1515
FLYWAY_VERSION: 7.5.4
1616

1717
jobs:

server/db/models.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
from sqlalchemy import (
22
TIME,
33
TIMESTAMP,
4+
BigInteger,
45
Boolean,
56
Column,
67
DateTime,
@@ -113,6 +114,13 @@
113114
Column("allow_override", Boolean)
114115
)
115116

117+
game_join_log = Table(
118+
"game_join_log", metadata,
119+
Column("id", BigInteger, primary_key=True),
120+
Column("player_id", Integer, ForeignKey("login.id"), nullable=False),
121+
Column("game_id", Integer, nullable=False),
122+
)
123+
116124
game_player_stats = Table(
117125
"game_player_stats", metadata,
118126
Column("id", Integer, primary_key=True),

server/lobbyconnection.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
ban,
2929
coop_map,
3030
friends_and_foes,
31+
game_join_log,
3132
lobby_ban
3233
)
3334
from .db.models import login as t_login
@@ -986,6 +987,17 @@ async def command_game_join(self, message):
986987
uuid = int(message["uid"])
987988
password = message.get("password")
988989

990+
async with self._db.acquire() as conn:
991+
try:
992+
await conn.execute(
993+
game_join_log.insert().values(
994+
player_id=self.player.id,
995+
game_id=uuid,
996+
)
997+
)
998+
except DBAPIError:
999+
self._logger.exception("writing to game join log failed")
1000+
9891001
self._logger.debug("joining: %d with pw: %s", uuid, password)
9901002
try:
9911003
game = self.game_service[uuid]

tests/integration_tests/test_game.py

Lines changed: 60 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
from unittest import mock
88

99
import pytest
10-
from sqlalchemy import select
10+
from sqlalchemy import and_, select
1111

12-
from server.db.models import game_player_stats
12+
from server.db.models import game_join_log, game_player_stats
1313
from server.games.game_results import GameOutcome
1414
from server.protocol import Protocol
1515
from server.timing import datetime_now
@@ -391,6 +391,61 @@ async def test_game_with_foed_player(lobby_server):
391391
await join_game(guest_proto, game_id)
392392

393393

394+
@fast_forward(60)
395+
async def test_game_join_log(lobby_server, database):
396+
_, _, host_proto = await connect_and_sign_in(
397+
("test", "test_password"), lobby_server
398+
)
399+
guest_id, _, guest_proto = await connect_and_sign_in(
400+
("Rhiza", "puff_the_magic_dragon"), lobby_server
401+
)
402+
await read_until_command(guest_proto, "game_info")
403+
await read_until_command(host_proto, "game_info")
404+
405+
# Host game
406+
await host_proto.send_message({
407+
"command": "game_host",
408+
"mod": "faf",
409+
"visibility": "public",
410+
})
411+
game_id = await host_game(host_proto)
412+
413+
# Join a player
414+
await join_game(guest_proto, game_id)
415+
416+
async with database.acquire() as conn:
417+
result = await conn.execute(
418+
select(game_join_log).where(
419+
and_(
420+
game_join_log.c.game_id == game_id,
421+
game_join_log.c.player_id == guest_id
422+
)
423+
)
424+
)
425+
row = result.one()
426+
assert row is not None
427+
428+
# Leave and re-join
429+
await guest_proto.send_message({
430+
"target": "game",
431+
"command": "GameState",
432+
"args": ["Ended"]
433+
})
434+
await join_game(guest_proto, game_id)
435+
436+
async with database.acquire() as conn:
437+
result = await conn.execute(
438+
select(game_join_log).where(
439+
and_(
440+
game_join_log.c.game_id == game_id,
441+
game_join_log.c.player_id == guest_id
442+
)
443+
)
444+
)
445+
rows = result.fetchall()
446+
assert len(rows) == 2
447+
448+
394449
@fast_forward(60)
395450
async def test_game_info_messages(lobby_server):
396451
host_id, _, host_proto = await connect_and_sign_in(
@@ -1126,9 +1181,9 @@ async def test_partial_game_ended_rates_game(lobby_server, tmp_user):
11261181
# Set player options
11271182
await send_player_options(
11281183
host_proto,
1129-
[guest_id, "Army", i+2],
1130-
[guest_id, "StartSpot", i+2],
1131-
[guest_id, "Color", i+2],
1184+
[guest_id, "Army", i + 2],
1185+
[guest_id, "StartSpot", i + 2],
1186+
[guest_id, "Color", i + 2],
11321187
[guest_id, "Faction", 1],
11331188
[guest_id, "Team", 3 if i % 2 == 0 else 2]
11341189
)

0 commit comments

Comments
 (0)