add circulation manager
This commit is contained in:
@@ -136,6 +136,9 @@ class InventoryMixin:
|
||||
"""
|
||||
from src.classes.prices import prices
|
||||
|
||||
# 记录流转
|
||||
self.world.circulation.add_weapon(weapon)
|
||||
|
||||
total = int(prices.get_weapon_price(weapon) * self._get_sell_multiplier())
|
||||
self.magic_stone = self.magic_stone + total
|
||||
return total
|
||||
@@ -147,6 +150,9 @@ class InventoryMixin:
|
||||
"""
|
||||
from src.classes.prices import prices
|
||||
|
||||
# 记录流转
|
||||
self.world.circulation.add_auxiliary(auxiliary)
|
||||
|
||||
total = int(prices.get_auxiliary_price(auxiliary) * self._get_sell_multiplier())
|
||||
self.magic_stone = self.magic_stone + total
|
||||
return total
|
||||
|
||||
71
src/classes/circulation.py
Normal file
71
src/classes/circulation.py
Normal file
@@ -0,0 +1,71 @@
|
||||
from __future__ import annotations
|
||||
from typing import Dict, List, TYPE_CHECKING
|
||||
import copy
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.classes.weapon import Weapon
|
||||
from src.classes.auxiliary import Auxiliary
|
||||
|
||||
|
||||
class CirculationManager:
|
||||
"""
|
||||
出世物品流通管理器
|
||||
记录所有从角色身上流出的贵重物品(出售、死亡掉落且未被夺取等)
|
||||
用于后续拍卖会等玩法的物品池
|
||||
"""
|
||||
|
||||
def __init__(self):
|
||||
# 存储被卖出的法宝
|
||||
self.sold_weapons: List[Weapon] = []
|
||||
# 存储被卖出的宝物
|
||||
self.sold_auxiliaries: List[Auxiliary] = []
|
||||
|
||||
def add_weapon(self, weapon: "Weapon") -> None:
|
||||
"""记录一件流出的兵器"""
|
||||
if weapon is None:
|
||||
return
|
||||
# 使用深拷贝存储,防止外部修改影响记录
|
||||
# 注意:这里假设 weapon 对象是可以被 copy 的
|
||||
self.sold_weapons.append(copy.deepcopy(weapon))
|
||||
|
||||
def add_auxiliary(self, auxiliary: "Auxiliary") -> None:
|
||||
"""记录一件流出的辅助装备"""
|
||||
if auxiliary is None:
|
||||
return
|
||||
self.sold_auxiliaries.append(copy.deepcopy(auxiliary))
|
||||
|
||||
def to_save_dict(self) -> dict:
|
||||
"""序列化为字典以便存档"""
|
||||
return {
|
||||
"weapons": [self._item_to_dict(w) for w in self.sold_weapons],
|
||||
"auxiliaries": [self._item_to_dict(a) for a in self.sold_auxiliaries]
|
||||
}
|
||||
|
||||
def load_from_dict(self, data: dict) -> None:
|
||||
"""从字典恢复数据"""
|
||||
from src.classes.weapon import weapons_by_id
|
||||
from src.classes.auxiliary import auxiliaries_by_id
|
||||
|
||||
self.sold_weapons = []
|
||||
for w_data in data.get("weapons", []):
|
||||
w_id = w_data.get("id")
|
||||
if w_id in weapons_by_id:
|
||||
weapon = copy.copy(weapons_by_id[w_id])
|
||||
weapon.special_data = w_data.get("special_data", {})
|
||||
self.sold_weapons.append(weapon)
|
||||
|
||||
self.sold_auxiliaries = []
|
||||
for a_data in data.get("auxiliaries", []):
|
||||
a_id = a_data.get("id")
|
||||
if a_id in auxiliaries_by_id:
|
||||
auxiliary = copy.copy(auxiliaries_by_id[a_id])
|
||||
auxiliary.special_data = a_data.get("special_data", {})
|
||||
self.sold_auxiliaries.append(auxiliary)
|
||||
|
||||
def _item_to_dict(self, item) -> dict:
|
||||
"""将物品对象转换为简略的存储格式"""
|
||||
return {
|
||||
"id": item.id,
|
||||
"special_data": getattr(item, "special_data", {})
|
||||
}
|
||||
|
||||
@@ -5,6 +5,7 @@ from src.classes.map import Map
|
||||
from src.classes.calendar import Year, Month, MonthStamp
|
||||
from src.classes.avatar_manager import AvatarManager
|
||||
from src.classes.event_manager import EventManager
|
||||
from src.classes.circulation import CirculationManager
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.classes.avatar import Avatar
|
||||
@@ -22,6 +23,8 @@ class World():
|
||||
current_phenomenon: Optional["CelestialPhenomenon"] = None
|
||||
# 天地灵机开始年份(用于计算持续时间)
|
||||
phenomenon_start_year: int = 0
|
||||
# 出世物品流通管理器
|
||||
circulation: CirculationManager = field(default_factory=CirculationManager)
|
||||
|
||||
def get_info(self, detailed: bool = False, avatar: Optional["Avatar"] = None) -> dict:
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user