From 43cc5f51fa654a8a29e951ca5f6ca9737b80bbbf Mon Sep 17 00:00:00 2001 From: gaofengduan Date: Tue, 2 Jul 2019 20:32:29 +0800 Subject: [PATCH] 调整 equip 数据结构 --- .luacheckrc | 4 ++++ src/ProtocolCode.lua | 4 ++++ src/RedisKeys.lua | 4 ++++ src/actions/CarAction.lua | 19 ++++++++++--------- src/actions/GmAction.lua | 8 ++++++++ src/actions/HeroAction.lua | 49 +++++++++++++++++++++++++++++++++++++++++++++++++ src/actions/RoleAction.lua | 26 +++++++++++++++++++++----- src/csvdata | 2 +- src/models/Equip.lua | 34 ++++++++++++++++++++++++++++++++++ src/models/Hero.lua | 3 ++- src/models/Role.lua | 2 ++ src/models/RolePlugin.lua | 181 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- src/models/Rune.lua | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 13 files changed, 363 insertions(+), 25 deletions(-) create mode 100644 src/models/Equip.lua create mode 100644 src/models/Rune.lua diff --git a/.luacheckrc b/.luacheckrc index ec98b41..d1ede57 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,5 +1,9 @@ std = "max" globals = { + "R_RUNE_ROOT", + "R_EQUIP_ROOT", + "R_EQUIP", + "R_RUNE", "DinerTask", "SendPipelining", "AdvEventType", diff --git a/src/ProtocolCode.lua b/src/ProtocolCode.lua index a0311c6..5b4a25d 100644 --- a/src/ProtocolCode.lua +++ b/src/ProtocolCode.lua @@ -23,6 +23,8 @@ actionCodes = { Role_pipelining = 109, Role_saleItemRpc = 110, Role_openItemRpc = 111, + Role_updateEquip = 112, + Role_updateRune = 113, Adv_startAdvRpc = 151, Adv_roleFormatRpc = 152, @@ -46,6 +48,8 @@ actionCodes = { Hero_loveTaskRpc = 213, Hero_changeSkinRpc = 214, Hero_createHeroRpc = 215, + Hero_referEquipsRpc = 216, + Hero_referRunesRpc = 217, Hang_startRpc = 251, Hang_checkRpc = 252, diff --git a/src/RedisKeys.lua b/src/RedisKeys.lua index eaf8480..dd849c0 100644 --- a/src/RedisKeys.lua +++ b/src/RedisKeys.lua @@ -4,6 +4,10 @@ R_HEROS = "role:%d:heroIds" R_HERO = "hero:%d:%d" R_DAILY = "role:%d:daily" R_DINER = "role:%d:diner" -- 餐厅 +R_EQUIP_ROOT = "role:%d:equip*" -- 装备根目录 +R_EQUIP = "role:%d:equip:%d:%d" -- 装备type:level +R_RUNE_ROOT = "role:%d:rune*" -- 符文根目录 +R_RUNE = "role:%d:rune:%d" -- 符文零件id:level -- -- role -- R_FARM_KEY = "role:%d:farm" diff --git a/src/actions/CarAction.lua b/src/actions/CarAction.lua index d0508cf..882c1ab 100644 --- a/src/actions/CarAction.lua +++ b/src/actions/CarAction.lua @@ -64,8 +64,14 @@ function _M.equipUpRpc( agent, data ) local nextEquip = dataSet[nextLv] if not nextEquip then return 23 end - local own = role:getItemCount(id) - if own < count then + local ownSet = role.equipBag[typ] + if not ownSet then return 31 end + local ownData = ownSet[lv] + if not ownData then return 32 end + + local own = role:getEquipCount(typ,lv) + local costCount = equipData.merge*count + if own < costCount then return 3 end @@ -77,14 +83,9 @@ function _M.equipUpRpc( agent, data ) return 4 end - local merge = {[id]=equipData.merge*count} - if not role:checkItemEnough(merge) then - return 5 - end - role:costItems(cost) - role:costItems(merge) - role:addItem({itemId = nextEquip.id,count = count}) + role:addEquip({type=typ,level=lv,count=-costCount}) + role:addEquip({type=typ,level=nextLv,count=count}) SendPacket(actionCodes.Car_equipUpRpc, '') return true end diff --git a/src/actions/GmAction.lua b/src/actions/GmAction.lua index 0ae0f5e..fe79b1d 100644 --- a/src/actions/GmAction.lua +++ b/src/actions/GmAction.lua @@ -22,6 +22,14 @@ function _M.hero(role, pms) return "成功" end +function _M.equip(role, pms) + local typ = tonum(pms.pm1) + local level = tonum(pms.pm2) + local count = tonum(pms.pm3) + role:addEquip({type = typ,level = level,count = count}) + return "成功" +end + function _M.get(role, pms) local itemId = tonum(pms.pm1) if not csvdb["itemCsv"][itemId] then diff --git a/src/actions/HeroAction.lua b/src/actions/HeroAction.lua index 0cbe33c..37b6d2b 100644 --- a/src/actions/HeroAction.lua +++ b/src/actions/HeroAction.lua @@ -451,4 +451,53 @@ function _M.createHeroRpc(agent, data) return true end +-- typ 位置,level等级对应唯一装备,level为0时为移除,不为0时无则装备,有则替换 +function _M.referEquips(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local hero = role.heros[msg.id] + if not hero then return 10 end + local equips = msg.equips + if not equips or not next(equips) then return 11 end + + for typ,level in pairs(equips) do + local ownLv = hero:getProperty("equip"):getv(typ,0) + if level == 0 then + if ownLv == 0 then return 2 end + else + if role:getEquipCount(typ,level) < 1 then return 3 end + end + local equipSet = csvdb["equipCsv"][typ] + if not equipSet then return 4 end + local equipData = equipSet[level] + if not equipData then return 5 end + end + + for typ,level in pairs(equips) do + local ownLv = hero:getProperty("equip"):getv(typ,0) + if level == 0 then + role:addEquip({{type=typ,level=ownLv,count=1}}) + else + role:addEquip({{type=typ,level=level,count=-1}}) + if ownLv ~= 0 then + role:addEquip({type=typ,level=ownLv,count=1}) + end + end + hero:updateProperty({field = "equip", value = hero:getProperty("equip"):setv(typ, level)}) + end + SendPacket(actionCodes.Hero_referEquipsRpc, "") + return true +end + +-- typ 位置,id等级对应唯一符文,id为0时为移除,不为0时无则装备,有则替换 +function _M.referRunes(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local hero = role.heros[msg.id] + if not hero then return 10 end + local runes = msg.runes + if not runes or not next(runes) then return 11 end + return true +end + return _M \ No newline at end of file diff --git a/src/actions/RoleAction.lua b/src/actions/RoleAction.lua index c2b8ccf..90ba490 100644 --- a/src/actions/RoleAction.lua +++ b/src/actions/RoleAction.lua @@ -97,7 +97,7 @@ function _M.loginRpc( agent, data ) else role:reloadWhenLogin() end - + if not msg.isGMlogin then local banTime = role:getProperty("banTime") if banTime > now then @@ -120,7 +120,7 @@ function _M.loginRpc( agent, data ) -- 跨天登陆事件 role:onCrossDay(now) role:setProperty("ltime", now) - + for _, name in ipairs({"dailyData", "dinerData"}) do response[name] = role[name]:data() @@ -149,10 +149,26 @@ function _M.loginRpc( agent, data ) end end - response.wave = 1 + heroWave + response.wave = 3 + heroWave SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(response)) + local equipResp = {equipBag = {}} + for _,set in pairs(role.equipBag) do + for _,equip in pairs(set) do + local data = equip:data() + if not equipResp.equipBag[data.type] then equipResp.equipBag[data.type] = {} end + equipResp.equipBag[data.type][data.level] = data + end + end + SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(equipResp)) + + local runeResp = {runeBag = {}} + for _,rune in pairs(role.runeBag) do + table_insert(runeResp.runeBag, rune:data()) + end + SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(runeResp)) + local heroIndex = 1 for index = 2, 1 + heroWave do local heroResponse = {heros = {}} @@ -177,7 +193,7 @@ function _M.loginRpc( agent, data ) gate_serv = agent.gate_serv, }) agent.role = role - + start_agent_timer() -- 注册全服广播 local channel = math.randomInt(1, 1) @@ -293,7 +309,7 @@ function _M.openItemRpc(agent, data) end role:costItems({[itemId] = count}) reward = role:award(reward) - + SendPacket(actionCodes.Role_openItemRpc, MsgPack.pack({reward = reward})) return true end diff --git a/src/csvdata b/src/csvdata index c4ec029..a7426f9 160000 --- a/src/csvdata +++ b/src/csvdata @@ -1 +1 @@ -Subproject commit c4ec029a5f26392cf6c1471d1db1b58e0bdb5802 +Subproject commit a7426f9183da7afcd6b2780d23a4f6241d3e3aab diff --git a/src/models/Equip.lua b/src/models/Equip.lua new file mode 100644 index 0000000..048ff4c --- /dev/null +++ b/src/models/Equip.lua @@ -0,0 +1,34 @@ +local Equip = class("Equip", require("shared.ModelBase")) + +Equip.schema = { + type = {"number"}, -- 类型 + level = {"number"}, -- 等级 + count = {"number", 0}, -- 数量 + refer = {"number", 0}, -- 已装备英雄数 +} + +function Equip:ctor( properties ) + Equip.super.ctor(self, properties) +end + +function Equip:updateProperty(params) + if not params.field or (not params.delta and not params.value) then + return + end + if params.delta then + self:incrProperty(params.field, params.delta) + elseif params.value then + self:setProperty(params.field, params.value) + end + SendPacket(actionCodes.Role_updateEquip, MsgPack.pack(self:data())) +end + +function Equip:data() + return { + type = self:getProperty("type"), + level = self:getProperty("level"), + count = self:getProperty("count"), + } +end + +return Equip \ No newline at end of file diff --git a/src/models/Hero.lua b/src/models/Hero.lua index 0fbf5e7..4c630e7 100644 --- a/src/models/Hero.lua +++ b/src/models/Hero.lua @@ -15,11 +15,12 @@ Hero.schema = { loveExp = {"number", 0}, --好感度经验 loveL = {"number", 0}, --好感度等级 skin = {"number", 0}, --皮肤 0 、 1、 2、 3 - equip = {"string",""}, --装备 type=level + equip = {"string","1=0 2=0 3=0 4=0"}, --装备 type=level } function Hero:ctor( properties ) Hero.super.ctor(self, properties) + self.runes = {} --符文-零件自增 id end function Hero:notifyUpdateProperty(field, newValue, oldValue) diff --git a/src/models/Role.lua b/src/models/Role.lua index cb3fe08..45f8644 100644 --- a/src/models/Role.lua +++ b/src/models/Role.lua @@ -13,6 +13,8 @@ function Role:ctor( properties ) Role.super.ctor(self, properties) self.ignoreHeartbeat = false self.heros = {} + self.equipBag = {} + self.runeBag = {} self.advData = nil end diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index 8706801..251b78a 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -10,6 +10,8 @@ function RolePlugin.bind(Role) function Role:loadAll() self:loadDaily() + self:loadEquips() + self:loadRunes() self:loadHeros() self:loadDiner() end @@ -33,7 +35,7 @@ function RolePlugin.bind(Role) end end function Role:onOfflineEvent() - + end local function checkItemCount(self, itemId, count) @@ -44,11 +46,10 @@ function RolePlugin.bind(Role) if limit and self:getItemCount(itemId) == 0 then local curCount = 0 local items = self:getProperty("items"):toNumMap() - for _itemId, _count in pairs(items) do - local _itemData = csvdb["itemCsv"][itemId] - if globalCsv.store_type[_itemData.type] == page then + for _,_ in pairs(items) do + if globalCsv.store_type[itemData.type] == page then curCount = curCount + 1 - if curCount >= limit then + if curCount >= limit then count = 0 break end @@ -135,7 +136,7 @@ function RolePlugin.bind(Role) if not csvdb["player_expCsv"][level + 1] then return end - + local exp = self:getProperty("exp") local newExp = exp + addExp while newExp >= csvdb["player_expCsv"][level].exp do @@ -207,7 +208,7 @@ function RolePlugin.bind(Role) end function Role:getItemCount(itemId) - if itemId == ItemId.Diamond then + if itemId == ItemId.Diamond then return self:getAllDiamond() end return self:getProperty("items"):getv(itemId, 0) @@ -283,7 +284,7 @@ function RolePlugin.bind(Role) id = heroId, type= heroType, } - + local newHero = require("models.Hero").new(heroInfo) newHero:create() newHero.owner = self @@ -352,8 +353,170 @@ function RolePlugin.bind(Role) end end + function Role:loadEquips() + local roleId = self:getProperty("id") + self.equipBag = {} + local keys = redisproxy:keys(string.format(R_EQUIP_ROOT, roleId)) + if keys and next(keys) then + for _,v in pairs(keys) do + local equip = require("models.Equip").new({key = v}) + equip:load() + local typ,lv = equip:getProperty("type"),equip:getProperty("level") + equip.owner = self + if not self.equipBag[typ] then self.equipBag[typ] = {} end + self.equipBag[typ][lv] = equip + end + end + end + + function Role:addEquip(params) + if params.type and params.level and params.count then + local equipType = params.type + local equipLv = params.level + local count = params.count + -- isRefer为true时count>0为卸载操作,<0为装备操作 + local isRefer = params.isRefer or false + local equip = nil + local equipSet = self.equipBag[equipType] + if equipSet then + equip = self.equipBag[equipType][equipLv] + else + self.equipBag[equipType] = {} + end + + if equip then + local originCount = equip:getProperty("count") + if isRefer and count < 0 then + if count < 0 then + if count+originCount<0 then + return + end + else + if equip:getProperty("refer")-count<0 then + return + end + end + end + count = originCount+count + else + if isRefer then return end + local roleId = self:getProperty("id") + local data = { + key = string.format(R_EQUIP,roleId,equipType,equipLv), + type = equipType, + level = equipLv, + } + equip = require("models.Equip").new(data) + equip.owner = self + equip:create() + end + equip:updateProperty({field = "count", value = math.min(count,0)}) + if isRefer then + equip:updateProperty({field = "refer", value = math.min(equip:getProperty("refer")+count,0)}) + end + self.equipBag[equipType][equipLv] = equip + end + end + + function Role:getEquipCount(typ,lv) + local equipSet = self.equipBag[typ] + if equipSet then + local equip = equipSet[lv] + if equip then + return equip:getProperty("count") + else + return 0 + end + else + return 0 + end + end + + function Role:loadRunes() + -- local roleId = self:getProperty("id") + -- self.runeBag = {} + -- local keys = redisproxy:keys(string.format(R_EQUIP_ROOT, roleId)) + -- if keys and next(keys) then + -- for _,v in pairs(keys) do + -- local rune = require("models.Rune").new({key = v}) + -- rune.owner = self + -- self.runeBag[rune.id][rune.level] = rune + -- end + -- end + end + + function Role:addHero(params) + local roleId = self:getProperty("id") + local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) + local heroType = params.type + + redisproxy:sadd(string.format(R_HEROS, roleId), heroId) + + local heroInfo = { + key = string.format(R_HERO, roleId, heroId), + id = heroId, + type= heroType, + } + + local newHero = require("models.Hero").new(heroInfo) + newHero:create() + newHero.owner = self + newHero:saveBattleValue() + self.heros[heroId] = newHero + + if not params.notNotify then + local heroResponse = {} + table.insert(heroResponse, newHero:data()) + local bin = MsgPack.pack(heroResponse) + SendPacket(actionCodes.Hero_loadInfos, bin) + end + end + + function Role:addRune(params) + local roleId = self:getProperty("id") + local runeId = params.id + local runeLv = params.level + local count = params.count + local rune = nil + local runeSet = self.runeBag[runeId] + if runeSet then + rune = self.runeBag[runeId][runeLv] + else + self.runeBag[runeId] = {} + end + + if rune then + count = rune:getProperty("count")+count + else + local data = { + key = string.format(R_RUNE,roleId,runeId,runeLv), + id = runeId, + level = runeLv, + } + rune = require("models.Rune").new(data) + rune.owner = self + rune:create() + end + rune:updateProperty({field = "count", value = math.min(count,0)}) + self.runeBag[runeId][runeLv] = rune + end + + function Role:getRuneCount(id,lv) + local runeSet = self.runeBag[id] + if runeSet then + local rune = runeSet[lv] + if rune then + return rune:getProperty("count") + else + return 0 + end + else + return 0 + end + end + function Role:getAdvData() - if not self.advData then + if not self.advData then self.advData = require("adv.Adv").new(self) self.advData:initByInfo() end diff --git a/src/models/Rune.lua b/src/models/Rune.lua new file mode 100644 index 0000000..c5c5842 --- /dev/null +++ b/src/models/Rune.lua @@ -0,0 +1,52 @@ +local Rune = class("Rune", require("shared.ModelBase")) +Rune.schema = { + id = {"number"}, + level = {"number", 0}, -- 等级 + count = {"number", 0}, -- 数量 + refer = {"number", 0}, -- 已装备英雄id +} + +function Rune:ctor( properties ) + Rune.super.ctor(self, properties) +end + +function Rune:notifyUpdateProperty(field, newValue, oldValue) + local datas = { + key = field, + newValue = newValue, + oldValue = oldValue, + } + self:notifyUpdateProperties(datas) +end + +function Rune:notifyUpdateProperties(params) + local updateData = { + id = self:getProperty("id"), + datas = params + } + SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) +end + +function Rune:updateProperty(params) + if not params.field or (not params.delta and not params.value) then + return + end + if params.delta then + self:incrProperty(params.field, params.delta) + elseif params.value then + self:setProperty(params.field, params.value) + end + local datas = {} + table.insert(datas, {key = params.field, newValue = self:getProperty(params.field)}) + self:notifyUpdateProperties(datas) +end + +function Rune:data() + return { + id = self:getProperty("id"), + level = self:getProperty("level"), + count = self:getProperty("count"), + } +end + +return Rune \ No newline at end of file -- libgit2 0.21.2