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

86 lines
3.0 KiB
Lua

local ChapterManager = class("ChapterManager", BaseModule)
function ChapterManager:openBox(chapterId, index)
if DataManager.ChapterData:getChapterBoxRewardGot(chapterId, index) then
return
end
local parmas = {
id = chapterId,
index = index,
}
ServerDataManager:dataOperate(GConst.ServerDataConst.DATA_OP_BEHAVIOR.OPEN_CHAPTER_BOX, parmas, function(result)
if result.status == 0 then
GFunc.addRewards(result.rewards, BIReport.ITEM_GET_TYPE.CHAPTER_BOX)
GFunc.showRewardBox(result.rewards)
DataManager.ChapterData:init(result.chapterData, true)
DataManager.ChapterData:setDirty()
end
end)
end
function ChapterManager:startFight()
if not ModuleManager.FormationManager:formationIsFull() then
GFunc.showToast(I18N:getGlobalText(I18N.GlobalConst.BATTLE_DESC_8))
return
end
local cost = DataManager.ChapterData:getFightCost()
local vitCostNum = 0
if cost then
vitCostNum = GFunc.getRewardNum(cost)
end
if vitCostNum > DataManager.BagData.ItemData:getVit() then
GFunc.showItemNotEnough(GConst.ItemConst.ITEM_ID_VIT)
return
end
local parmas = {
id = DataManager.ChapterData:getChapterId()
}
ServerDataManager:dataOperate(GConst.ServerDataConst.DATA_OP_BEHAVIOR.START_FIGHT, parmas, function(result)
if result.status == 0 then
if result.cost then
GFunc.addCosts(result.cost, BIReport.ITEM_GET_TYPE.CHAPTER_FIGHT_START)
end
ModuleManager.BattleManager:playBattle(GConst.BattleConst.BATTLE_TYPE.STAGE)
end
end)
end
function ChapterManager:endFight(id, combatReport)
local cfg = ConfigManager:getConfig("chapter")[id]
local rewards = {}
for i = 1, combatReport.wave do
for _, reward in ipairs(cfg.wave_reward) do
table.insert(rewards, reward)
end
end
if combatReport.victory then
for _, reward in ipairs(cfg.finish_reward) do
table.insert(rewards, reward)
end
end
local parmas = {
id = id,
combatReport = combatReport,
rewards = GFunc.mergeRewards(rewards)
}
ServerDataManager:dataOperate(GConst.ServerDataConst.DATA_OP_BEHAVIOR.END_FIGHT, parmas, function(result)
if result.status == 0 then
if combatReport.victory and DataManager.ChapterData:getChapterId() == DataManager.ChapterData:getMaxChapterId() + 1 then
DataManager.ChapterData:goNextChapter()
end
GFunc.addRewards(result.rewards, BIReport.ITEM_GET_TYPE.CHAPTER_FIGHT_END)
local newRewards = {}
if result.rewards then
GFunc.mergeRewards2(result.rewards, newRewards)
end
ModuleManager.BattleManager:showBattleResultUI(newRewards, combatReport)
DataManager.ChapterData:init(result.chapterData, true)
DataManager.ChapterData:setDirty()
end
end)
end
return ChapterManager