Commit a12bfccef8bc81403fa0abc563d57934f56ab514
1 parent
fa992c94
添加英雄表
Showing
10 changed files
with
98 additions
and
47 deletions
Show diff stats
src/GlobalVar.lua
@@ -21,6 +21,8 @@ STRUCT_VERSION = 3 -- 数据结构版本 | @@ -21,6 +21,8 @@ STRUCT_VERSION = 3 -- 数据结构版本 | ||
21 | IOS_SID = 4 -- 判断是不是ios设备 | 21 | IOS_SID = 4 -- 判断是不是ios设备 |
22 | 22 | ||
23 | MAX_ROLE_NUM = 1000000 | 23 | MAX_ROLE_NUM = 1000000 |
24 | +MAX_HERO_NUM = 1000000 -- 英雄的id = roleId * MAX_HERO_NUM + index | ||
25 | +MAX_RUNE_NUM = 1000000 -- 铭文的id = roleId * MAX_RUNE_NUM + index | ||
24 | -- 属性枚举 | 26 | -- 属性枚举 |
25 | AttsEnum = { | 27 | AttsEnum = { |
26 | hp = 1, -- 血量 | 28 | hp = 1, -- 血量 |
src/actions/RoleAction.lua
@@ -355,6 +355,7 @@ function _M.createRpc(agent, data) | @@ -355,6 +355,7 @@ function _M.createRpc(agent, data) | ||
355 | SendPacket(actionCodes.Role_createRpc, MsgPack.pack(response)) | 355 | SendPacket(actionCodes.Role_createRpc, MsgPack.pack(response)) |
356 | return true | 356 | return true |
357 | end | 357 | end |
358 | + newRole:loadRoleIncre() | ||
358 | newRole:startActionUcode() | 359 | newRole:startActionUcode() |
359 | newRole.sysVersion = msg.sysVersion | 360 | newRole.sysVersion = msg.sysVersion |
360 | newRole.ip = agent.ip:toArray(false, ":")[1] | 361 | newRole.ip = agent.ip:toArray(false, ":")[1] |
src/models/Hero.lua
1 | -local Hero = class("Hero", require("shared.ModelBase")) | 1 | +local Hero = class("Hero", require("shared.ModelBaseMysql")) |
2 | 2 | ||
3 | local HeroPlugin = import(".HeroPlugin") | 3 | local HeroPlugin = import(".HeroPlugin") |
4 | HeroPlugin.bind(Hero) | 4 | HeroPlugin.bind(Hero) |
5 | 5 | ||
6 | Hero.schema = { | 6 | Hero.schema = { |
7 | - id = {"number"}, | 7 | + id = {"number", 0, "pri"}, |
8 | + roleid = {"number", 0, "index"}, | ||
8 | type = {"number", 0}, | 9 | type = {"number", 0}, |
9 | level = {"number", 1}, -- 等级 | 10 | level = {"number", 1}, -- 等级 |
10 | breakL = {"number", 0}, -- 突破等级 | 11 | breakL = {"number", 0}, -- 突破等级 |
@@ -36,9 +37,13 @@ function Hero:notifyUpdateProperty(field, newValue, oldValue) | @@ -36,9 +37,13 @@ function Hero:notifyUpdateProperty(field, newValue, oldValue) | ||
36 | self:notifyUpdateProperties(datas) | 37 | self:notifyUpdateProperties(datas) |
37 | end | 38 | end |
38 | 39 | ||
40 | +function Hero:getSimpleHeroId() | ||
41 | + return self:getProperty("id") % (self:getProperty("roleid") * MAX_HERO_NUM) | ||
42 | +end | ||
43 | + | ||
39 | function Hero:notifyUpdateProperties(params) | 44 | function Hero:notifyUpdateProperties(params) |
40 | local updateData = { | 45 | local updateData = { |
41 | - id = self:getProperty("id"), | 46 | + id = self:getSimpleHeroId(), |
42 | datas = params | 47 | datas = params |
43 | } | 48 | } |
44 | SendPacket(actionCodes.Hero_updateProperty, MsgPack.pack(updateData)) | 49 | SendPacket(actionCodes.Hero_updateProperty, MsgPack.pack(updateData)) |
@@ -0,0 +1,20 @@ | @@ -0,0 +1,20 @@ | ||
1 | +local RoleIncre = class("RoleIncre", require("shared.ModelBaseMysql")) | ||
2 | + | ||
3 | +function RoleIncre:ctor( properties ) | ||
4 | + RoleIncre.super.ctor(self, properties) | ||
5 | +end | ||
6 | + | ||
7 | + | ||
8 | +RoleIncre.schema = { | ||
9 | + id = {"number", 0, "pri"}, | ||
10 | + heroId = {"number", 0}, | ||
11 | + runeId = {"number", 0}, | ||
12 | +} | ||
13 | + | ||
14 | +function RoleIncre:increBy(keyName, val) | ||
15 | + local curId = self:getProperty(keyName) | ||
16 | + self:setProperty(keyName, curId + val) | ||
17 | + return curId + val | ||
18 | +end | ||
19 | + | ||
20 | +return RoleIncre | ||
0 | \ No newline at end of file | 21 | \ No newline at end of file |
src/models/RolePlugin.lua
@@ -12,6 +12,7 @@ function RolePlugin.bind(Role) | @@ -12,6 +12,7 @@ function RolePlugin.bind(Role) | ||
12 | self:loadDiner() | 12 | self:loadDiner() |
13 | self:loadActivity() | 13 | self:loadActivity() |
14 | self:loadStoreInfo() | 14 | self:loadStoreInfo() |
15 | + self:loadRoleIncre() | ||
15 | end | 16 | end |
16 | 17 | ||
17 | function Role:reloadWhenLogin() | 18 | function Role:reloadWhenLogin() |
@@ -542,13 +543,12 @@ function RolePlugin.bind(Role) | @@ -542,13 +543,12 @@ function RolePlugin.bind(Role) | ||
542 | local unitData = csvdb["unitCsv"][heroType] | 543 | local unitData = csvdb["unitCsv"][heroType] |
543 | if not unitData then return false end | 544 | if not unitData then return false end |
544 | 545 | ||
545 | - local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) | ||
546 | - | ||
547 | - redisproxy:sadd(string.format(R_HEROS, roleId), heroId) | 546 | + local heroId = self.roleIncre:increBy("heroId" , 1) |
548 | 547 | ||
549 | local heroInfo = { | 548 | local heroInfo = { |
550 | - key = string.format(R_HERO, roleId, heroId), | ||
551 | - id = heroId, | 549 | + key = string.format("%d",roleId * MAX_HERO_NUM + heroId), |
550 | + id = roleId * MAX_HERO_NUM + heroId, | ||
551 | + roleid = roleId, | ||
552 | type= heroType, | 552 | type= heroType, |
553 | wakeL = globalCsv.unit_wake_initLevel[unitData.rare], | 553 | wakeL = globalCsv.unit_wake_initLevel[unitData.rare], |
554 | } | 554 | } |
@@ -611,17 +611,12 @@ function RolePlugin.bind(Role) | @@ -611,17 +611,12 @@ function RolePlugin.bind(Role) | ||
611 | 611 | ||
612 | function Role:loadHeros() | 612 | function Role:loadHeros() |
613 | local roleId = self:getProperty("id") | 613 | local roleId = self:getProperty("id") |
614 | - local heroIds = redisproxy:smembers(string.format(R_HEROS, roleId)) | ||
615 | - local redret = redisproxy:pipelining(function (red) | ||
616 | - for _, heroId in ipairs(heroIds) do | ||
617 | - red:hgetall(string.format(R_HERO, roleId, heroId)) | ||
618 | - end | ||
619 | - end) | ||
620 | - for index, heroId in ipairs(heroIds) do | ||
621 | - local hero = require("models.Hero").new({key = string.format(R_HERO, roleId, heroId)}) | ||
622 | - if hero:load(table.array2Table(redret[index])) then | 614 | + local res = mysqlproxy:query(string.format("SELECT * FROM `Hero` WHERE `roleid` = %s", roleId)) |
615 | + for _, data in ipairs(res) do | ||
616 | + local hero = require("models.Hero").new({key = string.format("%d",data.id), id=data.id}) | ||
617 | + if hero:load(data) then | ||
623 | hero.owner = self | 618 | hero.owner = self |
624 | - self.heros[tonumber(heroId)] = hero | 619 | + self.heros[hero:getSimpleRuneId()] = hero |
625 | end | 620 | end |
626 | end | 621 | end |
627 | end | 622 | end |
@@ -679,6 +674,18 @@ function RolePlugin.bind(Role) | @@ -679,6 +674,18 @@ function RolePlugin.bind(Role) | ||
679 | end | 674 | end |
680 | end | 675 | end |
681 | 676 | ||
677 | + function Role:loadRoleIncre() | ||
678 | + local roleId = self:getProperty("id") | ||
679 | + local dataKey = string.format("%d", roleId) | ||
680 | + self.roleIncre = require("models.RoleIncre").new({key = dataKey, id = roleId}) | ||
681 | + self.roleIncre.owner = self | ||
682 | + if not self.roleIncre:checkKeyExists(dataKey) then | ||
683 | + self.roleIncre:create() | ||
684 | + else | ||
685 | + self.roleIncre:load() | ||
686 | + end | ||
687 | + end | ||
688 | + | ||
682 | function Role:addEquip(equipType, equipLv, count, pms) | 689 | function Role:addEquip(equipType, equipLv, count, pms) |
683 | pms = pms or {} | 690 | pms = pms or {} |
684 | if count ~= count then return end | 691 | if count ~= count then return end |
@@ -734,17 +741,12 @@ function RolePlugin.bind(Role) | @@ -734,17 +741,12 @@ function RolePlugin.bind(Role) | ||
734 | 741 | ||
735 | function Role:loadRunes() | 742 | function Role:loadRunes() |
736 | local roleId = self:getProperty("id") | 743 | local roleId = self:getProperty("id") |
737 | - local runeIds = redisproxy:smembers(string.format(R_RUNEIDS, roleId)) | ||
738 | - local redret = redisproxy:pipelining(function (red) | ||
739 | - for _, runeId in ipairs(runeIds) do | ||
740 | - red:hgetall(string.format(R_RUNE, roleId, runeId)) | ||
741 | - end | ||
742 | - end) | ||
743 | - for index, runeId in ipairs(runeIds) do | ||
744 | - local rune = require("models.Rune").new({key = string.format(R_RUNE, roleId, runeId)}) | ||
745 | - if rune:load(table.array2Table(redret[index])) then | ||
746 | - rune.owner = self | ||
747 | - self.runeBag[tonumber(runeId)] = rune | 744 | + local res = mysqlproxy:query(string.format("SELECT * FROM `Rune` WHERE `roleid` = %s", roleId)) |
745 | + for _, data in ipairs(res) do | ||
746 | + local rune = require("models.Rune").new({key = string.format("%d",data.id), id=data.id}) | ||
747 | + if hero:load(data) then | ||
748 | + hero.owner = self | ||
749 | + self.runeBag[rune:getSimpleRuneId()] = rune | ||
748 | end | 750 | end |
749 | end | 751 | end |
750 | end | 752 | end |
@@ -758,18 +760,17 @@ function RolePlugin.bind(Role) | @@ -758,18 +760,17 @@ function RolePlugin.bind(Role) | ||
758 | if not data then return 3 end | 760 | if not data then return 3 end |
759 | 761 | ||
760 | local roleId = self:getProperty("id") | 762 | local roleId = self:getProperty("id") |
761 | - local runeUid = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "rune", 1)) | ||
762 | - | ||
763 | - redisproxy:sadd(string.format(R_RUNEIDS, roleId), runeUid) | 763 | + local runeUid = self.roleIncre:increBy("runeId" , 1) |
764 | 764 | ||
765 | - local heroInfo = { | ||
766 | - key = string.format(R_RUNE, roleId, runeUid), | ||
767 | - uid = runeUid, | 765 | + local runeInfo = { |
766 | + key = string.format("%d",roleId * MAX_RUNE_NUM + runeUid), | ||
767 | + uid = roleId * MAX_RUNE_NUM + runeUid, | ||
768 | + roleid = roleId, | ||
768 | type = params.type, | 769 | type = params.type, |
769 | id = params.id, | 770 | id = params.id, |
770 | } | 771 | } |
771 | 772 | ||
772 | - local newRune = require("models.Rune").new(heroInfo) | 773 | + local newRune = require("models.Rune").new(runeInfo) |
773 | newRune:create() | 774 | newRune:create() |
774 | newRune:generateAttrs() | 775 | newRune:generateAttrs() |
775 | newRune.owner = self | 776 | newRune.owner = self |
@@ -832,6 +833,7 @@ function RolePlugin.bind(Role) | @@ -832,6 +833,7 @@ function RolePlugin.bind(Role) | ||
832 | end | 833 | end |
833 | end | 834 | end |
834 | 835 | ||
836 | + -- delete rune | ||
835 | redisproxy:pipelining(function (red) | 837 | redisproxy:pipelining(function (red) |
836 | for _, runeId in pairs(bDel) do | 838 | for _, runeId in pairs(bDel) do |
837 | red:del(string.format(R_RUNE, roleId, runeId)) | 839 | red:del(string.format(R_RUNE, roleId, runeId)) |
src/models/Rune.lua
1 | -local Rune = class("Rune", require("shared.ModelBase")) | 1 | +local Rune = class("Rune", require("shared.ModelBaseMysql")) |
2 | Rune.schema = { | 2 | Rune.schema = { |
3 | - uid = {"number"}, -- 唯一自增id | 3 | + uid = {"number", 0, "pri"}, -- 唯一自增id |
4 | + roleid = {"number", 0, "index"}, | ||
4 | type = {"number"}, -- 装备位置 | 5 | type = {"number"}, -- 装备位置 |
5 | id = {"number"}, | 6 | id = {"number"}, |
6 | level = {"number", 0}, -- 等级 | 7 | level = {"number", 0}, -- 等级 |
@@ -12,6 +13,10 @@ function Rune:ctor( properties ) | @@ -12,6 +13,10 @@ function Rune:ctor( properties ) | ||
12 | Rune.super.ctor(self, properties) | 13 | Rune.super.ctor(self, properties) |
13 | end | 14 | end |
14 | 15 | ||
16 | +function Rune:getSimpleRuneId() | ||
17 | + return self:getProperty("uid") % (self:getProperty("roleid") * MAX_RUNE_NUM) | ||
18 | +end | ||
19 | + | ||
15 | function Rune:notifyUpdateProperty(field, newValue, oldValue) | 20 | function Rune:notifyUpdateProperty(field, newValue, oldValue) |
16 | local datas = { | 21 | local datas = { |
17 | uid = self:getProperty("uid"), | 22 | uid = self:getProperty("uid"), |
@@ -40,7 +45,7 @@ end | @@ -40,7 +45,7 @@ end | ||
40 | 45 | ||
41 | function Rune:notifyUpdateProperties(params) | 46 | function Rune:notifyUpdateProperties(params) |
42 | local updateData = { | 47 | local updateData = { |
43 | - uid = self:getProperty("uid"), | 48 | + uid = self:getSimpleRuneId(), |
44 | datas = params | 49 | datas = params |
45 | } | 50 | } |
46 | SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) | 51 | SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) |
src/services/dbseed.lua
@@ -89,7 +89,7 @@ local function initAdvSeasonTable() | @@ -89,7 +89,7 @@ local function initAdvSeasonTable() | ||
89 | end | 89 | end |
90 | 90 | ||
91 | local function checkRoleTables() | 91 | local function checkRoleTables() |
92 | - local list = {"Role", "Daily", "Activity", "Diner", "Store"} | 92 | + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune"} |
93 | for _, name in ipairs(list) do | 93 | for _, name in ipairs(list) do |
94 | local obj = require("models."..name).new({key = "key"}) | 94 | local obj = require("models."..name).new({key = "key"}) |
95 | print("check table [" .. name .. "] begin.") | 95 | print("check table [" .. name .. "] begin.") |
src/services/mysqld.lua
@@ -16,7 +16,7 @@ function command.open(conf) | @@ -16,7 +16,7 @@ function command.open(conf) | ||
16 | database="mysql", | 16 | database="mysql", |
17 | user="root", | 17 | user="root", |
18 | password="123456", | 18 | password="123456", |
19 | - max_packet_size = 1024 * 1024, | 19 | + max_packet_size = 5 * 1024 * 1024, |
20 | on_connect = on_connect | 20 | on_connect = on_connect |
21 | }) | 21 | }) |
22 | if not db then | 22 | if not db then |
src/shared/ModelBaseMysql.lua
@@ -65,13 +65,16 @@ function ModelBaseMysql:load(properties) | @@ -65,13 +65,16 @@ function ModelBaseMysql:load(properties) | ||
65 | print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname)) | 65 | print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname)) |
66 | return false | 66 | return false |
67 | end | 67 | end |
68 | - | 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 | end | 72 | end |
72 | if not next(properties) then return false end | 73 | if not next(properties) then return false end |
73 | 74 | ||
74 | - self:loadProperties(properties[1]) | 75 | + local data = load and properties[1] or properties |
76 | + | ||
77 | + self:loadProperties(data) | ||
75 | 78 | ||
76 | self:onLoad() | 79 | self:onLoad() |
77 | 80 | ||
@@ -309,15 +312,18 @@ function ModelBaseMysql:checkTableSchema() | @@ -309,15 +312,18 @@ function ModelBaseMysql:checkTableSchema() | ||
309 | local typeMap = { | 312 | local typeMap = { |
310 | number = {"int", 0, 10}, | 313 | number = {"int", 0, 10}, |
311 | string = {"varchar", "", 128}, | 314 | string = {"varchar", "", 128}, |
312 | - table = {"blob", "NULL"} | 315 | + table = {"blob", "NULL"}, |
316 | + pri = {"bigint", 0}, | ||
313 | } | 317 | } |
314 | local tbName = self.class.__cname | 318 | local tbName = self.class.__cname |
315 | local create_sql = [[ | 319 | local create_sql = [[ |
316 | CREATE TABLE IF NOT EXISTS `%s` ( | 320 | CREATE TABLE IF NOT EXISTS `%s` ( |
317 | %s | 321 | %s |
318 | - PRIMARY KEY (`%s`) | 322 | + PRIMARY KEY (`%s`)%s |
319 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; | 323 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; |
320 | ]] | 324 | ]] |
325 | + local index_tpl_str = [[,INDEX `%s_Index` (`%s`)]] | ||
326 | + | ||
321 | local alter_sql = [[ | 327 | local alter_sql = [[ |
322 | ALTER TABLE `%s` ADD COLUMN ( | 328 | ALTER TABLE `%s` ADD COLUMN ( |
323 | %s | 329 | %s |
@@ -327,6 +333,7 @@ function ModelBaseMysql:checkTableSchema() | @@ -327,6 +333,7 @@ function ModelBaseMysql:checkTableSchema() | ||
327 | local field_str = "" | 333 | local field_str = "" |
328 | 334 | ||
329 | local res = mysqlproxy:query("desc ".. tbName .. ";") | 335 | local res = mysqlproxy:query("desc ".. tbName .. ";") |
336 | + local keyList = {} | ||
330 | if res["err"] then -- 表不存在 | 337 | if res["err"] then -- 表不存在 |
331 | local schema = {} | 338 | local schema = {} |
332 | for k, v in pairs(self.class.schema) do | 339 | for k, v in pairs(self.class.schema) do |
@@ -335,6 +342,9 @@ function ModelBaseMysql:checkTableSchema() | @@ -335,6 +342,9 @@ function ModelBaseMysql:checkTableSchema() | ||
335 | self.pri_key = k | 342 | self.pri_key = k |
336 | table_insert(schema, 1, {k, v}) | 343 | table_insert(schema, 1, {k, v}) |
337 | else | 344 | else |
345 | + if keyType == "index" then | ||
346 | + table_insert(keyList, k) | ||
347 | + end | ||
338 | table_insert(schema, {k, v}) | 348 | table_insert(schema, {k, v}) |
339 | end | 349 | end |
340 | end | 350 | end |
@@ -342,6 +352,8 @@ function ModelBaseMysql:checkTableSchema() | @@ -342,6 +352,8 @@ function ModelBaseMysql:checkTableSchema() | ||
342 | local k, v = tbl[1], tbl[2] | 352 | local k, v = tbl[1], tbl[2] |
343 | local objType, def, keyType, length = table_unpack(v) | 353 | local objType, def, keyType, length = table_unpack(v) |
344 | assert(typeMap[objType], string_format("schema invalid type, %s, %s", tbName, k)) | 354 | assert(typeMap[objType], string_format("schema invalid type, %s, %s", tbName, k)) |
355 | + -- 主键使用bigint存储 | ||
356 | + if keyType == "pri" then objType = "pri" end | ||
345 | 357 | ||
346 | local info = typeMap[objType] | 358 | local info = typeMap[objType] |
347 | local suffix = "" | 359 | local suffix = "" |
@@ -362,8 +374,12 @@ function ModelBaseMysql:checkTableSchema() | @@ -362,8 +374,12 @@ function ModelBaseMysql:checkTableSchema() | ||
362 | end | 374 | end |
363 | 375 | ||
364 | assert(self.pri_key, string_format("table not include primary key, [%s]", tbName)) | 376 | assert(self.pri_key, string_format("table not include primary key, [%s]", tbName)) |
377 | + local index_key_str = "" | ||
378 | + for _, k in ipairs(keyList) do | ||
379 | + index_key_str = index_key_str .. string_format(index_tpl_str, k, k) | ||
380 | + end | ||
365 | -- 创建表格 | 381 | -- 创建表格 |
366 | - mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key)) | 382 | + mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) |
367 | else -- 检测是否有添加新字段 | 383 | else -- 检测是否有添加新字段 |
368 | local addCol = {} | 384 | local addCol = {} |
369 | local curCols = {} | 385 | local curCols = {} |