2023-02-17 07:02:21 +01:00
|
|
|
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
|
|
|
|
|
|
|
|
shop = Table(
|
|
|
|
"diva_profile_shop",
|
|
|
|
metadata,
|
|
|
|
Column("id", Integer, primary_key=True, nullable=False),
|
2023-03-09 17:38:58 +01:00
|
|
|
Column(
|
|
|
|
"user",
|
|
|
|
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
|
|
|
|
nullable=False,
|
|
|
|
),
|
2023-02-17 07:02:21 +01:00
|
|
|
Column("version", Integer, nullable=False),
|
|
|
|
Column("mdl_eqp_ary", String(32)),
|
2023-02-18 21:01:31 +01:00
|
|
|
Column("c_itm_eqp_ary", String(59)),
|
|
|
|
Column("ms_itm_flg_ary", String(59)),
|
2023-02-17 07:02:21 +01:00
|
|
|
UniqueConstraint("user", "version", name="diva_profile_shop_uk"),
|
2023-03-09 17:38:58 +01:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
|
2023-02-18 21:01:31 +01:00
|
|
|
|
|
|
|
class DivaItemData(BaseData):
|
2023-03-09 17:38:58 +01:00
|
|
|
def put_shop(
|
|
|
|
self,
|
|
|
|
aime_id: int,
|
|
|
|
version: int,
|
|
|
|
mdl_eqp_ary: str,
|
|
|
|
c_itm_eqp_ary: str,
|
|
|
|
ms_itm_flg_ary: str,
|
|
|
|
) -> None:
|
2023-02-17 07:02:21 +01:00
|
|
|
sql = insert(shop).values(
|
|
|
|
version=version,
|
|
|
|
user=aime_id,
|
2023-02-18 21:01:31 +01:00
|
|
|
mdl_eqp_ary=mdl_eqp_ary,
|
|
|
|
c_itm_eqp_ary=c_itm_eqp_ary,
|
2023-03-09 17:38:58 +01:00
|
|
|
ms_itm_flg_ary=ms_itm_flg_ary,
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
conflict = sql.on_duplicate_key_update(
|
2023-02-18 21:01:31 +01:00
|
|
|
mdl_eqp_ary=mdl_eqp_ary,
|
|
|
|
c_itm_eqp_ary=c_itm_eqp_ary,
|
2023-03-09 17:38:58 +01:00
|
|
|
ms_itm_flg_ary=ms_itm_flg_ary,
|
2023-02-17 07:02:21 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
result = self.execute(conflict)
|
|
|
|
if result is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
|
|
|
f"{__name__} Failed to insert diva profile! aime id: {aime_id} array: {mdl_eqp_ary}"
|
|
|
|
)
|
2023-02-17 07:02:21 +01:00
|
|
|
return None
|
|
|
|
return result.lastrowid
|
|
|
|
|
|
|
|
def get_shop(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 17:38:58 +01:00
|
|
|
sql = shop.select(and_(shop.c.version == version, shop.c.user == aime_id))
|
2023-02-18 21:01:31 +01:00
|
|
|
|
2023-02-17 07:02:21 +01:00
|
|
|
result = self.execute(sql)
|
2023-02-18 21:01:31 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
return result.fetchone()
|