Skip to content

Commit

Permalink
added player disconnect reason sending
Browse files Browse the repository at this point in the history
  • Loading branch information
Leroy-bit committed Oct 18, 2023
1 parent ab020d4 commit d0c4646
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 7 deletions.
1 change: 1 addition & 0 deletions server/explorer/game_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ async def makeTurn(self, game_id: int, player_id: int, column: int = None) -> No
Event(ServerEvents.PLAYER_WIN, {'player_id': winner})
)
self.GAMES[game_id].started = False
await self.explorer.ws.createRematchTimeoutTask(game_id, winner)
elif draw:
await self.explorer.ws.broadcast(
game_id,
Expand Down
15 changes: 13 additions & 2 deletions server/explorer/websocket_accessor.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
if typing.TYPE_CHECKING:
from base.application import Request

TURN_TIMEOUT: int = 20
TURN_TIMEOUT: int = 40
WAITING_FOR_PLAYER_TIMEOUT: int = 45
REMATCH_TIMEOUT: int = 30

Expand Down Expand Up @@ -110,7 +110,10 @@ async def close(self, game_id: int, player_id: int, reason: str | None = None) -
return
if reason:
await self.explorer.ws.send(game_id, player_id, Event(ServerEvents.DISCONNECTED, {'reason': reason}))
await self.broadcast(game_id, Event('PLAYER_DISCONNECTED', {'player_id': player_id}), [player_id])
eventData = {'player_id': player_id}
if reason:
eventData['reason'] = reason
await self.broadcast(game_id, Event('PLAYER_DISCONNECTED', eventData), [player_id])
await self._close(game_id, player_id)

async def closeAll(self, game_id: int, reason: str | None = None) -> None:
Expand Down Expand Up @@ -168,6 +171,14 @@ async def broadcast(self, game_id: int, event: Event, except_of: list[int] = [])
tasks.append(self.send(game_id, user_id, event))
await asyncio.gather(*tasks)

async def isClosed(self, game_id: int, player_id: int) -> bool:
'''Check if connection is closed.'''
if not self.connections.get(game_id):
return True
if not self.connections[game_id].get(player_id):
return True
return self.connections[game_id][player_id].ws.closed

async def createTurnTimeoutTask(self, game_id: int, player_id: int) -> None:
'''Create turn timeout task for a connection.'''
if not self.connections.get(game_id):
Expand Down
11 changes: 6 additions & 5 deletions server/explorer/websocket_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ async def handleConnection(self, game_id: int, user_id: int) -> None:

async def handleDisconnect(self, game_id: int, user_id: int) -> None:
'''Do some things when a user disconnects.'''
await self.explorer.ws.broadcast(
game_id,
Event(ServerEvents.PLAYER_DISCONNECTED, {'player_id': user_id}),
[user_id]
)
if not (await self.explorer.ws.isClosed(game_id, user_id)):
await self.explorer.ws.broadcast(
game_id,
Event(ServerEvents.PLAYER_DISCONNECTED, {'player_id': user_id}),
[user_id]
)
await self.explorer.game_accessor.closeGame(game_id)
self.explorer.logger.trace(f'Player {user_id} disconnected from game {game_id}')

0 comments on commit d0c4646

Please sign in to comment.