mirror of
https://github.com/Shu-Ying/main_plugin_farm.git
synced 2026-10-31 23:09:58 +08:00
🎨 使用Python包管理和模块访问
🎨 重构数据库结构, 使用集中式服务定位器与数据库交互 🎨 更改部分代码逻辑, 减小内存消耗
This commit is contained in:
+102
-31
@@ -1,44 +1,115 @@
|
||||
import asyncio
|
||||
from typing import Any
|
||||
|
||||
from zhenxun.services.log import logger
|
||||
|
||||
from .database import (
|
||||
CPlantManager,
|
||||
CUserDB,
|
||||
CUserItemDB,
|
||||
CUserPlantCountDB,
|
||||
CUserPlantDB,
|
||||
CUserSeedDB,
|
||||
CUserSignDB,
|
||||
CUserSoilDB,
|
||||
CUserStealDB,
|
||||
g_pDBlocator,
|
||||
)
|
||||
|
||||
|
||||
class CDBService:
|
||||
"""
|
||||
数据库服务管理器
|
||||
负责初始化和协调所有数据库服务
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
self.m_manager: dict[str, Any] = {}
|
||||
self.m_bInitialized = False
|
||||
|
||||
async def init(self):
|
||||
from .database.plant import CPlantManager
|
||||
from .database.user import CUserDB
|
||||
from .database.userItem import CUserItemDB
|
||||
from .database.userPlant import CUserPlantDB
|
||||
from .database.userPlantCount import CUserPlantCountDB
|
||||
from .database.userSeed import CUserSeedDB
|
||||
from .database.userSign import CUserSignDB
|
||||
from .database.userSoil import CUserSoilDB
|
||||
from .database.userSteal import CUserStealDB
|
||||
if self.m_bInitialized:
|
||||
logger.warning("真寻农场数据库服务已初始化")
|
||||
return
|
||||
|
||||
self.plant = CPlantManager()
|
||||
await self.plant.init()
|
||||
m_manager = {
|
||||
"plant": CPlantManager(),
|
||||
"user": CUserDB(),
|
||||
"userSoil": CUserSoilDB(),
|
||||
"userPlant": CUserPlantDB(),
|
||||
"userSeed": CUserSeedDB(),
|
||||
"userItem": CUserItemDB(),
|
||||
"userSteal": CUserStealDB(),
|
||||
"userSign": CUserSignDB(),
|
||||
"userPlantCount": CUserPlantCountDB(),
|
||||
}
|
||||
|
||||
self.user = CUserDB()
|
||||
await self.user.initDB()
|
||||
for name, manager in m_manager.items():
|
||||
g_pDBlocator.register(name, manager)
|
||||
self.m_manager[name] = manager
|
||||
|
||||
self.userSoil = CUserSoilDB()
|
||||
await self.userSoil.initDB()
|
||||
tasks = []
|
||||
for name, manager in self.m_manager.items():
|
||||
tasks.append(manager.init())
|
||||
tasks.append(manager.initDB())
|
||||
|
||||
self.userPlant = CUserPlantDB()
|
||||
await self.userPlant.initDB()
|
||||
await asyncio.gather(*tasks)
|
||||
|
||||
self.userSeed = CUserSeedDB()
|
||||
await self.userSeed.initDB()
|
||||
g_pDBlocator.markInitialized()
|
||||
self.m_bInitialized = True
|
||||
|
||||
self.userItem = CUserItemDB()
|
||||
await self.userItem.initDB()
|
||||
|
||||
self.userSteal = CUserStealDB()
|
||||
await self.userSteal.initDB()
|
||||
|
||||
self.userSign = CUserSignDB()
|
||||
await self.userSign.initDB()
|
||||
|
||||
self.userPlantCount = CUserPlantCountDB()
|
||||
await self.userPlantCount.initDB()
|
||||
logger.debug("真寻农场数据库服务初始化完成")
|
||||
|
||||
async def cleanup(self):
|
||||
await self.plant.cleanup()
|
||||
task = []
|
||||
|
||||
for manager in self.m_manager.values():
|
||||
if hasattr(manager, "cleanup"):
|
||||
task.append(manager.cleanup())
|
||||
|
||||
await asyncio.gather(*task)
|
||||
self.m_manager.clear()
|
||||
self.m_bInitialized = False
|
||||
|
||||
@property
|
||||
def plant(self):
|
||||
return self.m_manager["plant"]
|
||||
|
||||
@property
|
||||
def user(self):
|
||||
return self.m_manager["user"]
|
||||
|
||||
@property
|
||||
def userSoil(self):
|
||||
return self.m_manager["userSoil"]
|
||||
|
||||
@property
|
||||
def userPlant(self):
|
||||
return self.m_manager["userPlant"]
|
||||
|
||||
@property
|
||||
def userSeed(self):
|
||||
return self.m_manager["userSeed"]
|
||||
|
||||
@property
|
||||
def userItem(self):
|
||||
return self.m_manager["userItem"]
|
||||
|
||||
@property
|
||||
def userSteal(self):
|
||||
return self.m_manager["userSteal"]
|
||||
|
||||
@property
|
||||
def userSign(self):
|
||||
return self.m_manager["userSign"]
|
||||
|
||||
@property
|
||||
def userPlantCount(self):
|
||||
return self.m_manager["userPlantCount"]
|
||||
|
||||
@property
|
||||
def initialized(self) -> bool:
|
||||
return self.m_bInitialized
|
||||
|
||||
|
||||
g_pDBService = CDBService()
|
||||
|
||||
Reference in New Issue
Block a user