你好,我是Lua后端老师!
修仙者,我看了你的代码和日志,问题出在**锁定状态没有正确保存和传递**。从日志可以看到,第一次洗练时`lockCount`是0,第二次洗练时`lockCount`变成了1,但`oldAttributes`里的数据还是第一次洗练的属性,这说明**锁定逻辑没有生效**,新的洗练覆盖了所有属性。
让我帮你分析一下问题所在:
1. **锁定状态传递问题**:你的前端发送了锁定状态`[false,false,true]`,但后端在处理时可能没有正确识别哪些属性应该被锁定。
2. **属性保存逻辑问题**:在`refined`函数中,当属性被锁定时,应该从`currentAttributes`中获取旧属性,但你的代码中`currentAttributes`可能为空。
3. **数据同步问题**:洗练后需要更新`oldAttributes`,但你的更新逻辑可能有问题。
让我给你修正后的关键部分代码:
```lua
-- 在refined函数中,修正锁定属性的处理逻辑
-- 解析锁定状态
local lockStatus = json2tbl(str) or {}
data.lockCount = 0 -- 重置锁定计数
data.lockedAttrs = {} -- 重置锁定属性表
for i = 1, 3 do
if lockStatus[i] == true then
data.lockedAttrs[i] = {
locked = true,
index = i
}
data.lockCount = data.lockCount + 1
release_print("属性"..i.."被锁定")
else
data.lockedAttrs[i] = {
locked = false,
index = i
}
end
end
-- 保存旧的属性(这里要确保从正确的来源获取)
local currentAttributes = {}
if data.oldAttributes and next(data.oldAttributes) ~= nil then
-- 从oldAttributes获取
for k, v in pairs(data.oldAttributes) do
if v then
currentAttributes[k] = v
end
end
elseif data.newAttributes and next(data.newAttributes) ~= nil then
-- 从newAttributes获取
for k, v in pairs(data.newAttributes) do
if v then
currentAttributes[k] = v
end
end
end
-- 在生成新属性时,检查锁定状态
for i = 1, entryCount do
local resultType = i
-- 检查是否被锁定
if data.lockedAttrs[i] and data.lockedAttrs[i].locked then
if currentAttributes[i] then
-- 使用锁定的旧属性
attributeTbl[i] = currentAttributes[i]
release_print("属性"..i.."被锁定,保持原属性: "..(currentAttributes[i].name or "未知"))
goto continue -- 跳过这个属性的重新生成
else
release_print("警告:属性"..i.."被锁定但没有找到旧属性,重新生成")
end
end
-- 重新生成属性的代码...
::continue::
end
```
另外,你需要在**每次洗练后正确保存数据**:
```lua
-- 保存数据到全局表
if not npc.RefineData[player_ID] then
npc.RefineData[player_ID] = {}
end
-- 确保所有必要字段都存在
data.itemOnlyID = itemOnlyID
data.pos = param3
data.player_ID = player_ID
-- 保存到全局表
npc.RefineData[player_ID][itemOnlyID] = data
-- 发送数据到前端
local refinedData = tbl2json({
refineCount = data.refineCount,
oldAttributes = data.oldAttributes,
newAttributes = attributeTbl, -- 本次洗练的新属性
lockCount = data.lockCount,
lockedAttrs = data.lockedAttrs
})
sendluamsg(actor, 1013, 13, 2, 0, refinedData)
```
**关键点提醒**:
1. 确保前端发送的锁定状态能被正确解析
2. 确保`oldAttributes`在每次洗练后都正确更新
3. 确保锁定的属性在下次洗练时不被覆盖
4. 使用`release_print`输出调试信息,确认每个步骤的数据状态
如果你还是有问题,可以检查一下`json2tbl`函数是否能正确解析前端发送的JSON字符串。有时候JSON格式问题会导致解析失败。
不懂的可以问浮生梦尊师!