Skip to content

Commit

Permalink
Fix duplicate key error for coop_leaderboard
Browse files Browse the repository at this point in the history
  • Loading branch information
Askaholic committed Jan 8, 2024
1 parent d580021 commit 8f0462e
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
21 changes: 11 additions & 10 deletions server/gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,17 +336,18 @@ async def handle_operation_complete(

# Each player in a co-op game will send the OperationComplete
# message but we only need to perform this insert once
if not self.game.leaderboard_saved:
await conn.execute(
coop_leaderboard.insert().values(
mission=mission,
gameuid=self.game.id,
secondary=secondary,
time=delta,
player_count=len(self.game.players),
async with self.game.leaderboard_lock:
if not self.game.leaderboard_saved:
await conn.execute(
coop_leaderboard.insert().values(
mission=mission,
gameuid=self.game.id,
secondary=secondary,
time=delta,
player_count=len(self.game.players),
)
)
)
self.game.leaderboard_saved = True
self.game.leaderboard_saved = True

async def handle_json_stats(self, stats: str):
try:
Expand Down
3 changes: 3 additions & 0 deletions server/games/coop.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import asyncio

from .game import Game
from .typedefs import FA, GameType, InitMode, ValidityState, Victory

Expand All @@ -19,6 +21,7 @@ def __init__(self, *args, **kwargs):
"Difficulty": 3,
"Expansion": "true"
})
self.leaderboard_lock = asyncio.Lock()
self.leaderboard_saved = False

async def validate_game_mode_settings(self):
Expand Down
6 changes: 3 additions & 3 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ def opt(val):
user=user,
password=pw or "",
port=port,
db=name
db=name,
)


Expand All @@ -183,7 +183,7 @@ def opt(val):
user=user,
password=pw or "",
port=port,
db=name
db=name,
)
await db.connect()

Expand Down Expand Up @@ -211,7 +211,7 @@ async def game(database, players):


GAME_UID = 1
COOP_GAME_UID = 1
COOP_GAME_UID = 2


@pytest.fixture
Expand Down
15 changes: 6 additions & 9 deletions tests/unit_tests/test_gameconnection.py
Original file line number Diff line number Diff line change
Expand Up @@ -527,9 +527,7 @@ async def test_handle_action_TeamkillHappened(


async def test_handle_action_TeamkillHappened_AI(
game: Game,
game_connection: GameConnection,
database
):
# Should fail with a sql constraint error if this isn't handled correctly
game_connection.abort = mock.AsyncMock()
Expand Down Expand Up @@ -675,13 +673,12 @@ async def test_handle_action_OperationComplete_duplicate(
)

with caplog.at_level(logging.ERROR):
await game_connection.handle_action(
"OperationComplete", [1, 1, time_taken]
)
caplog.clear()
await game_connection.handle_action(
"OperationComplete", [1, 1, time_taken]
)
await asyncio.gather(*(
game_connection.handle_action(
"OperationComplete", [1, 1, time_taken]
)
for _ in range(10)
))

assert not any(
record.exc_info
Expand Down

0 comments on commit 8f0462e

Please sign in to comment.