function C:setWeaponIdQualityCount(weapon, isdelete) local weaponIdQualityCount = self:Get("weaponIdQualityCount") or {} local weaponIdCount = weaponIdQualityCount[weapon.cfgid] or {} weaponIdCount[weapon.quality] = (weaponIdCount[weapon.quality] or 0) + (isdelete and -1 or 1) if weaponIdCount[weapon.quality] == 0 then weaponIdCount[weapon.quality] = nil end weaponIdQualityCount[weapon.cfgid] = next(weaponIdCount) and weaponIdCount or nil self:Set("weaponIdQualityCount", next(weaponIdQualityCount) and weaponIdQualityCount or nil) end 重构此方法,我需要weaponIdQualityCount的结构为weaponIdQualityCount = { guid = weapon.guid, quality = weapon.quality, cfgid = weapon.cfgid, }
时间: 2024-04-23 09:24:27 浏览: 44
好的,根据你的要求,我已经重构了这个方法,代码如下:
function C:setWeaponIdQualityCount(weapon, isdelete)
local weaponIdQualityCount = self:Get("weaponIdQualityCount") or {}
local index = nil
for i, w in ipairs(weaponIdQualityCount) do
if w.guid == weapon.guid then
index = i
break
end
end
if not index then
index = #weaponIdQualityCount + 1
weaponIdQualityCount[index] = {}
end
weaponIdQualityCount[index].guid = weapon.guid
weaponIdQualityCount[index].quality = weapon.quality
weaponIdQualityCount[index].cfgid = weapon.cfgid
self:Set("weaponIdQualityCount", next(weaponIdQualityCount) and weaponIdQualityCount or nil)
end
重构后的方法会将每个武器的guid、quality和cfgid保存在一个table中,该table再存储在weaponIdQualityCount中。如果同一guid的武器已经存在于weaponIdQualityCount中,则直接更新其quality和cfgid;否则,将新的武器信息添加到weaponIdQualityCount中。
阅读全文