From a12bfccef8bc81403fa0abc563d57934f56ab514 Mon Sep 17 00:00:00 2001 From: liuzujun <307836273@qq.com> Date: Fri, 5 Mar 2021 10:57:24 +0800 Subject: [PATCH] 添加英雄表 --- src/GlobalVar.lua | 2 ++ src/actions/RoleAction.lua | 1 + src/csvdata | 2 +- src/models/Hero.lua | 11 ++++++++--- src/models/RoleIncre.lua | 20 ++++++++++++++++++++ src/models/RolePlugin.lua | 68 +++++++++++++++++++++++++++++++++++--------------------------------- src/models/Rune.lua | 11 ++++++++--- src/services/dbseed.lua | 2 +- src/services/mysqld.lua | 2 +- src/shared/ModelBaseMysql.lua | 26 +++++++++++++++++++++----- 10 files changed, 98 insertions(+), 47 deletions(-) create mode 100644 src/models/RoleIncre.lua diff --git a/src/GlobalVar.lua b/src/GlobalVar.lua index 395cb64..74469aa 100644 --- a/src/GlobalVar.lua +++ b/src/GlobalVar.lua @@ -21,6 +21,8 @@ STRUCT_VERSION = 3 -- 数据结构版本 IOS_SID = 4 -- 判断是不是ios设备 MAX_ROLE_NUM = 1000000 +MAX_HERO_NUM = 1000000 -- 英雄的id = roleId * MAX_HERO_NUM + index +MAX_RUNE_NUM = 1000000 -- 铭文的id = roleId * MAX_RUNE_NUM + index -- 属性枚举 AttsEnum = { hp = 1, -- 血量 diff --git a/src/actions/RoleAction.lua b/src/actions/RoleAction.lua index 04a94c0..ca95114 100644 --- a/src/actions/RoleAction.lua +++ b/src/actions/RoleAction.lua @@ -355,6 +355,7 @@ function _M.createRpc(agent, data) SendPacket(actionCodes.Role_createRpc, MsgPack.pack(response)) return true end + newRole:loadRoleIncre() newRole:startActionUcode() newRole.sysVersion = msg.sysVersion newRole.ip = agent.ip:toArray(false, ":")[1] diff --git a/src/csvdata b/src/csvdata index 5963ca5..f70c635 160000 --- a/src/csvdata +++ b/src/csvdata @@ -1 +1 @@ -Subproject commit 5963ca5e80ac32e27cadf4c2553f1f24af8d9435 +Subproject commit f70c635c2f99d9800b629e41e13aa5115d5f69cd diff --git a/src/models/Hero.lua b/src/models/Hero.lua index 5be87e8..7bc33bd 100644 --- a/src/models/Hero.lua +++ b/src/models/Hero.lua @@ -1,10 +1,11 @@ -local Hero = class("Hero", require("shared.ModelBase")) +local Hero = class("Hero", require("shared.ModelBaseMysql")) local HeroPlugin = import(".HeroPlugin") HeroPlugin.bind(Hero) Hero.schema = { - id = {"number"}, + id = {"number", 0, "pri"}, + roleid = {"number", 0, "index"}, type = {"number", 0}, level = {"number", 1}, -- 等级 breakL = {"number", 0}, -- 突破等级 @@ -36,9 +37,13 @@ function Hero:notifyUpdateProperty(field, newValue, oldValue) self:notifyUpdateProperties(datas) end +function Hero:getSimpleHeroId() + return self:getProperty("id") % (self:getProperty("roleid") * MAX_HERO_NUM) +end + function Hero:notifyUpdateProperties(params) local updateData = { - id = self:getProperty("id"), + id = self:getSimpleHeroId(), datas = params } SendPacket(actionCodes.Hero_updateProperty, MsgPack.pack(updateData)) diff --git a/src/models/RoleIncre.lua b/src/models/RoleIncre.lua new file mode 100644 index 0000000..1c89043 --- /dev/null +++ b/src/models/RoleIncre.lua @@ -0,0 +1,20 @@ +local RoleIncre = class("RoleIncre", require("shared.ModelBaseMysql")) + +function RoleIncre:ctor( properties ) + RoleIncre.super.ctor(self, properties) +end + + +RoleIncre.schema = { + id = {"number", 0, "pri"}, + heroId = {"number", 0}, + runeId = {"number", 0}, +} + +function RoleIncre:increBy(keyName, val) + local curId = self:getProperty(keyName) + self:setProperty(keyName, curId + val) + return curId + val +end + +return RoleIncre \ No newline at end of file diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index 52f8f3a..059540c 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -12,6 +12,7 @@ function RolePlugin.bind(Role) self:loadDiner() self:loadActivity() self:loadStoreInfo() + self:loadRoleIncre() end function Role:reloadWhenLogin() @@ -542,13 +543,12 @@ function RolePlugin.bind(Role) local unitData = csvdb["unitCsv"][heroType] if not unitData then return false end - local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) - - redisproxy:sadd(string.format(R_HEROS, roleId), heroId) + local heroId = self.roleIncre:increBy("heroId" , 1) local heroInfo = { - key = string.format(R_HERO, roleId, heroId), - id = heroId, + key = string.format("%d",roleId * MAX_HERO_NUM + heroId), + id = roleId * MAX_HERO_NUM + heroId, + roleid = roleId, type= heroType, wakeL = globalCsv.unit_wake_initLevel[unitData.rare], } @@ -611,17 +611,12 @@ function RolePlugin.bind(Role) function Role:loadHeros() local roleId = self:getProperty("id") - local heroIds = redisproxy:smembers(string.format(R_HEROS, roleId)) - local redret = redisproxy:pipelining(function (red) - for _, heroId in ipairs(heroIds) do - red:hgetall(string.format(R_HERO, roleId, heroId)) - end - end) - for index, heroId in ipairs(heroIds) do - local hero = require("models.Hero").new({key = string.format(R_HERO, roleId, heroId)}) - if hero:load(table.array2Table(redret[index])) then + local res = mysqlproxy:query(string.format("SELECT * FROM `Hero` WHERE `roleid` = %s", roleId)) + for _, data in ipairs(res) do + local hero = require("models.Hero").new({key = string.format("%d",data.id), id=data.id}) + if hero:load(data) then hero.owner = self - self.heros[tonumber(heroId)] = hero + self.heros[hero:getSimpleRuneId()] = hero end end end @@ -679,6 +674,18 @@ function RolePlugin.bind(Role) end end + function Role:loadRoleIncre() + local roleId = self:getProperty("id") + local dataKey = string.format("%d", roleId) + self.roleIncre = require("models.RoleIncre").new({key = dataKey, id = roleId}) + self.roleIncre.owner = self + if not self.roleIncre:checkKeyExists(dataKey) then + self.roleIncre:create() + else + self.roleIncre:load() + end + end + function Role:addEquip(equipType, equipLv, count, pms) pms = pms or {} if count ~= count then return end @@ -734,17 +741,12 @@ function RolePlugin.bind(Role) function Role:loadRunes() local roleId = self:getProperty("id") - local runeIds = redisproxy:smembers(string.format(R_RUNEIDS, roleId)) - local redret = redisproxy:pipelining(function (red) - for _, runeId in ipairs(runeIds) do - red:hgetall(string.format(R_RUNE, roleId, runeId)) - end - end) - for index, runeId in ipairs(runeIds) do - local rune = require("models.Rune").new({key = string.format(R_RUNE, roleId, runeId)}) - if rune:load(table.array2Table(redret[index])) then - rune.owner = self - self.runeBag[tonumber(runeId)] = rune + local res = mysqlproxy:query(string.format("SELECT * FROM `Rune` WHERE `roleid` = %s", roleId)) + for _, data in ipairs(res) do + local rune = require("models.Rune").new({key = string.format("%d",data.id), id=data.id}) + if hero:load(data) then + hero.owner = self + self.runeBag[rune:getSimpleRuneId()] = rune end end end @@ -758,18 +760,17 @@ function RolePlugin.bind(Role) if not data then return 3 end local roleId = self:getProperty("id") - local runeUid = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "rune", 1)) - - redisproxy:sadd(string.format(R_RUNEIDS, roleId), runeUid) + local runeUid = self.roleIncre:increBy("runeId" , 1) - local heroInfo = { - key = string.format(R_RUNE, roleId, runeUid), - uid = runeUid, + local runeInfo = { + key = string.format("%d",roleId * MAX_RUNE_NUM + runeUid), + uid = roleId * MAX_RUNE_NUM + runeUid, + roleid = roleId, type = params.type, id = params.id, } - local newRune = require("models.Rune").new(heroInfo) + local newRune = require("models.Rune").new(runeInfo) newRune:create() newRune:generateAttrs() newRune.owner = self @@ -832,6 +833,7 @@ function RolePlugin.bind(Role) end end + -- delete rune redisproxy:pipelining(function (red) for _, runeId in pairs(bDel) do red:del(string.format(R_RUNE, roleId, runeId)) diff --git a/src/models/Rune.lua b/src/models/Rune.lua index fb39fe4..7605739 100644 --- a/src/models/Rune.lua +++ b/src/models/Rune.lua @@ -1,6 +1,7 @@ -local Rune = class("Rune", require("shared.ModelBase")) +local Rune = class("Rune", require("shared.ModelBaseMysql")) Rune.schema = { - uid = {"number"}, -- 唯一自增id + uid = {"number", 0, "pri"}, -- 唯一自增id + roleid = {"number", 0, "index"}, type = {"number"}, -- 装备位置 id = {"number"}, level = {"number", 0}, -- 等级 @@ -12,6 +13,10 @@ function Rune:ctor( properties ) Rune.super.ctor(self, properties) end +function Rune:getSimpleRuneId() + return self:getProperty("uid") % (self:getProperty("roleid") * MAX_RUNE_NUM) +end + function Rune:notifyUpdateProperty(field, newValue, oldValue) local datas = { uid = self:getProperty("uid"), @@ -40,7 +45,7 @@ end function Rune:notifyUpdateProperties(params) local updateData = { - uid = self:getProperty("uid"), + uid = self:getSimpleRuneId(), datas = params } SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) diff --git a/src/services/dbseed.lua b/src/services/dbseed.lua index 4c1a52a..431eb22 100644 --- a/src/services/dbseed.lua +++ b/src/services/dbseed.lua @@ -89,7 +89,7 @@ local function initAdvSeasonTable() end local function checkRoleTables() - local list = {"Role", "Daily", "Activity", "Diner", "Store"} + local list = {"Role", "Daily", "Activity", "Diner", "Store", "Hero", "RoleIncre", "Rune"} for _, name in ipairs(list) do local obj = require("models."..name).new({key = "key"}) print("check table [" .. name .. "] begin.") diff --git a/src/services/mysqld.lua b/src/services/mysqld.lua index 2ba3a1f..bbd6ebd 100644 --- a/src/services/mysqld.lua +++ b/src/services/mysqld.lua @@ -16,7 +16,7 @@ function command.open(conf) database="mysql", user="root", password="123456", - max_packet_size = 1024 * 1024, + max_packet_size = 5 * 1024 * 1024, on_connect = on_connect }) if not db then diff --git a/src/shared/ModelBaseMysql.lua b/src/shared/ModelBaseMysql.lua index 4ea2499..bd9773d 100644 --- a/src/shared/ModelBaseMysql.lua +++ b/src/shared/ModelBaseMysql.lua @@ -65,13 +65,16 @@ function ModelBaseMysql:load(properties) print(string_format("%s [%s:id] should be set before load", tostring(self), self.class.__cname)) return false end - + local load = false if not properties then properties = mysqlproxy:query(string_format("SELECT * from %s where `%s` = %s;", self.class.__cname, self.pri_key, self:getKey())) + load = true end if not next(properties) then return false end - self:loadProperties(properties[1]) + local data = load and properties[1] or properties + + self:loadProperties(data) self:onLoad() @@ -309,15 +312,18 @@ function ModelBaseMysql:checkTableSchema() local typeMap = { number = {"int", 0, 10}, string = {"varchar", "", 128}, - table = {"blob", "NULL"} + table = {"blob", "NULL"}, + pri = {"bigint", 0}, } local tbName = self.class.__cname local create_sql = [[ CREATE TABLE IF NOT EXISTS `%s` ( %s - PRIMARY KEY (`%s`) + PRIMARY KEY (`%s`)%s ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; ]] + local index_tpl_str = [[,INDEX `%s_Index` (`%s`)]] + local alter_sql = [[ ALTER TABLE `%s` ADD COLUMN ( %s @@ -327,6 +333,7 @@ function ModelBaseMysql:checkTableSchema() local field_str = "" local res = mysqlproxy:query("desc ".. tbName .. ";") + local keyList = {} if res["err"] then -- 表不存在 local schema = {} for k, v in pairs(self.class.schema) do @@ -335,6 +342,9 @@ function ModelBaseMysql:checkTableSchema() self.pri_key = k table_insert(schema, 1, {k, v}) else + if keyType == "index" then + table_insert(keyList, k) + end table_insert(schema, {k, v}) end end @@ -342,6 +352,8 @@ function ModelBaseMysql:checkTableSchema() local k, v = tbl[1], tbl[2] local objType, def, keyType, length = table_unpack(v) assert(typeMap[objType], string_format("schema invalid type, %s, %s", tbName, k)) + -- 主键使用bigint存储 + if keyType == "pri" then objType = "pri" end local info = typeMap[objType] local suffix = "" @@ -362,8 +374,12 @@ function ModelBaseMysql:checkTableSchema() end assert(self.pri_key, string_format("table not include primary key, [%s]", tbName)) + local index_key_str = "" + for _, k in ipairs(keyList) do + index_key_str = index_key_str .. string_format(index_tpl_str, k, k) + end -- 创建表格 - mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key)) + mysqlproxy:query(string_format(create_sql, tbName, field_str, self.pri_key, index_key_str)) else -- 检测是否有添加新字段 local addCol = {} local curCols = {} -- libgit2 0.21.2