c1_lua/lua/app/userdata/bounty/bounty_data.lua
2023-05-17 15:00:29 +08:00

353 lines
8.4 KiB
Lua

local BountyData = class("BountyData", BaseData)
local ACT_GIFT_ID_BOUNTY = 70102
local ACT_GIFT_ID_BOUNTY_ADVANCED = 70202
function BountyData:ctor()
self.data.dirty = false
end
function BountyData:clear()
DataManager:unregisterCrossDayFunc("BountyData")
end
function BountyData:init(data)
data = data or GConst.EMPTY_TABLE
self.season = data.season or 1
self.level = data.level or 1
self.exp = data.exp or 0
self.bought = data.bought
self.popBoughtTime = LocalData:getBountyPopTime()
self.claimed = data.claimed or {}
self.claimedCount = 0
for k, v in pairs(self.claimed) do
if v then
self.claimedCount = self.claimedCount + 1
end
end
self.proClaimed = data.pro_claimed or {}
self.proClaimedCount = 0
for k, v in pairs(self.proClaimed) do
if v then
self.proClaimedCount = self.proClaimedCount + 1
end
end
self.endTime = 0
self:initBountyTime()
self:initBountyLevelCfg()
DataManager:registerCrossDayFunc("BountyData", function()
self:checkNextSeason()
self:markDirty()
end)
end
function BountyData:initBountyTime()
local info = ConfigManager:getConfig("bounty_time")[self.season]
if info == nil then
self.endTime = 0
return
end
self.endTime = Time:getCertainTimeByStr(info.end_time)
end
function BountyData:initBountyLevelCfg()
if self.bountyLevelCfg == nil then
self.bountyLevelCfg = {}
else
for i = 1, #self.bountyLevelCfg do
table.remove(self.bountyLevelCfg)
end
end
if self.proRewards then
for i = 1, #self.proRewards do
table.remove(self.proRewards)
end
end
local cfg = ConfigManager:getConfig("bounty_level")
for k, v in pairs(cfg) do
if v.season == self.season then
self.bountyLevelCfg[k % 100] = v
end
end
self.repeatLevelInfo = table.remove(self.bountyLevelCfg)
end
function BountyData:checkNextSeason()
if self.endTime > Time:getServerTime() then
return
end
local nextSeason = self.season + 1
local info = ConfigManager:getConfig("bounty_time")[nextSeason]
if info == nil then
self.endTime = 0
return
end
self.endTime = Time:getCertainTimeByStr(info.end_time)
self.season = nextSeason
self.level = 1
self.exp = 0
self.bought = false
for k, v in pairs(self.claimed) do
self.claimed[k] = false
end
self.claimedCount = 0
for k, v in pairs(self.proClaimed) do
self.proClaimed[k] = false
end
self.proClaimedCount = 0
self:initBountyLevelCfg()
end
function BountyData:getBought()
return self.bought
end
function BountyData:setBought(season, level)
if self.season ~= season then
return
end
self.bought = true
self.level = level or self.level
self:markDirty()
end
function BountyData:getLevel()
return self.level
end
function BountyData:getExp()
return self.exp
end
function BountyData:addExp(num)
self.exp = self.exp + num
while true do
local lvUpExp = self:getLvUpExp()
if self.exp >= lvUpExp then
self.exp = self.exp - lvUpExp
self.level = self.level + 1
else
break
end
end
self:markDirty()
end
function BountyData:getLvUpExp()
local info = self:getSeasonInfoByLevel(self.level + 1)
if info == nil then
return self.repeatLevelInfo and self.repeatLevelInfo.exp or 1
end
return info.exp
end
function BountyData:getIsOpen()
if ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.BOUNTY_OPEN, true) and self.endTime > Time:getServerTime() then
return true
end
return false
end
function BountyData:getBannerName()
return "bounty_btn_main_1"
end
function BountyData:getRewardBannerName()
return "bounty_btn_main_0"
end
function BountyData:getSeason()
return self.season
end
function BountyData:getSeasonInfoByLevel(lv)
return self.bountyLevelCfg[lv]
end
function BountyData:getMaxLevel()
return #self.bountyLevelCfg
end
function BountyData:getBountyLevelCfg()
return self.bountyLevelCfg
end
function BountyData:getAllProRewards()
if self.proRewards and #self.proRewards > 0 then
return self.proRewards
end
local rewards = {}
for i = 1, #self.bountyLevelCfg do
local cfg = self.bountyLevelCfg[i]
if cfg.reward_pro then
table.insert(rewards, cfg.reward_pro)
end
end
self.proRewards = GFunc.mergeRewards(rewards)
local cfg = ConfigManager:getConfig("item")
table.sort(self.proRewards, function(a, b)
if cfg[a.id].type == cfg[b.id].type then
return a.id > b.id
else
return cfg[a.id].type > cfg[b.id].type
end
end)
return self.proRewards
end
function BountyData:getLevelState(lv)
return self.claimed[lv]
end
function BountyData:getProLevelState(lv)
return self.proClaimed[lv]
end
function BountyData:getIfCanBuyLevel()
local maxLevel = self:getMaxLevel()
if maxLevel > 0 and self.level < maxLevel then
return true
end
return false
end
-- 未领取的最小奖励的index
function BountyData:getMinUnclaimedRewardIndex()
local level = self.level
local maxLevel = self:getMaxLevel()
if level > maxLevel then
level = maxLevel
end
if self.claimedCount < self.level then
for i = 1, level do
if not self.claimed[i] then
return i
end
end
end
if self.bought and self.proClaimedCount < self:getMaxLevel() then
for i = 1, level do
if not self.proClaimed[i] then
return i
end
end
end
return level
end
-- 是否有未领取的奖励
function BountyData:getIfCanClaimReward()
if not self:getIsOpen() then
return false
end
if self.claimedCount < self.level then
return true
end
if self.bought and self.proClaimedCount < self:getMaxLevel() then
return true
end
return false
end
-- 是否可以领取重复奖励
function BountyData:getIfCanClaimRepeatReward()
local maxLevel = self:getMaxLevel()
if maxLevel <= 0 then
return false
end
if self.level <= maxLevel then
return false
end
return not self:getLevelState(self.level)
end
function BountyData:onClaimReward(level)
if self.claimed[level] then
return
end
self.claimed[level] = true
self.claimedCount = self.claimedCount + 1
self:markDirty()
end
function BountyData:onClaimProReward(level)
if self.proClaimed[level] then
return
end
self.proClaimed[level] = true
self.proClaimedCount = self.proClaimedCount + 1
self:markDirty()
end
function BountyData:getBuyBountyLevelCost()
if self.buyBountyLevelCost == nil then
self.buyBountyLevelCost = ConfigManager:getConfig("const")["bounty_buy_cost"].reward
end
return self.buyBountyLevelCost
end
function BountyData:onBoughtLevel()
self.level = self.level + 1
self:markDirty()
end
function BountyData:getGiftId(advanced)
if advanced then
return ACT_GIFT_ID_BOUNTY_ADVANCED
else
return ACT_GIFT_ID_BOUNTY
end
end
function BountyData:markDirty()
self.data.dirty = not self.data.dirty
end
function BountyData:getEndTime()
return self.endTime
end
function BountyData:getExpItemIcon()
local id = GConst.ItemConst.ITEM_ID_BOUNTY_EXP
local info = ConfigManager:getConfig("item")[id]
if info == nil then
return GConst.EMPTY_STRING
end
return info.icon
end
function BountyData:getIsPopBought()
if not self:getIsOpen() then
return false
end
if self.bought then
return false
end
return self.popBoughtTime < Time:getBeginningOfServerToday()
end
function BountyData:markPopBought()
if self.popBoughtTime >= Time:getBeginningOfServerToday() then
return
end
self.popBoughtTime = Time:getBeginningOfServerToday()
LocalData:setBountyPopTime(self.popBoughtTime)
end
function BountyData:getRechargeId(advanced)
local cfg = ConfigManager:getConfig("act_gift")
local id = self:getGiftId(advanced)
if cfg[id] then
return cfg[id].recharge_id
end
return 0
end
function BountyData:getBuyProBountyAddLevelCount()
if self.buyProBountyAddLevelCount == nil then
self.buyProBountyAddLevelCount = ConfigManager:getConfig("const")["bounty_senior_rise"].value
end
return self.buyProBountyAddLevelCount
end
return BountyData