2024-04-27 10:13:54 +00:00
|
|
|
import datetime
|
|
|
|
from typing import Dict, List
|
|
|
|
|
|
|
|
from simnet.models.genshin.chronicle.abyss import SpiralAbyss
|
2024-07-29 11:06:00 +00:00
|
|
|
from simnet.models.genshin.chronicle.img_theater import ImgTheaterData
|
2024-05-24 09:40:09 +00:00
|
|
|
from simnet.models.genshin.diary import Diary
|
2024-04-27 10:13:54 +00:00
|
|
|
|
2024-07-29 11:06:00 +00:00
|
|
|
from core.services.history_data.models import (
|
|
|
|
HistoryData,
|
|
|
|
HistoryDataTypeEnum,
|
|
|
|
HistoryDataAbyss,
|
|
|
|
HistoryDataLedger,
|
|
|
|
HistoryDataImgTheater,
|
|
|
|
)
|
2024-04-27 10:13:54 +00:00
|
|
|
from gram_core.base_service import BaseService
|
|
|
|
from gram_core.services.history_data.services import HistoryDataBaseServices
|
|
|
|
|
|
|
|
try:
|
|
|
|
import ujson as jsonlib
|
|
|
|
except ImportError:
|
|
|
|
import json as jsonlib
|
|
|
|
|
|
|
|
|
|
|
|
__all__ = (
|
|
|
|
"HistoryDataBaseServices",
|
|
|
|
"HistoryDataAbyssServices",
|
2024-05-24 14:10:54 +00:00
|
|
|
"HistoryDataLedgerServices",
|
2024-07-29 11:06:00 +00:00
|
|
|
"HistoryDataImgTheaterServices",
|
2024-04-27 10:13:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class HistoryDataAbyssServices(BaseService, HistoryDataBaseServices):
|
|
|
|
DATA_TYPE = HistoryDataTypeEnum.ABYSS.value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def exists_data(data: HistoryData, old_data: List[HistoryData]) -> bool:
|
2024-06-04 11:32:11 +00:00
|
|
|
floor = data.data.get("floors")
|
|
|
|
return any(d.data.get("floors") == floor for d in old_data)
|
2024-04-27 10:13:54 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create(user_id: int, abyss_data: SpiralAbyss, character_data: Dict[int, int]):
|
|
|
|
data = HistoryDataAbyss(abyss_data=abyss_data, character_data=character_data)
|
2024-11-30 14:32:07 +00:00
|
|
|
json_data = data.model_dump_json(by_alias=True)
|
2024-04-27 10:13:54 +00:00
|
|
|
return HistoryData(
|
|
|
|
user_id=user_id,
|
|
|
|
data_id=abyss_data.season,
|
|
|
|
time_created=datetime.datetime.now(),
|
|
|
|
type=HistoryDataAbyssServices.DATA_TYPE,
|
|
|
|
data=jsonlib.loads(json_data),
|
|
|
|
)
|
2024-05-24 09:40:09 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryDataLedgerServices(BaseService, HistoryDataBaseServices):
|
|
|
|
DATA_TYPE = HistoryDataTypeEnum.LEDGER.value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create(user_id: int, diary_data: Diary):
|
|
|
|
data = HistoryDataLedger(diary_data=diary_data)
|
2024-11-30 14:32:07 +00:00
|
|
|
json_data = data.model_dump_json(by_alias=True)
|
2024-05-24 09:40:09 +00:00
|
|
|
return HistoryData(
|
|
|
|
user_id=user_id,
|
|
|
|
data_id=diary_data.data_id,
|
|
|
|
time_created=datetime.datetime.now(),
|
|
|
|
type=HistoryDataLedgerServices.DATA_TYPE,
|
|
|
|
data=jsonlib.loads(json_data),
|
|
|
|
)
|
2024-07-29 11:06:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
class HistoryDataImgTheaterServices(BaseService, HistoryDataBaseServices):
|
|
|
|
DATA_TYPE = HistoryDataTypeEnum.ROLE_COMBAT.value
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def exists_data(data: HistoryData, old_data: List[HistoryData]) -> bool:
|
|
|
|
floor = data.data.get("detail", {}).get("rounds_data")
|
|
|
|
return any(d.data.get("detail", {}).get("rounds_data") == floor for d in old_data)
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def create(user_id: int, abyss_data: ImgTheaterData, character_data: Dict[int, int]):
|
|
|
|
data = HistoryDataImgTheater(abyss_data=abyss_data, character_data=character_data)
|
2024-11-30 14:32:07 +00:00
|
|
|
json_data = data.model_dump_json(by_alias=True)
|
2024-07-29 11:06:00 +00:00
|
|
|
return HistoryData(
|
|
|
|
user_id=user_id,
|
|
|
|
data_id=abyss_data.schedule.id,
|
|
|
|
time_created=datetime.datetime.now(),
|
|
|
|
type=HistoryDataImgTheaterServices.DATA_TYPE,
|
|
|
|
data=jsonlib.loads(json_data),
|
|
|
|
)
|