Commit 913e070eddfdda141787f944774e3d156651d3cd
1 parent
52249374
添加订单表,全局id定时回写数据库
Showing
9 changed files
with
130 additions
and
40 deletions
Show diff stats
src/GlobalVar.lua
src/actions/HttpAction.lua
... | ... | @@ -147,6 +147,36 @@ function _M.gm_action(query) |
147 | 147 | return status |
148 | 148 | end |
149 | 149 | |
150 | +function _M.pay_action(query) | |
151 | + local gmFuncs = require "actions.GmAction" | |
152 | + if not query.cmd or not query.order or not gmFuncs[query.cmd] then return "指令不存在" end | |
153 | + local mysqlproxy = require "shared.mysqlproxy" | |
154 | + local res = mysqlproxy:query(string.format("SELECT roleid FROM `Order` WHERE `id` = %s", query.order)) | |
155 | + if res[1] then | |
156 | + query.id = res[1]["roleid"] | |
157 | + else | |
158 | + return "订单非法" | |
159 | + end | |
160 | + -- 在线操作 | |
161 | + query.id = tonumber(query.id) | |
162 | + local isOn = proc_online(query.cmd, query.id, query) | |
163 | + if isOn ~= "not_online" then | |
164 | + return isOn | |
165 | + end | |
166 | + -- 离线操作 | |
167 | + local role = require("models.Role").new({key = string.format("%d", query.id)}) | |
168 | + local ret = role:load() | |
169 | + if not ret then | |
170 | + return "角色不存在" | |
171 | + end | |
172 | + role:loadAll() | |
173 | + role:startActionUcode() | |
174 | + local status = gmFuncs[query.cmd](role, query) | |
175 | + role:endActionUcode() | |
176 | + | |
177 | + return status | |
178 | +end | |
179 | + | |
150 | 180 | function _M.query_role(query) |
151 | 181 | if not query.uid then return "not found" end |
152 | 182 | local user = redisproxy:get(string.format("uid:%s", query.uid)) | ... | ... |
src/actions/StoreAction.lua
src/models/Order.lua
1 | -local Order = class("Order", require("shared.ModelBase")) | |
1 | +local Order = class("Order", require("shared.ModelBaseMysql")) | |
2 | 2 | |
3 | 3 | function Order:ctor(properties) |
4 | - Order.super.ctor(self, properties) | |
4 | + Order.super.ctor(self, properties) | |
5 | 5 | end |
6 | 6 | |
7 | 7 | Order.schema = { |
8 | - key = {"string"}, -- redis key | |
9 | - order = {"string"}, -- 自己订单号 | |
8 | + id = {"number", 0, "pri"}, -- 自己的订单号 | |
9 | + roleid = {"number", 0, "index"}, | |
10 | + --order = {"string"}, -- 自己订单号 | |
10 | 11 | rechargeId = {"number", 0}, |
11 | 12 | transactionId = {"string", ""}, |
12 | 13 | createTime = {"number", skynet.timex()}, -- 订单创建时间 | ... | ... |
src/models/RolePlugin.lua
... | ... | @@ -1740,24 +1740,27 @@ function RolePlugin.bind(Role) |
1740 | 1740 | if not self.activity:isOpenById(rechargeData.activity_id, "ActShopGoods") then return "" end |
1741 | 1741 | end |
1742 | 1742 | |
1743 | - local orderId = redisproxy:hget(string.format(R_ORDERS, roleId), rechargeId) | |
1744 | - if orderId then | |
1745 | - local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) }) | |
1743 | + local orderId = redisproxy:hget(string.format(R_ORDERS, roleId), rechargeId) | |
1744 | + if orderId then | |
1745 | + local uid = orderId * MAX_SVR_ID + serverId | |
1746 | + local orderObject = require("models.Order").new({ key = string.format("%d", uid), id = uid }) | |
1746 | 1747 | if orderObject:load() and orderObject:getProperty("rechargeId") == rechargeId and math.abs(skynet.timex() - orderObject:getProperty("createTime")) < 5 * 60 then |
1747 | - return string.format("%d_%d_%d", serverId, roleId, orderId) | |
1748 | + return string.format("%d", uid) | |
1748 | 1749 | end |
1749 | - end | |
1750 | + end | |
1750 | 1751 | |
1751 | 1752 | orderId = redisproxy:hincrby("autoincrement_set", "order", 1) |
1752 | - local partnerOrderId = string.format("%d_%d_%d", serverId, roleId, orderId) | |
1753 | + local uid = orderId * MAX_SVR_ID + serverId | |
1754 | + local partnerOrderId = string.format("%d", orderId * MAX_SVR_ID + serverId) | |
1753 | 1755 | local orderKey = string.format(R_ORDER, roleId, orderId) |
1754 | 1756 | redisproxy:del(orderKey) -- 删掉可能有了 |
1755 | 1757 | local order = require("models.Order").new({ |
1756 | - key = orderKey, | |
1757 | - order = partnerOrderId, | |
1758 | + key = partnerOrderId, | |
1759 | + id = tonum(partnerOrderId), | |
1758 | 1760 | rechargeId = rechargeId, |
1759 | 1761 | createTime = skynet.timex(), |
1760 | 1762 | transactionId = transactionId, |
1763 | + roleid = roleId, | |
1761 | 1764 | sid = self:getProperty("sid"), |
1762 | 1765 | }) |
1763 | 1766 | order:create() |
... | ... | @@ -1779,10 +1782,8 @@ function RolePlugin.bind(Role) |
1779 | 1782 | --]] |
1780 | 1783 | function Role:updatePurchaseOrder(partnerOrderStr, platformOrder, status) |
1781 | 1784 | if not partnerOrderStr then return false end |
1782 | - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)") | |
1783 | - | |
1784 | 1785 | local roleId = self:getProperty("id") |
1785 | - local orderObject = require("models.Order").new({ key = string.format(R_ORDER, roleId, orderId) }) | |
1786 | + local orderObject = require("models.Order").new({ key = string.format("%d", partnerOrderStr), id = tonum(partnerOrderStr)}) | |
1786 | 1787 | if not orderObject:load() then |
1787 | 1788 | return false |
1788 | 1789 | end |
... | ... | @@ -1791,6 +1792,14 @@ function RolePlugin.bind(Role) |
1791 | 1792 | local dataSet = csvdb["shop_rechargeCsv"][rechargeId] |
1792 | 1793 | local sid = orderObject:getProperty("sid") |
1793 | 1794 | |
1795 | + if roleId ~= orderObject:getProperty("roleid") then | |
1796 | + skynet.error(string.format("[recharge] with a different role id, current roleId:%d, order role Id:%d, order cpOrder: %s, platformOrder : %s, hadPlatformOrder: %s, id: %s, overTime : %s", | |
1797 | + roleId, orderObject:getProperty("roleid"), | |
1798 | + partnerOrderStr, platformOrder, orderObject:getProperty("transactionId"), rechargeId, orderObject:getProperty("finishTime") | |
1799 | + )) | |
1800 | + return false, "unknow" | |
1801 | + end | |
1802 | + | |
1794 | 1803 | if orderObject:getProperty("finishTime") > 0 then |
1795 | 1804 | skynet.error(string.format("[recharge] is a finish order cpOrder: %s, platformOrder : %s, hadPlatformOrder: %s, id: %s, overTime : %s", |
1796 | 1805 | partnerOrderStr, platformOrder, orderObject:getProperty("transactionId"), rechargeId, orderObject:getProperty("finishTime") | ... | ... |
src/services/dbseed.lua
... | ... | @@ -4,6 +4,7 @@ require "GlobalVar" |
4 | 4 | require "RedisKeys" |
5 | 5 | require "ProtocolCode" |
6 | 6 | require "skynet.manager" |
7 | +require "utils.MysqlUtil" | |
7 | 8 | |
8 | 9 | skynet = require "skynet" |
9 | 10 | |
... | ... | @@ -12,19 +13,31 @@ mysqlproxy = require "shared.mysqlproxy" |
12 | 13 | |
13 | 14 | SendPacket = function ( ... ) end |
14 | 15 | |
16 | + | |
15 | 17 | local function initRedisDb( ... ) |
16 | - local servId = tonumber(skynet.getenv("servId")) | |
17 | - if servId then | |
18 | - redisproxy:hsetnx("autoincrement_set", "role", servId * MAX_ROLE_NUM) | |
19 | - redisproxy:hsetnx("autoincrement_set", "union", servId * MAX_ROLE_NUM) | |
20 | - redisproxy:hsetnx("autoincrement_set", "trade", servId * MAX_ROLE_NUM * 100) | |
21 | - redisproxy:hsetnx("autoincrement_set", "email", 0) | |
22 | - redisproxy:hsetnx("autoincrement_set", "emailTimestamp", 0) | |
23 | - redisproxy:hsetnx("autoincrement_set", "delay_email", 0) | |
24 | - redisproxy:hsetnx("adv_season", "idx", 0) | |
25 | - redisproxy:hsetnx("adv_season", "chapter", globalCsv.adv_endless_default_chapter) | |
26 | - redisproxy:hsetnx("adv_season", "overTime", 0) | |
18 | + local function initAutoIncrementUid(tbName, keyName, fieldName) | |
19 | + if not fieldName then fieldName = "value" end | |
20 | + local mysqlVal = getDbCfgVal(tbName, keyName, fieldName) | |
21 | + if not mysqlVal then | |
22 | + skynet.error(string.format("get db cfg fail, table %s, key %s, field %s", tbName, keyName, fieldName)) | |
23 | + return | |
24 | + end | |
25 | + local redisVal = tonum(redisproxy:hget(tbName, keyName)) | |
26 | + if redisVal < mysqlVal then | |
27 | + redisproxy:hset(tbName, keyName, mysqlVal) | |
28 | + end | |
27 | 29 | end |
30 | + | |
31 | + initAutoIncrementUid("autoincrement_set", "role") | |
32 | + initAutoIncrementUid("autoincrement_set", "union") | |
33 | + initAutoIncrementUid("autoincrement_set", "order") | |
34 | + initAutoIncrementUid("autoincrement_set", "email") | |
35 | + initAutoIncrementUid("autoincrement_set", "emailTimestamp") | |
36 | + initAutoIncrementUid("autoincrement_set", "delay_email") | |
37 | + | |
38 | + --redisproxy:hsetnx("adv_season", "idx", 0) | |
39 | + --redisproxy:hsetnx("adv_season", "chapter", globalCsv.adv_endless_default_chapter) | |
40 | + --redisproxy:hsetnx("adv_season", "overTime", 0) | |
28 | 41 | end |
29 | 42 | |
30 | 43 | -- 初始化服务器数据库以及服务器信息表 |
... | ... | @@ -52,7 +65,7 @@ end |
52 | 65 | |
53 | 66 | local function initAutoIncreUidTable() |
54 | 67 | mysqlproxy:query [[ |
55 | - CREATE TABLE IF NOT EXISTS `auto_increment_uid` ( | |
68 | + CREATE TABLE IF NOT EXISTS `autoincrement_set` ( | |
56 | 69 | `key` varchar(45) NOT NULL, |
57 | 70 | `value` int(11) DEFAULT NULL, |
58 | 71 | PRIMARY KEY (`key`) |
... | ... | @@ -60,10 +73,10 @@ local function initAutoIncreUidTable() |
60 | 73 | ]] |
61 | 74 | local servId = tonumber(skynet.getenv("servId")) |
62 | 75 | if servId then |
63 | - local tpl = "INSERT INTO `auto_increment_uid`(`key`, `value`) values('%s', %d)" | |
76 | + local tpl = "INSERT INTO `autoincrement_set`(`key`, `value`) values('%s', %d)" | |
64 | 77 | mysqlproxy:query(string.format(tpl, "role", servId * MAX_ROLE_NUM)) |
65 | 78 | mysqlproxy:query(string.format(tpl, "union", servId * MAX_ROLE_NUM)) |
66 | - mysqlproxy:query(string.format(tpl, "trade", servId * MAX_ROLE_NUM * 100)) | |
79 | + mysqlproxy:query(string.format(tpl, "order", 0)) | |
67 | 80 | mysqlproxy:query(string.format(tpl, "email", 0)) |
68 | 81 | mysqlproxy:query(string.format(tpl, "emailTimestamp", 0)) |
69 | 82 | mysqlproxy:query(string.format(tpl, "delay_email", 0)) |
... | ... | @@ -89,7 +102,7 @@ local function initAdvSeasonTable() |
89 | 102 | end |
90 | 103 | |
91 | 104 | local function checkRoleTables() |
92 | - local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune"} | |
105 | + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune", "Order"} | |
93 | 106 | for _, name in ipairs(list) do |
94 | 107 | local obj = require("models."..name).new({key = "key"}) |
95 | 108 | print("check table [" .. name .. "] begin.") |
... | ... | @@ -100,22 +113,18 @@ end |
100 | 113 | |
101 | 114 | local steps = { |
102 | 115 | [1] = { |
103 | - handler = initRedisDb, | |
104 | - desc = "initialize redis database " | |
105 | - }, | |
106 | - [2] = { | |
107 | 116 | handler = initServerDatabase, |
108 | 117 | desc = "initialize server database " |
109 | 118 | }, |
110 | - [3] = { | |
119 | + [2] = { | |
111 | 120 | handler = initAutoIncreUidTable, |
112 | 121 | desc = "initialize auto_increment_uid table " |
113 | 122 | }, |
114 | - [4] = { | |
123 | + [3] = { | |
115 | 124 | handler = initAdvSeasonTable, |
116 | 125 | desc = "initialize adv_season table " |
117 | 126 | }, |
118 | - [5] = { | |
127 | + [4] = { | |
119 | 128 | handler = checkRoleTables, |
120 | 129 | desc = "check role tables " |
121 | 130 | } |
... | ... | @@ -136,5 +145,6 @@ skynet.start(function () |
136 | 145 | action.handler() |
137 | 146 | print(action.desc .. "finished ...") |
138 | 147 | end |
148 | + initRedisDb() | |
139 | 149 | skynet.exit() |
140 | 150 | end) |
141 | 151 | \ No newline at end of file | ... | ... |
src/services/globald.lua
... | ... | @@ -8,6 +8,7 @@ require "utils.init" |
8 | 8 | require "RedisKeys" |
9 | 9 | require "skynet.manager" |
10 | 10 | require "GlobalVar" |
11 | +require "utils.MysqlUtil" | |
11 | 12 | |
12 | 13 | |
13 | 14 | local ipairs = ipairs |
... | ... | @@ -18,6 +19,7 @@ local string_format = string.format |
18 | 19 | |
19 | 20 | local CHECK_MAIL_STATUS_INTERVAL = 100 * 60 |
20 | 21 | local CHECK_BATTLE_ACT_CLOSE_INTERVAL = 100 * 1 |
22 | +local SAVE_AUTOINCREMENT_SET_INTERVAL = 100 * 2 | |
21 | 23 | local function mailQuene() |
22 | 24 | local delayEmail = tonum(redisproxy:hget("autoincrement_set", "delay_email")) |
23 | 25 | if delayEmail == 0 then |
... | ... | @@ -153,7 +155,25 @@ local function check_battle_act_close() |
153 | 155 | skynet.timeout(CHECK_BATTLE_ACT_CLOSE_INTERVAL, check_battle_act_close) |
154 | 156 | end |
155 | 157 | |
158 | +local function save_autoincrement_timer() | |
159 | + local function saveUidToMysql(tbName, keyName, fieldName) | |
160 | + if not fieldName then fieldName = "value" end | |
161 | + local mysqlVal = getDbCfgVal(tbName, keyName, fieldName) or 0 | |
162 | + local redisVal = tonum(redisproxy:hget(tbName, keyName)) | |
163 | + if redisVal > mysqlVal then | |
164 | + setDbCfgVal(tbName, keyName, fieldName, redisVal) | |
165 | + end | |
166 | + end | |
156 | 167 | |
168 | + saveUidToMysql("autoincrement_set", "role") | |
169 | + saveUidToMysql("autoincrement_set", "union") | |
170 | + saveUidToMysql("autoincrement_set", "order") | |
171 | + saveUidToMysql("autoincrement_set", "email") | |
172 | + saveUidToMysql("autoincrement_set", "emailTimestamp") | |
173 | + saveUidToMysql("autoincrement_set", "delay_email") | |
174 | + | |
175 | + skynet.timeout(SAVE_AUTOINCREMENT_SET_INTERVAL, save_autoincrement_timer) | |
176 | +end | |
157 | 177 | |
158 | 178 | local CMD = {} |
159 | 179 | |
... | ... | @@ -178,6 +198,7 @@ end |
178 | 198 | function CMD.start() |
179 | 199 | check_mail_queue() |
180 | 200 | --check_battle_act_close() |
201 | + save_autoincrement_timer() | |
181 | 202 | end |
182 | 203 | |
183 | 204 | local function __init__() | ... | ... |
src/shared/ModelBaseMysql.lua
... | ... | @@ -67,7 +67,7 @@ function ModelBaseMysql:load(properties) |
67 | 67 | end |
68 | 68 | local load = false |
69 | 69 | if not properties then |
70 | - properties = mysqlproxy:query(string_format("SELECT * from %s where `%s` = %s;", self.class.__cname, self.pri_key, self:getKey())) | |
70 | + properties = mysqlproxy:query(string_format("SELECT * from `%s` where `%s` = %s;", self.class.__cname, self.pri_key, self:getKey())) | |
71 | 71 | load = true |
72 | 72 | end |
73 | 73 | if not next(properties) then return false end |
... | ... | @@ -115,7 +115,7 @@ function ModelBaseMysql:save() |
115 | 115 | end |
116 | 116 | if next(params) then |
117 | 117 | -- insert update |
118 | - local sql = "INSERT INTO %s (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s;" | |
118 | + local sql = "INSERT INTO `%s` (%s) VALUES (%s) ON DUPLICATE KEY UPDATE %s;" | |
119 | 119 | local tbName = self.class.__cname |
120 | 120 | local key_list = "" |
121 | 121 | local value_list = "" | ... | ... |
... | ... | @@ -0,0 +1,19 @@ |
1 | +local skynet = require "skynet" | |
2 | +local mysqlproxy = require "shared.mysqlproxy" | |
3 | + | |
4 | + | |
5 | +function getDbCfgVal(tbName, keyName, fieldName) | |
6 | + local sql = string.format("SELECT * FROM `%s` WHERE `key` = '%s';", tbName, keyName) | |
7 | + local res = mysqlproxy:query(sql) | |
8 | + if not next(res) or res.errno then | |
9 | + return | |
10 | + end | |
11 | + | |
12 | + return res[1][fieldName] | |
13 | +end | |
14 | + | |
15 | +function setDbCfgVal(tbName, keyName, fieldName, fieldVal) | |
16 | + if type(fieldVal) == "string" then fieldVal = string.format("'%s'", fieldVal) end | |
17 | + local sql = string.format("UPDATE `%s` SET `%s` = %s WHERE `key` = '%s';", tbName, fieldName, fieldVal, keyName) | |
18 | + mysqlproxy:query(sql) | |
19 | +end | |
0 | 20 | \ No newline at end of file | ... | ... |