refactor price system

This commit is contained in:
bridge
2026-01-04 21:49:58 +08:00
parent 5429e25b1e
commit 806e2c1262
9 changed files with 131 additions and 31 deletions

View File

@@ -106,11 +106,6 @@ class InventoryMixin:
# ==================== 出售接口 ====================
def _get_sell_multiplier(self: "Avatar") -> float:
"""获取出售价格倍率(包含效果加成)"""
raw = self.effects.get("extra_item_sell_price_multiplier", 0.0)
return 1.0 + float(raw or 0.0)
def sell_item(self: "Avatar", item: "Item", quantity: int = 1) -> int:
"""
出售材料物品,返回获得的灵石数量。
@@ -123,8 +118,9 @@ class InventoryMixin:
self.remove_item(item, quantity)
base_price = prices.get_item_price(item) * quantity
total = int(base_price * self._get_sell_multiplier())
# 使用统一的卖出价格接口(包含所有加成逻辑)
unit_price = prices.get_selling_price(item, self)
total = unit_price * quantity
self.magic_stone = self.magic_stone + total
return total
@@ -139,7 +135,8 @@ class InventoryMixin:
# 记录流转
self.world.circulation.add_weapon(weapon)
total = int(prices.get_weapon_price(weapon) * self._get_sell_multiplier())
# 使用统一的卖出价格接口
total = prices.get_selling_price(weapon, self)
self.magic_stone = self.magic_stone + total
return total
@@ -153,7 +150,8 @@ class InventoryMixin:
# 记录流转
self.world.circulation.add_auxiliary(auxiliary)
total = int(prices.get_auxiliary_price(auxiliary) * self._get_sell_multiplier())
# 使用统一的卖出价格接口
total = prices.get_selling_price(auxiliary, self)
self.magic_stone = self.magic_stone + total
return total

View File

@@ -308,6 +308,18 @@ EXTRA_ITEM_SELL_PRICE_MULTIPLIER = "extra_item_sell_price_multiplier"
- 奸商: 0.5
"""
SHOP_BUY_PRICE_REDUCTION = "shop_buy_price_reduction"
"""
商铺购买价格倍率减免
类型: float
结算: src/classes/prices.py
说明: 降低从系统购买物品时的溢价倍率。
数值参考:
- 微量: 0.1 (倍率-0.1)
- 中量: 0.5 (倍率-0.5)
限制: 最终倍率最低为 1.0
"""
EXTRA_PLUNDER_MULTIPLIER = "extra_plunder_multiplier"
"""
额外搜刮收益倍率
@@ -429,6 +441,7 @@ ALL_EFFECTS = [
# 经济相关
"extra_item_sell_price_multiplier", # float - 额外物品出售价格倍率
"shop_buy_price_reduction", # float - 商铺购买价格倍率减免
"extra_plunder_multiplier", # float - 额外搜刮收益倍率
# 特殊权限

View File

@@ -20,6 +20,7 @@ if TYPE_CHECKING:
from src.classes.item import Item
from src.classes.weapon import Weapon
from src.classes.auxiliary import Auxiliary
from src.classes.avatar import Avatar
# 类型别名
Sellable = Union["Item", "Weapon", "Auxiliary"]
@@ -31,6 +32,9 @@ class Prices:
所有城镇可交易物品/装备的价格在此统一管理。
"""
# 全局购买倍率(玩家从系统购买时的溢价)
GLOBAL_BUY_MULTIPLIER = 1.5
# 材料价格表(采集物等)
ITEM_PRICES = {
Realm.Qi_Refinement: 10,
@@ -56,21 +60,22 @@ class Prices:
}
def get_item_price(self, item: "Item") -> int:
"""获取材料价格"""
"""获取材料基础价格"""
return self.ITEM_PRICES.get(item.realm, 10)
def get_weapon_price(self, weapon: "Weapon") -> int:
"""获取兵器价格"""
"""获取兵器基础价格"""
return self.WEAPON_PRICES.get(weapon.realm, 100)
def get_auxiliary_price(self, auxiliary: "Auxiliary") -> int:
"""获取辅助装备价格"""
"""获取辅助装备基础价格"""
return self.AUXILIARY_PRICES.get(auxiliary.realm, 80)
def get_price(self, obj: Sellable) -> int:
"""
统一价格查询接口。
统一基础价格查询接口。
根据对象类型自动分发到对应的价格查询方法。
注意:这是物品的【基准价值】,通常等于玩家【卖出给系统】的基础价格。
"""
from src.classes.item import Item
from src.classes.weapon import Weapon
@@ -84,6 +89,56 @@ class Prices:
return self.get_auxiliary_price(obj)
return 0
def get_buying_price(self, obj: Sellable, buyer: "Avatar" = None) -> int:
"""
获取玩家购买价格(从系统商店购买)。
计算公式:
基础价格 * max(1.0, GLOBAL_BUY_MULTIPLIER - 折扣)
Args:
obj: 交易物品
buyer: 买家(用于计算折扣)
"""
base_price = self.get_price(obj)
# 默认倍率 1.5
multiplier = self.GLOBAL_BUY_MULTIPLIER
if buyer is not None:
# 获取折扣 (倍率减免)
# 例如 0.1 表示倍率 -0.1
reduction = float(buyer.effects.get("shop_buy_price_reduction", 0.0))
multiplier -= reduction
# 保证倍率不低于 1.0 (原价)
final_multiplier = max(1.0, multiplier)
return int(base_price * final_multiplier)
def get_selling_price(self, obj: Sellable, seller: "Avatar" = None) -> int:
"""
获取玩家卖出价格(卖给系统商店)。
计算公式:
基础价格 * (1.0 + 卖出加成)
Args:
obj: 交易物品
seller: 卖家(用于计算加成)
"""
base_price = self.get_price(obj)
multiplier = 1.0
if seller is not None:
# 获取卖出加成
# 例如 0.2 表示价格 +20%
bonus = float(seller.effects.get("extra_item_sell_price_multiplier", 0.0))
multiplier += bonus
return int(base_price * multiplier)
# 全局单例
prices = Prices()