local Mail_Model = {} local private = {} local enums_pb = require("Proto/enums_pb") local mail_pb = require 'Proto/mail_pb' local get_mail_pb = require "Proto/get_mail_pb" local send_email_pb = require "Proto/send_email_pb" local EventSystem = require('Event/EventSystem') --region -- **************** 数据部分 ******************* -- --当前邮件列表 local _mailList = {} --配表的邮件总数 local _totalCount --提前发出(SendTime大于当前时间)的邮件数量 local _earlyCount; --邮件类型枚举 Mail_Model.Enum_MailType = { ["Official"] = 1, ["Union"] = 2, ["Country"] =3 } Mail_Model.Enum_SendMailType = { JPRechargeRebate = send_email_pb.EmailJPRechargeRebate, } local GMSMailTag = "gm" --GMS 邮件标识 --endregion 数据部分 function Mail_Model.OnStartup() NetManager.RegisterMsgCB(MessageEnums.MessageGetMailBoxInfo,private.GetMailInfoResponse) NetManager.RegisterMsgCB(MessageEnums.MessageOperateMailBox,private.OperateMailResponse) NetManager.RegisterMsgCB(MessageEnums.MessagePushMailNotify,private.MailNotifyPushResponse) NetManager.RegisterMsgCB(MessageEnums.MessagePushDelMailNotify,private.MailDeleteNotifyPushResponse) NetManager.RegisterMsgCB(MessageEnums.MessageSendEmail,private.OnSendEmailResp) end --region -- **************** 数据获取 ******************* -- function Mail_Model.GetMail(index) return _mailList[index] end function Mail_Model.GetMailCount() return #_mailList end function Mail_Model.GetTotalCount() if not _totalCount then _totalCount = DataSheetManager.ConstData:GetDataByID("MaxMailClient").IntValue end return _totalCount end function Mail_Model.GetEarlyCount() _earlyCount = 0 local timestamp = UTL.Time.GetServerTimestamp() for i=1,#_mailList do local sendTime = _mailList[i].SendTime if sendTime > timestamp then _earlyCount = _earlyCount + 1 end end return _earlyCount end function Mail_Model.GetMailClientCount() local totalCount = Mail_Model.GetTotalCount() local showCount = Mail_Model.GetMailCount() - Mail_Model.GetEarlyCount() if showCount >= totalCount then return totalCount else return showCount end end --这个offset依赖了邮件的排序,sendTime大的在前,主要用来过滤未来邮件 function Mail_Model.GetMailOffset() --当没有未来邮件时,不用返回offset local earlyCount = Mail_Model.GetEarlyCount() if earlyCount == 0 then return 0 end local mailCount = Mail_Model.GetMailCount() local totalCount = Mail_Model.GetTotalCount() local offset = 0 if mailCount - totalCount > earlyCount then offset = mailCount - totalCount else offset = earlyCount end return offset end --endregion 数据获取 --region -- **************** 数据操作 ******************* -- function private.SetMailData(mail,mailInfo) if not mail.UID then mail.UID = mailInfo.uid mail.MailID = mailInfo.cfgId mail.SendTime = mailInfo.sendTime mail.Type = mailInfo.tag local has, mailData = DataSheetManager.MailData:TryGetDataByID(mail.MailID) if not has then mail.Title = UTL.String.GetMessageStr(mailInfo.titleParams) mail.Content = private.GetMailMsgType(mailInfo.contentParams) if mail.Type == "country" then --国家 mail.Type = Mail_Model.Enum_MailType.Country else --势力 mail.Type = Mail_Model.Enum_MailType.Union end else --官方 mail.Title = UTL.String.GetMessageFormatStr(mailData.Title,mailInfo.titleParams) mail.Content = private.GetMailMsgFormatType(mailData.Content,mailInfo.contentParams) mail.Type = Mail_Model.Enum_MailType.Official end if mailInfo.itemList then mail.ItemList = {} for i = 1, #mailInfo.itemList do table.insert(mail.ItemList,mailInfo.itemList[i]) end end end mail.ExpireTime = mailInfo.expireTime mail.IsOpened = mailInfo.isOpen or mailInfo.isReceive mail.IsReceived = mailInfo.isReceive mail.tag = mailInfo.tag end function private.GetMailMsgFormatType(strFormat,params) if params and #params>0 then local strParams = {} for i, v in ipairs(params) do table.insert(strParams, UTL.String.GetShowStrByParamInfo(v)) end local str = string.FormatByIdsId(Const_IDS[strFormat], unpack(strParams)) return str end return UIUtils.GetLocalizedTxt(Const_IDS[strFormat]) end function private.GetMailMsgType(params) if params and #params>0 then for i, v in ipairs(params) do return UTL.String.GetShowStrByParamInfo(v) end end end --处理邮件的更新 function private.HandleUpdateMail(mailUpd,code) if not mailUpd then return end local receiveReward = false --判断是否是接受邮件奖励 for i = 1, #mailUpd.mailList do local mail local mailInfo = mailUpd.mailList[i] for mailIndex, mailValue in ipairs(_mailList) do if mailValue.UID == mailInfo.uid then mail = mailValue break end end if mail then if not receiveReward and mail.IsReceived ~= mailInfo.isReceive then receiveReward = true --改变了received状态说明有接收邮件奖励的信息 end private.SetMailData(mail,mailInfo) end end CTL.Mail.TryResult_UpdateMail(receiveReward,code) end --处理邮件的删除 function private.HandleDeleteMail(mailDel) if not mailDel then return end if #mailDel.keys <= 0 then return end for i = 1, #mailDel.keys do local index local deleteUID = mailDel.keys[i] for mailIndex, mailValue in ipairs(_mailList) do if mailValue.UID == deleteUID then index = mailIndex break end end if index then private.RemoveMail(index) end end CTL.Mail.ShowDeleteTip() end --处理本地删除过期邮件 function Mail_Model.HandleExpireAndEarlyMail() _earlyCount = 0 local timestamp = UTL.Time.GetServerTimestamp() for i = #_mailList, 1,-1 do local expireTime = _mailList[i].ExpireTime local sendTime = _mailList[i].SendTime if expireTime < timestamp and expireTime > 0 then private.RemoveMail(i) elseif timestamp < sendTime then _earlyCount = _earlyCount + 1 end end end function private.AddMail(mailInfo) if private.CheckMailVersion(mailInfo.version) == false then return end local mail = {} private.SetMailData(mail,mailInfo) local timestamp = UTL.Time.GetServerTimestamp() local index = 1 for i, v in ipairs(_mailList) do local sendTime = v.SendTime if mail.SendTime < sendTime then index = index + 1 end end if mail.SendTime > timestamp then _earlyCount = _earlyCount + 1 end local isExist = false for i,v in ipairs(_mailList) do if v.UID == mail.UID then _mailList[i] = mail isExist = true break end end if not isExist then table.insert(_mailList,mail) private.SortMails() end CTL.Mail.UpdateIndex_Add(index) CTL.Mail.TryResult_FullMail() end function private.RemoveMail(index) table.remove(_mailList,index) CTL.Mail.UpdateIndex_Remove(index) CTL.Mail.TryResult_FullMail() end function private.SortMails() private.currentTime = UTL.Time.GetServerTimestamp() table.sort(_mailList,private.SortBySendGMSAndTime) end --未阅读的GMS邮件,位置置顶,其他情况按发送事件排序 function private.SortBySendGMSAndTime(a,b) --未来邮件排在前面,防止显示出错 local scoreATime = a.SendTime > private.currentTime and 1 or 0 local scoreBTime = b.SendTime > private.currentTime and 1 or 0 if scoreATime ~= scoreBTime then return scoreATime > scoreBTime end --未读的GMS邮件 排序分为1,按分排序 local scoreA = (a.tag == GMSMailTag and not a.IsOpened) and 1 or 0 local scoreB = (b.tag == GMSMailTag and not b.IsOpened) and 1 or 0 if scoreA ~= scoreB then return scoreA > scoreB end return a.SendTime > b.SendTime end --endregion 数据操作 --region -- **************** 协议部分 ******************* -- function Mail_Model.GetMailInfoRequest() local msg = get_mail_pb.GetMailBoxInfoReq() logging.log(LTAG.LOG, MessageEnums.MessageGetMailBoxInfo) NetManager.SendMessage(MessageEnums.MessageGetMailBoxInfo, msg) end --邮件版本与母包版本号不一致,不显示该邮件 function private.CheckMailVersion(version) if not string.isNilOrEmpty(version) and version ~= AppInfo.marketVersion then return false end return true end function private.GetMailInfoResponse(msgResp) if msgResp.Resp.Code == 0 then _mailList = {} local mailInfos = msgResp.mailBoxInfo for i = 1, #mailInfos.mailList do local mailInfo = mailInfos.mailList[i] if private.CheckMailVersion(mailInfo.version) then local mail = {} private.SetMailData(mail, mailInfo) local isExist = false for i,v in ipairs(_mailList) do if v.UID == mail.UID then _mailList[i] = mail isExist = true break end end if not isExist then table.insert(_mailList,mail) end end end private.SortMails() CTL.Mail.RefreshRedPoint() CTL.Mail.CheckMailExpireTime() --CTL.Mail.UpdateMail(true) end end function Mail_Model.OperateMailRequest(optType,keys) logging.log(LTAG.LOG, "MessageOperateMailBoxReq", 16) local msg = get_mail_pb.OperateMailBoxReq() msg.operType = optType for i, v in ipairs(keys) do table.insert(msg.mailKeys,v) end logging.log(LTAG.LOG, MessageEnums.MessageOperateMailBox) NetManager.SendMessage(MessageEnums.MessageOperateMailBox, msg) end function private.OperateMailResponse(msgResp) logging.log(LTAG.LOG, "MessageOperateMailBoxResp", msgResp) if msgResp.Resp.Code == 0 then local playerChange = msgResp.Resp.PlayerChange if not playerChange then return end private.HandleDeleteMail(playerChange.MailDel) private.HandleUpdateMail(playerChange.MailAddUpdate,msgResp.Resp.Code) -- CTL.Mail.ShowMail(CTL.Mail.GetCurrentIndex()) CTL.Mail.RefreshRedPoint() EventSystem.Dispatch(EventID.UI_Mail_RefreshMail) end end function Mail_Model.MailNotifyPushRegister() --PushMailNotifyManager.PushMailNotifyRegister(private.MailNotifyPushResponse) end function private.MailNotifyPushResponse(msgResp) logging.log(LTAG.LOG, "MessagePushMailNotifyResp", msgResp) Mail_Model.HandleExpireAndEarlyMail() private.AddMail(msgResp.mail) CTL.Mail.UpdateMail() end function Mail_Model.MailDeleteNotifyPushRegister() --PushDelMailNotifyManager.PushDelMailNotifyRegister(private.MailDeleteNotifyPushResponse) end function private.MailDeleteNotifyPushResponse(msgResp) logging.log(LTAG.LOG, "MessagePushDelMailNotifyResp", msgResp) for i = 1, #msgResp.UID do local index local deleteUID = msgResp.UID[i] for mailIndex, mailValue in ipairs(_mailList) do if mailValue.UID == deleteUID then index = mailIndex break end end if index then private.RemoveMail(index) end end CTL.Mail.UpdateMail() end --发送内容到玩家的真实邮箱内,内容由服务器拼接,客户端只负责传参 --toEmails发送邮箱, ccEmails抄送邮箱, function Mail_Model.SendEmailReq(mailType, toEmails, ccEmails, mailBodyParams) if toEmails == nil or #toEmails == 0 then CTL.Tips.AddTipsMsg(Const_IDS["IDS_JP_RechargeRebate_Input"]) return end local msg = send_email_pb.SendEmailReq() msg.EmailType = mailType for i, v in ipairs(toEmails) do table.insert(msg.ToEmail, v) end if ccEmails then for i, v in ipairs(ccEmails) do table.insert(msg.CCEmail, v) end end if mailBodyParams then for i, v in ipairs(mailBodyParams) do table.insert(msg.EmailBody, v) end end NetManager.SendMessage(MessageEnums.MessageSendEmail, msg) end function private.OnSendEmailResp(msgResp) if msgResp.Resp.Code == 0 then CTL.Tips.AddTipsMsg(Const_IDS["IDS_JP_RechargeRebate_SendMailSuccess"]) end end --endregion 协议部分 return Mail_Model