2023-02-17 06:02:21 +00:00
|
|
|
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"),
|
2023-03-09 16:38:58 +00:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
class CxbProfileData(BaseData):
|
2023-03-09 16:38:58 +00:00
|
|
|
def put_profile(
|
|
|
|
self, user_id: int, version: int, index: int, data: JSON
|
|
|
|
) -> Optional[int]:
|
2023-02-17 06:02:21 +00:00
|
|
|
sql = insert(profile).values(
|
2023-03-09 16:38:58 +00:00
|
|
|
user=user_id, version=version, index=index, data=data
|
2023-02-17 06:02:21 +00:00
|
|
|
)
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
conflict = sql.on_duplicate_key_update(index=index, data=data)
|
2023-02-17 06:02:21 +00:00
|
|
|
|
|
|
|
result = self.execute(conflict)
|
|
|
|
if result is None:
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"{__name__} failed to update! user: {user_id}, index: {index}, data: {data}"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
return None
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
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
|
|
|
|
"""
|
2023-03-09 16:38:58 +00:00
|
|
|
sql = profile.select(
|
|
|
|
and_(profile.c.version == version, profile.c.user == aime_id)
|
|
|
|
)
|
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
result = self.execute(sql)
|
2023-03-09 16:38:58 +00:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.fetchall()
|
|
|
|
|
2023-03-09 16:38:58 +00:00
|
|
|
def get_profile_index(
|
|
|
|
self, index: int, aime_id: int = None, version: int = None
|
|
|
|
) -> Optional[Dict]:
|
2023-02-17 06:02:21 +00:00
|
|
|
"""
|
|
|
|
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:
|
2023-03-09 16:38:58 +00:00
|
|
|
sql = profile.select(
|
|
|
|
and_(
|
2023-02-17 06:02:21 +00:00
|
|
|
profile.c.version == version,
|
|
|
|
profile.c.user == aime_id,
|
2023-03-09 16:38:58 +00:00
|
|
|
profile.c.index == index,
|
|
|
|
)
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
else:
|
2023-03-09 16:38:58 +00:00
|
|
|
self.logger.error(
|
|
|
|
f"get_profile: Bad arguments!! aime_id {aime_id} version {version}"
|
|
|
|
)
|
2023-02-17 06:02:21 +00:00
|
|
|
return None
|
2023-03-09 16:38:58 +00:00
|
|
|
|
2023-02-17 06:02:21 +00:00
|
|
|
result = self.execute(sql)
|
2023-03-09 16:38:58 +00:00
|
|
|
if result is None:
|
|
|
|
return None
|
2023-02-17 06:02:21 +00:00
|
|
|
return result.fetchone()
|