304 lines
7.8 KiB
Lua
304 lines
7.8 KiB
Lua
local EquipData = class("EquipData", BaseData)
|
|
local EquipEntity = require "app/userdata/equip/equip_entity"
|
|
|
|
function EquipData:ctor()
|
|
self.data.isDirty = false
|
|
end
|
|
|
|
function EquipData:clear()
|
|
ModuleManager.EquipManager:updateEquipGiftTimer(true)
|
|
end
|
|
|
|
function EquipData:init(data)
|
|
data = data or GConst.EMPTY_TABLE
|
|
Logger.logHighlight("装备数据")
|
|
Logger.printTable(data)
|
|
self.equips = {}
|
|
if not data.HeroesEquips then
|
|
return
|
|
end
|
|
|
|
for heroId, equip in pairs(data.HeroesEquips) do
|
|
for part, level in pairs(equip.Equips) do
|
|
self:addEquip(heroId, part, level)
|
|
end
|
|
end
|
|
end
|
|
|
|
function EquipData:setDirty()
|
|
self.data.isDirty = not self.data.isDirty
|
|
end
|
|
|
|
-- 武器功能是否开启
|
|
function EquipData:isWeaponOpen(showToast)
|
|
if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.EQUIP_WEAPON, not showToast) then
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
-- 防具功能是否开启
|
|
function EquipData:isArmorOpen(showToast)
|
|
if not ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.EQUIP_ARMOR, not showToast) then
|
|
return false
|
|
end
|
|
return true
|
|
end
|
|
|
|
function EquipData:addEquip(heroId, part, level)
|
|
if not self.equips[heroId] then
|
|
self.equips[heroId] = {}
|
|
end
|
|
if self.equips[heroId][part] then
|
|
return
|
|
end
|
|
self.equips[heroId][part] = self:createEntity(heroId, part, level)
|
|
end
|
|
|
|
function EquipData:createEntity(heroId, part, level)
|
|
return EquipEntity:create(heroId, part, level)
|
|
end
|
|
|
|
function EquipData:getAllEquips()
|
|
return self.equips
|
|
end
|
|
|
|
function EquipData:getEquip(id, part)
|
|
if not self.equips[id] then
|
|
self.equips[id] = {}
|
|
end
|
|
if not self.equips[id][part] then
|
|
self.equips[id][part] = self:createEntity(id, part)
|
|
end
|
|
return self.equips[id][part]
|
|
end
|
|
|
|
-- 是否有装备可升级
|
|
function EquipData:canUpgradeEquip(heroId)
|
|
if self:canUpgradeWeapon(heroId) then
|
|
return true
|
|
end
|
|
|
|
if self:canUpgradeArmor(heroId) then
|
|
return true
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
-- 武器是否可升级
|
|
function EquipData:canUpgradeWeapon(heroId)
|
|
if not self:isWeaponOpen() then
|
|
return false
|
|
end
|
|
|
|
local entity = self:getEquip(heroId, GConst.EquipConst.PART_TYPE.WEAPON)
|
|
if entity:canLevelUp() then
|
|
return true
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- 防具是否可升级
|
|
function EquipData:canUpgradeArmor(heroId)
|
|
if not self:isArmorOpen() then
|
|
return false
|
|
end
|
|
|
|
local entity
|
|
for name, part in pairs(GConst.EquipConst.PART_TYPE) do
|
|
entity = self:getEquip(heroId, part)
|
|
if part ~= GConst.EquipConst.PART_TYPE.WEAPON then
|
|
if entity:canLevelUp() then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
return false
|
|
end
|
|
|
|
-- 获取防具最小阶段
|
|
function EquipData:getMinArmorStage(heroId)
|
|
local minStage = -1
|
|
for name, part in pairs(GConst.EquipConst.PART_TYPE) do
|
|
if part ~= GConst.EquipConst.PART_TYPE.WEAPON then
|
|
local entity = self:getEquip(heroId, part)
|
|
if minStage == -1 or minStage > entity:getStage() then
|
|
minStage = entity:getStage()
|
|
end
|
|
end
|
|
end
|
|
return minStage >= 0 and minStage or 0
|
|
end
|
|
|
|
-- 装备升级
|
|
function EquipData:onUpgradeEquip(heroId, part)
|
|
local entity = self:getEquip(heroId, part)
|
|
if not entity then
|
|
return
|
|
end
|
|
entity:onLevelUp()
|
|
DataManager.HeroData:getHeroById(heroId):onEquipAttrChange()
|
|
end
|
|
|
|
-- 装备礼包相关 -----------------------------------------------------------------------------
|
|
|
|
-- 初始化礼包信息
|
|
function EquipData:initGifts(data)
|
|
self.gifts = data.active_gifts or GConst.EMPTY_TABLE
|
|
|
|
Logger.logHighlight("初始化装备礼包")
|
|
Logger.printTable(self.gifts)
|
|
|
|
ModuleManager.EquipManager:updateEquipGiftTimer()
|
|
self:setDirty()
|
|
end
|
|
|
|
-- 获取礼包最近的结束时间
|
|
function EquipData:getGiftNearestRemainTime(actType)
|
|
local checkList = actType ~= nil and self:getGiftIdsByType(actType) or table.keys(self.gifts)
|
|
local nearest = nil
|
|
|
|
for idx, id in pairs(checkList) do
|
|
local time = self:getGiftRemainTime(id)
|
|
if nearest == nil or nearest > time then
|
|
nearest = time
|
|
end
|
|
end
|
|
|
|
return nearest
|
|
end
|
|
|
|
-- 获取所有存在的该类型礼包
|
|
function EquipData:getGiftIdsByType(actType)
|
|
local ids = {}
|
|
for id, data in pairs(self.gifts) do
|
|
local cfg = DataManager.ShopData:getActGiftConfig()[id]
|
|
if cfg and actType == cfg.type and self:getGiftRemainTime(id) > 0 then
|
|
table.insert(ids, id)
|
|
end
|
|
end
|
|
table.sort(ids, function(a, b) return a > b end)
|
|
|
|
return ids
|
|
end
|
|
|
|
-- 获取可以展示的礼包
|
|
function EquipData:getCanShowGiftId(heroId, part)
|
|
local giftId = self:getEquipGiftId(heroId, part)
|
|
|
|
if not self:hasGiftData(giftId) then
|
|
return nil
|
|
end
|
|
|
|
if self:getGiftRemainTime(giftId) > 0 then
|
|
return giftId
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
-- 当前是否存在礼包数据
|
|
function EquipData:hasGiftData(giftId)
|
|
return self.gifts[giftId] ~= nil
|
|
end
|
|
|
|
-- 获取装备对应的礼包id
|
|
function EquipData:getEquipGiftId(heroId, part)
|
|
local giftType
|
|
if part == GConst.EquipConst.PART_TYPE.WEAPON then
|
|
giftType = PayManager.PURCHARSE_ACT_TYPE.WEAPON_UPGRADE_GIFT
|
|
else
|
|
giftType = PayManager.PURCHARSE_ACT_TYPE.ARMOR_UPGRADE_GIFT
|
|
end
|
|
|
|
if giftType == nil then
|
|
return nil
|
|
end
|
|
|
|
local level = DataManager.EquipData:getEquip(heroId, part):getLevel()
|
|
for idx, id in pairs(self:getGiftIdsByType(giftType)) do
|
|
if self:meetGiftLevelRange(id, level) then
|
|
return id
|
|
end
|
|
end
|
|
|
|
return nil
|
|
end
|
|
|
|
-- 是否满足礼包的等级条件
|
|
function EquipData:meetGiftLevelRange(giftId, level)
|
|
local cfg = DataManager.ShopData:getActGiftConfig()[giftId]
|
|
if cfg == nil or cfg.parameter_pro == nil then
|
|
return
|
|
end
|
|
|
|
return level >= cfg.parameter_pro[1] and level <= cfg.parameter_pro[2]
|
|
end
|
|
|
|
-- 获取礼包剩余时间
|
|
function EquipData:getGiftRemainTime(giftId)
|
|
-- 没有礼包
|
|
if self.gifts[giftId] == nil then
|
|
return 0
|
|
end
|
|
|
|
-- 在冷却中
|
|
if self:isGiftCooling(giftId) then
|
|
return 0
|
|
end
|
|
|
|
return (self.gifts[giftId].expire_at // 1000) - Time:getServerTime()
|
|
end
|
|
|
|
-- 礼包是否在冷却时间中
|
|
function EquipData:isGiftCooling(giftId)
|
|
local giftData = DataManager.ShopData:getActGiftDetailData(PayManager.PURCHARSE_TYPE.ACT_GIFT, giftId)
|
|
|
|
if giftData then
|
|
local latestBuyTime = giftData.latest_buy_at // 1000
|
|
local cfg = DataManager.ShopData:getActGiftConfig()[giftId]
|
|
if latestBuyTime > 0 then
|
|
-- 购买冷却
|
|
local cdTime = (cfg.cd or 0) * 3600
|
|
if latestBuyTime + cdTime > Time:getServerTime() then
|
|
return true
|
|
end
|
|
elseif self:hasGiftData(giftId) then
|
|
-- 到期冷却
|
|
if self.gifts[giftId].cd_end_at // 1000 < Time:getServerTime() then
|
|
return true
|
|
end
|
|
end
|
|
end
|
|
|
|
return false
|
|
end
|
|
|
|
-- 获取礼包显示的标题
|
|
function EquipData:getGiftTitle(giftId)
|
|
local gift = DataManager.ShopData:getActGiftConfig()[giftId]
|
|
if gift == nil then
|
|
return nil
|
|
end
|
|
|
|
if gift.type == PayManager.PURCHARSE_ACT_TYPE.WEAPON_UPGRADE_GIFT then
|
|
return I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_28, gift.parameter_pro[1] + 20)
|
|
elseif gift.type == PayManager.PURCHARSE_ACT_TYPE.ARMOR_UPGRADE_GIFT then
|
|
return I18N:getGlobalText(I18N.GlobalConst.EQUIP_DESC_27, gift.parameter_pro[1] + 20)
|
|
end
|
|
end
|
|
|
|
-- 礼包状态改变
|
|
function EquipData:onGiftStateChange()
|
|
Logger.logHighlight("装备礼包状态改变")
|
|
self:setDirty()
|
|
end
|
|
|
|
-- 购买礼包成功
|
|
function EquipData:onBuyGiftSuccess(giftId)
|
|
Logger.logHighlight("购买装备礼包成功:"..giftId)
|
|
self:setDirty()
|
|
end
|
|
|
|
return EquipData |