Skip to content

Commit

Permalink
add first place scores table
Browse files Browse the repository at this point in the history
  • Loading branch information
minisbett committed Sep 4, 2024
1 parent bd9328d commit 1af0b36
Show file tree
Hide file tree
Showing 7 changed files with 95 additions and 3 deletions.
6 changes: 5 additions & 1 deletion app/api/domains/osu.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
from app.repositories import scores as scores_repo
from app.repositories import stats as stats_repo
from app.repositories import users as users_repo
from app.repositories import first_place_scores as first_place_scores_repo
from app.repositories.achievements import Achievement
from app.usecases import achievements as achievements_usecases
from app.usecases import user_achievements as user_achievements_usecases
Expand Down Expand Up @@ -812,10 +813,13 @@ async def osuSubmitModularSelector(
"client_flags": score.client_flags,
"user_id": score.player.id,
"perfect": score.perfect,
"checksum": score.client_checksum,
"checksum": score.client_checksum
},
)

#if score.rank == 1 and not player.restricted:
await first_place_scores_repo.create_or_update(bmap.md5, score.mode, score.id)

if score.passed:
replay_data = await replay_file.read()

Expand Down
1 change: 0 additions & 1 deletion app/objects/score.py
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,6 @@ def calculate_performance(self, beatmap_id: int) -> tuple[float, float]:
mods=int(self.mods),
combo=self.max_combo,
ngeki=self.ngeki,
n300=self.n300,
nkatu=self.nkatu,
n100=self.n100,
n50=self.n50,
Expand Down
66 changes: 66 additions & 0 deletions app/repositories/first_place_scores.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from __future__ import annotations

from typing import TypedDict
from typing import cast

from sqlalchemy import Column
from sqlalchemy import Index
from sqlalchemy import BigInteger
from sqlalchemy import SmallInteger
from sqlalchemy import String
from sqlalchemy.dialects.mysql import insert as mysql_insert
from sqlalchemy import select
from sqlalchemy import and_
from sqlalchemy import PrimaryKeyConstraint

import app.state.services
from app.repositories import Base
from app.constants.gamemodes import GameMode


class FirstPlaceScoresTable(Base):
__tablename__ = "first_place_scores"

map_md5 = Column("map_md5", String(32), nullable=False)
mode = Column("mode", SmallInteger, nullable=False)
score_id = Column("score_id", BigInteger, nullable=False)

__table_args__ = (
Index("first_place_scores_map_md5_mode_index", map_md5, mode),
PrimaryKeyConstraint(map_md5, mode)
)


READ_PARAMS = (
FirstPlaceScoresTable.map_md5,
FirstPlaceScoresTable.mode,
FirstPlaceScoresTable.score_id
)


class FirstPlaceScore(TypedDict):
map_md5: str
mode: int
score_id: int


async def create_or_update(
map_md5: str,
mode: int,
score_id: int
) -> None:
insert_stmt = mysql_insert(FirstPlaceScoresTable).values(
map_md5=map_md5,
mode=mode,
score_id=score_id
).on_duplicate_key_update(
score_id=score_id
)
print(insert_stmt)
await app.state.services.database.execute(insert_stmt)


async def fetch_one(map_md5: str, mode: GameMode) -> FirstPlaceScore | None:
select_stmt = select(*READ_PARAMS).where(and_(FirstPlaceScoresTable.map_md5 == map_md5, FirstPlaceScoresTable.mode == mode))
score = await app.state.services.database.fetch_one(select_stmt)
return cast(FirstPlaceScore | None, score)
2 changes: 2 additions & 0 deletions app/repositories/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,8 @@ async def partial_update(

await app.state.services.database.execute(update_stmt)

print(update_stmt)

select_stmt = (
select(*READ_PARAMS)
.where(StatsTable.id == player_id)
Expand Down
10 changes: 10 additions & 0 deletions migrations/base.sql
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,16 @@ create table ratings
primary key (userid, map_md5)
);

create table first_place_scores
(
map_md5 char(32) not null,
mode tinyint not null,
score_id bigint not null,
primary key(map_md5, mode)
);
create index first_place_scores_map_md5_mode_index
on first_place_scores (map_md5, mode);

create table scores
(
id bigint unsigned auto_increment
Expand Down
11 changes: 11 additions & 0 deletions migrations/migrations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -475,3 +475,14 @@ create index users_country_index
# v5.2.2
create index scores_fetch_leaderboard_generic_index
on scores (map_md5, status, mode);

# v5.2.3
create table first_place_scores
(
map_md5 char(32) not null,
mode tinyint not null,
score_id bigint not null,
primary key(map_md5, mode)
);
create index first_place_scores_map_md5_mode_index
on first_place_scores (map_md5, mode);
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ profile = "black"

[tool.poetry]
name = "bancho-py"
version = "5.2.2"
version = "5.2.3"
description = "An osu! server implementation optimized for maintainability in modern python"
authors = ["Akatsuki Team"]
license = "MIT"
Expand Down

0 comments on commit 1af0b36

Please sign in to comment.