PrivilegeCardScript = {} -- 构造方法 PrivilegeCardScript.__index = PrivilegeCardScript function PrivilegeCardScript:new() local instance = setmetatable({}, PrivilegeCardScript) -- 可以在这里添加初始化逻辑 return instance end local privilegeItems = { "autopick", "autorecovery", "autopotion", "withstore", "withwarehouse", "tradenumber", "trade" } -- local privilegeCount = 0 -- 用来维护每个人的提示过期信息,有没有被提示过 -- local hadTipPrivileges = {} local privilegeCardScript = PrivilegeCardScript:new() function PrivilegeCardScript.toUsePrivilegeCard(actor, msgData) privilegeCardScript:usePrivilegeCard(actor, msgData) end -- 时间字符串转换为毫秒数 function PrivilegeCardScript:timeStrToMilliseconds(timeStr) -- 如果字符串为空,则表示永久生效 if timeStr == "" then return -1 end local parts = {} for part in string.gmatch(timeStr, "%d+") do table.insert(parts, tonumber(part)) end local length = #parts local days, hours, minutes, seconds = 0, 0, 0, 0 if length >= 1 then days = parts[1] end if length >= 2 then hours = parts[2] end if length >= 3 then minutes = parts[3] end if length >= 4 then seconds = parts[4] end local milliseconds = (days * 24 + hours) * 60 * 60 * 1000 + minutes * 60 * 1000 + seconds * 1000 return milliseconds end ---@使用特权卡 function PrivilegeCardScript:usePrivilegeCard(actor, itemCfgId) -- local itemCfgId = itemCfgId or 30030122 -- lg("使用特权卡", itemCfgId) -- local hasCount = getbagitemcountbyid(actor, tonumber(itemCfgId)) -- lg("拥有特权卡数量", hasCount) -- if not (hasCount >= 1) then -- lg("背包里没有特权卡") -- return -- end local buff = self:getOnePrivilegeBuff(itemCfgId) -- lg("getOnePrivilegeBuff", buff) if buff ~= nil then self:handlePrivilegeData(actor, itemCfgId, buff) end end ---@获得特权卡数据 function PrivilegeCardScript:getOnePrivilegeBuff(itemCfgId) local allBuffs = ConfigDataManager.getList("cfg_free_buff") if allBuffs == nil or next(allBuffs) == nil then -- lg("没有特权卡数据") return nil end for index, buff in ipairs(allBuffs) do local condition = buff["conditions"] local result = strcontains(condition, "checkUseItemTime") if tonumber(result) == 1 then local itemId = string.match(condition, "%d+") itemId = itemId and tonumber(itemId) or 0 if itemId == 0 then goto next end if itemId == itemCfgId then return buff end end ::next:: end return nil end ---@更新特权卡数据 -- 这个方法中使用多个特权卡时间会累加,但是客户端的倒计时没有累加,后续有问题要修正 function PrivilegeCardScript:handlePrivilegeData(actor, itemCfgId, buff) -- lg("更新特权卡数据", itemCfgId) local item = ConfigDataManager.getTable("cfg_item", "id", itemCfgId) if item == nil or next(item) == nil then return end local buffTime = buff["time"] buffTime = self:timeStrToMilliseconds(buffTime) -- lg("特权卡过期时间", buffTime) local buffId = item[1]["useparam"] local realBuff = ConfigDataManager.getTable("cfg_buff", "id", buffId) if realBuff ~= nil and next(realBuff) ~= nil then local realBuffTime = realBuff[1]["bufftotaltime"] -- lg("特权卡真实过期时间", realBuffTime) if realBuffTime ~= "" then buffTime = tonumber(realBuffTime) end end local currTime = getbaseinfo(actor, "now") local buffExpireTime = buffTime -- 测试用 -- local buffExpireTime = 1000 * 10 local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {} local cardExpireTime = myPrivilegeCardInfos[tonumber(itemCfgId)] or 0 if cardExpireTime == 0 then -- 新增 if buffExpireTime == -1 then myPrivilegeCardInfos[tonumber(itemCfgId)] = -1 else myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime end elseif cardExpireTime > 0 then -- 更新 local overFlowTime = cardExpireTime - currTime if overFlowTime > 0 then -- 原来的没过期 myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime + overFlowTime else myPrivilegeCardInfos[tonumber(itemCfgId)] = currTime + buffExpireTime end end -- lg("更新特权卡数据", myPrivilegeCardInfos) setplaydef(actor, "T$PrivilegeCardInfos", myPrivilegeCardInfos) -- 这个buff如果有自动买药,就开启自动买药 -- local autoBuyPotionPrivilege = buff["autopotion"] -- if autoBuyPotionPrivilege ~= nil and autoBuyPotionPrivilege ~= "" then -- local autoBuyPotion = getplaydef(actor, "T$autoBuyPotion") or {} -- autoBuyPotion["on"] = 1 -- setplaydef(actor, "T$autoBuyPotion", autoBuyPotion) -- lg("开启自动买药",autoBuyPotion) -- end -- 复原提示过期信息 local currRoleTips = getplaydef(actor,"T$hadTipPrivileges") or {} for _, privilegeItem in pairs(privilegeItems) do local hadPrivilege = buff[privilegeItem] if hadPrivilege ~= nil and hadPrivilege ~= "" then -- 变为这个特权过期信息没提示过 currRoleTips[privilegeItem] = false end end setplaydef(actor,"T$hadTipPrivileges", currRoleTips) PrivilegeCardScript.getHasPrivileges(actor) end -- 检验表中用于特定功能的特权条件是否开启 function PrivilegeCardScript.checkIsOpenCondition(actor, cfgTable, thing, switchFlag) -- lg("检验特定功能是否开启") cfgTable = cfgTable ~= "" and cfgTable or "cfg_free_buff" local configs = ConfigDataManager.getTable(cfgTable, thing, switchFlag) if configs == nil or next(configs) == nil then -- lg(configs, "没有开启特权项") return false end local tobeCheckedCondition = configs[1]["conditions"] local name = configs[1]["name"] if tobeCheckedCondition ~= nil and tobeCheckedCondition ~= "" then -- 改走脚本验证 -- local resullt = checkcondition(actor, tobeCheckedCondition) -- lg("检验结果:", resullt) -- if resullt ~= 1 then -- lg(resullt, name, "没有开启特权") -- return false -- end local itemId = string.match(tobeCheckedCondition, "%d+") itemId = itemId and tonumber(itemId) or 0 if itemId == 0 then -- lg("没有可以检验的特权卡数据") return false end local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {} -- lg("检验特权卡数据", myPrivilegeCardInfos) local cardExpireTime = myPrivilegeCardInfos[tonumber(itemId)] or 0 -- lg("检验特权卡数据", cardExpireTime) if cardExpireTime == 0 then -- lg(name, "没有开启特权") return false elseif cardExpireTime == -1 then return true else local currTime = getbaseinfo(actor, "now") -- lg("检验特权卡数据", cardExpireTime, currTime) if cardExpireTime < currTime then -- lg(name, "特权已过期") return false end end end return true end -- 使用物品后判断是不是特权卡 function PrivilegeCardScript.setPrivilegeCardInfos(actor, itemCfgId, count) if itemCfgId == nil or tonumber(itemCfgId) <= 0 then return end privilegeCardScript:usePrivilegeCard(actor, itemCfgId) end function PrivilegeCardScript.getHasPrivileges(actor) local privileges = {} local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {} if next(myPrivilegeCardInfos) == nil then sendluamsg(actor, LuaMessageIdToClient.RES_PRIVILEGE_RESULT, privileges) return end for itemId, buffExpireTime in pairs(myPrivilegeCardInfos) do local now = getbaseinfo(actor, "now") --过期跳过 if now > buffExpireTime then goto next end local buff = privilegeCardScript:getOnePrivilegeBuff(itemId) if buff == nil then goto next end for i, v in ipairs(privilegeItems) do local state = buff[v] if state ~= "" and state ~= nil then privileges[v] = state end end ::next:: end -- lg("privileges", privileges) sendluamsg(actor, LuaMessageIdToClient.RES_PRIVILEGE_RESULT, privileges) end -- 检查卡片id判断是否有使用了该特权卡 function PrivilegeCardScript.checkPrivilegeCardById(actor, cardId) local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {} if next(myPrivilegeCardInfos) == nil then return false end local condition = ConfigDataManager.getTableValue("cfg_free_buff", "conditions", "id", cardId) if condition == "" then return false end local itemId = string.match(condition, "%d+") itemId = itemId and tonumber(itemId) or 0 if itemId == 0 then return false end return myPrivilegeCardInfos[itemId] ~= nil end -- 可以改为登录时候和使用特权卡时检查,不足一天的时候开启定时器每秒检测 function PrivilegeCardScript.checkPrivilegeExpire(actor) local myAllCradStates = {} local myPrivilegeCardInfos = getplaydef(actor, "T$PrivilegeCardInfos") or {} if next(myPrivilegeCardInfos) ~= nil then -- 找出所有卡的特权状态,包括有效的和过期的,因为不能凭借某个特权卡过期了就认为它有的特权过期了 -- 有可能存在没过期的卡也有这个权限 for itemId, buffExpireTime in pairs(myPrivilegeCardInfos) do local now = getbaseinfo(actor, "now") local buff = privilegeCardScript:getOnePrivilegeBuff(itemId) if buff == nil then goto nextBuff end local currCradStates = {} for i, privilegeItem in ipairs(privilegeItems) do local state = buff[privilegeItem] if state ~= "" and state ~= nil then --这个卡有这个特权 if buffExpireTime ~= -1 and now > buffExpireTime then currCradStates[privilegeItem] = 0 else currCradStates[privilegeItem] = 1 end end end myAllCradStates[itemId] = currCradStates ::nextBuff:: end end -- 所有使用过卡的特权卡的状态都有了,包括有效的和过期的 -- lg("myAllCradStates", myAllCradStates) -- 测试用 -- myAllCradStates = { -- [30030122] = { -- autopick = 0, -- withstore = 1, -- autopotion = 0 -- }, -- [30030121] = { -- withstore = 0, -- autorecovery = 0, -- withwarehouse = 0, -- } -- } local myExpirePrivileges = {} for key, cardState in pairs(myAllCradStates) do -- 遍历 cardState 中的所有属性 for privilegeKey, value in pairs(cardState) do if value == 0 then -- 为0说明可能过期了,看看有没有其他卡里也有这个特权而且没过期 [[检查其他所有键对应的属性值中是否有这个属性或属性值为 1]] local foundOtherNoExpired = false for otherKey, otherCardState in pairs(myAllCradStates) do if otherKey ~= key and otherCardState[privilegeKey] == 1 then foundOtherNoExpired = true break end end -- 如果没有找到其他键对应的属性值中有这个属性或属性值为1,说明这个特权没有其他卡有,或者也过期了,则添加到结果表 if not foundOtherNoExpired then myExpirePrivileges[privilegeKey] = 0 end end end end local currRoleTips = getplaydef(actor,"T$hadTipPrivileges") or {} -- lg("myExpirePrivileges", myExpirePrivileges) -- lg("currRoleTips", currRoleTips) if next(myExpirePrivileges) ~= nil then for privilegeKey, value in pairs(myExpirePrivileges) do -- 这个特权失效之前没有发送过,或者发送过但是使用特权卡之后复原了,就发送 if not currRoleTips[privilegeKey] then -- lg("发送过期特权提示", actor, privilegeKey) sendluamsg(actor, LuaMessageIdToClient.PRIVILEGE_INFO_TIP, { [privilegeKey] = value }) -- 记录发送信息 currRoleTips[privilegeKey] = true end end -- lg("afterRoleTips", currRoleTips) end setplaydef(actor,"T$hadTipPrivileges", currRoleTips) end