337 lines
12 KiB
Lua
337 lines
12 KiB
Lua
|
------------------------------------------------
|
|||
|
-- 作者: xihan
|
|||
|
-- 日期: 2020-01-13
|
|||
|
-- 文件: MailSystem.lua
|
|||
|
-- 模块: MailSystem
|
|||
|
-- 描述: 邮件系统
|
|||
|
------------------------------------------------
|
|||
|
local MailSystem = {
|
|||
|
-- 最大数量
|
|||
|
MaxMailCount = 0,
|
|||
|
-- 剩余未返给的邮件数量,如果没有此值为0即可
|
|||
|
RemainMailNum = 0,
|
|||
|
-- 当前读取的邮件ID
|
|||
|
CurReadMailId = -1,
|
|||
|
-- 文字缓存
|
|||
|
ContentList = List:New(),
|
|||
|
-- 所有邮件(摘要)
|
|||
|
AllMails = Dictionary:New(),
|
|||
|
-- 缓存邮件详细信息
|
|||
|
DetailInfos = Dictionary:New(),
|
|||
|
-- 服务器发来到邮件列表
|
|||
|
MsgMailList = {},
|
|||
|
-- MSG 读邮件
|
|||
|
ReqReadMail = nil,
|
|||
|
-- MSG 领取附件内容
|
|||
|
ReqReceiveSingleMailAttach = nil,
|
|||
|
-- MSG 一键领取所有邮件的附件
|
|||
|
ReqOneClickReceiveMailAttach = nil,
|
|||
|
-- MSG 一键删除所有没有附件的邮件
|
|||
|
ReqOneClickDeleteMail = nil
|
|||
|
}
|
|||
|
|
|||
|
function MailSystem:Initialize()
|
|||
|
self.MaxMailCount = tonumber(DataConfig.DataGlobal[33].Params)
|
|||
|
end
|
|||
|
|
|||
|
function MailSystem:UnInitialize()
|
|||
|
self.AllMails:Clear();
|
|||
|
self.DetailInfos:Clear();
|
|||
|
self.RemainMailNum = 0;
|
|||
|
self.CurReadMailId = -1;
|
|||
|
self.MsgMailList = {}
|
|||
|
end
|
|||
|
|
|||
|
-- 刷新红点
|
|||
|
function MailSystem:RefreshRepoint()
|
|||
|
GameCenter.MainFunctionSystem:SetAlertFlag(FunctionStartIdCode.Mail, self:GetMailNumPrompt() > 0)
|
|||
|
end
|
|||
|
|
|||
|
-- 排序
|
|||
|
function MailSystem:SortMailList()
|
|||
|
-- 1.未读,2.时间大
|
|||
|
local _keys = self.AllMails:GetKeys();
|
|||
|
table.sort(_keys, function(a, b)
|
|||
|
local _mailA = self.AllMails[a];
|
|||
|
local _mailB = self.AllMails[b];
|
|||
|
if _mailA.isRead == _mailB.isRead then
|
|||
|
local _isGetA = _mailA.hasAttachment and not _mailA.isAttachReceived;
|
|||
|
local _isGetB = _mailB.hasAttachment and not _mailB.isAttachReceived;
|
|||
|
if _isGetA == _isGetB then
|
|||
|
return self.AllMails[a].receiveTime > self.AllMails[b].receiveTime;
|
|||
|
else
|
|||
|
return _isGetA;
|
|||
|
end
|
|||
|
end
|
|||
|
return _mailB.isRead;
|
|||
|
end)
|
|||
|
end
|
|||
|
|
|||
|
-- 获取邮件提示数量
|
|||
|
function MailSystem:GetMailNumPrompt()
|
|||
|
-- 未读的、已读有附件未领取到、服务器存储未发送的
|
|||
|
local _allMails = self.AllMails;
|
|||
|
local _keys = _allMails:GetKeys();
|
|||
|
local _tipsCount = 0;
|
|||
|
if #_keys > 0 then
|
|||
|
for i = 1, #_keys do
|
|||
|
local _mail = _allMails[_keys[i]];
|
|||
|
if not _mail.isRead or (_mail.hasAttachment and not _mail.isAttachReceived) then
|
|||
|
_tipsCount = _tipsCount + 1;
|
|||
|
end
|
|||
|
end
|
|||
|
end
|
|||
|
local _cnt = _tipsCount + self.RemainMailNum;
|
|||
|
_cnt = _cnt > 999 and 999 or _cnt;
|
|||
|
return _cnt;
|
|||
|
end
|
|||
|
|
|||
|
-- MSG
|
|||
|
-- 请求读取单封邮件
|
|||
|
function MailSystem:ReqReadSingleMail(id)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
if not self.ReqReadMail then
|
|||
|
self.ReqReadMail = ReqMsg.MSG_Mail.ReqReadMail:New()
|
|||
|
end
|
|||
|
self.ReqReadMail.mailId = id
|
|||
|
self.ReqReadMail:Send()
|
|||
|
end
|
|||
|
|
|||
|
-- 请求获取当前阅读的邮件到奖励
|
|||
|
function MailSystem:ReqGetRewardByCurRead()
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
self:ReqReceiveSingleMail(self.CurReadMailId)
|
|||
|
end
|
|||
|
|
|||
|
-- 请求领取单封邮件的奖励
|
|||
|
function MailSystem:ReqReceiveSingleMail(id)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
if not self.ReqReceiveSingleMailAttach then
|
|||
|
self.ReqReceiveSingleMailAttach = ReqMsg.MSG_Mail.ReqReceiveSingleMailAttach:New();
|
|||
|
end
|
|||
|
self.ReqReceiveSingleMailAttach.mailId = id;
|
|||
|
self.ReqReceiveSingleMailAttach:Send();
|
|||
|
end
|
|||
|
|
|||
|
-- 请求一键领取奖励
|
|||
|
function MailSystem:ReqGetAllReward()
|
|||
|
if not self.ReqOneClickReceiveMailAttach then
|
|||
|
self.ReqOneClickReceiveMailAttach = ReqMsg.MSG_Mail.ReqOneClickReceiveMailAttach:New();
|
|||
|
end
|
|||
|
local _mailIds = self.ReqOneClickReceiveMailAttach.mailIdList;
|
|||
|
local _allMails = self.AllMails;
|
|||
|
local _keys = _allMails:GetKeys();
|
|||
|
for i = 1, #_keys do
|
|||
|
local _mail = _allMails[_keys[i]];
|
|||
|
if _mail.hasAttachment and not _mail.isAttachReceived then
|
|||
|
_mailIds:Add(_mail.mailId);
|
|||
|
end
|
|||
|
end
|
|||
|
|
|||
|
if #_mailIds <= 0 then
|
|||
|
Utils.ShowPromptByEnum("NoRewardMail")
|
|||
|
return false;
|
|||
|
end
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
self.ReqOneClickReceiveMailAttach:Send();
|
|||
|
_mailIds:Clear();
|
|||
|
return true;
|
|||
|
end
|
|||
|
|
|||
|
-- 请求删除当前阅读的邮件
|
|||
|
function MailSystem:ReqDeleteByCurRead()
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
self:ReqDeleteMaill(self.CurReadMailId)
|
|||
|
end
|
|||
|
|
|||
|
-- 请求删除邮件
|
|||
|
function MailSystem:ReqDeleteMaill(id)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
if not self.ReqOneClickDeleteMail then
|
|||
|
self.ReqOneClickDeleteMail = ReqMsg.MSG_Mail.ReqOneClickDeleteMail:New()
|
|||
|
end
|
|||
|
self.ReqOneClickDeleteMail.mailIdList:Add(id);
|
|||
|
self.ReqOneClickDeleteMail:Send()
|
|||
|
self.ReqOneClickDeleteMail.mailIdList:Clear();
|
|||
|
end
|
|||
|
|
|||
|
-- 请求一键删除邮件
|
|||
|
function MailSystem:ReqDeleteAllMail()
|
|||
|
if not self.ReqOneClickDeleteMail then
|
|||
|
self.ReqOneClickDeleteMail = ReqMsg.MSG_Mail.ReqOneClickDeleteMail:New()
|
|||
|
end
|
|||
|
local _mailIds = self.ReqOneClickDeleteMail.mailIdList;
|
|||
|
local _allMails = self.AllMails;
|
|||
|
local _keys = _allMails:GetKeys();
|
|||
|
for i = 1, #_keys do
|
|||
|
local _mail = _allMails[_keys[i]];
|
|||
|
if _mail.isRead and (not _mail.hasAttachment or (_mail.hasAttachment and _mail.isAttachReceived)) then
|
|||
|
_mailIds:Add(_mail.mailId);
|
|||
|
end
|
|||
|
end
|
|||
|
|
|||
|
if #_mailIds <= 0 then
|
|||
|
Utils.ShowPromptByEnum("NoDelateMail")
|
|||
|
return
|
|||
|
end
|
|||
|
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_OPEN);
|
|||
|
self.ReqOneClickDeleteMail:Send()
|
|||
|
_mailIds:Clear();
|
|||
|
end
|
|||
|
|
|||
|
-- 返给请求读取邮件
|
|||
|
-- required MailDetailInfo mailDetailInfo = 1; //所读取邮件的详细信息
|
|||
|
function MailSystem:ResReadMail(msg)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
|
|||
|
local _mailDetailInfo = msg.mailDetailInfo;
|
|||
|
if not _mailDetailInfo then
|
|||
|
return;
|
|||
|
end
|
|||
|
|
|||
|
local _mailId = _mailDetailInfo.mailId;
|
|||
|
if _mailId then
|
|||
|
local _mailInfo = self.AllMails[_mailId];
|
|||
|
_mailInfo.isRead = true;
|
|||
|
self.CurReadMailId = _mailId;
|
|||
|
if self.DetailInfos:ContainsKey(_mailId) then
|
|||
|
self.DetailInfos[_mailId] = _mailDetailInfo;
|
|||
|
else
|
|||
|
self.DetailInfos:Add(_mailId, _mailDetailInfo);
|
|||
|
end
|
|||
|
|
|||
|
local _isReadTable = _mailDetailInfo.readTable;
|
|||
|
local _GetByKeyFunc = DataConfig.DataMessageString.GetByKey;
|
|||
|
local _mailTitle = _mailDetailInfo.mailTitle;
|
|||
|
_mailDetailInfo.mailTitle = _isReadTable and _GetByKeyFunc(tonumber(_mailTitle)) or _mailTitle;
|
|||
|
_mailDetailInfo.sender = _isReadTable and _GetByKeyFunc(tonumber(_mailDetailInfo.sender)) or _mailDetailInfo.sender;
|
|||
|
if _isReadTable then
|
|||
|
_mailDetailInfo.mailContent = self:CetContent(_mailDetailInfo.mailContent, _mailDetailInfo.paramlists);
|
|||
|
end
|
|||
|
-- 刷新界面
|
|||
|
GameCenter.PushFixEvent(UILuaEventDefine.UIMailRefreshChangeMail);
|
|||
|
-- 刷新小红点
|
|||
|
self:RefreshRepoint();
|
|||
|
-- 刷新界面数量
|
|||
|
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_MAIL_MAILNUM_PROMPT);
|
|||
|
end
|
|||
|
end
|
|||
|
|
|||
|
-- 返给领取单封邮件的附件物品
|
|||
|
-- required uint64 mailId = 1; //所领取附件的邮件Id
|
|||
|
-- required bool isAttachReceived = 2; //附件是否领取成功
|
|||
|
function MailSystem:ResReceiveSingleMailAttach(msg)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
|
|||
|
if msg.isAttachReceived then
|
|||
|
self.AllMails[msg.mailId].isAttachReceived = true;
|
|||
|
self.DetailInfos[msg.mailId].isAttachReceived = true;
|
|||
|
else
|
|||
|
Utils.ShowPromptByEnum("GetMailRewardFail")
|
|||
|
end
|
|||
|
-- 刷新界面
|
|||
|
GameCenter.PushFixEvent(UILuaEventDefine.UIMailRefreshChangeMail);
|
|||
|
-- 刷新小红点
|
|||
|
self:RefreshRepoint();
|
|||
|
-- 刷新界面数量
|
|||
|
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_MAIL_MAILNUM_PROMPT);
|
|||
|
end
|
|||
|
|
|||
|
-- 返给客户端玩家邮件列表数据(登录、一键领取和删除后的同步需要发送此消息)
|
|||
|
-- repeated MailSummaryInfo mailList = 1; //返给的邮件列表
|
|||
|
-- required int32 remainMailNum = 2; //剩余未返给的邮件数量,如果没有此值为0即可
|
|||
|
function MailSystem:ResMailInfoList(msg)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
|
|||
|
if not msg.mailList then
|
|||
|
msg.mailList = {}
|
|||
|
end
|
|||
|
|
|||
|
self.RemainMailNum = msg.remainMailNum;
|
|||
|
self.AllMails:Clear();
|
|||
|
self.DetailInfos:Clear();
|
|||
|
|
|||
|
self.MsgMailList = msg.mailList;
|
|||
|
local _mailList = msg.mailList;
|
|||
|
for i = 1, #_mailList do
|
|||
|
local _mailMsgData = _mailList[i];
|
|||
|
local _GetByKeyFunc = DataConfig.DataMessageString.GetByKey;
|
|||
|
_mailMsgData.mailTitle = _mailMsgData.readTable and _GetByKeyFunc(tonumber(_mailMsgData.mailTitle)) or _mailMsgData.mailTitle;
|
|||
|
self.AllMails:Add(_mailList[i].mailId, _mailMsgData);
|
|||
|
end
|
|||
|
|
|||
|
-- 排序
|
|||
|
self:SortMailList();
|
|||
|
local _keys = self.AllMails:GetKeys();
|
|||
|
self.CurReadMailId = #_keys > 0 and self.AllMails[_keys[1]].mailId or -1;
|
|||
|
-- 刷新界面
|
|||
|
GameCenter.PushFixEvent(UILuaEventDefine.UIMailRefreshUI);
|
|||
|
-- 刷新小红点
|
|||
|
self:RefreshRepoint();
|
|||
|
-- 刷新界面数量
|
|||
|
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_MAIL_MAILNUM_PROMPT);
|
|||
|
end
|
|||
|
|
|||
|
-- 新邮件通知
|
|||
|
-- required MailSummaryInfo newMail = 1; //新邮件摘要信息
|
|||
|
function MailSystem:ResNewMail(msg)
|
|||
|
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
|
|||
|
if not msg.newMail then
|
|||
|
return
|
|||
|
end
|
|||
|
-- 超过最大数量
|
|||
|
Utils.ShowPromptByEnum("GetNewMail")
|
|||
|
table.insert(self.MsgMailList, 1, msg.newMail);
|
|||
|
if self.AllMails:Count() >= self.MaxMailCount then
|
|||
|
self.RemainMailNum = self.RemainMailNum + 1;
|
|||
|
Utils.ShowPromptByEnum("MailOverFlow")
|
|||
|
|
|||
|
for i = self.MaxMailCount + 1, #self.MsgMailList do
|
|||
|
local _mail = table.remove(self.MsgMailList, i);
|
|||
|
self.AllMails:Remove(_mail.mailId);
|
|||
|
end
|
|||
|
end
|
|||
|
|
|||
|
local _newMail = msg.newMail;
|
|||
|
--是否有附件
|
|||
|
if _newMail.hasAttachment then
|
|||
|
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_MAILEXISTITEMS);
|
|||
|
end
|
|||
|
|
|||
|
local _GetByKeyFunc = DataConfig.DataMessageString.GetByKey;
|
|||
|
_newMail.mailTitle = _newMail.readTable and _GetByKeyFunc(tonumber(_newMail.mailTitle)) or _newMail.mailTitle;
|
|||
|
self.CurReadMailId = _newMail.mailId;
|
|||
|
self.AllMails:Add(_newMail.mailId, _newMail);
|
|||
|
|
|||
|
-- 排序
|
|||
|
self:SortMailList();
|
|||
|
-- 刷新界面
|
|||
|
GameCenter.PushFixEvent(UILuaEventDefine.UIMailRefreshUI);
|
|||
|
-- 刷新小红点
|
|||
|
self:RefreshRepoint();
|
|||
|
-- 刷新界面数量
|
|||
|
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_MAIL_MAILNUM_PROMPT);
|
|||
|
end
|
|||
|
|
|||
|
function MailSystem:CetContent(content, params)
|
|||
|
local _key = tonumber(content);
|
|||
|
if not _key then
|
|||
|
return content
|
|||
|
end
|
|||
|
local _content = DataConfig.DataMessageString.GetByKey(_key)
|
|||
|
if _content and params then
|
|||
|
self.ContentList:Clear();
|
|||
|
for i = 1, #params do
|
|||
|
if params[i].mark == 0 then
|
|||
|
table.insert(self.ContentList, params[i].paramsValue)
|
|||
|
elseif params[i].mark == 1 then
|
|||
|
table.insert(self.ContentList, DataConfig.DataMessageString.GetByKey(tonumber(params[i].paramsValue)))
|
|||
|
elseif params[i].mark == 2 then
|
|||
|
table.insert(self.ContentList, GameCenter.LanguageConvertSystem:ConvertLan(params[i].paramsValue))
|
|||
|
end
|
|||
|
end
|
|||
|
return #self.ContentList > 0 and UIUtils.CSFormatLuaTable(_content, self.ContentList) or _content;
|
|||
|
end
|
|||
|
return _content or content
|
|||
|
end
|
|||
|
|
|||
|
return MailSystem
|