修复冒险模式使用食物的相关错误
This commit is contained in:
@@ -3,11 +3,13 @@
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
|
|
||||||
from datetime import datetime
|
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.GlobalConfig import ConsoleFrontColor, ProjectConfig
|
||||||
|
|
||||||
|
from PWF.Convention.Runtime.Architecture import Architecture
|
||||||
from Plugins.WPSAPI import GuideEntry, GuideSection
|
from Plugins.WPSAPI import GuideEntry, GuideSection
|
||||||
|
from Plugins.WPSBackpackSystem import WPSBackpackSystem
|
||||||
from .combat_plugin_base import WPSCombatBase
|
from .combat_plugin_base import WPSCombatBase
|
||||||
from .combat_models import CombatConfig
|
from .combat_models import CombatConfig
|
||||||
|
|
||||||
@@ -195,6 +197,42 @@ class WPSCombatAdventure(WPSCombatBase):
|
|||||||
food_items = tokens
|
food_items = tokens
|
||||||
return await self._handle_continue_adventure(chat_id, user_id, food_items)
|
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(
|
async def _handle_start_adventure(
|
||||||
self,
|
self,
|
||||||
chat_id: int,
|
chat_id: int,
|
||||||
@@ -207,11 +245,13 @@ class WPSCombatAdventure(WPSCombatBase):
|
|||||||
# 第1阶段
|
# 第1阶段
|
||||||
stage = 1
|
stage = 1
|
||||||
|
|
||||||
|
normalized_food = self._normalize_food_items(user_id, food_items)
|
||||||
|
|
||||||
success, msg, adventure_id = service.start_adventure(
|
success, msg, adventure_id = service.start_adventure(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
stage=stage,
|
stage=stage,
|
||||||
food_items=food_items,
|
food_items=normalized_food,
|
||||||
register_callback=self
|
register_callback=self
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -355,11 +395,13 @@ class WPSCombatAdventure(WPSCombatBase):
|
|||||||
# 下一阶段
|
# 下一阶段
|
||||||
next_stage = last_record["stage"] + 1
|
next_stage = last_record["stage"] + 1
|
||||||
|
|
||||||
|
normalized_food = self._normalize_food_items(user_id, food_items)
|
||||||
|
|
||||||
success, msg, adventure_id = service.start_adventure(
|
success, msg, adventure_id = service.start_adventure(
|
||||||
user_id=user_id,
|
user_id=user_id,
|
||||||
chat_id=chat_id,
|
chat_id=chat_id,
|
||||||
stage=next_stage,
|
stage=next_stage,
|
||||||
food_items=food_items,
|
food_items=normalized_food,
|
||||||
register_callback=self
|
register_callback=self
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
@@ -533,9 +533,9 @@ class CombatService:
|
|||||||
recommended_food = math.ceil(stage_time / food_support_time)
|
recommended_food = math.ceil(stage_time / food_support_time)
|
||||||
|
|
||||||
# 如果没有提供食物,给出提示但允许继续
|
# 如果没有提供食物,给出提示但允许继续
|
||||||
if len(food_items) == 0:
|
#if len(food_items) == 0:
|
||||||
msg = f"ℹ️ 未使用食物。推荐使用 {recommended_food} 个果酒以获得buff加成"
|
# msg = f"ℹ️ 未使用食物。推荐使用 {recommended_food} 个果酒以获得buff加成"
|
||||||
return True, msg, recommended_food
|
# return True, msg, recommended_food
|
||||||
|
|
||||||
return True, "", recommended_food
|
return True, "", recommended_food
|
||||||
|
|
||||||
@@ -696,9 +696,9 @@ class CombatService:
|
|||||||
# 验证拥有
|
# 验证拥有
|
||||||
user_items = backpack.get_user_items(user_id)
|
user_items = backpack.get_user_items(user_id)
|
||||||
for food_id, count in food_count.items():
|
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:
|
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():
|
for food_id, count in food_count.items():
|
||||||
|
|||||||
Reference in New Issue
Block a user