mai2: add opts to reader

This commit is contained in:
2025-04-08 17:42:17 -04:00
parent e16bfc713a
commit 47affd898f
2 changed files with 154 additions and 21 deletions

View File

@ -7,6 +7,7 @@ from sqlalchemy.schema import ForeignKey
from sqlalchemy.sql import func, select
from sqlalchemy.engine import Row
from sqlalchemy.dialects.mysql import insert
from sqlalchemy.sql.functions import coalesce
from datetime import datetime
opts = Table(
@ -92,16 +93,17 @@ cards = Table(
class Mai2StaticData(BaseData):
async def put_game_event(
self, version: int, type: int, event_id: int, name: str
self, version: int, type: int, event_id: int, name: str, opt_id: int = None
) -> Optional[int]:
sql = insert(event).values(
version=version,
type=type,
eventId=event_id,
name=name,
opt=coalesce(event.c.opt, opt_id)
)
conflict = sql.on_duplicate_key_update(eventId=event_id)
conflict = sql.on_duplicate_key_update(eventId=event_id, opt=coalesce(event.c.opt, opt_id))
result = await self.execute(conflict)
if result is None:
@ -154,6 +156,7 @@ class Mai2StaticData(BaseData):
added_version: str,
difficulty: float,
note_designer: str,
opt_id: int = None
) -> None:
sql = insert(music).values(
version=version,
@ -166,6 +169,7 @@ class Mai2StaticData(BaseData):
addedVersion=added_version,
difficulty=difficulty,
noteDesigner=note_designer,
opt=coalesce(music.c.opt, opt_id)
)
conflict = sql.on_duplicate_key_update(
@ -176,6 +180,7 @@ class Mai2StaticData(BaseData):
addedVersion=added_version,
difficulty=difficulty,
noteDesigner=note_designer,
opt=coalesce(music.c.opt, opt_id)
)
result = await self.execute(conflict)
@ -191,6 +196,7 @@ class Mai2StaticData(BaseData):
ticket_type: int,
ticket_price: int,
name: str,
opt_id: int = None
) -> Optional[int]:
sql = insert(ticket).values(
version=version,
@ -198,11 +204,10 @@ class Mai2StaticData(BaseData):
kind=ticket_type,
price=ticket_price,
name=name,
opt=coalesce(ticket.c.opt, opt_id)
)
conflict = sql.on_duplicate_key_update(price=ticket_price)
conflict = sql.on_duplicate_key_update(price=ticket_price)
conflict = sql.on_duplicate_key_update(price=ticket_price, opt=coalesce(ticket.c.opt, opt_id))
result = await self.execute(conflict)
if result is None:
@ -247,12 +252,12 @@ class Mai2StaticData(BaseData):
return None
return result.fetchone()
async def put_card(self, version: int, card_id: int, card_name: str, **card_data) -> int:
async def put_card(self, version: int, card_id: int, card_name: str, opt_id: int = None, **card_data) -> int:
sql = insert(cards).values(
version=version, cardId=card_id, cardName=card_name, **card_data
version=version, cardId=card_id, cardName=card_name, opt=coalesce(cards.c.opt, opt_id) **card_data
)
conflict = sql.on_duplicate_key_update(**card_data)
conflict = sql.on_duplicate_key_update(opt=coalesce(cards.c.opt, opt_id), **card_data)
result = await self.execute(conflict)
if result is None:
@ -282,3 +287,85 @@ class Mai2StaticData(BaseData):
result = await self.execute(event.update(event.c.id == table_id).values(enabled=is_enable, startDate = start_date))
if not result:
self.logger.error(f"Failed to update event {table_id} - {is_enable} {start_date}")
async def put_opt(self, version: int, folder: str, sequence: int, cm_seq: int = None) -> Optional[int]:
sql = insert(opts).values(version=version, name=folder, sequence=sequence, cmReleaseVer=cm_seq)
conflict = sql.on_duplicate_key_update(sequence=sequence, whenRead=datetime.now())
result = await self.execute(conflict)
if result is None:
self.logger.warning(f"Failed to insert opt! version {version} folder {folder} sequence {sequence}")
return None
return result.lastrowid
async def get_opt_by_version_folder(self, version: int, folder: str) -> Optional[Row]:
result = await self.execute(opts.select(and_(
opts.c.version == version,
opts.c.name == folder,
)))
if result is None:
return None
return result.fetchone()
async def get_opt_by_version_sequence(self, version: int, sequence: str) -> Optional[Row]:
result = await self.execute(opts.select(and_(
opts.c.version == version,
opts.c.sequence == sequence,
)))
if result is None:
return None
return result.fetchone()
async def get_opts_by_version(self, version: int) -> Optional[List[Row]]:
result = await self.execute(opts.select(opts.c.version == version))
if result is None:
return None
return result.fetchall()
async def get_opts_enabled_by_version(self, version: int) -> Optional[List[Row]]:
result = await self.execute(opts.select(and_(
opts.c.version == version,
opts.c.isEnable == True,
)))
if result is None:
return None
return result.fetchall()
async def get_latest_enabled_opt_by_version(self, version: int) -> Optional[Row]:
result = await self.execute(
opts.select(and_(
opts.c.version == version,
opts.c.isEnable == True,
)).order_by(opts.c.sequence.desc())
)
if result is None:
return None
return result.fetchone()
async def get_opts(self) -> Optional[List[Row]]:
result = await self.execute(opts.select())
if result is None:
return None
return result.fetchall()
async def get_opts(self) -> Optional[List[Row]]:
result = await self.execute(opts.select())
if result is None:
return None
return result.fetchall()
async def set_opt_enabled(self, opt_id: int, enabled: bool) -> bool:
result = await self.execute(opts.update(opts.c.id == opt_id).values(isEnable=enabled))
if result is None:
self.logger.error(f"Failed to set opt enabled status to {enabled} for opt {opt_id}")
return False
return True