2023-02-17 06:02:21 +00:00
|
|
|
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_
|
|
|
|
from sqlalchemy.types import Integer, String, TIMESTAMP, JSON, Boolean
|
|
|
|
from sqlalchemy.schema import ForeignKey
|
2023-02-18 20:01:31 +00:00
|
|
|
from sqlalchemy.sql import func, select
|
2023-02-17 06:02:21 +00:00
|
|
|
from sqlalchemy.dialects.mysql import insert
|
2023-04-30 21:31:13 +00:00
|
|
|
from sqlalchemy.engine import Row
|
2023-02-17 06:02:21 +00:00
|
|
|
from typing import Optional, List, Dict, Any
|
|
|
|
|
|
|
|
from core.data.schema import BaseData, metadata
|
|
|
|
from core.data import cached
|
|
|
|
|
|
|
|
score = Table(
|
|
|
|
"diva_score",
|
|
|
|
metadata,
|
|
|
|
Column("id", Integer, primary_key=True, nullable=False),
|
|
|
|
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
|
|
|
Column("version", Integer),
|
|
|
|
Column("pv_id", Integer),
|
|
|
|
Column("difficulty", Integer),
|
2023-02-18 20:01:31 +00:00
|
|
|
Column("edition", Integer),
|
2023-02-17 06:02:21 +00:00
|
|
|
Column("score", Integer),
|
|
|
|
Column("atn_pnt", Integer),
|
|
|
|
Column("clr_kind", Integer),
|
|
|
|
Column("sort_kind", Integer),
|
|
|
|
Column("cool", Integer),
|
|
|
|
Column("fine", Integer),
|
|
|
|
Column("safe", Integer),
|
|
|
|
Column("sad", Integer),
|
|
|
|
Column("worst", Integer),
|
|
|
|
Column("max_combo", Integer),
|
2023-02-18 20:01:31 +00:00
|
|
|
UniqueConstraint("user", "pv_id", "difficulty", "edition", name="diva_score_uk"),
|
2023-03-09 16:38:58 +00:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
playlog = Table(
|
|
|
|
"diva_playlog",
|
|
|
|
metadata,
|
|
|
|
Column("id", Integer, primary_key=True, nullable=False),
|
|
|
|
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
|
|
|
Column("version", Integer),
|
|
|
|
Column("pv_id", Integer),
|
|
|
|
Column("difficulty", Integer),
|
2023-02-18 20:01:31 +00:00
|
|
|
Column("edition", Integer),
|
2023-02-17 06:02:21 +00:00
|
|
|
Column("score", Integer),
|
|
|
|
Column("atn_pnt", Integer),
|
|
|
|
Column("clr_kind", Integer),
|
|
|
|
Column("sort_kind", Integer),
|
|
|
|
Column("cool", Integer),
|
|
|
|
Column("fine", Integer),
|
|
|
|
Column("safe", Integer),
|
|
|
|
Column("sad", Integer),
|
|
|
|
Column("worst", Integer),
|
|
|
|
Column("max_combo", Integer),
|
|
|
|
Column("date_scored", TIMESTAMP, server_default=func.now()),
|
2023-03-09 16:38:58 +00:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
2023-02-18 20:01:31 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
class DivaScoreData(BaseData):
|
2023-03-09 16:38:58 +00:00
|
|
|
def put_best_score(
|
|
|
|
self,
|
|
|
|
user_id: int,
|
|
|
|
game_version: int,
|
|
|
|
song_id: int,
|
|
|
|
difficulty: int,
|
|
|
|
edition: int,
|
|
|
|
song_score: int,
|
|
|
|
atn_pnt: int,
|
|
|
|
clr_kind: int,
|
|
|
|
sort_kind: int,
|
|
|
|
cool: int,
|
|
|
|
fine: int,
|
|
|
|
safe: int,
|
|
|
|
sad: int,
|
|
|
|
worst: int,
|
|
|
|
max_combo: int,
|
|
|
|
) -> Optional[int]:
|
2023-02-17 06:02:21 +00:00
|
|
|
"""
|
|
|
|
Update the user's best score for a chart
|
|
|
|
"""
|
|
|
|
sql = insert(score).values(
|
|
|
|
user=user_id,
|
|
|
|
version=game_version,
|
2023-02-18 20:01:31 +00:00
|
|
|
pv_id=song_id,
|
2023-02-17 06:02:21 +00:00
|
|
|
difficulty=difficulty,
|
2023-02-18 20:01:31 +00:00
|
|
|
edition=edition,
|
2023-02-17 06:02:21 +00:00
|
|
|
score=song_score,
|
2023-02-18 20:01:31 +00:00
|
|
|
atn_pnt=atn_pnt,
|
|
|
|
clr_kind=clr_kind,
|
|
|
|
sort_kind=sort_kind,
|
|
|
|
cool=cool,
|
|
|
|
fine=fine,
|
|
|
|
safe=safe,
|
|
|
|
sad=sad,
|
|
|
|
worst=worst,
|
|
|
|
max_combo=max_combo,
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
conflict = sql.on_duplicate_key_update(
|
|
|
|
score=song_score,
|
2023-02-18 20:01:31 +00:00
|
|
|
atn_pnt=atn_pnt,
|
|
|
|
clr_kind=clr_kind,
|
|
|
|
sort_kind=sort_kind,
|
|
|
|
cool=cool,
|
|
|
|
fine=fine,
|
|
|
|
safe=safe,
|
|
|
|
sad=sad,
|
|
|
|
worst=worst,
|
|
|
|
max_combo=max_combo,
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.execute(conflict)
|
|
|
|
if result is None:
|
2023-02-18 20:01:31 +00:00
|
|
|
self.logger.error(
|
2023-03-09 16:38:58 +00:00
|
|
|
f"{__name__} failed to insert best score! profile: {user_id}, song: {song_id}"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
return None
|
2023-02-18 20:01:31 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.lastrowid
|
2023-02-18 20:01:31 +00:00
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
def put_playlog(
|
|
|
|
self,
|
|
|
|
user_id: int,
|
|
|
|
game_version: int,
|
|
|
|
song_id: int,
|
|
|
|
difficulty: int,
|
|
|
|
edition: int,
|
|
|
|
song_score: int,
|
|
|
|
atn_pnt: int,
|
|
|
|
clr_kind: int,
|
|
|
|
sort_kind: int,
|
|
|
|
cool: int,
|
|
|
|
fine: int,
|
|
|
|
safe: int,
|
|
|
|
sad: int,
|
|
|
|
worst: int,
|
|
|
|
max_combo: int,
|
|
|
|
) -> Optional[int]:
|
2023-02-17 06:02:21 +00:00
|
|
|
"""
|
|
|
|
Add an entry to the user's play log
|
|
|
|
"""
|
|
|
|
sql = playlog.insert().values(
|
|
|
|
user=user_id,
|
|
|
|
version=game_version,
|
2023-02-18 20:01:31 +00:00
|
|
|
pv_id=song_id,
|
2023-02-17 06:02:21 +00:00
|
|
|
difficulty=difficulty,
|
2023-02-18 20:01:31 +00:00
|
|
|
edition=edition,
|
2023-02-17 06:02:21 +00:00
|
|
|
score=song_score,
|
2023-02-18 20:01:31 +00:00
|
|
|
atn_pnt=atn_pnt,
|
|
|
|
clr_kind=clr_kind,
|
|
|
|
sort_kind=sort_kind,
|
|
|
|
cool=cool,
|
|
|
|
fine=fine,
|
|
|
|
safe=safe,
|
|
|
|
sad=sad,
|
|
|
|
worst=worst,
|
2023-03-09 16:38:58 +00:00
|
|
|
max_combo=max_combo,
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.execute(sql)
|
|
|
|
if result is None:
|
2023-02-18 20:01:31 +00:00
|
|
|
self.logger.error(
|
2023-03-09 16:38:58 +00:00
|
|
|
f"{__name__} failed to insert playlog! profile: {user_id}, song: {song_id}, chart: {difficulty}"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
return None
|
2023-02-18 20:01:31 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.lastrowid
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
def get_best_user_score(
|
|
|
|
self, user_id: int, pv_id: int, difficulty: int, edition: int
|
2023-04-30 21:31:13 +00:00
|
|
|
) -> Optional[Row]:
|
2023-02-17 06:02:21 +00:00
|
|
|
sql = score.select(
|
2023-03-09 16:38:58 +00:00
|
|
|
and_(
|
|
|
|
score.c.user == user_id,
|
|
|
|
score.c.pv_id == pv_id,
|
|
|
|
score.c.difficulty == difficulty,
|
|
|
|
score.c.edition == edition,
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.execute(sql)
|
2023-02-18 20:01:31 +00:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.fetchone()
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
def get_top3_scores(
|
|
|
|
self, pv_id: int, difficulty: int, edition: int
|
2023-04-30 21:31:13 +00:00
|
|
|
) -> Optional[List[Row]]:
|
2023-03-09 16:38:58 +00:00
|
|
|
sql = (
|
|
|
|
score.select(
|
|
|
|
and_(
|
|
|
|
score.c.pv_id == pv_id,
|
|
|
|
score.c.difficulty == difficulty,
|
|
|
|
score.c.edition == edition,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
.order_by(score.c.score.desc())
|
|
|
|
.limit(3)
|
|
|
|
)
|
2023-02-18 20:01:31 +00:00
|
|
|
|
|
|
|
result = self.execute(sql)
|
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
return result.fetchall()
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
def get_global_ranking(
|
|
|
|
self, user_id: int, pv_id: int, difficulty: int, edition: int
|
2023-04-30 21:31:13 +00:00
|
|
|
) -> Optional[List[Row]]:
|
2023-02-18 20:01:31 +00:00
|
|
|
# get the subquery max score of a user with pv_id, difficulty and
|
|
|
|
# edition
|
2023-03-09 16:38:58 +00:00
|
|
|
sql_sub = (
|
|
|
|
select([score.c.score])
|
|
|
|
.filter(
|
|
|
|
score.c.user == user_id,
|
|
|
|
score.c.pv_id == pv_id,
|
|
|
|
score.c.difficulty == difficulty,
|
|
|
|
score.c.edition == edition,
|
|
|
|
)
|
|
|
|
.scalar_subquery()
|
|
|
|
)
|
2023-02-18 20:01:31 +00:00
|
|
|
|
|
|
|
# Perform the main query, also rename the resulting column to ranking
|
|
|
|
sql = select(func.count(score.c.id).label("ranking")).filter(
|
|
|
|
score.c.score >= sql_sub,
|
|
|
|
score.c.pv_id == pv_id,
|
|
|
|
score.c.difficulty == difficulty,
|
2023-03-09 16:38:58 +00:00
|
|
|
score.c.edition == edition,
|
2023-02-18 20:01:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.execute(sql)
|
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
return result.fetchone()
|
|
|
|
|
2023-04-30 21:31:13 +00:00
|
|
|
def get_best_scores(self, user_id: int) -> Optional[List[Row]]:
|
2023-02-17 06:02:21 +00:00
|
|
|
sql = score.select(score.c.user == user_id)
|
|
|
|
|
|
|
|
result = self.execute(sql)
|
2023-02-18 20:01:31 +00:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.fetchall()
|