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 | 21 | IOS_SID = 4 -- 判断是不是ios设备 |
22 | 22 | |
23 | 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 | 27 | AttsEnum = { |
26 | 28 | hp = 1, -- 血量 | ... | ... |
src/actions/RoleAction.lua
... | ... | @@ -355,6 +355,7 @@ function _M.createRpc(agent, data) |
355 | 355 | SendPacket(actionCodes.Role_createRpc, MsgPack.pack(response)) |
356 | 356 | return true |
357 | 357 | end |
358 | + newRole:loadRoleIncre() | |
358 | 359 | newRole:startActionUcode() |
359 | 360 | newRole.sysVersion = msg.sysVersion |
360 | 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 | 3 | local HeroPlugin = import(".HeroPlugin") |
4 | 4 | HeroPlugin.bind(Hero) |
5 | 5 | |
6 | 6 | Hero.schema = { |
7 | - id = {"number"}, | |
7 | + id = {"number", 0, "pri"}, | |
8 | + roleid = {"number", 0, "index"}, | |
8 | 9 | type = {"number", 0}, |
9 | 10 | level = {"number", 1}, -- 等级 |
10 | 11 | breakL = {"number", 0}, -- 突破等级 |
... | ... | @@ -36,9 +37,13 @@ function Hero:notifyUpdateProperty(field, newValue, oldValue) |
36 | 37 | self:notifyUpdateProperties(datas) |
37 | 38 | end |
38 | 39 | |
40 | +function Hero:getSimpleHeroId() | |
41 | + return self:getProperty("id") % (self:getProperty("roleid") * MAX_HERO_NUM) | |
42 | +end | |
43 | + | |
39 | 44 | function Hero:notifyUpdateProperties(params) |
40 | 45 | local updateData = { |
41 | - id = self:getProperty("id"), | |
46 | + id = self:getSimpleHeroId(), | |
42 | 47 | datas = params |
43 | 48 | } |
44 | 49 | SendPacket(actionCodes.Hero_updateProperty, MsgPack.pack(updateData)) | ... | ... |
... | ... | @@ -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 | 21 | \ No newline at end of file | ... | ... |
src/models/RolePlugin.lua
... | ... | @@ -12,6 +12,7 @@ function RolePlugin.bind(Role) |
12 | 12 | self:loadDiner() |
13 | 13 | self:loadActivity() |
14 | 14 | self:loadStoreInfo() |
15 | + self:loadRoleIncre() | |
15 | 16 | end |
16 | 17 | |
17 | 18 | function Role:reloadWhenLogin() |
... | ... | @@ -542,13 +543,12 @@ function RolePlugin.bind(Role) |
542 | 543 | local unitData = csvdb["unitCsv"][heroType] |
543 | 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 | 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 | 552 | type= heroType, |
553 | 553 | wakeL = globalCsv.unit_wake_initLevel[unitData.rare], |
554 | 554 | } |
... | ... | @@ -611,17 +611,12 @@ function RolePlugin.bind(Role) |
611 | 611 | |
612 | 612 | function Role:loadHeros() |
613 | 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 | 618 | hero.owner = self |
624 | - self.heros[tonumber(heroId)] = hero | |
619 | + self.heros[hero:getSimpleRuneId()] = hero | |
625 | 620 | end |
626 | 621 | end |
627 | 622 | end |
... | ... | @@ -679,6 +674,18 @@ function RolePlugin.bind(Role) |
679 | 674 | end |
680 | 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 | 689 | function Role:addEquip(equipType, equipLv, count, pms) |
683 | 690 | pms = pms or {} |
684 | 691 | if count ~= count then return end |
... | ... | @@ -734,17 +741,12 @@ function RolePlugin.bind(Role) |
734 | 741 | |
735 | 742 | function Role:loadRunes() |
736 | 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 | 750 | end |
749 | 751 | end |
750 | 752 | end |
... | ... | @@ -758,18 +760,17 @@ function RolePlugin.bind(Role) |
758 | 760 | if not data then return 3 end |
759 | 761 | |
760 | 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 | 769 | type = params.type, |
769 | 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 | 774 | newRune:create() |
774 | 775 | newRune:generateAttrs() |
775 | 776 | newRune.owner = self |
... | ... | @@ -832,6 +833,7 @@ function RolePlugin.bind(Role) |
832 | 833 | end |
833 | 834 | end |
834 | 835 | |
836 | + -- delete rune | |
835 | 837 | redisproxy:pipelining(function (red) |
836 | 838 | for _, runeId in pairs(bDel) do |
837 | 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 | 2 | Rune.schema = { |
3 | - uid = {"number"}, -- 唯一自增id | |
3 | + uid = {"number", 0, "pri"}, -- 唯一自增id | |
4 | + roleid = {"number", 0, "index"}, | |
4 | 5 | type = {"number"}, -- 装备位置 |
5 | 6 | id = {"number"}, |
6 | 7 | level = {"number", 0}, -- 等级 |
... | ... | @@ -12,6 +13,10 @@ function Rune:ctor( properties ) |
12 | 13 | Rune.super.ctor(self, properties) |
13 | 14 | end |
14 | 15 | |
16 | +function Rune:getSimpleRuneId() | |
17 | + return self:getProperty("uid") % (self:getProperty("roleid") * MAX_RUNE_NUM) | |
18 | +end | |
19 | + | |
15 | 20 | function Rune:notifyUpdateProperty(field, newValue, oldValue) |
16 | 21 | local datas = { |
17 | 22 | uid = self:getProperty("uid"), |
... | ... | @@ -40,7 +45,7 @@ end |
40 | 45 | |
41 | 46 | function Rune:notifyUpdateProperties(params) |
42 | 47 | local updateData = { |
43 | - uid = self:getProperty("uid"), | |
48 | + uid = self:getSimpleRuneId(), | |
44 | 49 | datas = params |
45 | 50 | } |
46 | 51 | SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) | ... | ... |
src/services/dbseed.lua
... | ... | @@ -89,7 +89,7 @@ local function initAdvSeasonTable() |
89 | 89 | end |
90 | 90 | |
91 | 91 | local function checkRoleTables() |
92 | - local list = {"Role", "Daily", "Activity", "Diner", "Store"} | |
92 | + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune"} | |
93 | 93 | for _, name in ipairs(list) do |
94 | 94 | local obj = require("models."..name).new({key = "key"}) |
95 | 95 | print("check table [" .. name .. "] begin.") | ... | ... |
src/services/mysqld.lua
src/shared/ModelBaseMysql.lua
... | ... | @@ -65,13 +65,16 @@ function ModelBaseMysql:load(properties) |
65 | 65 | print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname)) |
66 | 66 | return false |
67 | 67 | end |
68 | - | |
68 | + local load = false | |
69 | 69 | if not properties then |
70 | 70 | properties = mysqlproxy:query(string_format("SELECT * from %s where `%s` = %s;", self.class.__cname, self.pri_key, self:getKey())) |
71 | + load = true | |
71 | 72 | end |
72 | 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 | 79 | self:onLoad() |
77 | 80 | |
... | ... | @@ -309,15 +312,18 @@ function ModelBaseMysql:checkTableSchema() |
309 | 312 | local typeMap = { |
310 | 313 | number = {"int", 0, 10}, |
311 | 314 | string = {"varchar", "", 128}, |
312 | - table = {"blob", "NULL"} | |
315 | + table = {"blob", "NULL"}, | |
316 | + pri = {"bigint", 0}, | |
313 | 317 | } |
314 | 318 | local tbName = self.class.__cname |
315 | 319 | local create_sql = [[ |
316 | 320 | CREATE TABLE IF NOT EXISTS `%s` ( |
317 | 321 | %s |
318 | - PRIMARY KEY (`%s`) | |
322 | + PRIMARY KEY (`%s`)%s | |
319 | 323 | ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; |
320 | 324 | ]] |
325 | + local index_tpl_str = [[,INDEX `%s_Index` (`%s`)]] | |
326 | + | |
321 | 327 | local alter_sql = [[ |
322 | 328 | ALTER TABLE `%s` ADD COLUMN ( |
323 | 329 | %s |
... | ... | @@ -327,6 +333,7 @@ function ModelBaseMysql:checkTableSchema() |
327 | 333 | local field_str = "" |
328 | 334 | |
329 | 335 | local res = mysqlproxy:query("desc ".. tbName .. ";") |
336 | + local keyList = {} | |
330 | 337 | if res["err"] then -- 表不存在 |
331 | 338 | local schema = {} |
332 | 339 | for k, v in pairs(self.class.schema) do |
... | ... | @@ -335,6 +342,9 @@ function ModelBaseMysql:checkTableSchema() |
335 | 342 | self.pri_key = k |
336 | 343 | table_insert(schema, 1, {k, v}) |
337 | 344 | else |
345 | + if keyType == "index" then | |
346 | + table_insert(keyList, k) | |
347 | + end | |
338 | 348 | table_insert(schema, {k, v}) |
339 | 349 | end |
340 | 350 | end |
... | ... | @@ -342,6 +352,8 @@ function ModelBaseMysql:checkTableSchema() |
342 | 352 | local k, v = tbl[1], tbl[2] |
343 | 353 | local objType, def, keyType, length = table_unpack(v) |
344 | 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 | 358 | local info = typeMap[objType] |
347 | 359 | local suffix = "" |
... | ... | @@ -362,8 +374,12 @@ function ModelBaseMysql:checkTableSchema() |
362 | 374 | end |
363 | 375 | |
364 | 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 | 383 | else -- 检测是否有添加新字段 |
368 | 384 | local addCol = {} |
369 | 385 | local curCols = {} | ... | ... |