93 lines
2.6 KiB
Python
93 lines
2.6 KiB
Python
from typing import Final
|
|
|
|
from astrbot.api import AstrBotConfig, logger
|
|
|
|
from .adapters import APIAdapterRouter
|
|
from .api_config import APIEndpointRegistry
|
|
from .request import APIClient
|
|
from .services import (
|
|
AchievementService,
|
|
AdventureService,
|
|
ArenaService,
|
|
CalendarService,
|
|
CharacterService,
|
|
ContentService,
|
|
FactionService,
|
|
InformationService,
|
|
TradeService,
|
|
)
|
|
from .services.base import ServiceContext
|
|
from .services.cache import JSONCacheRepository
|
|
from .sqlite import AsyncSQLiteDB
|
|
|
|
|
|
class JX3Service:
|
|
"""组合各游戏领域 Service,并统一管理 HTTP 生命周期。"""
|
|
|
|
DOMAIN_NAMES: Final = (
|
|
"calendar",
|
|
"information",
|
|
"character",
|
|
"faction",
|
|
"adventure",
|
|
"arena",
|
|
"achievement",
|
|
"trade",
|
|
"content",
|
|
)
|
|
|
|
def __init__(
|
|
self,
|
|
api_data_path: str,
|
|
config: AstrBotConfig,
|
|
sqlite: AsyncSQLiteDB,
|
|
cache_sqlite: AsyncSQLiteDB | None = None,
|
|
):
|
|
registry = APIEndpointRegistry.from_file(api_data_path)
|
|
self._api = APIClient()
|
|
|
|
token = str(config.get("jx3api_token", ""))
|
|
ticket = str(config.get("jx3api_ticket", ""))
|
|
router = APIAdapterRouter(
|
|
self._api,
|
|
registry,
|
|
jx3api_token=token,
|
|
jx3api_ticket=ticket,
|
|
)
|
|
|
|
cache_db = cache_sqlite or sqlite
|
|
context = ServiceContext(
|
|
router=router,
|
|
database=sqlite,
|
|
cache=JSONCacheRepository(cache_db),
|
|
)
|
|
|
|
self.calendar = CalendarService(context)
|
|
self.information = InformationService(context)
|
|
self.character = CharacterService(context)
|
|
self.faction = FactionService(context)
|
|
self.adventure = AdventureService(context)
|
|
self.arena = ArenaService(context)
|
|
self.achievement = AchievementService(context)
|
|
self.trade = TradeService(context)
|
|
self.content = ContentService(context)
|
|
|
|
if not token:
|
|
logger.warning(
|
|
"获取配置 token 失败,请正确填写 token,否则部分功能无法正常使用"
|
|
)
|
|
else:
|
|
logger.debug("JX3API Token 已配置")
|
|
if not ticket:
|
|
logger.warning(
|
|
"获取配置 ticket 失败,请正确填写 ticket,否则部分功能无法正常使用"
|
|
)
|
|
else:
|
|
logger.debug("推栏 Ticket 已配置")
|
|
|
|
async def start(self) -> None:
|
|
await self._api.start()
|
|
|
|
async def close(self) -> None:
|
|
await self._api.close()
|