Files
Main/Assets/GameAssets/Resources/Lua/Logic/Notice/NoticeSystem.lua

275 lines
8.2 KiB
Lua
Raw Normal View History

2025-01-25 04:38:09 +08:00
------------------------------------------------
--作者: 丁华强
--日期: 2020-01-07
--文件: NoticeSystem.lua
--模块: NoticeSystem
--描述: 公告提示系统
------------------------------------------------
local SDKCacheData = CS.Thousandto.CoreSDK.SDKCacheData
local NoticeSystem=
{
--登陆公告
NoticeType_Login = "login",
--更新维护公告
NoticeType_Updae = "update",
--实时公告
NoticeType_Now = "activity",
--公告版本号,保存本地,每次判断版本是否不同来决定是否显示公告内容
Prefer_Version = "NoticeVerstion",
--是否自动弹出
IsAutoPop = false,
--公告标题
Title = nil,
--公告内容
Content = nil,
--当前公告类型
CurrentNoticeType = nil,
--是否正在请求数据
Requesting = nil,
--公告url前缀
NoticeUrl = nil,
IsFirstLogin = true;
--公告对应类型Dictionary<int, List<Notice>>
NoticeDic = nil,
--下载公告队列Queue<int>
DownloadNoticeList = nil,
--公告列表下载完毕回调句柄
OnNoticeDownloadFinishHandler = nil,
--公告列表下载失败回调句柄
OnDownloadFailHandler = nil,
};
--公告类型
local NoticeType =
{
--自动打开的公告,一般为登陆维护公告
Update = 0,
--手动点击登陆公告按钮
Login = 1,
--手动点击活动公告按钮
Action = 2,
}
function NoticeSystem:Initialize()
self.OnNoticeDownloadFinishHandler = Utils.Handler(self.OnNoticeDownloadFinish,self,nil,true);
self.OnDownloadFailHandler = Utils.Handler(self.OnDownloadFail,self,nil,true);
self.NoticeDic = Dictionary:New()
self.DownloadNoticeList = List:New()
end
function NoticeSystem:UnInitialize()
self.OnNoticeDownloadFinishHandler = nil;
self.OnDownloadFailHandler = nil;
self.NoticeDic = nil
self.DownloadNoticeList = nil
end
--获取公告内容
function NoticeSystem:GetNoticeByType(noticesType)
if (self.NoticeDic:ContainsKey(noticesType)) then
return self.NoticeDic[noticesType];
end
return nil;
end
--获取当前可显示的公告
function NoticeSystem:GetCurrentNotice()
return self:GetNoticeByType(self.CurrentNoticeType);
end
function NoticeSystem:OnNoticeDownloadFinish(wwwText)
--如果www不为空,表示通过url进行下载的.
if wwwText ~= nil and wwwText ~= "" then
--提前關閉waiting界面
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
local jsNode = Json.decode(wwwText)
--状态码; 1:成功,16:无效的请求参数 15:系统内部异常
local state = jsNode.state
if (state ~= 1) then
Debug.LogError("获取公告数据内容有误: state="..state);
return
end
local _jsonData = jsNode.data
if _jsonData ~= nil and _jsonData.notices ~= nil and #_jsonData.notices > 0 then
local _type = _jsonData.notices[1].type
if (_type == NoticeType.Login or _type == NoticeType.Update) then
self:parseLoginOrUpdateNotice(_jsonData.notices, _type);
else
self:parseActivityNotice(_jsonData.notices, _type);
end
else
Debug.LogError("获取公告数据内容有误: 没有配置公告数据!");
end
self.Requesting = false;
else
Utils.ShowMsgBoxAndBtn(nil,"C_MSGBOX_OK","C_LOGIN_DOWNLOAD_SERVER_LIST_FAIL");
end
end
function NoticeSystem:OnDownloadFail(errCode, error)
if (errCode <= 0 ) then
else
end
end
--解析登陆公告
function NoticeSystem:parseLoginOrUpdateNotice(jsonData, noticeType)
if (#jsonData > 0) then
--有多个登陆公告,只显示最新的一条
local noticeData = jsonData[1];
--auto Int 否 是否自动弹出 0:不自动弹出 1:自动弹出
self.IsAutoPop = noticeData.auto == 1
self.Title = noticeData.title
self.Content = noticeData.content
--保存数据
self:saveNotice(noticeType, self.Title, self.Content);
if (self.IsAutoPop) then
--打开ui
self:OpenLoginNoticeUI(noticeType);
--刷新ui数据
self:RefreshLoginNoticeUI();
end
end
self.IsFirstLogin = false;
return true;
end
--解析活动公告
function NoticeSystem:parseActivityNotice(jsonData, noticeType)
if (jsonData == nil) then
return false;
end
if (self.NoticeDic:ContainsKey(noticeType)) then
self.NoticeDic[noticeType]:Clear();
end
self.Title = nil
self.Content = nil
for i = 1, #jsonData do
--有多个登陆公告,只显示最新的一条
local noticeData = jsonData[i];
if (noticeData == nil) then return false end;
if (noticeData.auto ~= nil) then
--auto Int 否 是否自动弹出 0:不自动弹出 1:自动弹出
self.IsAutoPop = tonumber(noticeData.auto) == 1;
end
self.Title = noticeData.title
self.Content = noticeData.content
if (self.Title == nil or self.Content == nil) then
return false;
end
self:saveNotice(noticeType, self.Title, self.Content);
end
return true;
end
function NoticeSystem:GetNoticesURL()
local _url = GameCenter.ServerListSystem.ServerUrlInfo:GetDefaultURL()
if (SDKCacheData.AppID ~= nil and SDKCacheData.AppID ~= "") then
self.NoticeUrl = string.format("%s/PlatformKits/queryNotice?chn_id=%s",_url,SDKCacheData.ChannelID)
else
self.NoticeUrl = string.format("%s/PlatformKits/queryNotice?chn_id=73",_url)
end
return self.NoticeUrl;
end
--开始请求公告数据
function NoticeSystem:ReqNoticeData(noticeType, serverID)
local e_type = noticeType;
self.CurrentNoticeType = noticeType;
--如果已经下载完了,不重新下载
if (e_type ~= NoticeType.Action and self.NoticeDic:ContainsKey(noticeType)) then
GameCenter.PushFixEvent(UIEventDefine.UI_WAITING_CLOSE);
self:RefreshLoginNoticeUI();
--每次都还是让公告重新下载一次
--return true;
end
self:addDownloadType(e_type);
self:startDownload();
return true;
end
--开始下载公告
function NoticeSystem:startDownload()
if (self.DownloadNoticeList:Count() <= 0) then
--UnityEngine.Debug.Log("没有要下载的公告队列");
return;
end
if (self.Requesting) then
return;
end
self.Requesting = true;
LuaCoroutineUtils.WebRequestText(self:GetNoticesURL(), self.OnNoticeDownloadFinishHandler, self.OnDownloadFailHandler, nil);
end
--添加下载公告类型到队列中
function NoticeSystem:addDownloadType(noticeType)
if (not self.DownloadNoticeList:Contains(noticeType)) then
self.DownloadNoticeList:Add(noticeType)
--self.DownloadNoticeList:Enqueue(type);
end
end
--是否今天第一次显示公告
function NoticeSystem:totayFirstShowNotice()
local date = PlayerPrefs.GetString("TotayFirstShowNotice");
local totay = Time.StampToDateTime(os.time() ,"yyyy-MM-dd")
if (date ~= totay) then
PlayerPrefs.SetString("TotayFirstShowNotice", totay);
return true;
end
return false;
end
--保存公告信息到内存中
function NoticeSystem:saveNotice(noticeType, title, content)
if (self.NoticeDic:ContainsKey(noticeType)) then
local notice = {Title = "", Content = ""};
notice.Title = title;
notice.Content = content;
self.NoticeDic[noticeType].Add(notice);
else
local notice = {Title = "", Content = ""};
notice.Title = title;
notice.Content = content;
local noticeList = List:New()
noticeList:Add(notice);
self.NoticeDic:Add(noticeType, noticeList);
end
end
function NoticeSystem:RefreshLoginNoticeUI()
GameCenter.PushFixEvent(LogicLuaEventDefine.EID_EVENT_LOGINNOTICE_REFRESH);
end
--打开公告面板
function NoticeSystem:OpenLoginNoticeUI(noticeType)
GameCenter.PushFixEvent(UIEventDefine.UI_LOGIN_NOTICE_OPEN, noticeType);
end
--关闭公告面板
function NoticeSystem:CloseLoginNoticeUI()
GameCenter.PushFixEvent(UIEventDefine.UI_LOGIN_NOTICE_CLOSE);
end
return NoticeSystem;