mirror of
https://gitea.tendokyu.moe/Hay1tsme/artemis.git
synced 2026-02-14 03:37:29 +08:00
add back games, conform them to new title dispatch
This commit is contained in:
6
titles/cxb/schema/__init__.py
Normal file
6
titles/cxb/schema/__init__.py
Normal file
@@ -0,0 +1,6 @@
|
||||
from titles.cxb.schema.profile import CxbProfileData
|
||||
from titles.cxb.schema.score import CxbScoreData
|
||||
from titles.cxb.schema.item import CxbItemData
|
||||
from titles.cxb.schema.static import CxbStaticData
|
||||
|
||||
__all__ = [CxbProfileData, CxbScoreData, CxbItemData, CxbStaticData]
|
||||
45
titles/cxb/schema/item.py
Normal file
45
titles/cxb/schema/item.py
Normal file
@@ -0,0 +1,45 @@
|
||||
from typing import Optional, Dict, List
|
||||
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_, case
|
||||
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean
|
||||
from sqlalchemy.schema import ForeignKey
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
|
||||
from core.data.schema import BaseData, metadata
|
||||
|
||||
energy = Table(
|
||||
"cxb_rev_energy",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
||||
Column("energy", Integer, nullable=False, server_default="0"),
|
||||
UniqueConstraint("user", name="cxb_rev_energy_uk"),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
class CxbItemData(BaseData):
|
||||
def put_energy(self, user_id: int, rev_energy: int) -> Optional[int]:
|
||||
sql = insert(energy).values(
|
||||
user = user_id,
|
||||
energy = rev_energy
|
||||
)
|
||||
|
||||
conflict = sql.on_duplicate_key_update(
|
||||
energy = rev_energy
|
||||
)
|
||||
|
||||
result = self.execute(conflict)
|
||||
if result is None:
|
||||
self.logger.error(f"{__name__} failed to insert item! user: {user_id}, energy: {rev_energy}")
|
||||
return None
|
||||
|
||||
return result.lastrowid
|
||||
|
||||
def get_energy(self, user_id: int) -> Optional[Dict]:
|
||||
sql = energy.select(
|
||||
and_(energy.c.user == user_id)
|
||||
)
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchone()
|
||||
72
titles/cxb/schema/profile.py
Normal file
72
titles/cxb/schema/profile.py
Normal file
@@ -0,0 +1,72 @@
|
||||
from typing import Optional, Dict, List
|
||||
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_
|
||||
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON
|
||||
from sqlalchemy.schema import ForeignKey
|
||||
from sqlalchemy.sql import func, select
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
|
||||
from core.data.schema import BaseData, metadata
|
||||
|
||||
profile = Table(
|
||||
"cxb_profile",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
||||
Column("version", Integer, nullable=False),
|
||||
Column("index", Integer, nullable=False),
|
||||
Column("data", JSON, nullable=False),
|
||||
UniqueConstraint("user", "index", name="cxb_profile_uk"),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
class CxbProfileData(BaseData):
|
||||
def put_profile(self, user_id: int, version: int, index: int, data: JSON) -> Optional[int]:
|
||||
sql = insert(profile).values(
|
||||
user = user_id,
|
||||
version = version,
|
||||
index = index,
|
||||
data = data
|
||||
)
|
||||
|
||||
conflict = sql.on_duplicate_key_update(
|
||||
index = index,
|
||||
data = data
|
||||
)
|
||||
|
||||
result = self.execute(conflict)
|
||||
if result is None:
|
||||
self.logger.error(f"{__name__} failed to update! user: {user_id}, index: {index}, data: {data}")
|
||||
return None
|
||||
|
||||
return result.lastrowid
|
||||
|
||||
def get_profile(self, aime_id: int, version: int) -> Optional[List[Dict]]:
|
||||
"""
|
||||
Given a game version and either a profile or aime id, return the profile
|
||||
"""
|
||||
sql = profile.select(and_(
|
||||
profile.c.version == version,
|
||||
profile.c.user == aime_id
|
||||
))
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchall()
|
||||
|
||||
def get_profile_index(self, index: int, aime_id: int = None, version: int = None) -> Optional[Dict]:
|
||||
"""
|
||||
Given a game version and either a profile or aime id, return the profile
|
||||
"""
|
||||
if aime_id is not None and version is not None and index is not None:
|
||||
sql = profile.select(and_(
|
||||
profile.c.version == version,
|
||||
profile.c.user == aime_id,
|
||||
profile.c.index == index
|
||||
))
|
||||
else:
|
||||
self.logger.error(f"get_profile: Bad arguments!! aime_id {aime_id} version {version}")
|
||||
return None
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchone()
|
||||
166
titles/cxb/schema/score.py
Normal file
166
titles/cxb/schema/score.py
Normal file
@@ -0,0 +1,166 @@
|
||||
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_
|
||||
from sqlalchemy.types import Integer, String, TIMESTAMP, JSON, Boolean
|
||||
from sqlalchemy.schema import ForeignKey
|
||||
from sqlalchemy.sql import func
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
from typing import Optional, List, Dict, Any
|
||||
|
||||
from core.data.schema import BaseData, metadata
|
||||
from core.data import cached
|
||||
|
||||
score = Table(
|
||||
"cxb_score",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
||||
Column("game_version", Integer),
|
||||
Column("song_mcode", String(7)),
|
||||
Column("song_index", Integer),
|
||||
Column("data", JSON),
|
||||
UniqueConstraint("user", "song_mcode", "song_index", name="cxb_score_uk"),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
playlog = Table(
|
||||
"cxb_playlog",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
||||
Column("song_mcode", String(7)),
|
||||
Column("chart_id", Integer),
|
||||
Column("score", Integer),
|
||||
Column("clear", Integer),
|
||||
Column("flawless", Integer),
|
||||
Column("super", Integer),
|
||||
Column("cool", Integer),
|
||||
Column("fast", Integer),
|
||||
Column("fast2", Integer),
|
||||
Column("slow", Integer),
|
||||
Column("slow2", Integer),
|
||||
Column("fail", Integer),
|
||||
Column("combo", Integer),
|
||||
Column("date_scored", TIMESTAMP, server_default=func.now()),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
ranking = Table(
|
||||
"cxb_ranking",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("user", ForeignKey("aime_user.id", ondelete="cascade"), nullable=False),
|
||||
Column("rev_id", Integer),
|
||||
Column("song_id", Integer),
|
||||
Column("score", Integer),
|
||||
Column("clear", Integer),
|
||||
UniqueConstraint("user", "rev_id", name="cxb_ranking_uk"),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
class CxbScoreData(BaseData):
|
||||
def put_best_score(self, user_id: int, song_mcode: str, game_version: int, song_index: int, data: JSON) -> Optional[int]:
|
||||
"""
|
||||
Update the user's best score for a chart
|
||||
"""
|
||||
sql = insert(score).values(
|
||||
user=user_id,
|
||||
song_mcode=song_mcode,
|
||||
game_version=game_version,
|
||||
song_index=song_index,
|
||||
data=data
|
||||
)
|
||||
|
||||
conflict = sql.on_duplicate_key_update(
|
||||
data = sql.inserted.data
|
||||
)
|
||||
|
||||
result = self.execute(conflict)
|
||||
if result is None:
|
||||
self.logger.error(f"{__name__} failed to insert best score! profile: {user_id}, song: {song_mcode}, data: {data}")
|
||||
return None
|
||||
|
||||
return result.lastrowid
|
||||
|
||||
def put_playlog(self, user_id: int, song_mcode: str, chart_id: int, score: int, clear: int, flawless: int, this_super: int,
|
||||
cool: int, this_fast: int, this_fast2: int, this_slow: int, this_slow2: int, fail: int, combo: int) -> Optional[int]:
|
||||
"""
|
||||
Add an entry to the user's play log
|
||||
"""
|
||||
sql = playlog.insert().values(
|
||||
user=user_id,
|
||||
song_mcode=song_mcode,
|
||||
chart_id=chart_id,
|
||||
score=score,
|
||||
clear=clear,
|
||||
flawless=flawless,
|
||||
super=this_super,
|
||||
cool=cool,
|
||||
fast=this_fast,
|
||||
fast2=this_fast2,
|
||||
slow=this_slow,
|
||||
slow2=this_slow2,
|
||||
fail=fail,
|
||||
combo=combo
|
||||
)
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None:
|
||||
self.logger.error(f"{__name__} failed to insert playlog! profile: {user_id}, song: {song_mcode}, chart: {chart_id}")
|
||||
return None
|
||||
|
||||
return result.lastrowid
|
||||
|
||||
def put_ranking(self, user_id: int, rev_id: int, song_id: int, score: int, clear: int) -> Optional[int]:
|
||||
"""
|
||||
Add an entry to the user's ranking logs
|
||||
"""
|
||||
if song_id == 0:
|
||||
sql = insert(ranking).values(
|
||||
user=user_id,
|
||||
rev_id=rev_id,
|
||||
score=score,
|
||||
clear=clear
|
||||
)
|
||||
else:
|
||||
sql = insert(ranking).values(
|
||||
user=user_id,
|
||||
rev_id=rev_id,
|
||||
song_id=song_id,
|
||||
score=score,
|
||||
clear=clear
|
||||
)
|
||||
|
||||
conflict = sql.on_duplicate_key_update(
|
||||
score = score
|
||||
)
|
||||
|
||||
result = self.execute(conflict)
|
||||
if result is None:
|
||||
self.logger.error(f"{__name__} failed to insert ranking log! profile: {user_id}, score: {score}, clear: {clear}")
|
||||
return None
|
||||
|
||||
return result.lastrowid
|
||||
|
||||
def get_best_score(self, user_id: int, song_mcode: int) -> Optional[Dict]:
|
||||
sql = score.select(
|
||||
and_(score.c.user == user_id, score.c.song_mcode == song_mcode)
|
||||
)
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchone()
|
||||
|
||||
def get_best_scores(self, user_id: int) -> Optional[Dict]:
|
||||
sql = score.select(score.c.user == user_id)
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchall()
|
||||
|
||||
def get_best_rankings(self, user_id: int) -> Optional[List[Dict]]:
|
||||
sql = ranking.select(
|
||||
ranking.c.user == user_id
|
||||
)
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchall()
|
||||
74
titles/cxb/schema/static.py
Normal file
74
titles/cxb/schema/static.py
Normal file
@@ -0,0 +1,74 @@
|
||||
from typing import Dict, List, Optional
|
||||
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_
|
||||
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON, Float
|
||||
from sqlalchemy.engine.base import Connection
|
||||
from sqlalchemy.engine import Row
|
||||
from sqlalchemy.schema import ForeignKey
|
||||
from sqlalchemy.sql import func, select
|
||||
from sqlalchemy.dialects.mysql import insert
|
||||
|
||||
from core.data.schema import BaseData, metadata
|
||||
|
||||
music = Table(
|
||||
"cxb_static_music",
|
||||
metadata,
|
||||
Column("id", Integer, primary_key=True, nullable=False),
|
||||
Column("version", Integer, nullable=False),
|
||||
Column("songId", String(255)),
|
||||
Column("index", Integer),
|
||||
Column("chartId", Integer),
|
||||
Column("title", String(255)),
|
||||
Column("artist", String(255)),
|
||||
Column("category", String(255)),
|
||||
Column("level", Float),
|
||||
UniqueConstraint("version", "songId", "chartId", "index", name="cxb_static_music_uk"),
|
||||
mysql_charset='utf8mb4'
|
||||
)
|
||||
|
||||
class CxbStaticData(BaseData):
|
||||
def put_music(self, version: int, mcode: str, index: int, chart: int, title: str, artist: str, category: str, level: float ) -> Optional[int]:
|
||||
sql = insert(music).values(
|
||||
version = version,
|
||||
songId = mcode,
|
||||
index = index,
|
||||
chartId = chart,
|
||||
title = title,
|
||||
artist = artist,
|
||||
category = category,
|
||||
level = level
|
||||
)
|
||||
|
||||
conflict = sql.on_duplicate_key_update(
|
||||
title = title,
|
||||
artist = artist,
|
||||
category = category,
|
||||
level = level
|
||||
)
|
||||
|
||||
result = self.execute(conflict)
|
||||
if result is None: return None
|
||||
return result.lastrowid
|
||||
|
||||
def get_music(self, version: int, song_id: Optional[int] = None) -> Optional[List[Row]]:
|
||||
if song_id is None:
|
||||
sql = select(music).where(music.c.version == version)
|
||||
else:
|
||||
sql = select(music).where(and_(
|
||||
music.c.version == version,
|
||||
music.c.songId == song_id,
|
||||
))
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchall()
|
||||
|
||||
def get_music_chart(self, version: int, song_id: int, chart_id: int) -> Optional[List[Row]]:
|
||||
sql = select(music).where(and_(
|
||||
music.c.version == version,
|
||||
music.c.songId == song_id,
|
||||
music.c.chartId == chart_id
|
||||
))
|
||||
|
||||
result = self.execute(sql)
|
||||
if result is None: return None
|
||||
return result.fetchone()
|
||||
Reference in New Issue
Block a user