c1_lua/lua/app/userdata/chapter/chapter_data.lua
2023-04-24 10:30:40 +08:00

265 lines
6.3 KiB
Lua

local ChapterData = class("ChapterData", BaseData)
ChapterData.MIN_CHAPTER_ID = 1
local MIN_CHAPTER_ID = ChapterData.MIN_CHAPTER_ID
function ChapterData:ctor()
self.data.chapterId = MIN_CHAPTER_ID
self.data.maxChapterId = 0
self.data.isDirty = false
self.data.chapterBoxInfo = {}
self.data.chapterFightInfo = {}
end
function ChapterData:clear()
self.data.chapterId = MIN_CHAPTER_ID
self.data.maxChapterId = 0
self.data.chapterBoxInfo = {}
self.data.chapterFightInfo = {}
end
function ChapterData:init(data, notChangeChapterId)
self.data.maxChapterId = data and data.maxChapterId or MIN_CHAPTER_ID - 1
if not notChangeChapterId then
self.data.chapterId = self:getNextChapter(self.data.maxChapterId)
end
self.data.chapterBoxInfo = data and data.chapterBoxInfo or {}
self.data.chapterFightInfo = data and data.chapterFightInfo or {}
end
function ChapterData:getIsFirstChapter(chapterId)
return chapterId == MIN_CHAPTER_ID
end
function ChapterData:getIsFinalChapter(chapterId)
local chapterInfo = self:getChapterCfg()[chapterId]
if chapterInfo == nil then
return false
end
local chapterId = chapterInfo.next_chapter
if chapterId == nil then
return true
end
return false
end
function ChapterData:setDirty()
self.data.isDirty = not self.data.isDirty
end
function ChapterData:getChapterId()
return self.data.chapterId
end
function ChapterData:setChapterId(chapterId)
self.data.chapterId = chapterId
end
function ChapterData:getNextChapter(chapterId)
local chapterInfo = self:getChapterCfg()[chapterId]
if chapterInfo == nil then
return 1
end
if chapterInfo.next_chapter == nil then
return chapterId
end
return chapterInfo.next_chapter
end
function ChapterData:isFinalChapter(chapterId)
local chapterInfo = self:getChapterCfg()[chapterId]
if chapterInfo == nil then
return true
end
return chapterInfo.next_chapter == nil
end
function ChapterData:goNextChapter()
local chapterId = self:getNextChapter(self.data.chapterId)
if self.data.chapterId == chapterId then
return false
else
self.data.chapterId = chapterId
end
return true
end
function ChapterData:goLastChapter()
local chapterInfo = self:getChapterCfg()[self.data.chapterId]
if chapterInfo == nil then
return false
end
local chapterId = chapterInfo.before_chapter
if chapterId == nil then
return false
end
self.data.chapterId = chapterId
return true
end
function ChapterData:getChapterCfg()
if self.chapterCfg == nil then
self.chapterCfg = ConfigManager:getConfig("chapter")
end
return self.chapterCfg
end
function ChapterData:getChapterBoxCount(chapterId)
chapterId = chapterId or self:getChapterId()
local cfg = self:getChapterCfg()[chapterId]
if cfg and cfg.box_num then
return #cfg.box_num
end
return 1
end
function ChapterData:getChapterBoxNum(chapterId, index)
chapterId = chapterId or self:getChapterId()
local cfg = self:getChapterCfg()[chapterId]
if cfg and cfg.box_num then
return cfg.box_num[index] or 1
end
return 1
end
function ChapterData:getChapterBoxRewardGot(chapterId, index)
chapterId = tostring(chapterId or self:getChapterId())
local list = self.data.chapterBoxInfo[chapterId]
if not list then
return false
end
for _, idx in ipairs(list) do
if idx == index then
return true
end
end
return false
end
function ChapterData:boxCanGet(chapterId, index)
if self:getChapterBoxRewardGot(chapterId, index) then
return false
end
local needWave = DataManager.ChapterData:getChapterBoxNum(chapterId, index)
local curMaxWave = DataManager.ChapterData:getChapterMaxWave(chapterId)
return needWave <= curMaxWave
end
function ChapterData:getChapterBoxRewards(chapterId, index)
chapterId = chapterId or self:getChapterId()
if not self:getChapterBoxNum(chapterId, index) then
return
end
local cfg = self:getChapterCfg()[chapterId]
if cfg then
return cfg["box_reward_" .. index]
end
end
function ChapterData:getChapterMaxWave(chapterId)
chapterId = tostring(chapterId or self:getChapterId())
if self.data.chapterFightInfo[chapterId] then
return self.data.chapterFightInfo[chapterId].maxWave
end
return 0
end
function ChapterData:getFightCost()
return GFunc.getConstReward("chapter_cost")
end
function ChapterData:getMaxChapterId()
return self.data.maxChapterId
end
-- 此章节之前是否有未领取的奖励
function ChapterData:getIsHaveRewardsBeforeId(chapterId)
if chapterId == MIN_CHAPTER_ID then
return false
end
local chapterBefore = MIN_CHAPTER_ID
local chapterCfg = self:getChapterCfg()
local chapterInfo = chapterCfg[chapterBefore]
while true do
if self:getIsHaveRewards(chapterBefore) then
return true
end
chapterBefore = chapterInfo.next_chapter
if chapterBefore == nil then
break
end
if chapterBefore == chapterId then
break
end
chapterInfo = chapterCfg[chapterBefore]
if chapterInfo == nil then
break
end
end
return false
end
-- 此章节之后是否有未领取的奖励
function ChapterData:getIsHaveRewardsAfterId(chapterId)
if chapterId == MIN_CHAPTER_ID then
return false
end
if chapterId == self.data.maxChapterId + 1 then -- 未开启的章节不算
return false
end
local chapterAfter = chapterId + 1
local chapterCfg = self:getChapterCfg()
local chapterInfo = chapterCfg[chapterAfter]
if chapterInfo == nil then
return false
end
while true do
if self:getIsHaveRewards(chapterAfter) then
return true
end
chapterAfter = chapterInfo.next_chapter
if chapterAfter == nil then
break
end
if chapterAfter > self.data.maxChapterId + 1 then -- 未开启的章节不算
break
end
chapterInfo = chapterCfg[chapterAfter]
if chapterInfo == nil then
break
end
end
return false
end
-- 此章节是否有可领但是未领取的奖励
function ChapterData:getIsHaveRewards(chapterId)
if chapterId > self.data.maxChapterId + 1 then
return false
end
local chapterStr = tostring(chapterId)
local list = self.data.chapterBoxInfo[chapterStr]
local count = self:getChapterBoxCount(chapterId)
if list and #list == count then -- 数量一致说明都领完了
return false
elseif chapterId < self.data.maxChapterId then
return true
end
local curMaxWave = self:getChapterMaxWave(chapterStr)
for i = 1, count do
local needWave = self:getChapterBoxNum(chapterId, i)
local rewardGot = self:getChapterBoxRewardGot(chapterStr, i)
if needWave <= curMaxWave and not rewardGot then -- 有可以领但是没有领的奖励
return true
end
end
return false
end
return ChapterData