2023-02-18 21:01:31 +01:00
|
|
|
from typing import Optional, Dict, List
|
|
|
|
from sqlalchemy import Table, Column, UniqueConstraint, and_
|
|
|
|
from sqlalchemy.types import Integer
|
|
|
|
from sqlalchemy.schema import ForeignKey
|
|
|
|
from sqlalchemy.dialects.mysql import insert
|
|
|
|
|
|
|
|
from core.data.schema import BaseData, metadata
|
|
|
|
|
|
|
|
module = Table(
|
|
|
|
"diva_profile_module",
|
|
|
|
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-18 21:01:31 +01:00
|
|
|
Column("version", Integer, nullable=False),
|
|
|
|
Column("module_id", Integer, nullable=False),
|
|
|
|
UniqueConstraint("user", "version", "module_id", name="diva_profile_module_uk"),
|
2023-03-09 17:38:58 +01:00
|
|
|
mysql_charset="utf8mb4",
|
2023-02-18 21:01:31 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class DivaModuleData(BaseData):
|
2024-01-09 20:42:17 +01:00
|
|
|
async def put_module(self, aime_id: int, version: int, module_id: int) -> None:
|
2023-03-09 17:38:58 +01:00
|
|
|
sql = insert(module).values(version=version, user=aime_id, module_id=module_id)
|
2023-02-18 21:01:31 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-02-18 21:01:31 +01:00
|
|
|
if result is None:
|
2023-03-09 17:38:58 +01:00
|
|
|
self.logger.error(
|
|
|
|
f"{__name__} Failed to insert diva profile module! aime id: {aime_id} module: {module_id}"
|
|
|
|
)
|
2023-02-18 21:01:31 +01:00
|
|
|
return None
|
|
|
|
return result.lastrowid
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_modules(self, aime_id: int, version: int) -> Optional[List[Dict]]:
|
2023-02-18 21:01:31 +01:00
|
|
|
"""
|
|
|
|
Given a game version and an aime id, return all the modules, not used directly
|
|
|
|
"""
|
2023-03-09 17:38:58 +01:00
|
|
|
sql = module.select(and_(module.c.version == version, module.c.user == aime_id))
|
2023-02-18 21:01:31 +01:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(sql)
|
2023-02-18 21:01:31 +01:00
|
|
|
if result is None:
|
|
|
|
return None
|
|
|
|
return result.fetchall()
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def get_modules_have_string(self, aime_id: int, version: int) -> str:
|
2023-02-18 21:01:31 +01:00
|
|
|
"""
|
|
|
|
Given a game version and an aime id, return the mdl_have hex string
|
|
|
|
required for diva directly
|
|
|
|
"""
|
2024-05-26 15:58:28 +02:00
|
|
|
module_list = await self.get_modules(aime_id, version)
|
2023-02-18 21:01:31 +01:00
|
|
|
if module_list is None:
|
|
|
|
module_list = []
|
|
|
|
module_have = 0
|
|
|
|
|
|
|
|
for module in module_list:
|
|
|
|
module_have |= 1 << module["module_id"]
|
|
|
|
|
|
|
|
# convert the int to a 250 digit long hex string
|
|
|
|
return "{0:0>250}".format(hex(module_have).upper()[2:])
|