From 2ca9397254f418e2cff5487421720942c35ab1f3 Mon Sep 17 00:00:00 2001 From: liuzujun <307836273@qq.com> Date: Tue, 9 Mar 2021 20:50:12 +0800 Subject: [PATCH] 添加邮件表 --- src/actions/EmailAction.lua | 77 +++++++++++++++++++++++++++++++++++++---------------------------------------- src/actions/GmAction.lua | 2 +- src/actions/RoleAction.lua | 11 +++++++---- src/models/Email.lua | 8 ++++---- src/models/RolePlugin.lua | 33 ++++++++++++--------------------- src/services/dbseed.lua | 2 +- src/services/globald.lua | 2 +- src/shared/ModelBaseMysql.lua | 26 ++++++++++++++++++-------- src/shared/mysqlproxy.lua | 19 +++++++++++++++++++ 9 files changed, 100 insertions(+), 80 deletions(-) diff --git a/src/actions/EmailAction.lua b/src/actions/EmailAction.lua index eea28fd..ada7613 100644 --- a/src/actions/EmailAction.lua +++ b/src/actions/EmailAction.lua @@ -3,22 +3,32 @@ 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 + local res = mysqlproxy:query(string.format("SELECT * FROM `Email` WHERE `roleId` = %s", roleId)) + for _, data in ipairs(res) do + local email = require("models.Email").new({key = string.format("%d",data.id), id=data.id}) + if email:load(data) then table.insert(emails, email) end end return emails end +local function delExpireEmails(roleId) + local sql = [[ + DELETE FROM Email + WHERE id IN ( + SELECT id FROM ( + SELECT id + FROM Email WHERE roleId=%s + ORDER BY createtime DESC, id desc + LIMIT %s, 100000 + ) a + ); + ]] + sql = string.format(sql, roleId, EMAIL_LIMIT) + mysqlproxy:query(sql) +end + function _M.listRpc(agent, data) local role = agent.role local roleId = role:getProperty("id") @@ -51,7 +61,7 @@ function _M.listRpc(agent, data) if flag and ( not email.mid or tonum(email.mid) == mid ) and ( not email.endtime or tonum(email.endtime) > now )then local time = math.max(tonum(email.timestamp, 0) , tonum(email.createtime)) - redisproxy:insertEmail({ + mysqlproxy:insertEmail({ roleId = roleId, emailId = 0, createtime = time, @@ -64,6 +74,7 @@ function _M.listRpc(agent, data) end end end + delExpireEmails(roleId) local emails = loadEmails(roleId) for _, email in ipairs(emails) do @@ -120,9 +131,8 @@ function _M.drawAttachRpc(agent, data) 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}) + local email = require("models.Email").new({key = string.format("%d", id), id = id}) if not email:load() then return end local attachments = getEmailAttachments(email) @@ -143,8 +153,7 @@ function _M.checkRpc(agent, data) local msg = MsgPack.unpack(data) local id = msg.id - local emailRds = string.format("email:%d:%s", roleId, id) - local email = require("models.Email").new({key = emailRds}) + local email = require("models.Email").new({key = string.format("%d", id), id = id}) if not email:load() then return end email:setProperty("status", 1) @@ -158,33 +167,21 @@ 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 - email:log(role, 3) - red:lrem(rds, 0, id) - red:del(emailRds) - result[tonum(id)] = 1 - end - end + local tmp = {} + local emails = loadEmails(roleId) + for _, email in ipairs(emails) do + local attachments = getEmailAttachments(email) + + if email:getProperty("status") == 2 or (attachments == "" and email:getProperty("status") == 1) then + result[email:getProperty("id")] = 1 + email:log(role, 3) + table.insert(tmp, email:getProperty("id")) end - end) + end + + mysqlproxy:query(string.format("DELETE FROM `Email` WHERE `id` in (%s)", table.concat(tmp, ","))) + for delId, _ in ipairs(result) do role:mylog("mail_action", {desc = "del_mail", int1 = delId}) end diff --git a/src/actions/GmAction.lua b/src/actions/GmAction.lua index b6fdb83..cd7da3a 100644 --- a/src/actions/GmAction.lua +++ b/src/actions/GmAction.lua @@ -498,7 +498,7 @@ table.insert(helpDes, {"发送邮件", "email", "id", "奖励"}) function _M.email(role, pms) local id = tonum(pms.pm1, 0) local reward = pms.pm2 - redisproxy:insertEmail({ + mysqlproxy:insertEmail({ roleId = role:getProperty("id"), emailId = id, createtime = skynet.timex(), diff --git a/src/actions/RoleAction.lua b/src/actions/RoleAction.lua index ca95114..c02baf6 100644 --- a/src/actions/RoleAction.lua +++ b/src/actions/RoleAction.lua @@ -14,6 +14,8 @@ local table_insert = table.insert local tconcat = table.concat local httpc = require("http.httpc") +require "utils.MysqlUtil" + local WAVE_HERO_NUMS = 150 local WAVE_RUNE_NUMS = 150 @@ -154,7 +156,8 @@ function _M.loginRpc( agent, data ) end end - SERV_OPEN = redisproxy:hget("autoincrement_set", "server_start") + --SERV_OPEN = redisproxy:hget("autoincrement_set", "server_start") + SERV_OPEN = getDbCfgVal("server_info", "server_start", "str_value") role:changeStructVersion() -- 数据结构 版本更新 role:getAdvData(true) -- 清掉不合格的数据 @@ -364,7 +367,7 @@ function _M.createRpc(agent, data) newRole:award(globalCsv.birthItem, {log = {desc = "birth"}, notNotify = true}) -- 欢迎邮件 - redisproxy:insertEmail({roleId = roleId, emailId = 1}) + mysqlproxy:insertEmail({roleId = roleId, emailId = 1}) if msg.newuser then newRole:log("onCreateAccount") @@ -394,9 +397,9 @@ function _M.createRpc(agent, data) reward = reward:setv(itemId, count) end if back.reward[70] then - redisproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward2, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) + mysqlproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward2, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) else - redisproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) + mysqlproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) end newRole:mylog("cbback", {key1 = uid, int2 = roleId}) end diff --git a/src/models/Email.lua b/src/models/Email.lua index 20b3d3a..bb7d710 100644 --- a/src/models/Email.lua +++ b/src/models/Email.lua @@ -1,17 +1,17 @@ -local Email = class("Email", require("shared.ModelBase")) +local Email = class("Email", require("shared.ModelBaseMysql")) function Email:ctor(properties) Email.super.ctor(self, properties) end Email.schema = { - key = {"string"}, -- redis key - id = {"number", 0}, -- 数据库ID + id = {"number", 0, "pri_auto"}, -- 数据库ID + roleId = {"number", 0, "index"}, -- 角色 ID emailId = {"number", 0}, -- 邮件csv ID title = {"string", ""}, -- 邮件标题 stitle = {"string", ""}, -- 小标题 content = {"string", ""}, -- 邮件正文 - attachments = {"string", ""}, + attachments = {"string", "", 512}, status = {"number", 0}, -- 邮件状态: 未读, 已读, 已领取 createtime = {"number", skynet.timex()}, contentPms = {"table", {}}, diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index f0f8016..634b0fd 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -234,7 +234,7 @@ function RolePlugin.bind(Role) local headData = csvdb["player_iconCsv"][itemId] -- pvp 跨服竞技场奖励 if headData and headData.path == 2 then - redisproxy:insertEmail({roleId = self:getProperty("id"), emailId = 19}) + mysqlproxy:insertEmail({roleId = self:getProperty("id"), emailId = 19}) end end end @@ -834,12 +834,14 @@ function RolePlugin.bind(Role) end -- delete rune - redisproxy:pipelining(function (red) - for _, runeId in pairs(bDel) do - red:del(string.format(R_RUNE, roleId, runeId)) - red:srem(string.format(R_RUNEIDS, roleId), runeId) - end - end) + --redisproxy:pipelining(function (red) + -- for _, runeId in pairs(bDel) do + -- red:del(string.format(R_RUNE, roleId, runeId)) + -- red:srem(string.format(R_RUNEIDS, roleId), runeId) + -- end + --end) + mysqlproxy:query(string.format("DELETE FROM `Rune` WHERE `uid` in (%s)", table.concat(bDel, ","))) + local response = {} for _, runeId in pairs(bDel) do table.insert(response, {uid = runeId, bDel = true}) @@ -1612,21 +1614,10 @@ function RolePlugin.bind(Role) end local roleId = self:getProperty("id") - local email_rds = string.format(R_EMAIL, roleId) - - local emailIds = redisproxy:lrange(email_rds, 0, EMAIL_LIMIT - 1) or {} - local redret = redisproxy:pipelining(function (red) - for _, id in ipairs(emailIds) do - red:hget(string.format(R_EMAIL_ITEM, roleId, id), "status") - end - end) + local res = mysqlproxy:query(string.format("SELECT `id` FROM `Email` WHERE `roleId` = %d AND `status` = 0;", roleId)) self.SendMailFlag = false - for index, id in ipairs(emailIds) do - if tonumber(redret[index]) == 0 then - return true - end - end + return next(res) end checks["pvphg"] = function() @@ -1954,7 +1945,7 @@ function RolePlugin.bind(Role) for k, v in pairs(tgift) do gift = gift .. k.."="..v.." " end - redisproxy:insertEmail({roleId = self:getProperty("id"), emailId = mailId, createtime = createTime, attachments = gift, contentPms = contentPms}) + mysqlproxy:insertEmail({roleId = self:getProperty("id"), emailId = mailId, createtime = createTime, attachments = gift, contentPms = contentPms}) self.sendMailFlag = true end diff --git a/src/services/dbseed.lua b/src/services/dbseed.lua index dbed0e7..9d5592e 100644 --- a/src/services/dbseed.lua +++ b/src/services/dbseed.lua @@ -102,7 +102,7 @@ local function initAdvSeasonTable() end local function checkRoleTables() - local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order"} + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order", "Email"} for _, name in ipairs(list) do local obj = require("models."..name).new({key = "key"}) print("check table [" .. name .. "] begin.") diff --git a/src/services/globald.lua b/src/services/globald.lua index b11f984..39dc9b8 100644 --- a/src/services/globald.lua +++ b/src/services/globald.lua @@ -135,7 +135,7 @@ local function check_battle_act_close() rankIndex = rankIndex + 1 for _, cfg in pairs(actData) do if rankIndex <= cfg.rank then - redisproxy:insertEmail({ + mysqlproxy:insertEmail({ roleId = roleId, emailId = cfg.email_1, attachments = cfg.reward_1, diff --git a/src/shared/ModelBaseMysql.lua b/src/shared/ModelBaseMysql.lua index 033750b..c4c9d52 100644 --- a/src/shared/ModelBaseMysql.lua +++ b/src/shared/ModelBaseMysql.lua @@ -53,7 +53,7 @@ end function ModelBaseMysql:getPriKey() for k, v in pairs(self.class.schema) do local objType, def, keyType, length = table_unpack(v) - if keyType == "pri" then + if keyType == "pri" or keyType == "pri_auto" then self.pri_key = k break end @@ -310,7 +310,7 @@ end function ModelBaseMysql:checkTableSchema() -- 1.检测是否表存在 local typeMap = { - number = {"int", 0, 10}, + number = {"int", 0}, string = {"varchar", "", 128}, table = {"blob", "NULL"}, pri = {"bigint", 0}, @@ -330,15 +330,16 @@ function ModelBaseMysql:checkTableSchema() ) ; ]] local field_tpl_str = "`%s` %s%s DEFAULT %s" + local auto_increment_str = "`%s` %s NOT NULL AUTO_INCREMENT" local field_str = "" - local res = mysqlproxy:query("desc ".. tbName .. ";") + local res = mysqlproxy:query("desc `".. tbName .. "`;") local keyList = {} if res["err"] then -- 表不存在 local schema = {} for k, v in pairs(self.class.schema) do local keyType = v[3] - if keyType == "pri" then + if keyType == "pri" or keyType == "pri_auto" then self.pri_key = k table_insert(schema, 1, {k, v}) else @@ -351,9 +352,16 @@ function ModelBaseMysql:checkTableSchema() for _, tbl in ipairs(schema) do local k, v = tbl[1], tbl[2] local objType, def, keyType, length = table_unpack(v) + local isAutoPriKey = false assert(typeMap[objType], string_format("schema invalid type, %s, %s", tbName, k)) -- 主键使用bigint存储 if keyType == "pri" then objType = "pri" end + if keyType == "pri" or keyType == "pri_auto" then + objType = "pri" + if keyType == "pri_auto" then + isAutoPriKey = true + end + end local info = typeMap[objType] local suffix = "" @@ -370,7 +378,11 @@ function ModelBaseMysql:checkTableSchema() def = "NULL" end - field_str = field_str .. string.format(field_tpl_str..",", k, fieldType, suffix, def) + if not isAutoPriKey then + field_str = field_str .. string.format(field_tpl_str..",", k, fieldType, suffix, def) + else + field_str = field_str .. string.format(auto_increment_str..",", k, fieldType) + end end assert(self.pri_key, string_format("table not include primary key, [%s]", tbName)) @@ -379,6 +391,7 @@ function ModelBaseMysql:checkTableSchema() index_key_str = index_key_str .. string_format(index_tpl_str, k, k) end -- 创建表格 + print(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) else -- 检测是否有添加新字段 local addCol = {} @@ -388,9 +401,6 @@ function ModelBaseMysql:checkTableSchema() end for k, v in pairs(self.class.schema) do local objType, def, keyType, length = table_unpack(v) - if keyType == "pri" then - self.pri_key = k - end if not curCols[k] then print(string_format("table [%s] add new column [%s]", tbName, k)) assert(typeMap[objType], string_format("schema invalid type, [%s], [%s]", tbName, k)) diff --git a/src/shared/mysqlproxy.lua b/src/shared/mysqlproxy.lua index 3685436..36c3ca2 100644 --- a/src/shared/mysqlproxy.lua +++ b/src/shared/mysqlproxy.lua @@ -31,4 +31,23 @@ setmetatable(mysqlproxy, { __index = function(t, k) return f end}) +function mysqlproxy:insertEmail(params) + local pms = { + key = string.format("%s", 0), + roleId = params.roleId, + emailId = params.emailId, + createtime = params.createtime or skynet.timex(), + contentPms = params.contentPms or {}, + rewardPms = params.rewardPms or {}, + title = params.title or "", + stitle = params.stitle or "", + content = params.content or "", + attachments = params.attachments or "", + } + + local email = require("models.Email").new(pms) + email:create() + return true +end + return mysqlproxy \ No newline at end of file -- libgit2 0.21.2