c1_lua/lua/app/userdata/bag/bag_data.lua
2023-04-13 21:13:43 +08:00

195 lines
5.9 KiB
Lua
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

local BagData = class("BagData", BaseData)
local ItemConst = require "app/module/item/item_const"
local SECONDS_PRE_DAY = 86400
BagData.RECOVERY_TYPE = {
TIMELY = 1,
DAILY = 2,
}
function BagData:ctor()
self.ItemData = require("app/userdata/bag/item_data"):create()
end
function BagData:init(data)
if data then
self.ItemData:init(data.ItemData.items)
else
self.ItemData:init()
end
self.recoveries = data.recoveries or {}
-- ts在此处做转换对每日回复来说ts指下次回复时间对时间回复来说ts仍指上次回复时间
for i,v in pairs(self.recoveries) do
self.recoveries[i].ts = self.recoveries[i].ts // 1000
-- local cfg = RecoveryCfg[v.id]
-- if cfg.type == self.RECOVERY_TYPE.DAILY then
-- self.recoveries[i].ts = self.recoveries[i].ts + SECONDS_PRE_DAY
-- end
end
DataManager:registerDataCd("BagData")
end
function BagData:clear()
self.ItemData:clear()
self.recoveries = {}
DataManager:unregisterDataCd("BagData")
end
-- 重置为上次回复的时间
function BagData:resetItemRecoveryTime(itemId)
for i,v in pairs(self.recoveries) do
if v.id == itemId and self.recoveries[i] then
self.recoveries[i].ts = Time:getServerTime()
end
end
end
function BagData:getTimelyItemRecoveryTime(itemId)
local data = self.recoveries[itemId]
if not data then
return -1
end
-- 计算当前值和最大值
-- local cfg = RecoveryCfg[itemId]
-- local curBigNum = self.ItemData:getItemBigNumById(itemId)
-- local maxBigNum = BigNumOpt.getEmptyBigNum()
-- if itemId == ItemConst.ITEM_ID_MINING_PICK then
-- maxBigNum = DataManager.MiningData:getItemMineLimitNum()
-- else
-- maxBigNum = cfg.limit
-- end
-- -- 如果是最大,则重置上一次时间为当前时间
-- if BigNumOpt.bigNumCompare(curBigNum, BigNumOpt.num2BigNum(maxBigNum)) == 0 then
-- data.ts = Time:getServerTime()
-- return 1000 -- 需要持续重置,保持时间更新
-- end
-- -- 计算时间
-- local intervalTime = cfg.time
-- if itemId == GConst.ItemConst.ITEM_ID_MINING_PICK then
-- intervalTime = DataManager.MiningData:getRecoverIntervalTime()
-- end
-- local remainTime = data.ts + intervalTime - Time:getServerTime()
-- return remainTime
end
function BagData:getDailyItemRecoveryMaxTime(itemId)
-- if itemId == ItemConst.ITEM_ID_PVP_KEY then
-- if self.ItemData:isPvPKeyMax() then
-- return -1
-- end
-- elseif itemId == ItemConst.ITEM_ID_MOPPING_UP then
-- if self.ItemData:isMoppingUpMax() then
-- return -1
-- end
-- end
local data = self.recoveries[itemId]
if data then
return data.ts - Time:getServerTime()
end
return -1
end
function BagData:getTimelyItemRecoveryMaxTime(itemId)
local maxVit = 0
local vit = 0
-- if itemId == ItemConst.ITEM_ID_VIT then
-- if self.ItemData:isVitMax() then
-- return -1
-- end
-- vit = self.ItemData:getVit()
-- maxVit = DataManager.PlayerData:getMaxVit()
-- elseif itemId == ItemConst.ITEM_ID_GUILD_KEY then
-- if self.ItemData:isGuildBossKeyMax() then
-- return -1
-- end
-- vit = self.ItemData:getGuildBossKey()
-- maxVit = self.ItemData:getMaxGuildBossKey()
-- end
local curTime = self:getTimelyItemRecoveryTime(itemId)
if curTime < 0 then
return -1
end
-- local cfg = RecoveryCfg[itemId]
-- if not cfg then
-- return -1
-- end
-- curTime = curTime + (maxVit - vit - 1)*cfg.time
-- return curTime
end
-- 按时间回复的在此回复
function BagData:recoveryItem(data, maxNum)
-- 计算已经达到上限
local curBigNum = self.ItemData:getItemNumById(data.id)
if curBigNum >= maxNum then
return
end
-- 计算恢复间隔
-- local cfg = RecoveryCfg[data.id]
-- local nowTime = Time:getServerTime()
-- local diffTime = nowTime - data.ts -- 上次回复时间,此处计算离线总共回复多少个
-- if diffTime <= 0 then
-- return
-- end
-- -- 计算增加数量
-- local recoverTime = cfg.time
-- if data.id == GConst.ItemConst.ITEM_ID_MINING_PICK then -- 挖矿特殊处理,受研究属性影响
-- recoverTime = DataManager.MiningData:getRecoverIntervalTime()
-- end
-- local addCount = math.floor(diffTime / recoverTime)
-- if addCount <= 0 then
-- return
-- end
-- local addBigNum = BigNumOpt.num2BigNum(addCount)
-- -- 计算此次实际增加数量
-- local addAfterNum = BigNumOpt.bigNumAdd(curBigNum, addBigNum)
-- if BigNumOpt.bigNumCompare(addAfterNum, maxBigNum) > 0 then
-- addBigNum = BigNumOpt.bigNumSub(maxBigNum, curBigNum)
-- end
-- Logger.logHighlight("实际恢复数量:".. BigNumOpt.bigNum2Num(addBigNum) )
-- -- 根据实际增加的数量,计算恢复时间
-- data.ts = data.ts + recoverTime * BigNumOpt.bigNum2Num(addBigNum)
-- self.ItemData:addItemNumById(data.id, addBigNum, BIReport.ITEM_GET_TYPE.UPDATE_TIME)
end
-- 每日回复的在此回复
function BagData:recoveryDailyItem(key, data)
-- local nowTime = Time:getServerTime()
-- if nowTime < self.recoveries[key].ts then
-- return
-- end
-- local itemId = data.id
-- local cfg = RecoveryCfg[data.id]
-- local curBigNum = self.ItemData:getItemBigNumById(itemId)
-- local maxBigNum = cfg.limit
-- if BigNumOpt.bigNumCompare(curBigNum, maxBigNum) < 0 then
-- self.ItemData:addItemNumById(itemId, BigNumOpt.bigNumSub(maxBigNum, curBigNum), BIReport.ITEM_GET_TYPE.CROSS_DAY)
-- end
-- -- 计算下次回复的时间
-- self.recoveries[key].ts = self.recoveries[key].ts + SECONDS_PRE_DAY
end
function BagData:updateCd()
if not self.recoveries then
return
end
for i,v in pairs(self.recoveries) do
-- local cfg = RecoveryCfg[v.id]
-- if cfg.type == self.RECOVERY_TYPE.DAILY then
-- self:recoveryDailyItem(i, v) -- 每日的直接加满
-- else
-- local limit = cfg.limit
-- if v.id == GConst.ItemConst.ITEM_ID_MINING_PICK then -- 挖矿的上限特殊处理,受属性影响
-- limit = DataManager.MiningData:getItemMineLimitNum()
-- limit = BigNumOpt.num2BigNum(limit)
-- end
-- self:recoveryItem(v, limit) -- 根据间隔时间增加
-- end
end
end
return BagData