local Mail_Controller = {} local private = {} --region -- **************** 数据部分 ******************* -- --协议定义枚举 local mail_pb = require("Proto/mail_pb") local enums_pb = require("Proto/enums_pb") --事件系统 local EventSystem = require('Event/EventSystem') --当前邮件索引 local _currentIndex = 1 --是否刷新过入口红点 local _entryRefreshed = false --是否在邮件界面内 local _inUIMailPanel = false --操作的邮件ID列表 local _operateUIDList = {} --endregion 数据部分 local _mailTimerHandle = nil --region -- **************** 数据获取 ******************* -- function Mail_Controller.GetCurrentIndex() return _currentIndex end function Mail_Controller.GetClientIndex() return Mail_Controller.GetCurrentIndex() - MDL.Mail.GetMailOffset() - 1 end --endregion 数据获取 --region -- **************** 外部事件 ******************* -- --打开邮件初始化 function Mail_Controller.InitMail() MDL.Mail.GetMailInfoRequest() end --打开邮件界面 function Mail_Controller.OpenUIMailPanel() Mail_Controller.OnOpenMail() CTL.Friend.OpenFriendPanel(CTL.Friend.MainIndex.Mail) end function Mail_Controller.OnOpenMail() MDL.Mail.HandleExpireAndEarlyMail() _currentIndex = private.FindFirstMailIndex() _inUIMailPanel = true Mail_Controller.ShowMail(Mail_Controller.GetCurrentIndex()) end --关闭界面时进行刷新 function Mail_Controller.OnMailPanelClosed() _inUIMailPanel = false EventSystem.Dispatch(EventID.UI_TownHome_RefreshMailEntry,false) end --打开邮件 function Mail_Controller.ShowMail(index) if index == 0 then --EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) return end _currentIndex = index local mail = MDL.Mail.GetMail(index) if not mail then logging.error(LTAG.ERROR, "No Mail Found in the MailList with Index:", index) return end if not mail.IsOpened then table.clear(_operateUIDList) table.insert(_operateUIDList,mail.UID) MDL.Mail.OperateMailRequest(mail_pb.Box_Mail_Open,_operateUIDList) else --EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) end end --领取邮件 function Mail_Controller.ReceiveMail() local mail = MDL.Mail.GetMail(_currentIndex) if mail == nil then logging.error(LTAG.ERROR, "No Mail Found in the MailList with Index:", _currentIndex) return end table.clear(_operateUIDList) table.insert(_operateUIDList,mail.UID) MDL.Mail.OperateMailRequest(mail_pb.Box_Mail_Receive,_operateUIDList) end --一键领取 function Mail_Controller.ReceiveAllMail() table.clear(_operateUIDList) for i = MDL.Mail.GetMailOffset() + 1, MDL.Mail.GetMailCount() do local mail = MDL.Mail.GetMail(i) if Mail_Controller.CheckUnreceivedReward(mail) then table.insert(_operateUIDList,mail.UID) end end if #_operateUIDList == 0 then CTL.Tips.AddTipsMsg(Const_IDS["IDS_AwardReceive_None"]) return end MDL.Mail.OperateMailRequest(mail_pb.Box_Mail_AllReceive,_operateUIDList) end --删除邮件 function Mail_Controller.DeleteMail() local mail = MDL.Mail.GetMail(_currentIndex) if mail == nil then logging.error(LTAG.ERROR, "No Mail Found in the MailList with Index:", _currentIndex) return end if not private.CheckCanDelete(mail) then CTL.Tips.AddTipsMsg(Const_IDS["IDS_MailDelet_Failed"]) return end table.clear(_operateUIDList) table.insert(_operateUIDList,mail.UID) MDL.Mail.OperateMailRequest(mail_pb.Box_Mail_Del,_operateUIDList) end --一键删除 function Mail_Controller.DeleteAllMail() if not Mail_Controller.CheckHasMail() then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Mail_Empty"]) return end UTL.MessageBox.ShowOKCancelBox(nil,Const_IDS.IDS_DeletMail_Confirm,private.OnDeleteConfirm,nil) end --邮件刷新 function Mail_Controller.UpdateMail() if _inUIMailPanel then Mail_Controller.ShowMail(Mail_Controller.GetCurrentIndex()) else EventSystem.Dispatch(EventID.UI_TownHome_RefreshMailEntry,true) end Mail_Controller.CheckMailExpireTime() Mail_Controller.RefreshRedPoint() end function Mail_Controller.ShowDeleteTip() CTL.Tips.AddTipsMsg(Const_IDS["IDS_Mail_DeletSuccess"]) CTL.Sound.PlayGameSound("UI_delete") end --endregion 外部事件 --region -- **************** 事件处理 ******************* -- function Mail_Controller.UpdateIndex_Add(index) if index <= _currentIndex then _currentIndex = _currentIndex + 1 end EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) end function Mail_Controller.UpdateIndex_Remove(index) if index < _currentIndex then _currentIndex = _currentIndex - 1 end if _currentIndex > MDL.Mail.GetMailCount() then _currentIndex = MDL.Mail.GetMailCount() end EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) end function Mail_Controller.TryResult_FullMail() if Mail_Controller.CheckMailFull() then CTL.Tips.AddTipsMsg(Const_IDS["IDS_FullMail_Tips"]) return end end function Mail_Controller.TryResult_UpdateMail(receiveReward,code) if code == enums_pb.Receive_Mail_Award_Faild then CTL.Tips.AddTipsMsg(Const_IDS["IDS_AwardReceive_Failed"]) elseif receiveReward then CTL.Tips.AddTipsMsg(Const_IDS["IDS_AwardReceive_Success"]) end end --endregion 事件处理 --region -- **************** 内部方法 ******************* -- function private.FindFirstMailIndex() local mailCount = MDL.Mail.GetMailCount() if mailCount == 0 then return 0 end local offset = MDL.Mail.GetMailOffset() if offset >= mailCount then return mailCount end return offset + 1 end function private.OnDeleteConfirm(buttonID) table.clear(_operateUIDList) for i = MDL.Mail.GetMailOffset() + 1, MDL.Mail.GetMailCount() do local mail = MDL.Mail.GetMail(i) if mail.IsOpened and not Mail_Controller.CheckUnreceivedReward(mail) then table.insert(_operateUIDList,mail.UID) end end if #_operateUIDList == 0 then CTL.Tips.AddTipsMsg(Const_IDS["IDS_Mail_DeletFailed"]) return end MDL.Mail.OperateMailRequest(mail_pb.Box_Mail_AllDel,_operateUIDList) end --endregion 内部方法 --region -- **************** 检测相关 ******************* -- function private.CheckCanDelete(mail) if Mail_Controller.CheckUnreceivedReward(mail) then return false end return true end function Mail_Controller.CheckUnreceivedMailExist() for i = MDL.Mail.GetMailOffset() + 1, MDL.Mail.GetMailCount() do local mail = MDL.Mail.GetMail(i) if Mail_Controller.CheckUnreceivedReward(mail) then return true end end return false end function Mail_Controller.CheckUnopenedMailExist() for i = MDL.Mail.GetMailOffset() + 1, MDL.Mail.GetMailCount() do local mail = MDL.Mail.GetMail(i) if not mail.IsOpened then return true end end return false end function Mail_Controller.CheckMailExpireTime() --检查邮件过期和将要显示的邮件 算出最小时间 启动定时器刷新 local minTime = 0 local minSendTime = 0 for i = 1, MDL.Mail.GetMailCount() do local mail = MDL.Mail.GetMail(i) if mail.ExpireTime - UTL.Time.GetServerTimestamp() > 0 then if minTime == 0 or minTime>mail.ExpireTime then minTime = mail.ExpireTime end end if mail.SendTime - UTL.Time.GetServerTimestamp() > 0 then if minSendTime == 0 or minSendTime>mail.SendTime then minSendTime = mail.SendTime end end end if _mailTimerHandle then TimerUtil.Remove(_mailTimerHandle) _mailTimerHandle = nil end if minTime == 0 then minTime = minSendTime elseif minSendTime > 0 then minTime = (minTime>minSendTime and minSendTime) or minTime end if minTime > 0 then local fun = function ( ) MDL.Mail.HandleExpireAndEarlyMail() Mail_Controller.RefreshRedPoint() EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) _mailTimerHandle = nil Mail_Controller.CheckMailExpireTime() end _mailTimerHandle = TimerUtil.Interval(fun,minTime - UTL.Time.GetServerTimestamp() + 1,1) end end function Mail_Controller.CheckUnreceivedReward(mail) return Mail_Controller.CheckHasReward(mail) and not mail.IsReceived end function Mail_Controller.CheckHasReward(mail) return mail.ItemList and #mail.ItemList > 0 end --无附件的邮件,打开即认为已领取 function Mail_Controller.CheckMailReceived(mail) if Mail_Controller.CheckHasReward(mail) == false then return mail.IsOpened end return mail.IsReceived end function Mail_Controller.CheckHasMail() return MDL.Mail.GetMailCount() - MDL.Mail.GetMailOffset() > 0 end function Mail_Controller.CheckMailFull() return MDL.Mail.GetMailCount() >= MDL.Mail.GetTotalCount() end function Mail_Controller.CheckEntryRefreshed() local refreshed = _entryRefreshed _entryRefreshed = true return refreshed end function Mail_Controller.RefreshRedPoint() if MDL.Unlock.IsUnlock(MDL.Unlock.FunctionType.Mail) == false then CTL.HomeMain.RemoveMainNotice(CTL.HomeMain.NoticeType.Mail) CTL.RedPoint.RemoveRedPoint(CTL.RedPoint.RedPointKey.Mail) else if Mail_Controller.CheckUnreceivedMailExist() or Mail_Controller.CheckUnopenedMailExist() then CTL.HomeMain.AddMainNotice(CTL.HomeMain.NoticeType.Mail) CTL.RedPoint.AddRedPoint(CTL.RedPoint.RedPointKey.Mail) else CTL.HomeMain.RemoveMainNotice(CTL.HomeMain.NoticeType.Mail) CTL.RedPoint.RemoveRedPoint(CTL.RedPoint.RedPointKey.Mail) end end end function private.OnMailUnlock(funcID, isComplete) if funcID == MDL.Unlock.FunctionType.Mail then Mail_Controller.RefreshRedPoint() if isComplete then CTL.Mail.InitMail() end end end --endregion 检测相关 --region -- **************** start&shut ******************* -- function Mail_Controller.OnStartup() EventSystem.Regist(EventID.FunctionUnlockUpdate, private.OnMailUnlock) end function Mail_Controller.OnShutdown() table.clear(_operateUIDList) end --endregion start&shut return Mail_Controller