修复冒险模式使用食物的相关错误

This commit is contained in:
2025-11-12 23:49:39 +08:00
parent 7332141a92
commit 90c2fa79cb
2 changed files with 50 additions and 8 deletions

View File

@@ -3,11 +3,13 @@
from __future__ import annotations
from datetime import datetime
from typing import Optional, Sequence
from typing import List, Optional, Sequence
from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig
from PWF.Convention.Runtime.Architecture import Architecture
from Plugins.WPSAPI import GuideEntry, GuideSection
from Plugins.WPSBackpackSystem import WPSBackpackSystem
from .combat_plugin_base import WPSCombatBase
from .combat_models import CombatConfig
@@ -195,6 +197,42 @@ class WPSCombatAdventure(WPSCombatBase):
food_items = tokens
return await self._handle_continue_adventure(chat_id, user_id, food_items)
def _normalize_food_items(self, user_id: int, raw_items: List[str]) -> List[str]:
"""将用户输入的食物标识转换为背包中的物品ID"""
if not raw_items:
return []
backpack: WPSBackpackSystem = Architecture.Get(WPSBackpackSystem)
try:
user_items = backpack.get_user_items(user_id)
except Exception:
return [item for item in raw_items if item]
id_lookup = {item.item_id.lower(): item.item_id for item in user_items}
name_lookup = {
item.definition.name.lower(): item.item_id for item in user_items
if item.definition and item.definition.name
}
normalized: List[str] = []
for raw in raw_items:
token = raw.strip()
if not token:
continue
lowered = token.lower()
if lowered in id_lookup:
normalized.append(id_lookup[lowered])
continue
if lowered in name_lookup:
normalized.append(name_lookup[lowered])
continue
normalized.append(token)
return normalized
async def _handle_start_adventure(
self,
chat_id: int,
@@ -207,11 +245,13 @@ class WPSCombatAdventure(WPSCombatBase):
# 第1阶段
stage = 1
normalized_food = self._normalize_food_items(user_id, food_items)
success, msg, adventure_id = service.start_adventure(
user_id=user_id,
chat_id=chat_id,
stage=stage,
food_items=food_items,
food_items=normalized_food,
register_callback=self
)
@@ -355,11 +395,13 @@ class WPSCombatAdventure(WPSCombatBase):
# 下一阶段
next_stage = last_record["stage"] + 1
normalized_food = self._normalize_food_items(user_id, food_items)
success, msg, adventure_id = service.start_adventure(
user_id=user_id,
chat_id=chat_id,
stage=next_stage,
food_items=food_items,
food_items=normalized_food,
register_callback=self
)

View File

@@ -533,9 +533,9 @@ class CombatService:
recommended_food = math.ceil(stage_time / food_support_time)
# 如果没有提供食物,给出提示但允许继续
if len(food_items) == 0:
msg = f" 未使用食物。推荐使用 {recommended_food} 个果酒以获得buff加成"
return True, msg, recommended_food
#if len(food_items) == 0:
# msg = f" 未使用食物。推荐使用 {recommended_food} 个果酒以获得buff加成"
# return True, msg, recommended_food
return True, "", recommended_food
@@ -696,9 +696,9 @@ class CombatService:
# 验证拥有
user_items = backpack.get_user_items(user_id)
for food_id, count in food_count.items():
owned = sum(1 for item in user_items if item.item_id == food_id)
owned = sum(item.quantity for item in user_items if item.item_id == food_id)
if owned < count:
return False, f"{food_id} 数量不足,需要 {count} ", None
return False, f"{food_id} 数量不足,需要 {count - owned} 个, 当前只有{owned}", None
# 消耗食物
for food_id, count in food_count.items():