新增战斗/冒险系统
This commit is contained in:
186
Plugins/WPSCombatSystem/combat_plugin_base.py
Normal file
186
Plugins/WPSCombatSystem/combat_plugin_base.py
Normal file
@@ -0,0 +1,186 @@
|
||||
"""战斗系统基础插件类"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import List, Type
|
||||
|
||||
from PWF.Convention.Runtime.Architecture import Architecture
|
||||
from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig
|
||||
from PWF.CoreModules.plugin_interface import DatabaseModel
|
||||
|
||||
from Plugins.WPSAPI import WPSAPI
|
||||
from Plugins.WPSBackpackSystem import BackpackItemTier, WPSBackpackSystem
|
||||
from Plugins.WPSStoreSystem import WPSStoreSystem
|
||||
from Plugins.WPSConfigSystem import WPSConfigAPI
|
||||
from Plugins.WPSFortuneSystem import WPSFortuneSystem
|
||||
|
||||
from .combat_models import (
|
||||
ADVENTURE_MATERIALS,
|
||||
ADVENTURE_SEEDS,
|
||||
ADVENTURE_SOUVENIRS,
|
||||
COMBAT_POTIONS,
|
||||
EQUIPMENT_REGISTRY,
|
||||
get_combat_db_models,
|
||||
)
|
||||
from .combat_service import CombatService, get_combat_service
|
||||
|
||||
|
||||
logger: ProjectConfig = Architecture.Get(ProjectConfig)
|
||||
|
||||
|
||||
class WPSCombatBase(WPSAPI):
|
||||
"""战斗系统基础插件类"""
|
||||
|
||||
_service: CombatService | None = None
|
||||
_initialized: bool = False
|
||||
|
||||
@classmethod
|
||||
def service(cls) -> CombatService:
|
||||
"""获取共享的战斗服务实例"""
|
||||
if cls._service is None:
|
||||
cls._service = get_combat_service()
|
||||
return cls._service
|
||||
|
||||
def dependencies(self) -> List[Type]:
|
||||
"""声明依赖的插件"""
|
||||
return [
|
||||
WPSAPI,
|
||||
WPSConfigAPI,
|
||||
WPSBackpackSystem,
|
||||
WPSStoreSystem,
|
||||
WPSFortuneSystem,
|
||||
# 注:不强制依赖 WPSGardenSystem,果酒buff配置在 combat_models.py 中
|
||||
]
|
||||
|
||||
def register_db_model(self) -> List[DatabaseModel]:
|
||||
"""注册数据库表"""
|
||||
return get_combat_db_models()
|
||||
|
||||
def wake_up(self) -> None:
|
||||
"""插件初始化(只执行一次)"""
|
||||
if WPSCombatBase._initialized:
|
||||
return
|
||||
WPSCombatBase._initialized = True
|
||||
|
||||
logger.Log(
|
||||
"Info",
|
||||
f"{ConsoleFrontColor.GREEN}WPSCombat 系统开始初始化{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
|
||||
backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem)
|
||||
store: WPSStoreSystem = Architecture.Get(WPSStoreSystem)
|
||||
|
||||
# 1. 注册所有装备
|
||||
for equipment in EQUIPMENT_REGISTRY.values():
|
||||
self._safe_register_item(backpack, equipment.item_id, equipment.name, equipment.tier)
|
||||
# 装备价格根据品质和属性计算
|
||||
price = self._calculate_equipment_price(equipment)
|
||||
self._safe_register_store(store, equipment.item_id, price, limit=3)
|
||||
|
||||
# 2. 注册材料
|
||||
for item_id, (name, tier) in ADVENTURE_MATERIALS.items():
|
||||
self._safe_register_item(backpack, item_id, name, tier)
|
||||
# 材料可以在商店出售(但不购买)
|
||||
|
||||
# 3. 注册纪念品
|
||||
for item_id, (name, tier, sell_price) in ADVENTURE_SOUVENIRS.items():
|
||||
self._safe_register_item(backpack, item_id, name, tier)
|
||||
# 纪念品只能出售
|
||||
|
||||
# 4. 注册药剂
|
||||
for item_id, (name, tier, desc) in COMBAT_POTIONS.items():
|
||||
self._safe_register_item(backpack, item_id, name, tier)
|
||||
# 药剂价格根据品质
|
||||
potion_prices = {
|
||||
BackpackItemTier.COMMON: 50,
|
||||
BackpackItemTier.RARE: 150,
|
||||
BackpackItemTier.EPIC: 500,
|
||||
}
|
||||
price = potion_prices.get(tier, 100)
|
||||
self._safe_register_store(store, item_id, price, limit=10)
|
||||
|
||||
# 5. 注册冒险种子
|
||||
for item_id, (name, tier) in ADVENTURE_SEEDS.items():
|
||||
self._safe_register_item(backpack, item_id, name, tier)
|
||||
# 种子只能通过冒险获得
|
||||
|
||||
# 6. 恢复过期任务和超时战斗
|
||||
try:
|
||||
service = self.service()
|
||||
service.recover_overdue_adventures()
|
||||
service.check_battle_timeout()
|
||||
except Exception as e:
|
||||
logger.Log(
|
||||
"Warning",
|
||||
f"{ConsoleFrontColor.YELLOW}恢复任务时出错: {e}{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
|
||||
logger.Log(
|
||||
"Info",
|
||||
f"{ConsoleFrontColor.GREEN}WPSCombat 系统初始化完成:{len(EQUIPMENT_REGISTRY)}件装备、"
|
||||
f"{len(COMBAT_POTIONS)}种药剂已注册{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
|
||||
# ========================================================================
|
||||
# 辅助方法
|
||||
# ========================================================================
|
||||
|
||||
def _safe_register_item(
|
||||
self,
|
||||
backpack: WPSBackpackSystem,
|
||||
item_id: str,
|
||||
name: str,
|
||||
tier: BackpackItemTier,
|
||||
) -> None:
|
||||
"""安全注册物品到背包系统"""
|
||||
try:
|
||||
backpack.register_item(item_id, name, tier)
|
||||
except Exception as e:
|
||||
logger.Log(
|
||||
"Warning",
|
||||
f"{ConsoleFrontColor.YELLOW}注册物品 {item_id} 时出错: {e}{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
|
||||
def _safe_register_store(
|
||||
self,
|
||||
store: WPSStoreSystem,
|
||||
item_id: str,
|
||||
price: int,
|
||||
*,
|
||||
limit: int = 5,
|
||||
) -> None:
|
||||
"""安全注册物品到商店系统"""
|
||||
try:
|
||||
store.register_mode(
|
||||
item_id=item_id,
|
||||
price=price,
|
||||
limit_amount=limit,
|
||||
)
|
||||
except Exception as e:
|
||||
logger.Log(
|
||||
"Warning",
|
||||
f"{ConsoleFrontColor.YELLOW}注册商店物品 {item_id} 时出错: {e}{ConsoleFrontColor.RESET}"
|
||||
)
|
||||
|
||||
def _calculate_equipment_price(self, equipment) -> int:
|
||||
"""根据装备品质和属性计算价格"""
|
||||
# 基础价格
|
||||
base_prices = {
|
||||
BackpackItemTier.COMMON: 100,
|
||||
BackpackItemTier.RARE: 500,
|
||||
BackpackItemTier.EPIC: 2000,
|
||||
BackpackItemTier.LEGENDARY: 10000,
|
||||
}
|
||||
base_price = base_prices.get(equipment.tier, 100)
|
||||
|
||||
# 属性加成
|
||||
attr_sum = sum(equipment.attributes.values())
|
||||
price = base_price + attr_sum * 5
|
||||
|
||||
# 技能加成
|
||||
skill_bonus = len(equipment.skill_ids) * 200
|
||||
|
||||
return price + skill_bonus
|
||||
|
||||
|
||||
__all__ = ["WPSCombatBase"]
|
||||
Reference in New Issue
Block a user