589 lines
14 KiB
C#
589 lines
14 KiB
C#
![]() |
// ***********************************************************************
|
|||
|
// Assembly : Unity
|
|||
|
// Author : Kimch
|
|||
|
// Created :
|
|||
|
//
|
|||
|
// Last Modified By : Kimch
|
|||
|
// Last Modified On :
|
|||
|
// ***********************************************************************
|
|||
|
// <copyright file= "MailProxy" company=""></copyright>
|
|||
|
// <summary></summary>
|
|||
|
// ***********************************************************************
|
|||
|
namespace G
|
|||
|
{
|
|||
|
using F;
|
|||
|
using F.Network;
|
|||
|
using System.Collections.Generic;
|
|||
|
using UnityEngine;
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 邮件系统管理器
|
|||
|
/// </summary>
|
|||
|
public class MailProxy : F.GameProxy
|
|||
|
{
|
|||
|
#region FIELD
|
|||
|
|
|||
|
private readonly Dictionary<int, Mail> _serverMails = new Dictionary<int, Mail>();
|
|||
|
private readonly Dictionary<int, Mail> _localMails = new Dictionary<int, Mail>();
|
|||
|
|
|||
|
private readonly List<Mail> _mails = new List<Mail>();
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region PROPERTY
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 系统邮件
|
|||
|
/// </summary>
|
|||
|
public IList<Mail> allMails
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
_mails.Clear();
|
|||
|
_mails.AddRange(_localMails.Values);
|
|||
|
_mails.AddRange(_serverMails.Values);
|
|||
|
return _mails;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public bool hasEmail
|
|||
|
{
|
|||
|
get { return _localMails.Count > 0 || _serverMails.Count > 0; }
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region METHOD
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 客户端向服务器拉取邮件列表
|
|||
|
/// </summary>
|
|||
|
/// <param name="callback"></param>
|
|||
|
public void GetMails(Callback2 callback)
|
|||
|
{
|
|||
|
ServerProxy.Instance.PullMails(callback);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <param name="error"></param>
|
|||
|
/// <param name="message"></param>
|
|||
|
private void OnGetMails(int error, string message)
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 客户端向服务器设置邮件已读标志
|
|||
|
/// </summary>
|
|||
|
/// <param name="mail"></param>
|
|||
|
/// <param name="callback"></param>
|
|||
|
public void Read(Mail mail, Callback callback)
|
|||
|
{
|
|||
|
if (!mail.isLocal && mail.status == 0)
|
|||
|
{
|
|||
|
mail.status = 1;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 客户端向服务器发送删除邮件请求
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <param name="callback"></param>
|
|||
|
/// </summary>
|
|||
|
public void Delete(Mail mail, Callback2 callback)
|
|||
|
{
|
|||
|
if (mail.isLocal)
|
|||
|
{
|
|||
|
_localMails.Remove(mail.id);
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ServerProxy.Instance.DeleteMails(new string[] { mail.mailKey },
|
|||
|
(error, message) =>
|
|||
|
{
|
|||
|
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void DeleteAll(Callback2 callback)
|
|||
|
{
|
|||
|
_localMails.Clear();
|
|||
|
_serverMails.Clear();
|
|||
|
callback?.Invoke(0, null);
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
/// 客户端向服务器发送领取附件的请求
|
|||
|
/// </summary>
|
|||
|
/// <param name="id"></param>
|
|||
|
/// <param name="callback"></param>
|
|||
|
public void GetAttach(Mail mail, Callback2 callback)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
mail.status = 2;
|
|||
|
if (mail.isLocal)
|
|||
|
{
|
|||
|
if (mail.giveItems != null && mail.giveItems.Length > 0)
|
|||
|
{
|
|||
|
RewardProxy.Instance.GetRewardsWithUI(mail.giveItems, callback);
|
|||
|
}
|
|||
|
_localMails.Remove(mail.id);
|
|||
|
SaveLocalMail();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
ServerProxy.Instance.DeleteMails(new string[] { mail.mailKey },
|
|||
|
(error, message) =>
|
|||
|
{
|
|||
|
if (error == 0)
|
|||
|
{
|
|||
|
if (mail.giveItems != null && mail.giveItems.Length > 0)
|
|||
|
{
|
|||
|
RewardProxy.Instance.GetRewardsWithUI(mail.giveItems, (err, mes) =>
|
|||
|
{
|
|||
|
if (err == 0)
|
|||
|
{
|
|||
|
PostNotification(GlobalDefine.EVENT_MONEY_CHANGED, "email");
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
_serverMails.Remove(mail.id);
|
|||
|
}
|
|||
|
callback?.Invoke(error, message);
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void GetAllAttach(Callback2 callback)
|
|||
|
{
|
|||
|
bool flag = false;
|
|||
|
if (_serverMails.Count > 0)
|
|||
|
{
|
|||
|
var mailIdList = F.ListPool<string>.Get();
|
|||
|
foreach (var mail in _serverMails.Values)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
mailIdList.Add(mail.mailKey);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (mailIdList.Count > 0)
|
|||
|
{
|
|||
|
flag = true;
|
|||
|
ServerProxy.Instance.DeleteMails(mailIdList.ToArray(),
|
|||
|
(error, message) =>
|
|||
|
{
|
|||
|
if (error == 0)
|
|||
|
{
|
|||
|
var rewardList = F.ListPool<Item.ItemInfo>.Get();
|
|||
|
foreach (var mail in _serverMails.Values)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
mail.status = 2;
|
|||
|
if (mail.giveItems != null)
|
|||
|
{
|
|||
|
rewardList.AddRange(mail.giveItems);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
_serverMails.Clear();
|
|||
|
|
|||
|
if (rewardList.Count > 0)
|
|||
|
{
|
|||
|
flag = true;
|
|||
|
RewardProxy.Instance.GetRewardsWithUI(rewardList,(err, mes) =>
|
|||
|
{
|
|||
|
if (err == 0)
|
|||
|
{
|
|||
|
PostNotification(GlobalDefine.EVENT_MONEY_CHANGED, "email");
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
F.ListPool<Item.ItemInfo>.Release(rewardList);
|
|||
|
}
|
|||
|
callback?.Invoke(error, message);
|
|||
|
});
|
|||
|
//KPlatform.Instance.AckMail(mailIdList.ToArray());
|
|||
|
}
|
|||
|
F.ListPool<string>.Release(mailIdList);
|
|||
|
}
|
|||
|
|
|||
|
if (_localMails.Count > 0)
|
|||
|
{
|
|||
|
var rewardList = F.ListPool<Item.ItemInfo>.Get();
|
|||
|
foreach (var mail in _localMails.Values)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
mail.status = 2;
|
|||
|
if (mail.giveItems != null)
|
|||
|
{
|
|||
|
rewardList.AddRange(mail.giveItems);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
_localMails.Clear();
|
|||
|
|
|||
|
if (rewardList.Count > 0)
|
|||
|
{
|
|||
|
flag = true;
|
|||
|
RewardProxy.Instance.GetRewardsWithUI(rewardList, callback);
|
|||
|
SaveLocalMail();
|
|||
|
}
|
|||
|
F.ListPool<Item.ItemInfo>.Release(rewardList);
|
|||
|
}
|
|||
|
|
|||
|
if (!flag)
|
|||
|
PostNotification(GlobalDefine.EVENT_SHOW_TOAST, "暂无可领取的邮件");
|
|||
|
}
|
|||
|
|
|||
|
private void OnGetAllAttach()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
/// <returns></returns>
|
|||
|
public int GetRedPoint1()
|
|||
|
{
|
|||
|
if (_serverMails.Count > 0)
|
|||
|
{
|
|||
|
foreach (var mail in _serverMails.Values)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
return 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (_localMails.Count > 0)
|
|||
|
{
|
|||
|
foreach (var mail in _localMails.Values)
|
|||
|
{
|
|||
|
if (mail.status < 2)
|
|||
|
{
|
|||
|
return 1;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
return 0;
|
|||
|
}
|
|||
|
|
|||
|
int GenerateLocalId()
|
|||
|
{
|
|||
|
return Random.Range(100000000, 999999999);
|
|||
|
}
|
|||
|
|
|||
|
public void PostLocalMail(int id)
|
|||
|
{
|
|||
|
var mail = CreateLocalMail(id, Launch.Timestamp);
|
|||
|
if (mail != null)
|
|||
|
{
|
|||
|
_localMails[mail.id] = mail;
|
|||
|
SaveLocalMail();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public void PostLocalMail(int id, int itemId, int multiple)
|
|||
|
{
|
|||
|
var mail = CreateLocalMail(id, itemId, Launch.Timestamp);
|
|||
|
if (mail != null)
|
|||
|
{
|
|||
|
mail.multiple = multiple;
|
|||
|
if (mail.id > 0)
|
|||
|
_localMails[mail.id] = mail;
|
|||
|
else
|
|||
|
{
|
|||
|
int count = 7;
|
|||
|
do
|
|||
|
{
|
|||
|
mail.id = GenerateLocalId();
|
|||
|
Debug.Log("GenerateLocalId");
|
|||
|
if (!_localMails.ContainsKey(mail.id))
|
|||
|
{
|
|||
|
_localMails.Add(mail.id, mail);
|
|||
|
break;
|
|||
|
}
|
|||
|
} while (count-- > 0);
|
|||
|
}
|
|||
|
SaveLocalMail();
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private Mail CreateLocalMail(int id, int timestamp)
|
|||
|
{
|
|||
|
var mailItem = ItemProxy.Instance.GetStaticItem<ItemMail>(id);
|
|||
|
if (mailItem != null)
|
|||
|
return new Mail
|
|||
|
{
|
|||
|
isLocal = true,
|
|||
|
id = id,
|
|||
|
itemId = id,
|
|||
|
status = 0,
|
|||
|
startTimestamp = timestamp,
|
|||
|
endTimestamp = timestamp + mailItem.life,
|
|||
|
title = mailItem.title,
|
|||
|
content = mailItem.content,
|
|||
|
giveItems = mailItem.rewardInfos,
|
|||
|
sender = mailItem.sender,
|
|||
|
isForever = mailItem.forever != 0,
|
|||
|
};
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
private Mail CreateLocalMail(int id, int itemId, int timestamp)
|
|||
|
{
|
|||
|
var mailItem = ItemProxy.Instance.GetStaticItem<ItemMail>(itemId);
|
|||
|
if (mailItem != null)
|
|||
|
return new Mail
|
|||
|
{
|
|||
|
isLocal = true,
|
|||
|
id = id,
|
|||
|
itemId = itemId,
|
|||
|
status = 0,
|
|||
|
startTimestamp = timestamp,
|
|||
|
endTimestamp = timestamp + mailItem.life,
|
|||
|
title = mailItem.title,
|
|||
|
content = mailItem.content,
|
|||
|
giveItems = mailItem.rewardInfos,
|
|||
|
sender = mailItem.sender,
|
|||
|
isForever = mailItem.forever != 0,
|
|||
|
};
|
|||
|
return null;
|
|||
|
}
|
|||
|
|
|||
|
private const string ARCHIVE_KEY = "mails";
|
|||
|
private const string ARCHIVE_KEY_V1 = "mail_v1";
|
|||
|
|
|||
|
private void LoadLocalMail()
|
|||
|
{
|
|||
|
_localMails.Clear();
|
|||
|
|
|||
|
var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1);
|
|||
|
if (dataList != null && dataList.Count > 0)
|
|||
|
{
|
|||
|
int index = 0;
|
|||
|
while (index < dataList.Count)
|
|||
|
{
|
|||
|
int mailId = dataList[index++];
|
|||
|
int mailItemId = dataList[index++];
|
|||
|
int mailTimestamp = dataList[index++];
|
|||
|
int multiple = dataList[index++];
|
|||
|
|
|||
|
var mail = CreateLocalMail(mailId, mailItemId, mailTimestamp);
|
|||
|
if (mail != null)
|
|||
|
{
|
|||
|
mail.multiple = multiple;
|
|||
|
_localMails.Add(mail.id, mail);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY);
|
|||
|
if (dataList != null && dataList.Count > 0)
|
|||
|
{
|
|||
|
int index = 0;
|
|||
|
while (index < dataList.Count)
|
|||
|
{
|
|||
|
int mailId = dataList[index++];
|
|||
|
int mailTimestamp = dataList[index++];
|
|||
|
|
|||
|
var mail = CreateLocalMail(mailId, mailTimestamp);
|
|||
|
if (mail != null)
|
|||
|
_localMails.Add(mail.id, mail);
|
|||
|
}
|
|||
|
}
|
|||
|
ArchiveProxy.Instance.RemoveIntList(ARCHIVE_KEY);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void SaveLocalMail()
|
|||
|
{
|
|||
|
var dataList = ArchiveProxy.Instance.GetIntList(ARCHIVE_KEY_V1);
|
|||
|
if (dataList != null)
|
|||
|
{
|
|||
|
dataList.Clear();
|
|||
|
|
|||
|
foreach (var mail in _localMails.Values)
|
|||
|
{
|
|||
|
if (!mail.isForever && mail.status != 2)
|
|||
|
{
|
|||
|
dataList.Add(mail.id);
|
|||
|
dataList.Add(mail.itemId);
|
|||
|
dataList.Add(mail.startTimestamp);
|
|||
|
dataList.Add(mail.multiple);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
ArchiveProxy.Instance.SetIntList(ARCHIVE_KEY_V1, dataList);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
|
|||
|
#region Proxy
|
|||
|
|
|||
|
public static MailProxy Instance => GetInstance<MailProxy>();
|
|||
|
|
|||
|
public override void InitCompleted()
|
|||
|
{
|
|||
|
KFramework.GameFacade.RegisterResp(GlobalDefine.PACKET_ID_MAIL_GET_ALL, this);
|
|||
|
//KFramework.GameFacade.RegisterResp(101, this);
|
|||
|
}
|
|||
|
|
|||
|
public override void LoadCompleted()
|
|||
|
{
|
|||
|
var mailItems = ItemProxy.Instance.GetStaticItems<ItemMail>();
|
|||
|
for (int i = 0; i < mailItems.Count; i++)
|
|||
|
{
|
|||
|
if (mailItems[i].forever > 0)
|
|||
|
{
|
|||
|
var mail = CreateLocalMail(mailItems[i].id, Launch.Timestamp);
|
|||
|
if (mail != null)
|
|||
|
_localMails.Add(mail.id, mail);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public override void ReadArchive()
|
|||
|
{
|
|||
|
LoadLocalMail();
|
|||
|
}
|
|||
|
|
|||
|
public override void Recv(IPacket packet)
|
|||
|
{
|
|||
|
var gp = packet as GamePacket;
|
|||
|
if (gp.error != GamePacket.ERROR_CODE_OK)
|
|||
|
{
|
|||
|
PostNotification(GlobalDefine.EVENT_SHOW_TOAST, gp.data as string);
|
|||
|
return;
|
|||
|
}
|
|||
|
|
|||
|
if (packet.id == GlobalDefine.PACKET_ID_MAIL_GET_ALL)
|
|||
|
{
|
|||
|
_serverMails.Clear();
|
|||
|
var list = gp.data as IList<object>;
|
|||
|
for (int i = 0; i < list.Count; i++)
|
|||
|
{
|
|||
|
var mailData = list.GetDictionary(i);
|
|||
|
var mail = new Mail();
|
|||
|
mail.mailKey = mailData.GetString("id");
|
|||
|
mail.title = mailData.GetString("title");
|
|||
|
mail.content = mailData.GetString("content");
|
|||
|
//mail.startTimestamp = mailData.GetInt("start_time");
|
|||
|
//mail.endTimestamp = mailData.GetInt("end_time");
|
|||
|
//mail.status = mailData.GetString("is_ack", "false") == "true" ? 2 : 1;
|
|||
|
//mail.giveItems = Item.ItemInfo.FormJsonList(mailData.GetList("bonuses"));
|
|||
|
var rewards = mailData.GetString("rewards");
|
|||
|
if (!string.IsNullOrEmpty(rewards))
|
|||
|
{
|
|||
|
var rewardInfos = F.ListPool<Item.ItemInfo>.Get();
|
|||
|
|
|||
|
var splits = rewards.Split('|');
|
|||
|
if (splits != null)
|
|||
|
for (int j = 0; j < splits.Length; j++)
|
|||
|
{
|
|||
|
if (!string.IsNullOrEmpty(splits[j]))
|
|||
|
{
|
|||
|
var idcount = splits[j].Split(',');
|
|||
|
if (idcount != null && idcount.Length > 1)
|
|||
|
{
|
|||
|
int.TryParse(idcount[0], out int rId);
|
|||
|
int.TryParse(idcount[1], out int rCount);
|
|||
|
rewardInfos.Add(new Item.ItemInfo
|
|||
|
{
|
|||
|
id = rId,
|
|||
|
count = rCount,
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
mail.giveItems = rewardInfos.ToArray();
|
|||
|
F.ListPool<Item.ItemInfo>.Release(rewardInfos);
|
|||
|
}
|
|||
|
|
|||
|
mail.id = mail.mailKey.GetHashCode();
|
|||
|
_serverMails.Add(mail.id, mail);
|
|||
|
}
|
|||
|
}
|
|||
|
else if (packet.id == 100)
|
|||
|
{
|
|||
|
_serverMails.Clear();
|
|||
|
// [
|
|||
|
// {
|
|||
|
// "id": 201,
|
|||
|
// "title": "版本范围测试",
|
|||
|
// "content": "版本范围测试",
|
|||
|
// "bonuses":[
|
|||
|
// {
|
|||
|
// "id": "1",
|
|||
|
// "count": 1
|
|||
|
|
|||
|
//}
|
|||
|
// ],
|
|||
|
// "start_time": 1598606506,
|
|||
|
// "end_time": 1604049629,
|
|||
|
// "is_ack": true,
|
|||
|
// "forever": false
|
|||
|
// },
|
|||
|
// {
|
|||
|
// "id": 205,
|
|||
|
// "title": "不输入时间不输入时间",
|
|||
|
// "content": "不输入时间不输入时间不输入时间",
|
|||
|
// "bonuses": null,
|
|||
|
// "start_time": 57600,
|
|||
|
// "end_time": 2145888000,
|
|||
|
// "is_ack": false,
|
|||
|
// "forever": true
|
|||
|
// }
|
|||
|
// ]
|
|||
|
var json = (string)packet.data;
|
|||
|
var list = F.Utils.ToJsonList(json);
|
|||
|
for (int i = 0; i < list.Count; i++)
|
|||
|
{
|
|||
|
var mailData = list.GetDictionary(i);
|
|||
|
var mail = new Mail();
|
|||
|
mail.id = mailData.GetInt("id");
|
|||
|
mail.title = mailData.GetString("title");
|
|||
|
mail.content = mailData.GetString("content");
|
|||
|
mail.startTimestamp = mailData.GetInt("start_time");
|
|||
|
mail.endTimestamp = mailData.GetInt("end_time");
|
|||
|
mail.status = mailData.GetString("is_ack", "false") == "true" ? 2 : 1;
|
|||
|
mail.giveItems = Item.ItemInfo.FormJsonList(mailData.GetList("bonuses"));
|
|||
|
|
|||
|
_serverMails.Add(mail.id, mail);
|
|||
|
}
|
|||
|
}
|
|||
|
else if (packet.id == 101)
|
|||
|
{
|
|||
|
var rewards = F.ListPool<Item.ItemInfo>.Get();
|
|||
|
var mailIds = (int[])packet.data;
|
|||
|
for (int i = 0; i < mailIds.Length; i++)
|
|||
|
{
|
|||
|
if (_serverMails.TryGetValue(mailIds[i], out var mail))
|
|||
|
{
|
|||
|
mail.status = 2;
|
|||
|
rewards.AddRange(mail.giveItems);
|
|||
|
}
|
|||
|
}
|
|||
|
RewardProxy.Instance.GetRewardsWithUI(rewards, null);
|
|||
|
F.ListPool<Item.ItemInfo>.Release(rewards);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
#endregion
|
|||
|
}
|
|||
|
}
|