local ItemEntity = require "app/userdata/bag/item_entity" local ItemConst = require "app/module/item/item_const" local ItemData = class("ItemData", BaseData) local CACHE_ITEM = { cfg_id = 0, count = 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 -- parmas.gold_unit = info.count -- CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas) -- end self:_add(info.cfg_id, info.count) end self.data.dirty = false end function ItemData:_add(id, num) self.items[id] = ItemEntity:create(id, num) 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, 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]:getNum() else num = 0 end return num end function ItemData:getItemNumById(id) local num if self.items[id] then num = self.items[id]:getNum() else num = 0 end return num end function ItemData:addItemReward(item, itemGetType) CACHE_ITEM.cfg_id = GFunc.getRewardId(item) CACHE_ITEM.count = GFunc.getRewardNum(item) self:addItem(CACHE_ITEM, itemGetType) end function ItemData:addItemCost(cost, itemGetType) CACHE_ITEM.cfg_id = GFunc.getRewardId(cost) CACHE_ITEM.count = -GFunc.getRewardNum(cost) self:addItem(CACHE_ITEM, itemGetType) end function ItemData:addItemCosts(costs, itemGetType) if not costs then return end for _, cost in ipairs(costs) do self:addItemCost(cost, 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 self:_addItemNumById(data.cfg_id, data.count) -- if data.cfg_id == GConst.ItemConst.ITEM_ID_GEM or data.cfg_id == GConst.ItemConst.ITEM_ID_GOLD then -- if data.count < 0 then -- if data.cfg_id == GConst.ItemConst.ITEM_ID_GOLD and itemGetType == BIReport.ITEM_GET_TYPE.TRAIN_UP then -- else -- BIReport:postGemUse(data.count, itemGetType, data.cfg_id) -- if data.cfg_id == GConst.ItemConst.ITEM_ID_GEM then -- CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserAdd("gem", data.count) -- elseif data.cfg_id == GConst.ItemConst.ITEM_ID_GOLD then -- local goldNum = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GOLD) -- local parmas = {} -- parmas.gold = goldNum -- CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas) -- end -- end -- else -- if data.cfg_id == GConst.ItemConst.ITEM_ID_GOLD and itemGetType == BIReport.ITEM_GET_TYPE.CHAPTER_DROP then -- else -- BIReport:postGemGet(data.count, itemGetType, data.cfg_id) -- if data.cfg_id == GConst.ItemConst.ITEM_ID_GEM then -- CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserAdd("gem", data.count) -- elseif data.cfg_id == GConst.ItemConst.ITEM_ID_GOLD then -- local goldNum = DataManager.BagData.ItemData:getItemNumById(GConst.ItemConst.ITEM_ID_GOLD) -- local parmas = {} -- parmas.gold = goldNum -- CS.ThinkingAnalytics.ThinkingAnalyticsAPI.UserSet(parmas) -- end -- end -- end -- else -- if data.count < 0 then -- BIReport:postItemUse(data.count, data.cfg_id, itemGetType) -- else -- BIReport:postItemGet(data.count, data.cfg_id, itemGetType) -- end -- end end function ItemData:addItemNumById(id, num, itemGetType) CACHE_ITEM.cfg_id = id CACHE_ITEM.count = num self:addItem(CACHE_ITEM, 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]:addNum(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