NLDClient-yudde/ProjectNLD/Assets/Code/Scripts/Gameplay/Notice/NoticeManager.cs

364 lines
12 KiB
C#
Raw Normal View History

2024-12-18 14:03:15 +08:00
using System;
using PhxhSDK;
2024-12-24 12:12:45 +08:00
using Framework;
2024-12-18 14:03:15 +08:00
using UnityEngine;
2024-12-23 17:13:39 +08:00
using System.Linq;
using Gameplay.Net;
2024-12-24 12:12:45 +08:00
using cfg.RedPoint;
2024-12-23 17:13:39 +08:00
using Newtonsoft.Json;
2024-12-18 14:03:15 +08:00
using UnityEngine.Networking;
2024-12-23 17:13:39 +08:00
using Cysharp.Threading.Tasks;
using System.Collections.Generic;
using System.Text.RegularExpressions;
2024-12-23 19:32:37 +08:00
using Constants = Framework.Constants;
2024-12-18 14:03:15 +08:00
2024-12-19 19:05:05 +08:00
public class NoticeDataForDisplay
{
public int ID;
public string Title;
2024-12-23 17:13:39 +08:00
public E_NoticeType Type;
public int LinkType;
2024-12-19 19:05:05 +08:00
public Sprite Bannersprite;
public Sprite ContentSprite;
public string Content;
2024-12-23 17:13:39 +08:00
public bool Rotate;
public string UpdateTime;
2024-12-23 19:32:37 +08:00
public RedPointNodeNotice RedPointNodeNotice;
2024-12-23 17:13:39 +08:00
}
/// <summary>
/// 公告类型
/// </summary>
public enum E_NoticeType
{
None = 0,
/// <summary>
/// 系统
/// </summary>
System = 1,
/// <summary>
/// 活动
/// </summary>
Activity = 2,
/// <summary>
/// 通行证
/// </summary>
BattlePass = 3,
2024-12-19 19:05:05 +08:00
}
2024-12-23 19:32:37 +08:00
public class NoticeManager : Singlenton<NoticeManager>, IInitable
2024-12-18 14:03:15 +08:00
{
private const string NoticePost = "notices";
2024-12-26 14:38:17 +08:00
private const int NoticeRedNod = 100;
2024-12-18 14:03:15 +08:00
/// <summary>
/// 公告请求结果
/// </summary>
private class NoticeGetResult
{
public int code;
public string message;
public List<NoticeData> data;
}
2024-12-23 17:13:39 +08:00
/// <summary>
2024-12-25 18:45:03 +08:00
/// link_type: 1-内链, 2-外链(需点击跳转链接后红点才消失)
2024-12-24 14:25:33 +08:00
/// is_swiper: 1-需轮播, 2-否
2024-12-23 17:13:39 +08:00
/// </summary>
2024-12-18 14:03:15 +08:00
private class NoticeData
{
public int id;
public string title;
public int type;
2024-12-23 17:13:39 +08:00
public int link_type;
2024-12-24 14:25:33 +08:00
public int is_swiper;
2024-12-18 14:03:15 +08:00
public string url;
public string cover;
public string content;
public int application_id;
public string created_at;
public string update_at;
}
2024-12-23 19:32:37 +08:00
/// <summary>
/// 真是公告数据
/// </summary>
2024-12-18 14:03:15 +08:00
private List<NoticeData> curNoticeData;
2024-12-19 19:05:05 +08:00
/// <summary>
/// 公告数据 UI
/// </summary>
2024-12-23 17:13:39 +08:00
private Dictionary<int, NoticeDataForDisplay> activityNoticeDataForDisplays;
2024-12-19 19:05:05 +08:00
2024-12-23 17:13:39 +08:00
/// <summary>
/// 系统公告数据 UI
/// </summary>
private Dictionary<int, NoticeDataForDisplay> systemNoticeDataForDisplays;
/// <summary>
/// 轮播公告数据 UI
/// </summary>
private List<NoticeDataForDisplay> rotateNoticeDataForDisplays;
2024-12-23 19:32:37 +08:00
/// <summary>
/// 公告红点缓存数据
/// </summary>
private NoticeRedStorage noticeRedStorage;
/// <summary>
/// 公告页签红点字典
/// </summary>
private Dictionary<E_NoticeType, RedPointNodeNotice> redPointNodeNoticeTypes;
2024-12-23 17:13:39 +08:00
public Dictionary<int, NoticeDataForDisplay> ActivityNoticeDataForDisplays => activityNoticeDataForDisplays;
public Dictionary<int, NoticeDataForDisplay> SystemNoticeDataForDisplays => systemNoticeDataForDisplays;
public List<NoticeDataForDisplay> RotateNoticeDataForDisplays => rotateNoticeDataForDisplays;
2024-12-19 19:05:05 +08:00
2024-12-23 19:32:37 +08:00
public Dictionary<E_NoticeType, RedPointNodeNotice> RedPointNodeNoticeTypes => redPointNodeNoticeTypes;
2024-12-18 14:03:15 +08:00
public async void GetNotice()
{
var requestUrl = AppConfig.Instance.LoginURL + NoticePost;
DebugUtil.Log("拉起公告请求地址: " + requestUrl);
try
{
var headData = PostHeader.PostHeaderInfo(new Dictionary<string, string>
{
{ "timestamp", DateTimeOffset.UtcNow.ToUnixTimeSeconds().ToString() }
});
var request = new UnityWebRequest(requestUrl, UnityWebRequest.kHttpVerbGET)
{
downloadHandler = new DownloadHandlerBuffer(),
};
request.SetRequestHeader("Content-Type", "application/json;charset=utf-8"); //为了保证客户端发送的json格式在后端能解析
request.SetRequestHeader("signature", headData["signature"]);
request.SetRequestHeader("timestamp", headData["timestamp"]);
request.SetRequestHeader("accessKey", headData["accessKey"]);
// 异步等待请求完成
await request.SendWebRequest();
if (request.result != UnityWebRequest.Result.Success)
{
DebugUtil.LogError(request.downloadHandler.text);
}
else
{
2024-12-26 17:26:46 +08:00
Debug.Log("http消息请求成功!返回内容:\n" + request.downloadHandler.text);
2024-12-18 14:03:15 +08:00
var result = JsonConvert.DeserializeObject<NoticeGetResult>(request.downloadHandler.text);
if (result.code == 0)
{
curNoticeData = result.data;
ConvertNotice();
}
else
{
DebugUtil.LogError("获取公告失败");
}
}
}
catch (UnityWebRequestException e)
{
DebugUtil.LogError(e);
var pattern = @"\{.*\}";
var match = Regex.Match(e.Message, pattern);
if (match.Success)
{
var json = match.Value;
NoticeGetResult res = JsonConvert.DeserializeObject<NoticeGetResult>(json);
DebugUtil.LogError("请求失败, 服务器有错误响应: " + res.message);
}
else
{
DebugUtil.LogError("请求失败!");
}
}
}
private async void ConvertNotice()
{
2024-12-23 17:13:39 +08:00
var activityIndex = 0;
var systemIndex = 0;
2024-12-18 14:03:15 +08:00
foreach (var data in curNoticeData)
{
2024-12-26 17:26:46 +08:00
Debug.Log($"公告ID:{data.id}, Title:{data.title}");
Debug.Log($"公告内容:{data.content}");
2024-12-19 19:05:05 +08:00
var sprite = await DownLoadImg(data.cover);
var noticeDataForDisplay = new NoticeDataForDisplay
{
ID = data.id,
Content = data.content,
Bannersprite = sprite,
ContentSprite = sprite,
Title = data.title,
2024-12-23 17:13:39 +08:00
LinkType = data.link_type,
UpdateTime = data.update_at,
2024-12-19 19:05:05 +08:00
};
2024-12-23 17:13:39 +08:00
noticeDataForDisplay.Type = (E_NoticeType)data.type;
2024-12-23 19:32:37 +08:00
// 红点
var param = new ValueTuple<NoticeRedType, int>(NoticeRedType.Notice, data.id);
noticeDataForDisplay.RedPointNodeNotice =
RedPointManager.Instance.AddDynamicNode(NodeType.Notice, NoticeRedNod, param) as RedPointNodeNotice;
2024-12-23 17:13:39 +08:00
if (data.type == 1)
{
systemNoticeDataForDisplays[systemIndex] = noticeDataForDisplay;
systemIndex++;
}
else
{
activityNoticeDataForDisplays[activityIndex] = noticeDataForDisplay;
activityIndex++;
}
2024-12-24 14:25:33 +08:00
if (data.is_swiper == 1)
rotateNoticeDataForDisplays.Add(noticeDataForDisplay);
2024-12-18 14:03:15 +08:00
}
2024-12-23 17:13:39 +08:00
SortRotateNotice();
2024-12-23 19:32:37 +08:00
EventManager.Instance.Send(EventManager.EventName.RefreshNotice);
2024-12-18 14:03:15 +08:00
}
2024-12-19 19:05:05 +08:00
private async UniTask<Sprite> DownLoadImg(string url)
2024-12-18 14:03:15 +08:00
{
2024-12-31 17:31:48 +08:00
if (string.IsNullOrEmpty(url)) return null;
2024-12-19 19:05:05 +08:00
Sprite sprite = null;
2024-12-18 14:03:15 +08:00
try
{
2024-12-19 19:05:05 +08:00
using var request = UnityWebRequestTexture.GetTexture(url);
2024-12-18 14:03:15 +08:00
var operation = request.SendWebRequest();
await operation.ToUniTask();
if (request.result == UnityWebRequest.Result.Success)
{
var texture = DownloadHandlerTexture.GetContent(request);
if (texture != null)
2024-12-19 19:05:05 +08:00
{
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height),
new Vector2(0.5f, 0.5f));
}
2024-12-18 14:03:15 +08:00
}
else
{
Debug.LogError($"公告图片下载失败: {request.error}");
}
2024-12-19 19:05:05 +08:00
return sprite;
2024-12-18 14:03:15 +08:00
}
catch (Exception e)
{
DebugUtil.LogError("NoticeManager.DownLoadImg error: {0}", e);
2024-12-19 19:05:05 +08:00
return sprite;
2024-12-18 14:03:15 +08:00
}
}
2024-12-19 19:05:05 +08:00
2024-12-23 17:13:39 +08:00
private void SortRotateNotice()
{
// 如果解析失败,返回一个最小值,这样无效时间会排在最后
rotateNoticeDataForDisplays = rotateNoticeDataForDisplays
.OrderByDescending(notice => DateTime.TryParse(notice.UpdateTime, out var time) ? time : DateTime.MinValue)
.ToList();
}
2024-12-24 12:12:45 +08:00
#region 红点
public bool GetNoticeTypeRedNode(int type)
{
switch (type)
{
case 1:
{
foreach (var notice in systemNoticeDataForDisplays.Values)
{
if (noticeRedStorage.CheckNotice(notice.ID))
return true;
}
break;
}
case 2:
{
foreach (var notice in activityNoticeDataForDisplays.Values)
{
if (noticeRedStorage.CheckNotice(notice.ID))
return true;
}
break;
}
}
return false;
}
public bool GetNoticeRedNode(int id)
{
return noticeRedStorage.CheckNotice(id);
}
public void CloseRedNode(int id)
{
if (!noticeRedStorage.NoticeList.Contains(id))
noticeRedStorage.NoticeList.Add(id);
StorageMgr.Instance.SyncForce = true;
EventManager.Instance.Send(EventManager.EventName.RefreshNotice);
}
#endregion
#region 初始化与释放
2024-12-23 19:32:37 +08:00
public void Init()
{
activityNoticeDataForDisplays = new Dictionary<int, NoticeDataForDisplay>();
systemNoticeDataForDisplays = new Dictionary<int, NoticeDataForDisplay>();
rotateNoticeDataForDisplays = new List<NoticeDataForDisplay>();
redPointNodeNoticeTypes = new Dictionary<E_NoticeType, RedPointNodeNotice>();
noticeRedStorage = StorageMgr.Instance.GetStorage<NoticeRedStorage>(Constants.Storage.NoticeRed);
noticeRedStorage ??= new NoticeRedStorage();
// 公告类型红点
var systemParam = new ValueTuple<NoticeRedType, int>(NoticeRedType.NoticeType, (int)E_NoticeType.System);
var systemNode =
RedPointManager.Instance.AddDynamicNode(NodeType.Notice, NoticeRedNod, systemParam) as RedPointNodeNotice;
var activityParam = new ValueTuple<NoticeRedType, int>(NoticeRedType.NoticeType, (int)E_NoticeType.Activity);
var activityNode =
RedPointManager.Instance.AddDynamicNode(NodeType.Notice, NoticeRedNod, activityParam) as RedPointNodeNotice;
redPointNodeNoticeTypes.Add(E_NoticeType.System, systemNode);
redPointNodeNoticeTypes.Add(E_NoticeType.Activity, activityNode);
}
2024-12-19 19:05:05 +08:00
public void Release()
{
2024-12-23 17:13:39 +08:00
foreach (var noticeData in activityNoticeDataForDisplays)
{
noticeData.Value.ContentSprite = null;
noticeData.Value.Bannersprite = null;
}
foreach (var noticeData in activityNoticeDataForDisplays)
2024-12-19 19:05:05 +08:00
{
noticeData.Value.ContentSprite = null;
noticeData.Value.Bannersprite = null;
}
2024-12-23 19:32:37 +08:00
foreach (var redPointNodeNotice in redPointNodeNoticeTypes)
{
if (redPointNodeNotice.Value != null)
RedPointManager.Instance.RemoveDynamicNode(redPointNodeNotice.Value);
}
2024-12-23 17:13:39 +08:00
activityNoticeDataForDisplays = null;
systemNoticeDataForDisplays = null;
2024-12-19 19:05:05 +08:00
}
2024-12-24 12:12:45 +08:00
#endregion
2024-12-18 14:03:15 +08:00
}