local _M = {} local tarr2tab = table.array2Table local function loadEmails(roleId) local emails = {} local rds = string.format(R_EMAIL, roleId) local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1) local redret = redisproxy:pipelining(function (red) for _, id in ipairs(ids) do red:hgetall(string.format("email:%d:%s", roleId, id)) end end) for index, id in ipairs(ids) do local email = require("models.Email").new({key = string.format("email:%d:%s", roleId, id)}) if email:load(tarr2tab(redret[index])) then table.insert(emails, email) end end return emails end function _M.listRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") local now = skynet.timex() local result = {} local mid = role:getProperty("sid") local globalEmail = redisproxy:hget("autoincrement_set", "email") globalEmail = tonum(globalEmail) local emailSync = role:getProperty("emailSync") if globalEmail > emailSync then role:setProperty("emailSync", globalEmail) emailSync = math.max(emailSync + 1, globalEmail - EMAIL_LIMIT + 1) local result = redisproxy:pipelining(function (red) for id = emailSync, globalEmail do red:hgetall(string.format("globalEmail:%s", id)) end end) local count = 1 for _, data in ipairs(result) do local email = tarr2tab(data) if tonum(email.createtime) > role:getProperty("ctime") and ( not email.mid or tonum(email.mid) == mid ) and ( not email.endtime or tonum(email.endtime) > now )then local time = email.timestamp and tonum(email.timestamp) or email.createtime redisproxy:insertEmail({ roleId = roleId, emailId = 0, createtime = time, title = email.title, content = email.content, attachments = email.attachments }) --role:log("mail_actions", {desc = "get_global", s1 = email.title, s2 = email.attachments}) end end end local emails = loadEmails(roleId) for _, email in ipairs(emails) do table.insert(result, email:data()) end SendPacket(actionCodes.Email_listRpc, MsgPack.pack({list = result})) return true end local function getEmailAttachments( email ) if email:getProperty("status") == 2 then return "" end local attachments = email:getProperty("attachments") if attachments:len() == 0 then local rewardPms = email:getProperty("rewardPms") local emailData = csvdb["emailCsv"][email:getProperty("emailId")] if emailData then attachments = emailData.attachment:format(table.unpack(rewardPms)) end end return attachments end function _M.drawAllAttachRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") local reward = {} local ids = {} local emails = loadEmails(roleId) redisproxy:pipelining(function (red) for _, email in ipairs(emails) do local attachments = getEmailAttachments(email) if attachments ~= "" then local emailId = email:getProperty("id") local items = role:award(attachments) ids[emailId] = 1 red:hset(string.format(R_EMAIL_ITEM, roleId, emailId), "status", 2) -- role:log("mail_actions", {desc = "draw_attach", int1 = emailId, s1 = email:getProperty("title"), s2 = attachments}) for key, v in pairs(items) do reward[key] = (reward[key] or 0) + v end end end end) SendPacket(actionCodes.Email_drawAllAttachRpc, MsgPack.pack({ids = ids, reward = reward})) return true end function _M.drawAttachRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") local msg = MsgPack.unpack(data) local id = msg.id local rds = string.format(R_EMAIL_ITEM, roleId, id) local email = require("models.Email").new({key = rds}) if not email:load() then return end local attachments = getEmailAttachments(email) if attachments == "" then return end local reward = role:award(attachments) redisproxy:hset(rds, "status", 2) -- 领取标记 SendPacket(actionCodes.Email_drawAttachRpc, MsgPack.pack({reward = reward})) -- role:log("mail_actions", {desc = "draw_attach", int1 = id, s1 = email:getProperty("title"), s2 = attachments, ucode = ucode}) return true end function _M.checkRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") local msg = MsgPack.unpack(data) local id = msg.id local rds = string.format(R_EMAIL_ITEM, roleId, id) if not redisproxy:exists(rds) then return end redisproxy:hset(rds, "status", 1) -- role:log("mail_actions", {desc = "check_mail", int1 = id}) SendPacket(actionCodes.Email_checkRpc, '') return true end function _M.delRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") local rds = string.format(R_EMAIL, roleId) local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1) local now = skynet.timex() local result = {} local emailSet = csvdb["emailCsv"] redisproxy:pipelining(function (red) for _, id in ipairs(ids) do local emailRds = string.format("email:%d:%s", roleId, id) local email = require("models.Email").new({key = emailRds}) if email:load() then local status = email:getProperty("status") local attachments = email:getProperty("attachments") local emailData = emailSet[email:getProperty("emailId")] if emailData and attachments:len() == 0 then attachments = emailData.attachment end if status == 2 or (status == 1 and attachments:len() == 0) then red:lrem(rds, 0, id) red:del(emailRds) result[tonum(id)] = 1 end end end end) -- for delId, _ in ipairs(result) do -- role:log("mail_actions", {desc = "del_mail", int1 = delId}) -- end SendPacket(actionCodes.Email_delRpc, MsgPack.pack({result = result})) return true end return _M