Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix match host #9

Merged
merged 2 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/api/domains/cho.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async def handle(self, player: Player) -> None:
app.state.sessions.players.enqueue(app.packets.user_stats(player))


IGNORED_CHANNELS = ["#highlight", "#userlog"]
IGNORED_CHANNELS = ["#highlight", "#userlog", "#multiplayer"]


@register(ClientPackets.SEND_PUBLIC_MESSAGE)
Expand Down
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
Loading