artemis/titles/diva/schema/module.py

63 lines
2.2 KiB
Python
Raw Permalink Normal View History

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 16:38:58 +00:00
Column(
"user",
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
nullable=False,
),
Column("version", Integer, nullable=False),
Column("module_id", Integer, nullable=False),
UniqueConstraint("user", "version", "module_id", name="diva_profile_module_uk"),
2023-03-09 16:38:58 +00:00
mysql_charset="utf8mb4",
)
class DivaModuleData(BaseData):
2024-01-09 19:42:17 +00:00
async def put_module(self, aime_id: int, version: int, module_id: int) -> None:
2023-03-09 16:38:58 +00:00
sql = insert(module).values(version=version, user=aime_id, module_id=module_id)
2024-01-09 19:42:17 +00:00
result = await self.execute(sql)
if result is None:
2023-03-09 16:38:58 +00:00
self.logger.error(
f"{__name__} Failed to insert diva profile module! aime id: {aime_id} module: {module_id}"
)
return None
return result.lastrowid
2024-01-09 19:42:17 +00:00
async def get_modules(self, aime_id: int, version: int) -> Optional[List[Dict]]:
"""
Given a game version and an aime id, return all the modules, not used directly
"""
2023-03-09 16:38:58 +00:00
sql = module.select(and_(module.c.version == version, module.c.user == aime_id))
2024-01-09 19:42:17 +00:00
result = await self.execute(sql)
if result is None:
return None
return result.fetchall()
2024-01-09 19:42:17 +00:00
async def get_modules_have_string(self, aime_id: int, version: int) -> str:
"""
Given a game version and an aime id, return the mdl_have hex string
required for diva directly
"""
module_list = self.get_modules(aime_id, version)
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:])