Commit 913e070eddfdda141787f944774e3d156651d3cd

Authored by liuzujun
1 parent 52249374

添加订单表,全局id定时回写数据库

@@ -20,6 +20,7 @@ STRUCT_VERSION = 3 -- 数据结构版本 @@ -20,6 +20,7 @@ STRUCT_VERSION = 3 -- 数据结构版本
20 20
21 IOS_SID = 4 -- 判断是不是ios设备 21 IOS_SID = 4 -- 判断是不是ios设备
22 22
  23 +MAX_SVR_ID = 10000
23 MAX_ROLE_NUM = 1000000 24 MAX_ROLE_NUM = 1000000
24 MAX_HERO_NUM = 1000000 -- 英雄的id = roleId * MAX_HERO_NUM + index 25 MAX_HERO_NUM = 1000000 -- 英雄的id = roleId * MAX_HERO_NUM + index
25 MAX_RUNE_NUM = 1000000 -- 铭文的id = roleId * MAX_RUNE_NUM + index 26 MAX_RUNE_NUM = 1000000 -- 铭文的id = roleId * MAX_RUNE_NUM + index
src/actions/HttpAction.lua
@@ -147,6 +147,36 @@ function _M.gm_action(query) @@ -147,6 +147,36 @@ function _M.gm_action(query)
147 return status 147 return status
148 end 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 function _M.query_role(query) 180 function _M.query_role(query)
151 if not query.uid then return "not found" end 181 if not query.uid then return "not found" end
152 local user = redisproxy:get(string.format("uid:%s", query.uid)) 182 local user = redisproxy:get(string.format("uid:%s", query.uid))
src/actions/StoreAction.lua
@@ -18,7 +18,6 @@ function _M.rechargeRpc(agent , data) @@ -18,7 +18,6 @@ function _M.rechargeRpc(agent , data)
18 end 18 end
19 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId })) 19 SendPacket(actionCodes.Store_rechargeRpc, MsgPack.pack({ order = partnerOrderId }))
20 20
21 -  
22 -- 测试的 直接发奖励了 21 -- 测试的 直接发奖励了
23 skynet.timeout(10, function () 22 skynet.timeout(10, function ()
24 role:handlePurchase({ 23 role:handlePurchase({
src/models/Order.lua
1 -local Order = class("Order", require("shared.ModelBase")) 1 +local Order = class("Order", require("shared.ModelBaseMysql"))
2 2
3 function Order:ctor(properties) 3 function Order:ctor(properties)
4 - Order.super.ctor(self, properties) 4 + Order.super.ctor(self, properties)
5 end 5 end
6 6
7 Order.schema = { 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 rechargeId = {"number", 0}, 11 rechargeId = {"number", 0},
11 transactionId = {"string", ""}, 12 transactionId = {"string", ""},
12 createTime = {"number", skynet.timex()}, -- 订单创建时间 13 createTime = {"number", skynet.timex()}, -- 订单创建时间
src/models/RolePlugin.lua
@@ -1740,24 +1740,27 @@ function RolePlugin.bind(Role) @@ -1740,24 +1740,27 @@ function RolePlugin.bind(Role)
1740 if not self.activity:isOpenById(rechargeData.activity_id, "ActShopGoods") then return "" end 1740 if not self.activity:isOpenById(rechargeData.activity_id, "ActShopGoods") then return "" end
1741 end 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 if orderObject:load() and orderObject:getProperty("rechargeId") == rechargeId and math.abs(skynet.timex() - orderObject:getProperty("createTime")) < 5 * 60 then 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 end 1749 end
1749 - end 1750 + end
1750 1751
1751 orderId = redisproxy:hincrby("autoincrement_set", "order", 1) 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 local orderKey = string.format(R_ORDER, roleId, orderId) 1755 local orderKey = string.format(R_ORDER, roleId, orderId)
1754 redisproxy:del(orderKey) -- 删掉可能有了 1756 redisproxy:del(orderKey) -- 删掉可能有了
1755 local order = require("models.Order").new({ 1757 local order = require("models.Order").new({
1756 - key = orderKey,  
1757 - order = partnerOrderId, 1758 + key = partnerOrderId,
  1759 + id = tonum(partnerOrderId),
1758 rechargeId = rechargeId, 1760 rechargeId = rechargeId,
1759 createTime = skynet.timex(), 1761 createTime = skynet.timex(),
1760 transactionId = transactionId, 1762 transactionId = transactionId,
  1763 + roleid = roleId,
1761 sid = self:getProperty("sid"), 1764 sid = self:getProperty("sid"),
1762 }) 1765 })
1763 order:create() 1766 order:create()
@@ -1779,10 +1782,8 @@ function RolePlugin.bind(Role) @@ -1779,10 +1782,8 @@ function RolePlugin.bind(Role)
1779 --]] 1782 --]]
1780 function Role:updatePurchaseOrder(partnerOrderStr, platformOrder, status) 1783 function Role:updatePurchaseOrder(partnerOrderStr, platformOrder, status)
1781 if not partnerOrderStr then return false end 1784 if not partnerOrderStr then return false end
1782 - local _, _, orderId = string.match(partnerOrderStr, "(.+)_(.+)_(.+)")  
1783 -  
1784 local roleId = self:getProperty("id") 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 if not orderObject:load() then 1787 if not orderObject:load() then
1787 return false 1788 return false
1788 end 1789 end
@@ -1791,6 +1792,14 @@ function RolePlugin.bind(Role) @@ -1791,6 +1792,14 @@ function RolePlugin.bind(Role)
1791 local dataSet = csvdb["shop_rechargeCsv"][rechargeId] 1792 local dataSet = csvdb["shop_rechargeCsv"][rechargeId]
1792 local sid = orderObject:getProperty("sid") 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 if orderObject:getProperty("finishTime") > 0 then 1803 if orderObject:getProperty("finishTime") > 0 then
1795 skynet.error(string.format("[recharge] is a finish order cpOrder: %s, platformOrder : %s, hadPlatformOrder: %s, id: %s, overTime : %s", 1804 skynet.error(string.format("[recharge] is a finish order cpOrder: %s, platformOrder : %s, hadPlatformOrder: %s, id: %s, overTime : %s",
1796 partnerOrderStr, platformOrder, orderObject:getProperty("transactionId"), rechargeId, orderObject:getProperty("finishTime") 1805 partnerOrderStr, platformOrder, orderObject:getProperty("transactionId"), rechargeId, orderObject:getProperty("finishTime")
src/services/dbseed.lua
@@ -4,6 +4,7 @@ require &quot;GlobalVar&quot; @@ -4,6 +4,7 @@ require &quot;GlobalVar&quot;
4 require "RedisKeys" 4 require "RedisKeys"
5 require "ProtocolCode" 5 require "ProtocolCode"
6 require "skynet.manager" 6 require "skynet.manager"
  7 +require "utils.MysqlUtil"
7 8
8 skynet = require "skynet" 9 skynet = require "skynet"
9 10
@@ -12,19 +13,31 @@ mysqlproxy = require &quot;shared.mysqlproxy&quot; @@ -12,19 +13,31 @@ mysqlproxy = require &quot;shared.mysqlproxy&quot;
12 13
13 SendPacket = function ( ... ) end 14 SendPacket = function ( ... ) end
14 15
  16 +
15 local function initRedisDb( ... ) 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 end 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 end 41 end
29 42
30 -- 初始化服务器数据库以及服务器信息表 43 -- 初始化服务器数据库以及服务器信息表
@@ -52,7 +65,7 @@ end @@ -52,7 +65,7 @@ end
52 65
53 local function initAutoIncreUidTable() 66 local function initAutoIncreUidTable()
54 mysqlproxy:query [[ 67 mysqlproxy:query [[
55 - CREATE TABLE IF NOT EXISTS `auto_increment_uid` ( 68 + CREATE TABLE IF NOT EXISTS `autoincrement_set` (
56 `key` varchar(45) NOT NULL, 69 `key` varchar(45) NOT NULL,
57 `value` int(11) DEFAULT NULL, 70 `value` int(11) DEFAULT NULL,
58 PRIMARY KEY (`key`) 71 PRIMARY KEY (`key`)
@@ -60,10 +73,10 @@ local function initAutoIncreUidTable() @@ -60,10 +73,10 @@ local function initAutoIncreUidTable()
60 ]] 73 ]]
61 local servId = tonumber(skynet.getenv("servId")) 74 local servId = tonumber(skynet.getenv("servId"))
62 if servId then 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 mysqlproxy:query(string.format(tpl, "role", servId * MAX_ROLE_NUM)) 77 mysqlproxy:query(string.format(tpl, "role", servId * MAX_ROLE_NUM))
65 mysqlproxy:query(string.format(tpl, "union", servId * MAX_ROLE_NUM)) 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 mysqlproxy:query(string.format(tpl, "email", 0)) 80 mysqlproxy:query(string.format(tpl, "email", 0))
68 mysqlproxy:query(string.format(tpl, "emailTimestamp", 0)) 81 mysqlproxy:query(string.format(tpl, "emailTimestamp", 0))
69 mysqlproxy:query(string.format(tpl, "delay_email", 0)) 82 mysqlproxy:query(string.format(tpl, "delay_email", 0))
@@ -89,7 +102,7 @@ local function initAdvSeasonTable() @@ -89,7 +102,7 @@ local function initAdvSeasonTable()
89 end 102 end
90 103
91 local function checkRoleTables() 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 for _, name in ipairs(list) do 106 for _, name in ipairs(list) do
94 local obj = require("models."..name).new({key = "key"}) 107 local obj = require("models."..name).new({key = "key"})
95 print("check table [" .. name .. "] begin.") 108 print("check table [" .. name .. "] begin.")
@@ -100,22 +113,18 @@ end @@ -100,22 +113,18 @@ end
100 113
101 local steps = { 114 local steps = {
102 [1] = { 115 [1] = {
103 - handler = initRedisDb,  
104 - desc = "initialize redis database "  
105 - },  
106 - [2] = {  
107 handler = initServerDatabase, 116 handler = initServerDatabase,
108 desc = "initialize server database " 117 desc = "initialize server database "
109 }, 118 },
110 - [3] = { 119 + [2] = {
111 handler = initAutoIncreUidTable, 120 handler = initAutoIncreUidTable,
112 desc = "initialize auto_increment_uid table " 121 desc = "initialize auto_increment_uid table "
113 }, 122 },
114 - [4] = { 123 + [3] = {
115 handler = initAdvSeasonTable, 124 handler = initAdvSeasonTable,
116 desc = "initialize adv_season table " 125 desc = "initialize adv_season table "
117 }, 126 },
118 - [5] = { 127 + [4] = {
119 handler = checkRoleTables, 128 handler = checkRoleTables,
120 desc = "check role tables " 129 desc = "check role tables "
121 } 130 }
@@ -136,5 +145,6 @@ skynet.start(function () @@ -136,5 +145,6 @@ skynet.start(function ()
136 action.handler() 145 action.handler()
137 print(action.desc .. "finished ...") 146 print(action.desc .. "finished ...")
138 end 147 end
  148 + initRedisDb()
139 skynet.exit() 149 skynet.exit()
140 end) 150 end)
141 \ No newline at end of file 151 \ No newline at end of file
src/services/globald.lua
@@ -8,6 +8,7 @@ require &quot;utils.init&quot; @@ -8,6 +8,7 @@ require &quot;utils.init&quot;
8 require "RedisKeys" 8 require "RedisKeys"
9 require "skynet.manager" 9 require "skynet.manager"
10 require "GlobalVar" 10 require "GlobalVar"
  11 +require "utils.MysqlUtil"
11 12
12 13
13 local ipairs = ipairs 14 local ipairs = ipairs
@@ -18,6 +19,7 @@ local string_format = string.format @@ -18,6 +19,7 @@ local string_format = string.format
18 19
19 local CHECK_MAIL_STATUS_INTERVAL = 100 * 60 20 local CHECK_MAIL_STATUS_INTERVAL = 100 * 60
20 local CHECK_BATTLE_ACT_CLOSE_INTERVAL = 100 * 1 21 local CHECK_BATTLE_ACT_CLOSE_INTERVAL = 100 * 1
  22 +local SAVE_AUTOINCREMENT_SET_INTERVAL = 100 * 2
21 local function mailQuene() 23 local function mailQuene()
22 local delayEmail = tonum(redisproxy:hget("autoincrement_set", "delay_email")) 24 local delayEmail = tonum(redisproxy:hget("autoincrement_set", "delay_email"))
23 if delayEmail == 0 then 25 if delayEmail == 0 then
@@ -153,7 +155,25 @@ local function check_battle_act_close() @@ -153,7 +155,25 @@ local function check_battle_act_close()
153 skynet.timeout(CHECK_BATTLE_ACT_CLOSE_INTERVAL, check_battle_act_close) 155 skynet.timeout(CHECK_BATTLE_ACT_CLOSE_INTERVAL, check_battle_act_close)
154 end 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 local CMD = {} 178 local CMD = {}
159 179
@@ -178,6 +198,7 @@ end @@ -178,6 +198,7 @@ end
178 function CMD.start() 198 function CMD.start()
179 check_mail_queue() 199 check_mail_queue()
180 --check_battle_act_close() 200 --check_battle_act_close()
  201 + save_autoincrement_timer()
181 end 202 end
182 203
183 local function __init__() 204 local function __init__()
src/shared/ModelBaseMysql.lua
@@ -67,7 +67,7 @@ function ModelBaseMysql:load(properties) @@ -67,7 +67,7 @@ function ModelBaseMysql:load(properties)
67 end 67 end
68 local load = false 68 local load = false
69 if not properties then 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 load = true 71 load = true
72 end 72 end
73 if not next(properties) then return false end 73 if not next(properties) then return false end
@@ -115,7 +115,7 @@ function ModelBaseMysql:save() @@ -115,7 +115,7 @@ function ModelBaseMysql:save()
115 end 115 end
116 if next(params) then 116 if next(params) then
117 -- insert update 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 local tbName = self.class.__cname 119 local tbName = self.class.__cname
120 local key_list = "" 120 local key_list = ""
121 local value_list = "" 121 local value_list = ""
src/utils/MysqlUtil.lua 0 → 100644
@@ -0,0 +1,19 @@ @@ -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 \ No newline at end of file 20 \ No newline at end of file