From f11e0fae914a597125dc07d6c99492751658b472 Mon Sep 17 00:00:00 2001 From: arily Date: Sun, 12 Nov 2023 14:13:18 +0900 Subject: [PATCH] handle invalid slot states --- app/objects/match.py | 5 ++--- app/objects/player.py | 10 ++++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/app/objects/match.py b/app/objects/match.py index 9d3a865e..69ac15a0 100644 --- a/app/objects/match.py +++ b/app/objects/match.py @@ -257,11 +257,10 @@ def __init__( self.tourney_clients: set[int] = set() # player ids + # TODO: handle user not found in session situation. @property # TODO: test cache speed def host(self) -> Player: - player = app.state.sessions.players.get(id=self.host_id) - assert player is not None - return player + return app.state.sessions.players.get(id=self.host_id) or app.state.sessions.bot @property def url(self) -> str: diff --git a/app/objects/player.py b/app/objects/player.py index 7d50436c..0ae25a78 100644 --- a/app/objects/player.py +++ b/app/objects/player.py @@ -685,10 +685,12 @@ def leave_match(self) -> None: # player was host, trasnfer to first occupied slot for s in self.match.slots: # add double check to ensure match player - if s.player is not None and s.player.match is not None: - self.match.host_id = s.player.id - self.match.host.enqueue(app.packets.match_transfer_host()) - break + if s.player is None or s.player.match is None: + continue + + self.match.host_id = s.player.id + self.match.host.enqueue(app.packets.match_transfer_host()) + break if self in self.match._refs: self.match._refs.remove(self)