221 lines
5.6 KiB
Lua
221 lines
5.6 KiB
Lua
local ServerItemData = class("ServerItemData", ServerBaseData)
|
|
|
|
function ServerItemData:init(data)
|
|
self.data.items = {}
|
|
if data then
|
|
for idStr, item in pairs(data.items) do
|
|
local id = tonumber(idStr)
|
|
if id == GConst.ItemConst.ITEM_ID_VIT then
|
|
local ServerGameData = require "app/server/server_game_data"
|
|
ServerGameData.PlayerData:setVit(item.count)
|
|
end
|
|
self.data.items[idStr] = {cfg_id = id, count = item.count}
|
|
end
|
|
self.data.recoveries = data.recoveries
|
|
else
|
|
self.data.recoveries = {}
|
|
end
|
|
self:checkRecoveries()
|
|
end
|
|
|
|
function ServerItemData:tryAddItem(id, num)
|
|
local idStr = tostring(id)
|
|
if not self.data.items[idStr] then
|
|
if num < 0 then
|
|
return false
|
|
end
|
|
else
|
|
if self.data.items[idStr].count + num < 0 then
|
|
return false
|
|
end
|
|
end
|
|
return true
|
|
end
|
|
|
|
function ServerItemData:getAllItems()
|
|
return self.data.items
|
|
end
|
|
|
|
function ServerItemData:getItemNumById(id)
|
|
local idStr = tostring(id)
|
|
if not self.data.items[idStr] then
|
|
return 0
|
|
end
|
|
local num = self.data.items[idStr].count or 0
|
|
return num
|
|
end
|
|
|
|
function ServerItemData:checkRecoveries()
|
|
local recoveryCfg = self:getRecoveryCfg()
|
|
for k, v in pairs(recoveryCfg) do
|
|
local inList = false
|
|
for ii, vv in ipairs(self.data.recoveries) do
|
|
if vv.cfg_id == k then
|
|
inList = true
|
|
end
|
|
end
|
|
if not inList then
|
|
table.insert(self.data.recoveries, {cfg_id = k, ts = Time:getServerTime()})
|
|
end
|
|
end
|
|
self:recoveryItems()
|
|
end
|
|
|
|
function ServerItemData:recoveryItems()
|
|
if not self.data.recoveries then
|
|
return
|
|
end
|
|
for i,v in ipairs(self.data.recoveries) do
|
|
if v.cfg_id == GConst.ItemConst.ITEM_ID_VIT then
|
|
local maxCount = self:getMaxVit()
|
|
self:recoveryItem(v, maxCount)
|
|
end
|
|
end
|
|
end
|
|
|
|
|
|
function ServerItemData:recoveryItem(data, maxCount)
|
|
if not CS.BF.BFMain.IsGotServerTime then
|
|
return
|
|
end
|
|
if data.cfg_id == GConst.ItemConst.ITEM_ID_VIT then
|
|
local ServerGameData = require "app/server/server_game_data"
|
|
local currentCount = ServerGameData.PlayerData:getVit()
|
|
if currentCount >= maxCount then -- 已经达到上限
|
|
return
|
|
end
|
|
|
|
local cfg = self:getRecoveryCfg()[data.cfg_id]
|
|
local nowTime = Time:getServerTime()
|
|
local diffTime = nowTime - data.ts
|
|
if diffTime <= 0 then
|
|
return
|
|
end
|
|
local addCount = math.floor(diffTime / cfg.time)
|
|
if addCount <= 0 then
|
|
return
|
|
end
|
|
end
|
|
|
|
local currentCount = self:getItemNumById(data.cfg_id)
|
|
if currentCount >= maxCount then -- 已经达到上限
|
|
return
|
|
end
|
|
local cfg = self:getRecoveryCfg()[data.cfg_id]
|
|
local nowTime = Time:getServerTime()
|
|
local diffTime = nowTime - data.ts
|
|
if diffTime <= 0 then
|
|
return
|
|
end
|
|
local addCount = math.floor(diffTime / cfg.time)
|
|
if addCount <= 0 then
|
|
return
|
|
end
|
|
data.ts = data.ts + cfg.time*addCount
|
|
if currentCount + addCount > maxCount then
|
|
addCount = maxCount - currentCount
|
|
end
|
|
self:_addItem(data.cfg_id, addCount)
|
|
ServerDataManager:saveData()
|
|
end
|
|
|
|
function ServerItemData:resetVitRecoverTime()
|
|
local maxCount = self:getMaxVit()
|
|
local ServerGameData = require "app/server/data/server_game_data"
|
|
local currentCount = ServerGameData.PlayerData:getVit()
|
|
if currentCount >= maxCount then -- 已经达到上限
|
|
return
|
|
end
|
|
|
|
local currentCount = self:getItemNumById(GConst.ItemConst.ITEM_ID_VIT)
|
|
if currentCount >= maxCount then -- 已经达到上限
|
|
return
|
|
end
|
|
for i,v in ipairs(self.recoveries) do
|
|
if v.cfg_id == GConst.ItemConst.ITEM_ID_VIT then
|
|
self.data.recoveries[i].ts = Time:getServerTime()
|
|
end
|
|
end
|
|
end
|
|
|
|
function ServerItemData:getRecoveryCfg()
|
|
if self.recoveryCfg == nil then
|
|
self.recoveryCfg = ConfigManager:getConfig("recovery")
|
|
end
|
|
return self.recoveryCfg
|
|
end
|
|
|
|
function ServerItemData:getMaxVit()
|
|
if self.maxVit == nil then
|
|
local cfg = self:getRecoveryCfg()[GConst.ItemConst.ITEM_ID_VIT]
|
|
self.maxVit = cfg.limit
|
|
end
|
|
return self.maxVit
|
|
end
|
|
|
|
function ServerItemData:addItemReward(item, getType)
|
|
local id = GFunc.getRewardId(item)
|
|
local num = GFunc.getRewardNum(item)
|
|
self:addItem(id, num, getType)
|
|
end
|
|
|
|
function ServerItemData:addItemCost(cost)
|
|
local id = GFunc.getRewardId(cost)
|
|
local num = GFunc.getRewardNum(cost)
|
|
self:addItem(id, -num)
|
|
end
|
|
|
|
function ServerItemData:addItem(id, num, getType)
|
|
if id == GConst.ItemConst.ITEM_ID_VIT then
|
|
local maxCount = self:getMaxVit()
|
|
local currentCount = self:getItemNumById(GConst.ItemConst.ITEM_ID_VIT)
|
|
local isMax = currentCount >= maxCount
|
|
self:_addItem(id, num, getType)
|
|
local currentCount = currentCount + num
|
|
local isMax1 = currentCount >= maxCount
|
|
if not isMax1 then
|
|
if isMax then
|
|
self:resetVitRecoverTime()
|
|
else
|
|
self:recoveryItems()
|
|
end
|
|
end
|
|
else
|
|
self:_addItem(id, num, getType)
|
|
end
|
|
end
|
|
|
|
function ServerItemData:_addItem(id, num, getType)
|
|
local idStr = tostring(id)
|
|
local hadNum = 0
|
|
if self.data.items[idStr] then
|
|
hadNum = self.data.items[idStr].count
|
|
else
|
|
self.data.items[idStr] = {}
|
|
self.data.items[idStr].cfg_id = id
|
|
self.data.items[idStr].count = 0
|
|
end
|
|
local finalNum = (hadNum or 0) + num
|
|
if finalNum < 0 then
|
|
finalNum = 0
|
|
end
|
|
local ServerGameData = require "app/server/server_game_data"
|
|
if id == GConst.ItemConst.ITEM_ID_VIT then
|
|
ServerGameData.PlayerData:setVit(finalNum)
|
|
end
|
|
self.data.items[idStr].count = finalNum
|
|
if num < 0 then
|
|
if id == GConst.ItemConst.ITEM_ID_GEM then
|
|
ServerGameData.TaskData:addTaskProgress(GConst.TASK_TYPE.TASK_GEM_COST, -num)
|
|
end
|
|
end
|
|
|
|
getType = getType or ""
|
|
if num > 0 and id == GConst.ItemConst.ITEM_ID_GOLD and
|
|
getType ~= GConst.ServerDataConst.DATA_OP_BEHAVIOR.EQUIP_RESOLVE and
|
|
getType ~= GConst.ServerDataConst.DATA_OP_BEHAVIOR.EQUIP_REBACK then
|
|
ServerGameData.TaskData:addTaskProgress(GConst.TASK_TYPE.TASK_GOLD_GET, num)
|
|
end
|
|
end
|
|
|
|
return ServerItemData |