MibooGram/core/services/history_data/services.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

73 lines
2.2 KiB
Python
Raw Normal View History

2024-04-27 10:13:54 +00:00
import datetime
2024-07-15 08:32:25 +00:00
from typing import List
2024-04-27 10:13:54 +00:00
from pytz import timezone
2024-07-15 08:32:25 +00:00
from simnet.models.starrail.diary import StarRailDiary
from simnet.models.zzz.chronicle.challenge import ZZZChallenge
2024-04-27 10:13:54 +00:00
2024-07-15 08:32:25 +00:00
from core.services.history_data.models import (
HistoryData,
HistoryDataTypeEnum,
HistoryDataAbyss,
HistoryDataLedger,
)
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",
"HistoryDataLedgerServices",
2024-04-27 10:13:54 +00:00
)
TZ = timezone("Asia/Shanghai")
def json_encoder(value):
if isinstance(value, datetime.datetime):
return value.astimezone(TZ).strftime("%Y-%m-%d %H:%M:%S")
return value
class HistoryDataAbyssServices(BaseService, HistoryDataBaseServices):
DATA_TYPE = HistoryDataTypeEnum.ABYSS.value
@staticmethod
def exists_data(data: HistoryData, old_data: List[HistoryData]) -> bool:
2024-07-15 08:32:25 +00:00
floors = data.data.get("abyss_data", {}).get("all_floor_detail")
return any(d.data.get("abyss_data", {}).get("all_floor_detail") == floors for d in old_data)
2024-04-27 10:13:54 +00:00
@staticmethod
2024-07-15 08:32:25 +00:00
def create(user_id: int, abyss_data: ZZZChallenge):
data = HistoryDataAbyss(abyss_data=abyss_data)
2024-04-27 10:13:54 +00:00
json_data = data.json(by_alias=True, encoder=json_encoder)
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
2024-07-15 08:32:25 +00:00
def create(user_id: int, diary_data: StarRailDiary):
2024-05-24 09:40:09 +00:00
data = HistoryDataLedger(diary_data=diary_data)
json_data = data.json(by_alias=True, encoder=json_encoder)
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),
)