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

Hotfix slow batch #8

Closed
wants to merge 9 commits into from
Closed
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
20 changes: 12 additions & 8 deletions app/repositories/maps.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
from app._typing import _UnsetSentinel
from app._typing import UNSET

from app.utils import remove_none_values

# +--------------+------------------------+------+-----+---------+-------+
# | Field | Type | Null | Key | Default | Extra |
# +--------------+------------------------+------+-----+---------+-------+
Expand Down Expand Up @@ -183,19 +185,21 @@ async def fetch_one(
if id is None and md5 is None and filename is None:
raise ValueError("Must provide at least one parameter.")

query = f"""\
SELECT {READ_PARAMS}
FROM maps
WHERE id = COALESCE(:id, id)
AND md5 = COALESCE(:md5, md5)
AND filename = COALESCE(:filename, filename)
"""
queries = [
f"SELECT {READ_PARAMS} FROM maps WHERE 1 = 1",
"AND id = :id" if id is not None else None,
"AND md5 = :md5" if md5 is not None else None,
"AND filename = :filename" if filename is not None else None,
]

params: dict[str, Any] = {
"id": id,
"md5": md5,
"filename": filename,
}
map = await app.state.services.database.fetch_one(query, params)
map = await app.state.services.database.fetch_one(
" ".join(q for q in queries if q is not None), remove_none_values(params)
)

return cast(Map, dict(map._mapping)) if map is not None else None

Expand Down
38 changes: 21 additions & 17 deletions app/repositories/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import app.state.services
from app._typing import _UnsetSentinel
from app._typing import UNSET
from app.utils import remove_none_values

# +-----------------+-----------------+------+-----+---------+----------------+
# | Field | Type | Null | Key | Default | Extra |
Expand Down Expand Up @@ -213,31 +214,34 @@ async def fetch_many(
page: int | None = None,
page_size: int | None = None,
) -> list[Score]:
query = f"""\
SELECT {READ_PARAMS}
FROM scores
WHERE map_md5 = COALESCE(:map_md5, map_md5)
AND mods = COALESCE(:mods, mods)
AND status = COALESCE(:status, status)
AND mode = COALESCE(:mode, mode)
AND userid = COALESCE(:userid, userid)
"""
queries = [
f"SELECT {READ_PARAMS} FROM scores WHERE 1 = 1",
"AND map_md5 = :map_md5" if map_md5 is not None else None,
"AND mods = :mods" if mods is not None else None,
"AND status = :status" if status is not None else None,
"AND mode = :mode" if mode is not None else None,
"AND userid = :userid" if user_id is not None else None,
"""\
LIMIT :page_size
OFFSET :offset
"""
if page is not None and page_size is not None
else None,
]

params: dict[str, Any] = {
"map_md5": map_md5,
"mods": mods,
"status": status,
"mode": mode,
"userid": user_id,
"page_size": page_size,
"offset": (page - 1) * page_size if page is not None else None,
}
if page is not None and page_size is not None:
query += """\
LIMIT :page_size
OFFSET :offset
"""
params["page_size"] = page_size
params["offset"] = (page - 1) * page_size

recs = await app.state.services.database.fetch_all(query, params)
recs = await app.state.services.database.fetch_all(
" ".join(q for q in queries if q is not None), remove_none_values(params)
)
return cast(list[Score], [dict(r._mapping) for r in recs])


Expand Down
7 changes: 7 additions & 0 deletions app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,3 +430,10 @@ def has_png_headers_and_trailers(data_view: memoryview) -> bool:
data_view[:8] == b"\x89PNG\r\n\x1a\n"
and data_view[-8:] == b"\x49END\xae\x42\x60\x82"
)


def remove_none_values(input_dict: dict[str, T | None]) -> dict[str, T]:
filtered_dict = {
key: value for key, value in input_dict.items() if value is not None
}
return filtered_dict
Loading