Skip to content

Commit

Permalink
depart leaving match logic in player.py
Browse files Browse the repository at this point in the history
  • Loading branch information
TrueRou committed Dec 5, 2023
1 parent 9c30c19 commit 84ef3ba
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 34 deletions.
45 changes: 43 additions & 2 deletions app/objects/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from datetime import timedelta as timedelta
from enum import IntEnum
from enum import unique
from typing import TYPE_CHECKING
from typing import TYPE_CHECKING, Optional
from typing import TypedDict

import databases.core
Expand Down Expand Up @@ -257,10 +257,19 @@ def __init__(

self.tourney_clients: set[int] = set() # player ids

@property
def empty(self) -> bool:
return all(s.empty() for s in self.slots)

# TODO: handle user not found in session situation.
@property # TODO: test cache speed
def host(self) -> Player:
return app.state.sessions.players.get(id=self.host_id) or app.state.sessions.bot
player = app.state.sessions.players.get(id=self.host_id)
if not player:
player = self.find_next_host()
if not player:
player = app.state.sessions.bot
return player

@property
def url(self) -> str:
Expand Down Expand Up @@ -451,6 +460,38 @@ async def await_submissions(

# all scores retrieved, update the match.
return scores, didnt_submit

def terminate(self):
log(f"Match {self} finished.")
if self.starting is not None:
self.starting["start"].cancel()
for alert in self.starting["alerts"]:
alert.cancel()
self.starting = None
app.state.sessions.matches.remove(self)
lobby = app.state.sessions.channels.get_by_name("#lobby")
if lobby:
lobby.enqueue(app.packets.dispose_match(self.id))


def find_next_host(self) -> Optional[Player]:
current_host_id = self.host_id
next_host = None
for slot in self.slots:
if slot.empty() or slot.player.id == current_host_id:
continue
next_host = slot.player
break
if not next_host:
# Normally, match is terminated rather than without next host.
log(f"An empty match was trying to find next host.")
self.terminate()
return
self.host_id = next_host.id
next_host.enqueue(app.packets.match_transfer_host())
self.enqueue_state() # boardcast to all players in the match
return next_host


async def update_matchpoints(self, was_playing: Sequence[Slot]) -> None:
"""\
Expand Down
35 changes: 3 additions & 32 deletions app/objects/player.py
Original file line number Diff line number Diff line change
Expand Up @@ -661,44 +661,15 @@ def leave_match(self) -> None:

self.leave_channel(self.match.chat)

if all(s.empty() for s in self.match.slots):
# multi is now empty, chat has been removed.
# remove the multi from the channels list.
log(f"Match {self.match} finished.")

# cancel any pending start timers
if self.match.starting is not None:
self.match.starting["start"].cancel()
for alert in self.match.starting["alerts"]:
alert.cancel()

self.match.starting = None

app.state.sessions.matches.remove(self.match)

lobby = app.state.sessions.channels.get_by_name("#lobby")
if lobby:
lobby.enqueue(app.packets.dispose_match(self.match.id))

if self.match.empty:
self.match.terminate()
else: # multi is not empty
if self is self.match.host:
# 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 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
self.match.find_next_host()

if self in self.match._refs:
self.match._refs.remove(self)
self.match.chat.send_bot(f"{self.name} removed from match referees.")

# notify others of our deprature
self.match.enqueue_state()

self.match = None

async def join_clan(self, clan: Clan) -> bool:
Expand Down

0 comments on commit 84ef3ba

Please sign in to comment.