local Biography_Model = {}
local private = {}
local EventSystem = require('Event/EventSystem')
local sweep_pb = require 'Proto.sweep_pb'
local RED_STR = "%d/%d"
local NONE_RED_STR = "%d/%d"
private.hasNums = 0
private.officialPosNums = 0
function Biography_Model.OnStartup()
NetManager.RegisterMsgCB(MessageEnums.MessageSweepBiography, private.SweepBiographyResp)
NetManager.RegisterMsgCB(MessageEnums.MessageOneKeySweepBiography, nil)
NetManager.RegisterMsgCB(MessageEnums.MessageSetSweepBiographyConfig, private.SetSweepBiographyConfigResp)
NetManager.RegisterMsgCB(MessageEnums.MessageGetSweepBiographyConfig, private.GetSweepBiographyConfigResp)
EventSystem.Regist(EventID.UI_RefreshOfficialPositionInfo, private.OnOfficialPositionUpdate)
private.OneKeySweepSetting = nil -- 一键扫荡设置
end
--region -- **************** 数据部分 ******************* --
--需要监听官职变化,修改挑战次数
function private.OnOfficialPositionUpdate()
local newNums = CTL.Activities.GetLeftTimesByType(LuaConst.ActivityType.HeroBiography, private.hasNums, private.officialPosNums)
if newNums ~= private.enterNums then
private.enterNums = newNums
EventSystem.Dispatch(EventID.UI_PurchaseNumsSuccess)
end
end
function Biography_Model.SetBiographyInfoMsg(biographyInfos)
if not IsServerDataNotNil(biographyInfos) then
return
end
private.biographyInfo = {}
private.biographyLevelInfo = {}
--这个次数的含义变了,原来表示副本剩余次数,现在更改为已经挑战的次数
--不影响原来调用的逻辑,在更新源数据的这里把字段改成原来的含义
private.purchaseNums = biographyInfos.purchaseNums
private.hasNums = biographyInfos.enterNums
private.officialPosNums = biographyInfos.freeUsedNums
private.enterNums = CTL.Activities.GetLeftTimesByType(LuaConst.ActivityType.HeroBiography, private.hasNums, private.officialPosNums)
private.unlockedBiographyLevelId = biographyInfos.unlockedHeroBiographyLevelId
private.unlockedBiographyId = biographyInfos.unlockedHeroBiographyId
private.DealBiographyInfoMsg(biographyInfos)
private.DealBiographyLevelInfoMsg(biographyInfos)
EventSystem.Dispatch(EventID.UI_HeroBiography_UpdateInfo)
end
function private.DealBiographyLevelInfoMsg(biographyInfos)
for i = 1, #biographyInfos.heroBiographyLevelInfo do
local isUpdate = false
for j = 1, #private.biographyLevelInfo do
if biographyInfos.heroBiographyLevelInfo[i].levelId == private.biographyLevelInfo[j].levelId then
private.biographyLevelInfo[j].star = {}
for i, v in ipairs(biographyInfos.heroBiographyLevelInfo[i].star) do
private.biographyLevelInfo[j].star[i] = v
end
isUpdate = true
end
end
if not isUpdate then
local starTable = {}
for i, v in ipairs(biographyInfos.heroBiographyLevelInfo[i].star) do
starTable[i] = v
end
table.insert(private.biographyLevelInfo, {
levelId = biographyInfos.heroBiographyLevelInfo[i].levelId,
star = starTable
})
end
end
end
function private.DealBiographyInfoMsg(biographyInfos)
for i = 1, #biographyInfos.heroBiographyInfo do
table.insert(private.biographyInfo, {
heroBiographyId = biographyInfos.heroBiographyInfo[i].heroBiographyId,
dailyCount = biographyInfos.heroBiographyInfo[i].dailyCount,
})
end
end
function Biography_Model.GetBiographyInfo()
return private.biographyInfo
end
function Biography_Model.GetUnlockBiographyId()
return private.unlockedBiographyId
end
function Biography_Model.GetUnlockBiographyLevelId()
return private.unlockedBiographyLevelId
end
function Biography_Model.GetBiographyLevelDataById(levelId)
local levelInfos = private.biographyLevelInfo
if levelInfos then
for i, v in ipairs(levelInfos) do
if v.levelId == levelId then
return v.star
end
end
end
return {}
end
function Biography_Model.GetBiographyLevelStarNumById(levelId)
local starNum = 0
local starData = Biography_Model.GetBiographyLevelDataById(levelId)
for i, v in ipairs(starData) do
starNum = starNum + v
end
return starNum
end
function Biography_Model.GetLastestSweepIndex(biographyId)
local levelId
for i, v in DataSheetManager.HeroBiographyLevel:GetEnumerator() do
if v.BelongToBiography == biographyId then
local levelInfo = Biography_Model.GetBiographyLevelDataById(v.ID)
if #levelInfo ~= 0 then
levelId = v.ID
end
end
end
return levelId
end
function Biography_Model.GetBiographyDailyCountShowById(id)
local dailyCount = 0
local biographyInfo = private.biographyInfo
if biographyInfo then
for i, v in ipairs(biographyInfo) do
if v.heroBiographyId == id then
dailyCount = v.dailyCount or 0
end
end
end
local biographyData = DataSheetManager.HeroBiographyData:GetDataByID(id)
local dailyMaxCount = biographyData.DailyLimit
local leftCount = dailyMaxCount - dailyCount
if leftCount <= 0 then
return string.format(RED_STR, 0, dailyMaxCount)
end
return string.format(NONE_RED_STR, leftCount, dailyMaxCount)
end
function Biography_Model.GetBiographyIfDailyCountLeft(id)
local dailyCount = nil
local biographyInfo = private.biographyInfo
if biographyInfo then
for i, v in ipairs(biographyInfo) do
if v.heroBiographyId == id then
dailyCount = v.dailyCount or 0
end
end
end
if dailyCount == nil then
return true
end
local biographyData = DataSheetManager.HeroBiographyData:GetDataByID(id)
local dailyMaxCount = biographyData.DailyLimit
return dailyMaxCount - dailyCount > 0
end
function Biography_Model.GetBiographyDataById(id)
local biographyInfo = private.biographyInfo
if biographyInfo then
for i, v in ipairs(biographyInfo) do
if v.heroBiographyId == id then
return v.isGot
end
end
else
return 0
end
end
function Biography_Model.GetHeroBiographyStarNums(id)
local totalStarNums = 0
for i, v in DataSheetManager.HeroBiographyLevel:GetEnumerator() do
if v.BelongToBiography == id then
totalStarNums = totalStarNums + Biography_Model.GetBiographyLevelStarNumById(v.ID)
end
end
return totalStarNums
end
function Biography_Model.GetPurchaseNums()
return private.purchaseNums or 0
end
function Biography_Model.GetEnterNums()
return private.enterNums
end
--region 一键扫荡数据
function Biography_Model.GetAutoSweepSettingInfo()
return private.OneKeySweepSetting
end
-- 获取一键扫荡已设置level数
function Biography_Model.GetSweepSettledLevelCount()
local count = 0
if private.OneKeySweepSetting then
for i, v in ipairs(private.OneKeySweepSetting.levelId) do
if v > 0 then count = count + 1 end
end
end
return count
end
function private.IsVaildOneKeySweepSetting()
if not private.OneKeySweepSetting or Biography_Model.GetSweepSettledLevelCount() < 5 then
return false, Const_IDS["IDS_HeroBiography_Sweep11"]
end
local leftTime = Biography_Model.GetEnterNums()
-- 有免费使用次数但是额外购买的次数费用不足
local additionalBuyCount = MDL.Purchase.GetAvailablePurchaseTimes(LuaConst.ActivityType.HeroBiography)
if leftTime + additionalBuyCount <= 0 then
return false, Const_IDS["IDS_HeroBiography_Sweep14"]
end
return true
end
--endregion
--endregion 数据部分
--region -- **************** 协议部分 ******************* --
function Biography_Model.EnterHeroBiographyRequest(biographyId, id)
if CTL.Biography.IsCanEnterSingleBiography(biographyId, id) then
--进关之前存储一个列传章节ID,方便退出关卡的时候再次打开界面使用
local data = DataSheetManager.HeroBiographyLevel:GetDataByID(id)
if data then
CTL.Biography.SetTempBiographyId(data.BelongToBiography)
CTL.Biography.SetTempBiographyLevelId(id)
MDL.LevelSelect.EnterFuben(LuaConst.LevelType.Biography, id, nil, data.MissonDataID)
end
else
if MDL.Biography.GetEnterNums() == 0 then
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_NoNum"])
elseif Biography_Model.GetBiographyIfDailyCountLeft(biographyId) == false then
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_Limit"])
end
end
end
function Biography_Model.SweepBiographyReq(biographyId, levelId, times)
if MDL.Biography.GetEnterNums() == 0 then
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_NoNum"])
return
elseif Biography_Model.GetBiographyIfDailyCountLeft(biographyId) == false then
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_Limit"])
return
end
if MDL.Biography.GetBiographyLevelStarNumById(levelId) > 0 then
if MDL.Biography.GetEnterNums() >= times then
local msg = sweep_pb.SweepBiographyReq()
msg.FubenId =levelId
msg.SweepTimes = times
NetManager.SendMessage(MessageEnums.MessageSweepBiography, msg)
else
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_NoNum"])
end
else
CTL.Tips.AddTipsMsg(Const_IDS["IDS_HeroBiography_NotClear"])
end
end
function private.SweepBiographyResp(msgResp)
if msgResp.Resp.Code == 0 then
EventSystem.Dispatch(EventID.UI_HeroBiography_FinishSweep)
end
end
--- 名将列传一键扫荡
function Biography_Model.OneKeySweepBiographyReq()
local msg = sweep_pb.OneKeySweepBiographyReq()
local isVaildSetting, tipMsg = private.IsVaildOneKeySweepSetting()
if not isVaildSetting then
CTL.Tips.AddTipsMsg(tipMsg)
return
end
NetManager.SendMessage(MessageEnums.MessageOneKeySweepBiography, msg)
end
--- 名将列传设置一键扫荡
function Biography_Model.SetSweepBiographyConfigReq(sweepList, buyCount)
local msg = sweep_pb.SetSweepBiographyConfigReq()
for i, v in ipairs(sweepList) do
msg.levelId:append(v)
end
msg.buyNums = buyCount or 0
NetManager.SendMessage(MessageEnums.MessageSetSweepBiographyConfig, msg)
end
function private.SetSweepBiographyConfigResp(msg)
if msg.Resp.Code == 0 then
for i, v in ipairs(msg.levelId) do
private.OneKeySweepSetting.levelId[i] = v
private.OneKeySweepSetting.levelId = msg.levelId
end
private.OneKeySweepSetting.buyNums = msg.buyNums
CTL.Tips.AddTipsMsg(Const_IDS.IDS_HeroBiography_Sweep10)
end
end
--- 名将列传获取一键扫荡设置
function Biography_Model.GetSweepBiographyConfigReq()
local msg = sweep_pb.GetSweepBiographyConfigReq()
NetManager.SendMessage(MessageEnums.MessageGetSweepBiographyConfig, msg)
end
function private.GetSweepBiographyConfigResp(msg)
if msg.Resp.Code == 0 then
if not IsProtoDataNotNil(private.OneKeySweepSetting) or not IsProtoDataNotNil(private.OneKeySweepSetting.levelId) then
private.OneKeySweepSetting = {}
private.OneKeySweepSetting.levelId = { 0, 0, 0, 0, 0 }
end
if IsProtoDataNotNil(msg.levelId) then
for i, v in ipairs(msg.levelId) do
private.OneKeySweepSetting.levelId[i] = v
end
end
private.OneKeySweepSetting.buyNums = msg.buyNums
EventSystem.Dispatch(EventID.UI_HeroBiography_OneKeySweepRefresh)
end
end
--endregion 协议部分
return Biography_Model