refactor buy and sell
This commit is contained in:
@@ -25,7 +25,7 @@ from .breakthrough import Breakthrough
|
||||
from .play import Play
|
||||
from .hunt import Hunt
|
||||
from .harvest import Harvest
|
||||
from .sell import SellItems
|
||||
from .sell import Sell
|
||||
from .attack import Attack
|
||||
from .plunder_mortals import PlunderMortals
|
||||
from .help_mortals import HelpMortals
|
||||
@@ -37,7 +37,7 @@ from .switch_weapon import SwitchWeapon
|
||||
from .assassinate import Assassinate
|
||||
from .move_to_direction import MoveToDirection
|
||||
from .cast import Cast
|
||||
from .buy import BuyItem
|
||||
from .buy import Buy
|
||||
|
||||
# 注册到 ActionRegistry(标注是否为实际可执行动作)
|
||||
register_action(actual=False)(Action)
|
||||
@@ -59,7 +59,7 @@ register_action(actual=True)(Breakthrough)
|
||||
register_action(actual=True)(Play)
|
||||
register_action(actual=True)(Hunt)
|
||||
register_action(actual=True)(Harvest)
|
||||
register_action(actual=True)(SellItems)
|
||||
register_action(actual=True)(Sell)
|
||||
register_action(actual=False)(Attack)
|
||||
register_action(actual=True)(PlunderMortals)
|
||||
register_action(actual=True)(HelpMortals)
|
||||
@@ -71,7 +71,7 @@ register_action(actual=True)(SwitchWeapon)
|
||||
register_action(actual=True)(Assassinate)
|
||||
register_action(actual=True)(MoveToDirection)
|
||||
register_action(actual=True)(Cast)
|
||||
register_action(actual=True)(BuyItem)
|
||||
register_action(actual=True)(Buy)
|
||||
# Talk 已移动到 mutual_action 模块,在那里注册
|
||||
|
||||
__all__ = [
|
||||
@@ -96,7 +96,7 @@ __all__ = [
|
||||
"Play",
|
||||
"Hunt",
|
||||
"Harvest",
|
||||
"SellItems",
|
||||
"Sell",
|
||||
"Attack",
|
||||
"PlunderMortals",
|
||||
"HelpMortals",
|
||||
@@ -108,7 +108,7 @@ __all__ = [
|
||||
"Assassinate",
|
||||
"MoveToDirection",
|
||||
"Cast",
|
||||
"BuyItem",
|
||||
"Buy",
|
||||
# Talk 已移动到 mutual_action 模块
|
||||
# Occupy 已移动到 mutual_action 模块
|
||||
]
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
from __future__ import annotations
|
||||
|
||||
import copy
|
||||
from typing import TYPE_CHECKING, Tuple, Any
|
||||
|
||||
from src.classes.action import InstantAction
|
||||
@@ -7,62 +8,75 @@ from src.classes.event import Event
|
||||
from src.classes.region import CityRegion
|
||||
from src.classes.elixir import elixirs_by_name, Elixir
|
||||
from src.classes.item import items_by_name, Item
|
||||
from src.classes.weapon import weapons_by_name, Weapon
|
||||
from src.classes.auxiliary import auxiliaries_by_name, Auxiliary
|
||||
from src.classes.prices import prices
|
||||
from src.classes.normalize import normalize_item_name
|
||||
from src.classes.normalize import normalize_goods_name
|
||||
|
||||
if TYPE_CHECKING:
|
||||
from src.classes.avatar import Avatar
|
||||
|
||||
|
||||
class BuyItem(InstantAction):
|
||||
class Buy(InstantAction):
|
||||
"""
|
||||
在城镇购买物品。
|
||||
|
||||
如果是丹药:购买后强制立即服用。
|
||||
如果是其他物品:购买后放入背包。
|
||||
如果是装备(兵器/法宝):购买后直接装备(替换原有装备)。
|
||||
"""
|
||||
|
||||
ACTION_NAME = "购买物品"
|
||||
ACTION_NAME = "购买"
|
||||
EMOJI = "💸"
|
||||
DESC = "在城镇购买物品(丹药购买后将立即服用)"
|
||||
elixir_names_str = ", ".join(elixirs_by_name.keys())
|
||||
DESC = f"在城镇购买物品/装备(丹药购买后将立即服用)。可选丹药:{elixir_names_str}"
|
||||
DOABLES_REQUIREMENTS = "在城镇且金钱足够"
|
||||
PARAMS = {"item_name": "str"}
|
||||
PARAMS = {"target_name": "str"}
|
||||
|
||||
def _resolve_obj(self, item_name: str) -> Tuple[Any, str, str]:
|
||||
def _resolve_obj(self, target_name: str) -> Tuple[Any, str, str]:
|
||||
"""
|
||||
解析物品名称,返回 (对象, 类型, 显示名称)。
|
||||
类型字符串: "elixir", "item", "unknown"
|
||||
类型字符串: "elixir", "item", "weapon", "auxiliary", "unknown"
|
||||
"""
|
||||
normalized_name = normalize_item_name(item_name)
|
||||
normalized_name = normalize_goods_name(target_name)
|
||||
|
||||
# 1. 尝试作为丹药查找
|
||||
if normalized_name in elixirs_by_name:
|
||||
# 这里的 elixirs_by_name 返回的是 list,我们取第一个作为购买对象
|
||||
# TODO: 如果未来有同名不同级的丹药,这里可能需要更精确的逻辑
|
||||
elixir = elixirs_by_name[normalized_name][0]
|
||||
return elixir, "elixir", elixir.name
|
||||
|
||||
# 2. 尝试作为普通物品查找
|
||||
# 2. 尝试作为兵器查找
|
||||
weapon = weapons_by_name.get(normalized_name)
|
||||
if weapon:
|
||||
return weapon, "weapon", weapon.name
|
||||
|
||||
# 3. 尝试作为辅助装备查找
|
||||
auxiliary = auxiliaries_by_name.get(normalized_name)
|
||||
if auxiliary:
|
||||
return auxiliary, "auxiliary", auxiliary.name
|
||||
|
||||
# 4. 尝试作为普通物品查找
|
||||
item = items_by_name.get(normalized_name)
|
||||
if item:
|
||||
return item, "item", item.name
|
||||
|
||||
return None, "unknown", normalized_name
|
||||
|
||||
def can_start(self, item_name: str | None = None) -> tuple[bool, str]:
|
||||
def can_start(self, target_name: str | None = None) -> tuple[bool, str]:
|
||||
region = self.avatar.tile.region
|
||||
if not isinstance(region, CityRegion):
|
||||
return False, "仅能在城市区域执行"
|
||||
|
||||
if item_name is None:
|
||||
if target_name is None:
|
||||
# 用于动作空间检查
|
||||
# 理论上只要有钱就可以买东西,这里简单判定金钱>0
|
||||
ok = self.avatar.magic_stone > 0
|
||||
return (ok, "" if ok else "身无分文")
|
||||
|
||||
obj, obj_type, display_name = self._resolve_obj(item_name)
|
||||
obj, obj_type, display_name = self._resolve_obj(target_name)
|
||||
if obj_type == "unknown":
|
||||
return False, f"未知物品: {item_name}"
|
||||
return False, f"未知物品: {target_name}"
|
||||
|
||||
# 检查价格
|
||||
price = prices.get_buying_price(obj, self.avatar)
|
||||
@@ -85,8 +99,8 @@ class BuyItem(InstantAction):
|
||||
|
||||
return True, ""
|
||||
|
||||
def _execute(self, item_name: str) -> None:
|
||||
obj, obj_type, display_name = self._resolve_obj(item_name)
|
||||
def _execute(self, target_name: str) -> None:
|
||||
obj, obj_type, display_name = self._resolve_obj(target_name)
|
||||
if obj_type == "unknown":
|
||||
return
|
||||
|
||||
@@ -98,11 +112,25 @@ class BuyItem(InstantAction):
|
||||
self.avatar.consume_elixir(obj)
|
||||
elif obj_type == "item":
|
||||
self.avatar.add_item(obj)
|
||||
elif obj_type == "weapon":
|
||||
# 购买装备需要深拷贝,因为装备有独立状态
|
||||
new_weapon = copy.deepcopy(obj)
|
||||
self.avatar.change_weapon(new_weapon)
|
||||
elif obj_type == "auxiliary":
|
||||
# 购买装备需要深拷贝
|
||||
new_auxiliary = copy.deepcopy(obj)
|
||||
self.avatar.change_auxiliary(new_auxiliary)
|
||||
|
||||
def start(self, item_name: str) -> Event:
|
||||
obj, obj_type, display_name = self._resolve_obj(item_name)
|
||||
def start(self, target_name: str) -> Event:
|
||||
obj, obj_type, display_name = self._resolve_obj(target_name)
|
||||
|
||||
action_desc = "购买并服用了" if obj_type == "elixir" else "购买了"
|
||||
if obj_type == "elixir":
|
||||
action_desc = "购买并服用了"
|
||||
elif obj_type in ["weapon", "auxiliary"]:
|
||||
action_desc = "购买并装备了"
|
||||
else:
|
||||
action_desc = "购买了"
|
||||
|
||||
price = prices.get_buying_price(obj, self.avatar) if obj else 0
|
||||
|
||||
return Event(
|
||||
@@ -111,6 +139,5 @@ class BuyItem(InstantAction):
|
||||
related_avatars=[self.avatar.id]
|
||||
)
|
||||
|
||||
async def finish(self, item_name: str) -> list[Event]:
|
||||
async def finish(self, target_name: str) -> list[Event]:
|
||||
return []
|
||||
|
||||
|
||||
@@ -1,70 +1,94 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from typing import Tuple, Any
|
||||
|
||||
from src.classes.action import InstantAction
|
||||
from src.classes.event import Event
|
||||
from src.classes.region import CityRegion
|
||||
from src.classes.item import items_by_name
|
||||
from src.classes.normalize import normalize_item_name
|
||||
from src.classes.normalize import normalize_goods_name
|
||||
|
||||
|
||||
class SellItems(InstantAction):
|
||||
class Sell(InstantAction):
|
||||
"""
|
||||
在城镇出售指定名称的物品,一次性卖出持有的全部数量。
|
||||
收益通过 avatar.sell_item() 结算。
|
||||
在城镇出售指定名称的物品/装备。
|
||||
如果是材料:一次性卖出持有的全部数量。
|
||||
如果是装备:卖出当前装备的(如果是当前装备)。
|
||||
收益通过 avatar.sell_item() / sell_weapon() / sell_auxiliary() 结算。
|
||||
"""
|
||||
|
||||
ACTION_NAME = "出售物品"
|
||||
ACTION_NAME = "出售"
|
||||
EMOJI = "💰"
|
||||
DESC = "在城镇出售持有的某类物品的全部"
|
||||
DOABLES_REQUIREMENTS = "在城镇且背包非空"
|
||||
PARAMS = {"item_name": "str"}
|
||||
DESC = "在城镇出售持有的某类物品的全部,或当前装备"
|
||||
DOABLES_REQUIREMENTS = "在城镇且持有可出售物品/装备"
|
||||
PARAMS = {"target_name": "str"}
|
||||
|
||||
def _execute(self, item_name: str) -> None:
|
||||
def _resolve_obj(self, target_name: str) -> Tuple[Any, str, str]:
|
||||
"""
|
||||
解析出售对象
|
||||
返回: (对象, 类型, 显示名称)
|
||||
类型: "item", "weapon", "auxiliary", "none"
|
||||
"""
|
||||
normalized_name = normalize_goods_name(target_name)
|
||||
|
||||
# 1. 检查背包材料
|
||||
item = items_by_name.get(normalized_name)
|
||||
if item and self.avatar.get_item_quantity(item) > 0:
|
||||
return item, "item", item.name
|
||||
|
||||
# 2. 检查当前兵器
|
||||
if self.avatar.weapon and normalize_goods_name(self.avatar.weapon.name) == normalized_name:
|
||||
return self.avatar.weapon, "weapon", self.avatar.weapon.name
|
||||
|
||||
# 3. 检查当前辅助装备
|
||||
if self.avatar.auxiliary and normalize_goods_name(self.avatar.auxiliary.name) == normalized_name:
|
||||
return self.avatar.auxiliary, "auxiliary", self.avatar.auxiliary.name
|
||||
|
||||
return None, "none", normalized_name
|
||||
|
||||
def _execute(self, target_name: str) -> None:
|
||||
region = self.avatar.tile.region
|
||||
if not isinstance(region, CityRegion):
|
||||
return
|
||||
|
||||
# 规范化物品名称(去除境界等附加信息)
|
||||
normalized_name = normalize_item_name(item_name)
|
||||
obj, obj_type, _ = self._resolve_obj(target_name)
|
||||
|
||||
# 找到物品
|
||||
item = items_by_name.get(normalized_name)
|
||||
if item is None:
|
||||
return
|
||||
if obj_type == "item":
|
||||
quantity = self.avatar.get_item_quantity(obj)
|
||||
self.avatar.sell_item(obj, quantity)
|
||||
elif obj_type == "weapon":
|
||||
self.avatar.sell_weapon(obj)
|
||||
self.avatar.change_weapon(None) # 卖出后卸下
|
||||
elif obj_type == "auxiliary":
|
||||
self.avatar.sell_auxiliary(obj)
|
||||
self.avatar.change_auxiliary(None) # 卖出后卸下
|
||||
|
||||
# 检查持有数量
|
||||
quantity = self.avatar.get_item_quantity(item)
|
||||
if quantity <= 0:
|
||||
return
|
||||
|
||||
# 通过统一接口出售
|
||||
self.avatar.sell_item(item, quantity)
|
||||
|
||||
def can_start(self, item_name: str | None = None) -> tuple[bool, str]:
|
||||
def can_start(self, target_name: str | None = None) -> tuple[bool, str]:
|
||||
region = self.avatar.tile.region
|
||||
if not isinstance(region, CityRegion):
|
||||
return False, "仅能在城市区域执行"
|
||||
if item_name is None:
|
||||
# 用于动作空间:只要背包非空即可
|
||||
ok = bool(self.avatar.items)
|
||||
return (ok, "" if ok else "背包为空,无可出售物品")
|
||||
|
||||
if target_name is None:
|
||||
# 用于动作空间:只要有任何可卖东西即可
|
||||
has_items = bool(self.avatar.items)
|
||||
has_weapon = self.avatar.weapon is not None
|
||||
has_auxiliary = self.avatar.auxiliary is not None
|
||||
ok = has_items or has_weapon or has_auxiliary
|
||||
return (ok, "" if ok else "背包为空且无装备,无可出售物品")
|
||||
|
||||
# 规范化物品名称
|
||||
normalized_name = normalize_item_name(item_name)
|
||||
item = items_by_name.get(normalized_name)
|
||||
if item is None:
|
||||
return False, f"未知物品: {item_name}"
|
||||
ok = self.avatar.get_item_quantity(item) > 0
|
||||
return (ok, "" if ok else "该物品数量为0")
|
||||
obj, obj_type, _ = self._resolve_obj(target_name)
|
||||
if obj_type == "none":
|
||||
return False, f"未持有物品/装备: {target_name}"
|
||||
|
||||
return True, ""
|
||||
|
||||
def start(self, item_name: str) -> Event:
|
||||
# 规范化物品名称用于显示(与执行逻辑一致)
|
||||
normalized_name = normalize_item_name(item_name)
|
||||
# 尝试获取标准物品名(如果存在)
|
||||
item = items_by_name.get(normalized_name)
|
||||
display_name = item.name if item is not None else normalized_name
|
||||
return Event(self.world.month_stamp, f"{self.avatar.name} 在城镇出售 {display_name}", related_avatars=[self.avatar.id])
|
||||
def start(self, target_name: str) -> Event:
|
||||
obj, obj_type, display_name = self._resolve_obj(target_name)
|
||||
return Event(
|
||||
self.world.month_stamp,
|
||||
f"{self.avatar.name} 在城镇出售了 {display_name}",
|
||||
related_avatars=[self.avatar.id]
|
||||
)
|
||||
|
||||
async def finish(self, item_name: str) -> list[Event]:
|
||||
async def finish(self, target_name: str) -> list[Event]:
|
||||
return []
|
||||
|
||||
|
||||
@@ -105,28 +105,30 @@ def normalize_region_name(name: str) -> str:
|
||||
return s
|
||||
|
||||
|
||||
def normalize_item_name(name: str) -> str:
|
||||
def normalize_goods_name(name: str) -> str:
|
||||
"""
|
||||
规范化物品名称:去除境界标识等附加信息。
|
||||
规范化商品名称(包括物品、兵器、法宝、丹药)。
|
||||
|
||||
处理格式:
|
||||
- "青云鹿角 -(练气)" -> "青云鹿角"
|
||||
- "风速马皮(筑基)" -> "风速马皮"
|
||||
统一逻辑:
|
||||
1. 移除括号及内容(如境界、类型说明)
|
||||
2. 移除尾部的 " -" 标记(常见于材料生成名)
|
||||
3. 移除首尾空格
|
||||
|
||||
Args:
|
||||
name: 原始物品名称,可能包含境界等附加信息
|
||||
name: 原始商品名称
|
||||
|
||||
Returns:
|
||||
规范化后的物品名称
|
||||
规范化后的商品名称
|
||||
|
||||
Examples:
|
||||
>>> normalize_item_name("青云鹿角 -(练气)")
|
||||
>>> normalize_goods_name("青云鹿角 -(练气)") # item
|
||||
'青云鹿角'
|
||||
>>> normalize_item_name("风速马皮(筑基)")
|
||||
'风速马皮'
|
||||
>>> normalize_goods_name("精铁剑(练气)") # weapon
|
||||
'精铁剑'
|
||||
>>> normalize_goods_name("聚气丹(练气)") # elixir
|
||||
'聚气丹'
|
||||
"""
|
||||
s = _remove_parentheses(name)
|
||||
# 额外处理:去除尾部的 " -" 标记
|
||||
s = s.rstrip(" -").strip()
|
||||
return s
|
||||
|
||||
|
||||
Reference in New Issue
Block a user