Commit 2ca9397254f418e2cff5487421720942c35ab1f3
1 parent
913e070e
添加邮件表
Showing
9 changed files
with
100 additions
and
80 deletions
Show diff stats
src/actions/EmailAction.lua
... | ... | @@ -3,22 +3,32 @@ local tarr2tab = table.array2Table |
3 | 3 | |
4 | 4 | local function loadEmails(roleId) |
5 | 5 | local emails = {} |
6 | - local rds = string.format(R_EMAIL, roleId) | |
7 | - local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1) | |
8 | - local redret = redisproxy:pipelining(function (red) | |
9 | - for _, id in ipairs(ids) do | |
10 | - red:hgetall(string.format("email:%d:%s", roleId, id)) | |
11 | - end | |
12 | - end) | |
13 | - for index, id in ipairs(ids) do | |
14 | - local email = require("models.Email").new({key = string.format("email:%d:%s", roleId, id)}) | |
15 | - if email:load(tarr2tab(redret[index])) then | |
6 | + local res = mysqlproxy:query(string.format("SELECT * FROM `Email` WHERE `roleId` = %s", roleId)) | |
7 | + for _, data in ipairs(res) do | |
8 | + local email = require("models.Email").new({key = string.format("%d",data.id), id=data.id}) | |
9 | + if email:load(data) then | |
16 | 10 | table.insert(emails, email) |
17 | 11 | end |
18 | 12 | end |
19 | 13 | return emails |
20 | 14 | end |
21 | 15 | |
16 | +local function delExpireEmails(roleId) | |
17 | + local sql = [[ | |
18 | + DELETE FROM Email | |
19 | + WHERE id IN ( | |
20 | + SELECT id FROM ( | |
21 | + SELECT id | |
22 | + FROM Email WHERE roleId=%s | |
23 | + ORDER BY createtime DESC, id desc | |
24 | + LIMIT %s, 100000 | |
25 | + ) a | |
26 | + ); | |
27 | + ]] | |
28 | + sql = string.format(sql, roleId, EMAIL_LIMIT) | |
29 | + mysqlproxy:query(sql) | |
30 | +end | |
31 | + | |
22 | 32 | function _M.listRpc(agent, data) |
23 | 33 | local role = agent.role |
24 | 34 | local roleId = role:getProperty("id") |
... | ... | @@ -51,7 +61,7 @@ function _M.listRpc(agent, data) |
51 | 61 | if flag and ( not email.mid or tonum(email.mid) == mid ) |
52 | 62 | and ( not email.endtime or tonum(email.endtime) > now )then |
53 | 63 | local time = math.max(tonum(email.timestamp, 0) , tonum(email.createtime)) |
54 | - redisproxy:insertEmail({ | |
64 | + mysqlproxy:insertEmail({ | |
55 | 65 | roleId = roleId, |
56 | 66 | emailId = 0, |
57 | 67 | createtime = time, |
... | ... | @@ -64,6 +74,7 @@ function _M.listRpc(agent, data) |
64 | 74 | end |
65 | 75 | end |
66 | 76 | end |
77 | + delExpireEmails(roleId) | |
67 | 78 | |
68 | 79 | local emails = loadEmails(roleId) |
69 | 80 | for _, email in ipairs(emails) do |
... | ... | @@ -120,9 +131,8 @@ function _M.drawAttachRpc(agent, data) |
120 | 131 | local msg = MsgPack.unpack(data) |
121 | 132 | local id = msg.id |
122 | 133 | |
123 | - local rds = string.format(R_EMAIL_ITEM, roleId, id) | |
124 | 134 | |
125 | - local email = require("models.Email").new({key = rds}) | |
135 | + local email = require("models.Email").new({key = string.format("%d", id), id = id}) | |
126 | 136 | if not email:load() then return end |
127 | 137 | |
128 | 138 | local attachments = getEmailAttachments(email) |
... | ... | @@ -143,8 +153,7 @@ function _M.checkRpc(agent, data) |
143 | 153 | local msg = MsgPack.unpack(data) |
144 | 154 | local id = msg.id |
145 | 155 | |
146 | - local emailRds = string.format("email:%d:%s", roleId, id) | |
147 | - local email = require("models.Email").new({key = emailRds}) | |
156 | + local email = require("models.Email").new({key = string.format("%d", id), id = id}) | |
148 | 157 | if not email:load() then return end |
149 | 158 | |
150 | 159 | email:setProperty("status", 1) |
... | ... | @@ -158,33 +167,21 @@ end |
158 | 167 | function _M.delRpc(agent, data) |
159 | 168 | local role = agent.role |
160 | 169 | local roleId = role:getProperty("id") |
161 | - | |
162 | - local rds = string.format(R_EMAIL, roleId) | |
163 | - local ids = redisproxy:lrange(rds, 0, EMAIL_LIMIT - 1) | |
164 | - local now = skynet.timex() | |
165 | 170 | local result = {} |
166 | - local emailSet = csvdb["emailCsv"] | |
167 | - | |
168 | - redisproxy:pipelining(function (red) | |
169 | - for _, id in ipairs(ids) do | |
170 | - local emailRds = string.format("email:%d:%s", roleId, id) | |
171 | - local email = require("models.Email").new({key = emailRds}) | |
172 | - if email:load() then | |
173 | - local status = email:getProperty("status") | |
174 | - local attachments = email:getProperty("attachments") | |
175 | - local emailData = emailSet[email:getProperty("emailId")] | |
176 | - if emailData and attachments:len() == 0 then | |
177 | - attachments = emailData.attachment | |
178 | - end | |
179 | - if status == 2 or (status == 1 and attachments:len() == 0) then | |
180 | - email:log(role, 3) | |
181 | - red:lrem(rds, 0, id) | |
182 | - red:del(emailRds) | |
183 | - result[tonum(id)] = 1 | |
184 | - end | |
185 | - end | |
171 | + local tmp = {} | |
172 | + local emails = loadEmails(roleId) | |
173 | + for _, email in ipairs(emails) do | |
174 | + local attachments = getEmailAttachments(email) | |
175 | + | |
176 | + if email:getProperty("status") == 2 or (attachments == "" and email:getProperty("status") == 1) then | |
177 | + result[email:getProperty("id")] = 1 | |
178 | + email:log(role, 3) | |
179 | + table.insert(tmp, email:getProperty("id")) | |
186 | 180 | end |
187 | - end) | |
181 | + end | |
182 | + | |
183 | + mysqlproxy:query(string.format("DELETE FROM `Email` WHERE `id` in (%s)", table.concat(tmp, ","))) | |
184 | + | |
188 | 185 | for delId, _ in ipairs(result) do |
189 | 186 | role:mylog("mail_action", {desc = "del_mail", int1 = delId}) |
190 | 187 | end | ... | ... |
src/actions/GmAction.lua
... | ... | @@ -498,7 +498,7 @@ table.insert(helpDes, {"发送邮件", "email", "id", "奖励"}) |
498 | 498 | function _M.email(role, pms) |
499 | 499 | local id = tonum(pms.pm1, 0) |
500 | 500 | local reward = pms.pm2 |
501 | - redisproxy:insertEmail({ | |
501 | + mysqlproxy:insertEmail({ | |
502 | 502 | roleId = role:getProperty("id"), |
503 | 503 | emailId = id, |
504 | 504 | createtime = skynet.timex(), | ... | ... |
src/actions/RoleAction.lua
... | ... | @@ -14,6 +14,8 @@ local table_insert = table.insert |
14 | 14 | local tconcat = table.concat |
15 | 15 | local httpc = require("http.httpc") |
16 | 16 | |
17 | +require "utils.MysqlUtil" | |
18 | + | |
17 | 19 | local WAVE_HERO_NUMS = 150 |
18 | 20 | local WAVE_RUNE_NUMS = 150 |
19 | 21 | |
... | ... | @@ -154,7 +156,8 @@ function _M.loginRpc( agent, data ) |
154 | 156 | end |
155 | 157 | end |
156 | 158 | |
157 | - SERV_OPEN = redisproxy:hget("autoincrement_set", "server_start") | |
159 | + --SERV_OPEN = redisproxy:hget("autoincrement_set", "server_start") | |
160 | + SERV_OPEN = getDbCfgVal("server_info", "server_start", "str_value") | |
158 | 161 | |
159 | 162 | role:changeStructVersion() -- 数据结构 版本更新 |
160 | 163 | role:getAdvData(true) -- 清掉不合格的数据 |
... | ... | @@ -364,7 +367,7 @@ function _M.createRpc(agent, data) |
364 | 367 | |
365 | 368 | newRole:award(globalCsv.birthItem, {log = {desc = "birth"}, notNotify = true}) |
366 | 369 | -- 欢迎邮件 |
367 | - redisproxy:insertEmail({roleId = roleId, emailId = 1}) | |
370 | + mysqlproxy:insertEmail({roleId = roleId, emailId = 1}) | |
368 | 371 | |
369 | 372 | if msg.newuser then |
370 | 373 | newRole:log("onCreateAccount") |
... | ... | @@ -394,9 +397,9 @@ function _M.createRpc(agent, data) |
394 | 397 | reward = reward:setv(itemId, count) |
395 | 398 | end |
396 | 399 | if back.reward[70] then |
397 | - redisproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward2, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) | |
400 | + mysqlproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward2, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) | |
398 | 401 | else |
399 | - redisproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) | |
402 | + mysqlproxy:insertEmail({roleId = roleId, emailId = MailId.CBBackAward, contentPms = {back.money}, createtime = skynet.timex(), attachments = reward}) | |
400 | 403 | end |
401 | 404 | newRole:mylog("cbback", {key1 = uid, int2 = roleId}) |
402 | 405 | end | ... | ... |
src/models/Email.lua
1 | -local Email = class("Email", require("shared.ModelBase")) | |
1 | +local Email = class("Email", require("shared.ModelBaseMysql")) | |
2 | 2 | |
3 | 3 | function Email:ctor(properties) |
4 | 4 | Email.super.ctor(self, properties) |
5 | 5 | end |
6 | 6 | |
7 | 7 | Email.schema = { |
8 | - key = {"string"}, -- redis key | |
9 | - id = {"number", 0}, -- 数据库ID | |
8 | + id = {"number", 0, "pri_auto"}, -- 数据库ID | |
9 | + roleId = {"number", 0, "index"}, -- 角色 ID | |
10 | 10 | emailId = {"number", 0}, -- 邮件csv ID |
11 | 11 | title = {"string", ""}, -- 邮件标题 |
12 | 12 | stitle = {"string", ""}, -- 小标题 |
13 | 13 | content = {"string", ""}, -- 邮件正文 |
14 | - attachments = {"string", ""}, | |
14 | + attachments = {"string", "", 512}, | |
15 | 15 | status = {"number", 0}, -- 邮件状态: 未读, 已读, 已领取 |
16 | 16 | createtime = {"number", skynet.timex()}, |
17 | 17 | contentPms = {"table", {}}, | ... | ... |
src/models/RolePlugin.lua
... | ... | @@ -234,7 +234,7 @@ function RolePlugin.bind(Role) |
234 | 234 | local headData = csvdb["player_iconCsv"][itemId] |
235 | 235 | -- pvp 跨服竞技场奖励 |
236 | 236 | if headData and headData.path == 2 then |
237 | - redisproxy:insertEmail({roleId = self:getProperty("id"), emailId = 19}) | |
237 | + mysqlproxy:insertEmail({roleId = self:getProperty("id"), emailId = 19}) | |
238 | 238 | end |
239 | 239 | end |
240 | 240 | end |
... | ... | @@ -834,12 +834,14 @@ function RolePlugin.bind(Role) |
834 | 834 | end |
835 | 835 | |
836 | 836 | -- delete rune |
837 | - redisproxy:pipelining(function (red) | |
838 | - for _, runeId in pairs(bDel) do | |
839 | - red:del(string.format(R_RUNE, roleId, runeId)) | |
840 | - red:srem(string.format(R_RUNEIDS, roleId), runeId) | |
841 | - end | |
842 | - end) | |
837 | + --redisproxy:pipelining(function (red) | |
838 | + -- for _, runeId in pairs(bDel) do | |
839 | + -- red:del(string.format(R_RUNE, roleId, runeId)) | |
840 | + -- red:srem(string.format(R_RUNEIDS, roleId), runeId) | |
841 | + -- end | |
842 | + --end) | |
843 | + mysqlproxy:query(string.format("DELETE FROM `Rune` WHERE `uid` in (%s)", table.concat(bDel, ","))) | |
844 | + | |
843 | 845 | local response = {} |
844 | 846 | for _, runeId in pairs(bDel) do |
845 | 847 | table.insert(response, {uid = runeId, bDel = true}) |
... | ... | @@ -1612,21 +1614,10 @@ function RolePlugin.bind(Role) |
1612 | 1614 | end |
1613 | 1615 | |
1614 | 1616 | local roleId = self:getProperty("id") |
1615 | - local email_rds = string.format(R_EMAIL, roleId) | |
1616 | - | |
1617 | - local emailIds = redisproxy:lrange(email_rds, 0, EMAIL_LIMIT - 1) or {} | |
1618 | - local redret = redisproxy:pipelining(function (red) | |
1619 | - for _, id in ipairs(emailIds) do | |
1620 | - red:hget(string.format(R_EMAIL_ITEM, roleId, id), "status") | |
1621 | - end | |
1622 | - end) | |
1617 | + local res = mysqlproxy:query(string.format("SELECT `id` FROM `Email` WHERE `roleId` = %d AND `status` = 0;", roleId)) | |
1623 | 1618 | |
1624 | 1619 | self.SendMailFlag = false |
1625 | - for index, id in ipairs(emailIds) do | |
1626 | - if tonumber(redret[index]) == 0 then | |
1627 | - return true | |
1628 | - end | |
1629 | - end | |
1620 | + return next(res) | |
1630 | 1621 | end |
1631 | 1622 | |
1632 | 1623 | checks["pvphg"] = function() |
... | ... | @@ -1954,7 +1945,7 @@ function RolePlugin.bind(Role) |
1954 | 1945 | for k, v in pairs(tgift) do |
1955 | 1946 | gift = gift .. k.."="..v.." " |
1956 | 1947 | end |
1957 | - redisproxy:insertEmail({roleId = self:getProperty("id"), emailId = mailId, createtime = createTime, attachments = gift, contentPms = contentPms}) | |
1948 | + mysqlproxy:insertEmail({roleId = self:getProperty("id"), emailId = mailId, createtime = createTime, attachments = gift, contentPms = contentPms}) | |
1958 | 1949 | self.sendMailFlag = true |
1959 | 1950 | end |
1960 | 1951 | ... | ... |
src/services/dbseed.lua
... | ... | @@ -102,7 +102,7 @@ local function initAdvSeasonTable() |
102 | 102 | end |
103 | 103 | |
104 | 104 | local function checkRoleTables() |
105 | - local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order"} | |
105 | + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order", "Email"} | |
106 | 106 | for _, name in ipairs(list) do |
107 | 107 | local obj = require("models."..name).new({key = "key"}) |
108 | 108 | print("check table [" .. name .. "] begin.") | ... | ... |
src/services/globald.lua
... | ... | @@ -135,7 +135,7 @@ local function check_battle_act_close() |
135 | 135 | rankIndex = rankIndex + 1 |
136 | 136 | for _, cfg in pairs(actData) do |
137 | 137 | if rankIndex <= cfg.rank then |
138 | - redisproxy:insertEmail({ | |
138 | + mysqlproxy:insertEmail({ | |
139 | 139 | roleId = roleId, |
140 | 140 | emailId = cfg.email_1, |
141 | 141 | attachments = cfg.reward_1, | ... | ... |
src/shared/ModelBaseMysql.lua
... | ... | @@ -53,7 +53,7 @@ end |
53 | 53 | function ModelBaseMysql:getPriKey() |
54 | 54 | for k, v in pairs(self.class.schema) do |
55 | 55 | local objType, def, keyType, length = table_unpack(v) |
56 | - if keyType == "pri" then | |
56 | + if keyType == "pri" or keyType == "pri_auto" then | |
57 | 57 | self.pri_key = k |
58 | 58 | break |
59 | 59 | end |
... | ... | @@ -310,7 +310,7 @@ end |
310 | 310 | function ModelBaseMysql:checkTableSchema() |
311 | 311 | -- 1.检测是否表存在 |
312 | 312 | local typeMap = { |
313 | - number = {"int", 0, 10}, | |
313 | + number = {"int", 0}, | |
314 | 314 | string = {"varchar", "", 128}, |
315 | 315 | table = {"blob", "NULL"}, |
316 | 316 | pri = {"bigint", 0}, |
... | ... | @@ -330,15 +330,16 @@ function ModelBaseMysql:checkTableSchema() |
330 | 330 | ) ; |
331 | 331 | ]] |
332 | 332 | local field_tpl_str = "`%s` %s%s DEFAULT %s" |
333 | + local auto_increment_str = "`%s` %s NOT NULL AUTO_INCREMENT" | |
333 | 334 | local field_str = "" |
334 | 335 | |
335 | - local res = mysqlproxy:query("desc ".. tbName .. ";") | |
336 | + local res = mysqlproxy:query("desc `".. tbName .. "`;") | |
336 | 337 | local keyList = {} |
337 | 338 | if res["err"] then -- 表不存在 |
338 | 339 | local schema = {} |
339 | 340 | for k, v in pairs(self.class.schema) do |
340 | 341 | local keyType = v[3] |
341 | - if keyType == "pri" then | |
342 | + if keyType == "pri" or keyType == "pri_auto" then | |
342 | 343 | self.pri_key = k |
343 | 344 | table_insert(schema, 1, {k, v}) |
344 | 345 | else |
... | ... | @@ -351,9 +352,16 @@ function ModelBaseMysql:checkTableSchema() |
351 | 352 | for _, tbl in ipairs(schema) do |
352 | 353 | local k, v = tbl[1], tbl[2] |
353 | 354 | local objType, def, keyType, length = table_unpack(v) |
355 | + local isAutoPriKey = false | |
354 | 356 | assert(typeMap[objType], string_format("schema invalid type, %s, %s", tbName, k)) |
355 | 357 | -- 主键使用bigint存储 |
356 | 358 | if keyType == "pri" then objType = "pri" end |
359 | + if keyType == "pri" or keyType == "pri_auto" then | |
360 | + objType = "pri" | |
361 | + if keyType == "pri_auto" then | |
362 | + isAutoPriKey = true | |
363 | + end | |
364 | + end | |
357 | 365 | |
358 | 366 | local info = typeMap[objType] |
359 | 367 | local suffix = "" |
... | ... | @@ -370,7 +378,11 @@ function ModelBaseMysql:checkTableSchema() |
370 | 378 | def = "NULL" |
371 | 379 | end |
372 | 380 | |
373 | - field_str = field_str .. string.format(field_tpl_str..",", k, fieldType, suffix, def) | |
381 | + if not isAutoPriKey then | |
382 | + field_str = field_str .. string.format(field_tpl_str..",", k, fieldType, suffix, def) | |
383 | + else | |
384 | + field_str = field_str .. string.format(auto_increment_str..",", k, fieldType) | |
385 | + end | |
374 | 386 | end |
375 | 387 | |
376 | 388 | assert(self.pri_key, string_format("table not include primary key, [%s]", tbName)) |
... | ... | @@ -379,6 +391,7 @@ function ModelBaseMysql:checkTableSchema() |
379 | 391 | index_key_str = index_key_str .. string_format(index_tpl_str, k, k) |
380 | 392 | end |
381 | 393 | -- 创建表格 |
394 | + print(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) | |
382 | 395 | mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) |
383 | 396 | else -- 检测是否有添加新字段 |
384 | 397 | local addCol = {} |
... | ... | @@ -388,9 +401,6 @@ function ModelBaseMysql:checkTableSchema() |
388 | 401 | end |
389 | 402 | for k, v in pairs(self.class.schema) do |
390 | 403 | local objType, def, keyType, length = table_unpack(v) |
391 | - if keyType == "pri" then | |
392 | - self.pri_key = k | |
393 | - end | |
394 | 404 | if not curCols[k] then |
395 | 405 | print(string_format("table [%s] add new column [%s]", tbName, k)) |
396 | 406 | assert(typeMap[objType], string_format("schema invalid type, [%s], [%s]", tbName, k)) | ... | ... |
src/shared/mysqlproxy.lua
... | ... | @@ -31,4 +31,23 @@ setmetatable(mysqlproxy, { __index = function(t, k) |
31 | 31 | return f |
32 | 32 | end}) |
33 | 33 | |
34 | +function mysqlproxy:insertEmail(params) | |
35 | + local pms = { | |
36 | + key = string.format("%s", 0), | |
37 | + roleId = params.roleId, | |
38 | + emailId = params.emailId, | |
39 | + createtime = params.createtime or skynet.timex(), | |
40 | + contentPms = params.contentPms or {}, | |
41 | + rewardPms = params.rewardPms or {}, | |
42 | + title = params.title or "", | |
43 | + stitle = params.stitle or "", | |
44 | + content = params.content or "", | |
45 | + attachments = params.attachments or "", | |
46 | + } | |
47 | + | |
48 | + local email = require("models.Email").new(pms) | |
49 | + email:create() | |
50 | + return true | |
51 | +end | |
52 | + | |
34 | 53 | return mysqlproxy |
35 | 54 | \ No newline at end of file | ... | ... |