修复新增炼金系统时出现的错误

This commit is contained in:
2025-11-11 19:28:34 +08:00
parent fae54c966d
commit e949938263

View File

@@ -11,6 +11,7 @@ from PWF.Convention.Runtime.Architecture import Architecture
from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig from PWF.Convention.Runtime.GlobalConfig import ConsoleFrontColor, ProjectConfig
from PWF.CoreModules.database import get_db, STATUS_COMPLETED from PWF.CoreModules.database import get_db, STATUS_COMPLETED
from PWF.CoreModules.plugin_interface import DatabaseModel from PWF.CoreModules.plugin_interface import DatabaseModel
from PWF.CoreModules.flags import get_internal_debug
from .WPSAPI import WPSAPI from .WPSAPI import WPSAPI
from .WPSBackpackSystem import ( from .WPSBackpackSystem import (
@@ -25,6 +26,7 @@ from .WPSFortuneSystem import WPSFortuneSystem
logger: ProjectConfig = Architecture.Get(ProjectConfig) logger: ProjectConfig = Architecture.Get(ProjectConfig)
FORTUNE_COEFF:float = logger.FindItem("alchemy_fortune_coeff", 0.03) FORTUNE_COEFF:float = logger.FindItem("alchemy_fortune_coeff", 0.03)
COOLDOWN_MINUTES:int = logger.FindItem("alchemy_cooldown_minutes", 2)
logger.SaveProperties() logger.SaveProperties()
@@ -64,12 +66,8 @@ class WPSAlchemyGame(WPSAPI):
self._fail_index: Dict[str, Set[Tuple[str, str, str]]] = defaultdict(set) self._fail_index: Dict[str, Set[Tuple[str, str, str]]] = defaultdict(set)
self._fortune_coeff = FORTUNE_COEFF self._fortune_coeff = FORTUNE_COEFF
# 从配置读取冷却时间(分钟) # 从配置读取冷却时间(分钟)
from PWF.CoreModules.flags import get_internal_debug self._cooldown_minutes = 0 if get_internal_debug() else COOLDOWN_MINUTES
cooldown_minutes = logger.FindItem("alchemy_cooldown_minutes", 2) self._cooldown_ms = int(self._cooldown_minutes * 60 * 1000)
if get_internal_debug():
cooldown_minutes = 0
self._cooldown_minutes = cooldown_minutes
self._cooldown_ms = int(cooldown_minutes * 60 * 1000)
logger.SaveProperties() logger.SaveProperties()
@override @override
@@ -322,9 +320,7 @@ class WPSAlchemyGame(WPSAPI):
) )
# 注册定时任务 # 注册定时任务
task_id = None task_id = self.register_clock(
if self._cooldown_ms > 0:
task_id = self.register_clock(
self._settle_alchemy_callback, self._settle_alchemy_callback,
self._cooldown_ms, self._cooldown_ms,
kwargs={ kwargs={
@@ -332,18 +328,14 @@ class WPSAlchemyGame(WPSAPI):
"user_id": user_id, "user_id": user_id,
"chat_id": chat_id, "chat_id": chat_id,
}, },
) )
# 更新记录的任务ID # 更新记录的任务ID
cursor = get_db().conn.cursor() cursor = get_db().conn.cursor()
cursor.execute( cursor.execute(
"UPDATE alchemy_records SET scheduled_task_id = ? WHERE alchemy_id = ?", "UPDATE alchemy_records SET scheduled_task_id = ? WHERE alchemy_id = ?",
(task_id, alchemy_id), (task_id, alchemy_id),
) )
get_db().conn.commit() get_db().conn.commit()
else:
# Debug模式立即结算
success, msg, rewards = self.settle_alchemy(alchemy_id)
return msg
# 计算预计完成时间 # 计算预计完成时间
cursor = get_db().conn.cursor() cursor = get_db().conn.cursor()
@@ -354,7 +346,6 @@ class WPSAlchemyGame(WPSAPI):
record = cursor.fetchone() record = cursor.fetchone()
expected_end_time = datetime.fromisoformat(record["expected_end_time"]) expected_end_time = datetime.fromisoformat(record["expected_end_time"])
from PWF.CoreModules.flags import get_internal_debug
debug_hint = " **[DEBUG模式]**" if get_internal_debug() else "" debug_hint = " **[DEBUG模式]**" if get_internal_debug() else ""
time_str = "立即结算" if self._cooldown_minutes == 0 else f"{self._cooldown_minutes} 分钟" time_str = "立即结算" if self._cooldown_minutes == 0 else f"{self._cooldown_minutes} 分钟"
@@ -430,28 +421,22 @@ class WPSAlchemyGame(WPSAPI):
) )
# 注册定时任务 # 注册定时任务
task_id = None task_id = self.register_clock(
if self._cooldown_ms > 0: self._settle_alchemy_callback,
task_id = self.register_clock( self._cooldown_ms,
self._settle_alchemy_callback, kwargs={
self._cooldown_ms, "alchemy_id": alchemy_id,
kwargs={ "user_id": user_id,
"alchemy_id": alchemy_id, "chat_id": chat_id,
"user_id": user_id, },
"chat_id": chat_id, )
}, # 更新记录的任务ID
) cursor = get_db().conn.cursor()
# 更新记录的任务ID cursor.execute(
cursor = get_db().conn.cursor()
cursor.execute(
"UPDATE alchemy_records SET scheduled_task_id = ? WHERE alchemy_id = ?", "UPDATE alchemy_records SET scheduled_task_id = ? WHERE alchemy_id = ?",
(task_id, alchemy_id), (task_id, alchemy_id),
) )
get_db().conn.commit() get_db().conn.commit()
else:
# Debug模式立即结算
success, msg, rewards = self.settle_alchemy(alchemy_id)
return msg
# 计算预计完成时间 # 计算预计完成时间
cursor = get_db().conn.cursor() cursor = get_db().conn.cursor()
@@ -462,7 +447,6 @@ class WPSAlchemyGame(WPSAPI):
record = cursor.fetchone() record = cursor.fetchone()
expected_end_time = datetime.fromisoformat(record["expected_end_time"]) expected_end_time = datetime.fromisoformat(record["expected_end_time"])
from PWF.CoreModules.flags import get_internal_debug
debug_hint = " **[DEBUG模式]**" if get_internal_debug() else "" debug_hint = " **[DEBUG模式]**" if get_internal_debug() else ""
time_str = "立即结算" if self._cooldown_minutes == 0 else f"{self._cooldown_minutes} 分钟" time_str = "立即结算" if self._cooldown_minutes == 0 else f"{self._cooldown_minutes} 分钟"
@@ -580,12 +564,14 @@ class WPSAlchemyGame(WPSAPI):
def settle_alchemy(self, alchemy_id: int) -> Tuple[bool, str, Optional[Dict]]: def settle_alchemy(self, alchemy_id: int) -> Tuple[bool, str, Optional[Dict]]:
"""结算炼金""" """结算炼金"""
import sqlite3
cursor = get_db().conn.cursor() cursor = get_db().conn.cursor()
cursor.execute( cursor.execute(
"SELECT * FROM alchemy_records WHERE alchemy_id = ?", "SELECT * FROM alchemy_records WHERE alchemy_id = ?",
(alchemy_id,), (alchemy_id,),
) )
record = cursor.fetchone() record: sqlite3.Row = cursor.fetchone()
if not record: if not record:
return False, "❌ 炼金记录不存在", None return False, "❌ 炼金记录不存在", None
@@ -738,14 +724,9 @@ class WPSAlchemyGame(WPSAPI):
) )
# 更新定时任务状态 # 更新定时任务状态
scheduled_task_id = record.get("scheduled_task_id") scheduled_task_id: int = record["scheduled_task_id"]
if scheduled_task_id: if scheduled_task_id:
try: get_db().update_task_status(scheduled_task_id, STATUS_COMPLETED)
get_db().update_task_status(int(scheduled_task_id), STATUS_COMPLETED)
except Exception:
pass
get_db().conn.commit()
return True, "".join(message_lines), result_data return True, "".join(message_lines), result_data