c1_lua/lua/app/userdata/mail/mail_data.lua
2023-05-19 18:32:23 +08:00

203 lines
5.0 KiB
Lua

local MailData = class("MailData", BaseData)
local MailEntity = "app/userdata/mail/mail_entity"
local TIME_TYPE = {
DAY = 1,
WEEK = 2,
MONTH = 3,
}
function MailData:ctor()
self.mails = {}
self.needGetNewMail = false
self.data.isDirty = false
self.data.redPoint = false
self.maxMailId = 0
end
function MailData:clear()
self.mails = {}
self.data.redPoint = false
self.lastMailId = nil
self.mailIsOpen = false
end
function MailData:init(data)
data = data or GConst.EMPTY_TABLE
self.mails = {}
self.needGetNewMail = false
self:addMails(data.mails)
self:updateRedPointCd()
DataManager:registerDataCd("MailData")
end
function MailData:updateRedPointCd()
self.needUpdateRedPointInfo = {}
local cfg = ConfigManager:getConfig("mail")
for i,v in ipairs(cfg) do
if v.time_type == TIME_TYPE.DAY then
for ii = 1, 7 do
self.needUpdateRedPointInfo[ii] = self.needUpdateRedPointInfo[ii] or {}
if #v.time_send == 3 then
local time = v.time_send[1] * 3600 + v.time_send[2] * 60 + v.time_send[3]
if not table.containValue(self.needUpdateRedPointInfo[ii], time) then
table.insert(self.needUpdateRedPointInfo[ii], time)
end
end
end
elseif v.time_type == TIME_TYPE.WEEK then
if #v.time_send == 4 then
local day = v.time_send[1]
self.needUpdateRedPointInfo[day] = self.needUpdateRedPointInfo[day] or {}
local time = v.time_send[2] * 3600 + v.time_send[3] * 60 + v.time_send[4]
if not table.containValue(self.needUpdateRedPointInfo[day], time) then
table.insert(self.needUpdateRedPointInfo[day], time)
end
end
elseif v.time_type == TIME_TYPE.MONTH then
--TODO 暂时没有 无数据格式
end
end
end
function MailData:updateCd()
local nowTime = Time:getServerTime()
local today = Time:getDayofWeek()
local times = self.needUpdateRedPointInfo[today] or {}
local curTime = nowTime % 86400
for i, v in ipairs(times) do
if v == curTime then
EventManager:dispatchEvent(EventManager.CUSTOM_EVENT.TIME_TRIGGERED_NEW_EMAIL)
break
end
end
end
function MailData:addMails(mails)
if not mails then
return
end
for _, info in pairs(mails) do
local entity = require(MailEntity):create(info)
self.mails[info.id] = entity
if self.maxMailId < info.id then
self.maxMailId = info.id
end
end
self:setDirty()
self:refreshRedPoint()
end
function MailData:getMails()
return self.mails
end
function MailData:setDirty()
self.data.isDirty = not self.data.isDirty
end
function MailData:setMailReceived(id)
if not self.mails[id] then
return
end
self.mails[id]:receivedThis()
self:setDirty()
self:refreshRedPoint()
end
function MailData:setMailRead(id)
if not self.mails[id] then
return
end
self.mails[id]:readThis()
self:refreshRedPoint()
self:setDirty()
end
function MailData:deleteMail(ids)
if not ids then
return
end
for _, id in ipairs(ids) do
self.mails[id] = nil
end
self:setDirty()
end
function MailData:refreshRedPoint()
if self.needGetNewMail then
self.data.redPoint = true
self.isAdRedPoint = false
return
end
local reset = true
local haveNoAdRp = false
for id, entity in pairs(self.mails) do
if entity:canClaim() then
self.data.redPoint = true
if entity:getIsAdMail() then
if not haveNoAdRp then
self.isAdRedPoint = true
end
else
haveNoAdRp = true
self.isAdRedPoint = false
end
reset = false
end
end
if reset then
self.data.redPoint = false
self.isAdRedPoint = false
end
end
function MailData:getRedPoint()
return self.data.redPoint, self.isAdRedPoint
end
function MailData:setNeedGetNewMail(need)
self.needGetNewMail = need == true
if self.needGetNewMail then
self:refreshRedPoint()
end
end
function MailData:getNeedGetNewMail()
return self.needGetNewMail
end
function MailData:getLastMailId()
if not self.lastMailId then
self.lastMailId = LocalData:getLastMailId()
end
return self.lastMailId
end
function MailData:setLastMailId()
if not self.maxMailId or not self.lastMailId then
return
end
if self.lastMailId < self.maxMailId then
self.lastMailId = self.maxMailId
LocalData:setLastMailId(self.lastMailId)
end
end
function MailData:getIsOpen()
if not self.mailIsOpen then
self.mailIsOpen = ModuleManager:getIsOpen(ModuleManager.MODULE_KEY.MAIL_OPEN, true)
end
return self.mailIsOpen
end
return MailData