229 lines
6.1 KiB
Lua
229 lines
6.1 KiB
Lua
local ItemEntity = require "app/userdata/bag/item_entity"
|
|
|
|
local ItemConst = require "app/module/item/item_const"
|
|
local ItemData = class("ItemData", BaseData)
|
|
|
|
local CACHE_ITEM = {
|
|
id = 0,
|
|
count = {
|
|
value = 0,
|
|
unit = 0,
|
|
}
|
|
}
|
|
|
|
function ItemData:ctor()
|
|
self.items = {}
|
|
end
|
|
|
|
function ItemData:init(data)
|
|
self.items = {}
|
|
data = data or {}
|
|
for _, info in pairs(data) do
|
|
if info.id == GConst.ItemConst.ITEM_ID_GEM then
|
|
local parmas = {}
|
|
parmas.gem = BigNumOpt.bigNum2Num(info.count)
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas)
|
|
elseif info.id == GConst.ItemConst.ITEM_ID_GOLD then
|
|
local parmas = {}
|
|
parmas.gold_value = info.count.value
|
|
parmas.gold_unit = info.count.unit
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas)
|
|
end
|
|
self:_add(info.id, info.count)
|
|
end
|
|
|
|
self.data.dirty = false
|
|
end
|
|
|
|
function ItemData:_add(id, bigNum)
|
|
self.items[id] = ItemEntity:create(id, bigNum)
|
|
end
|
|
|
|
function ItemData:clear()
|
|
self.items = {}
|
|
end
|
|
|
|
-- 根据id获取道具
|
|
function ItemData:getItemById(id)
|
|
if self.items[id] then
|
|
return self.items[id]
|
|
end
|
|
local item = ItemEntity:create(id, BigNumOpt.num2BigNum(0))
|
|
self.items[id] = item
|
|
return self.items[id]
|
|
end
|
|
|
|
-- 获取所有道具数据
|
|
function ItemData:getAllItems()
|
|
return self.items
|
|
end
|
|
|
|
function ItemData:getItemBigNumById(id)
|
|
local num
|
|
if self.items[id] then
|
|
num = self.items[id]:getBigNum()
|
|
else
|
|
num = BigNumOpt.num2BigNum(0)
|
|
end
|
|
return num
|
|
end
|
|
|
|
function ItemData:getItemBigNumStrById(id)
|
|
local num = 0
|
|
if self.items[id] then
|
|
num = self.items[id]:getBigNumStr()
|
|
end
|
|
return num
|
|
end
|
|
|
|
function ItemData:addItemReward(item, itemGetType)
|
|
CACHE_ITEM.id = item.id
|
|
CACHE_ITEM.count.unit = item.count.unit
|
|
CACHE_ITEM.count.value = item.count.value
|
|
self:addItem(CACHE_ITEM, itemGetType)
|
|
end
|
|
|
|
function ItemData:addItemCost(cost, itemGetType)
|
|
CACHE_ITEM.id = cost.id
|
|
CACHE_ITEM.count.unit = cost.count.unit
|
|
CACHE_ITEM.count.value = -cost.count.value
|
|
self:addItem(CACHE_ITEM, itemGetType)
|
|
end
|
|
|
|
function ItemData:addItemCosts(costs, itemGetType)
|
|
if not costs then
|
|
return
|
|
end
|
|
for _, unitCost in ipairs(costs) do
|
|
self:addItemCost(unitCost, itemGetType)
|
|
end
|
|
end
|
|
|
|
function ItemData:addItem(data, itemGetType)
|
|
if data == nil then
|
|
return
|
|
end
|
|
|
|
if EDITOR_MODE then
|
|
if not itemGetType then
|
|
local params = {
|
|
content = "ItemData addItem has no itemGetType",
|
|
boxType = GConst.MESSAGE_BOX_TYPE.MB_OK,
|
|
okText = I18N:getGlobalText(I18N.GlobalConst.BTN_TEXT_OK),
|
|
}
|
|
GFunc.showMessageBox(params)
|
|
Logger.log("ItemData addItem has no itemGetType")
|
|
end
|
|
end
|
|
|
|
local itemCfg = ConfigManager:getConfig("item")[data.id]
|
|
|
|
self:_addItemNumById(data.id, data.count)
|
|
|
|
if data.id == GConst.ItemConst.ITEM_ID_GEM or data.id == GConst.ItemConst.ITEM_ID_GOLD then
|
|
if data.count.value < 0 then
|
|
if data.id == GConst.ItemConst.ITEM_ID_GOLD and itemGetType == BIReport.ITEM_GET_TYPE.TRAIN_UP then
|
|
else
|
|
BIReport:postGemUse(data.count, itemGetType, data.id)
|
|
if data.id == GConst.ItemConst.ITEM_ID_GEM then
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserAdd("gem", BigNumOpt.bigNum2Num(data.count))
|
|
elseif data.id == GConst.ItemConst.ITEM_ID_GOLD then
|
|
local goldBigNum = DataManager.BagData.ItemData:getItemBigNumById(GConst.ItemConst.ITEM_ID_GOLD)
|
|
local parmas = {}
|
|
parmas.gold_value = goldBigNum.value
|
|
parmas.gold_unit = goldBigNum.unit
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas)
|
|
end
|
|
end
|
|
else
|
|
if data.id == GConst.ItemConst.ITEM_ID_GOLD and itemGetType == BIReport.ITEM_GET_TYPE.CHAPTER_DROP then
|
|
else
|
|
BIReport:postGemGet(data.count, itemGetType, data.id)
|
|
if data.id == GConst.ItemConst.ITEM_ID_GEM then
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserAdd("gem", BigNumOpt.bigNum2Num(data.count))
|
|
elseif data.id == GConst.ItemConst.ITEM_ID_GOLD then
|
|
local goldBigNum = DataManager.BagData.ItemData:getItemBigNumById(GConst.ItemConst.ITEM_ID_GOLD)
|
|
local parmas = {}
|
|
parmas.gold_value = goldBigNum.value
|
|
parmas.gold_unit = goldBigNum.unit
|
|
CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas)
|
|
end
|
|
end
|
|
end
|
|
else
|
|
if data.count.value < 0 then
|
|
BIReport:postItemUse(data.count, data.id, itemGetType)
|
|
else
|
|
BIReport:postItemGet(data.count, data.id, itemGetType)
|
|
end
|
|
end
|
|
|
|
if data.id == GConst.ItemConst.ITEM_ID_MINING_PICK then
|
|
local num = BigNumOpt.bigNum2Num(data.count)
|
|
if num < 0 then
|
|
num = -num
|
|
ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_HOE_USE, {num = num})
|
|
end
|
|
elseif data.id == GConst.ItemConst.ITEM_ID_DRILL then
|
|
local num = BigNumOpt.bigNum2Num(data.count)
|
|
if num < 0 then
|
|
num = -num
|
|
ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_BORING_CROWN_USE, {num = num})
|
|
end
|
|
elseif data.id == GConst.ItemConst.ITEM_ID_EXPLOSIVE then
|
|
local num = BigNumOpt.bigNum2Num(data.count)
|
|
if num < 0 then
|
|
num = -num
|
|
ModuleManager.TaskManager:addTaskProgress(GConst.TaskConst.TASK_TYPE.X_BOOM_USE, {num = num})
|
|
end
|
|
end
|
|
end
|
|
|
|
function ItemData:addItemNumById(id, num, itemGetType)
|
|
local data = {id = id, count = num}
|
|
self:addItem(data, itemGetType)
|
|
end
|
|
|
|
function ItemData:_addItemNumById(id, bigNum)
|
|
if bigNum == nil then
|
|
return
|
|
end
|
|
-- local isFull = false
|
|
-- if id == ItemConst.ITEM_ID_VIT then
|
|
-- -- isFull = DataManager.BagData.ItemData:isVitMax()
|
|
-- local maxVit = DataManager.PlayerData:getMaxVit()
|
|
-- isFull = currentCount >= maxVit
|
|
-- end
|
|
if self.items[id] then
|
|
self.items[id]:addBigNum(bigNum)
|
|
else
|
|
self:_add(id, bigNum)
|
|
end
|
|
-- if id == ItemConst.ITEM_ID_VIT and isFull and not DataManager.BagData.ItemData:isVitMax() then
|
|
-- if id == ItemConst.ITEM_ID_VIT and isFull then
|
|
-- local maxVit = DataManager.PlayerData:getMaxVit()
|
|
-- local isNewFull = currentCount >= maxVit
|
|
-- if not isNewFull then
|
|
-- DataManager.BagData:resetItemRecoveryTime(ItemConst.ITEM_ID_VIT)
|
|
-- end
|
|
-- end
|
|
self:setDirty()
|
|
end
|
|
|
|
function ItemData:setItemNumById(id, bigNum)
|
|
if bigNum == nil then
|
|
return
|
|
end
|
|
if self.items[id] then
|
|
self.items[id]:setNum(bigNum)
|
|
else
|
|
self:_add(id, bigNum)
|
|
end
|
|
self:setDirty()
|
|
end
|
|
|
|
function ItemData:setDirty()
|
|
self.data.dirty = not self.data.dirty
|
|
end
|
|
|
|
return ItemData |