From 056c01a07602117c9543bb5cb6157bfa00477da8 Mon Sep 17 00:00:00 2001 From: zhouhaihai Date: Sat, 13 Jul 2019 17:19:11 +0800 Subject: [PATCH] 简化装备 --- src/ProtocolCode.lua | 2 +- src/RedisKeys.lua | 1 - src/actions/CarAction.lua | 44 ++++++++++++++++++++++++++++++++++++-------- src/actions/GmAction.lua | 10 ++++++---- src/actions/HeroAction.lua | 46 +++++++++++++++++++++++++--------------------- src/actions/RoleAction.lua | 14 ++------------ src/models/Role.lua | 6 ++++-- src/models/RolePlugin.lua | 117 ++++++++++++++++++++++++++++++++++++++++++++++----------------------------------------------------------------------- 8 files changed, 120 insertions(+), 120 deletions(-) diff --git a/src/ProtocolCode.lua b/src/ProtocolCode.lua index 1cfe567..16d03d8 100644 --- a/src/ProtocolCode.lua +++ b/src/ProtocolCode.lua @@ -23,7 +23,6 @@ actionCodes = { Role_pipelining = 109, Role_saleItemRpc = 110, Role_openItemRpc = 111, - Role_updateEquip = 112, Role_updateRune = 113, Role_storyBookRewardRpc = 114, Role_unLockStoryBookRpc = 115, @@ -81,6 +80,7 @@ actionCodes = { Car_makePotionRpc = 400, Car_equipUpRpc = 401, Car_runeUpRpc = 402, + Car_saleEquipRpc = 403, } rpcResponseBegin = 10000 diff --git a/src/RedisKeys.lua b/src/RedisKeys.lua index 19be3d3..7e3cc2b 100644 --- a/src/RedisKeys.lua +++ b/src/RedisKeys.lua @@ -5,7 +5,6 @@ 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_RUNEIDS = "role:%d:runeIds" -- 玩家拥有符文自增id R_RUNE = "role:%d:rune:%d" -- 符文详细信息 diff --git a/src/actions/CarAction.lua b/src/actions/CarAction.lua index 98db567..8d8c2f0 100644 --- a/src/actions/CarAction.lua +++ b/src/actions/CarAction.lua @@ -47,7 +47,7 @@ function _M.equipUpRpc( agent, data ) local msg = MsgPack.unpack(data) local id = msg.id local count = msg.count - if count < 1 then return 0 end + local typ = math.floor((id-7000)/100) local lv = (id-7000)%100 @@ -56,6 +56,7 @@ function _M.equipUpRpc( agent, data ) local equipData = dataSet[lv] if not equipData then return 21 end if equipData.merge < 1 then return 22 end + local maxLv = 3 local nextLv = lv+1 if nextLv%10 > maxLv then @@ -64,13 +65,12 @@ function _M.equipUpRpc( agent, data ) local nextEquip = dataSet[nextLv] if not nextEquip then return 23 end - local ownSet = role.equipBag[typ] - if not ownSet then return 31 end - local ownData = ownSet[lv] - if not ownData then return 32 end + local limit = csvdb["ItemCsv"][nextEquip.id].limit ~= 0 and csvdb["ItemCsv"][nextEquip.id].limit or math.huge + + if math.illegalNum(count, 1, limit) then return 0 end local own = role:getEquipCount(typ,lv) - local costCount = equipData.merge*count + local costCount = equipData.merge * count if own < costCount then return 3 end @@ -84,8 +84,9 @@ function _M.equipUpRpc( agent, data ) end role:costItems(cost) - role:addEquip({type=typ,level=lv,count=-costCount}) - role:addEquip({type=typ,level=nextLv,count=count}) + role:addEquip(typ, lv, -costCount) + role:addEquip(typ, nextLv ,count) + SendPacket(actionCodes.Car_equipUpRpc, '') return true end @@ -121,4 +122,31 @@ function _M.runeUpRpc( agent, data ) return true end +function _M.Car_saleEquipRpc(agent, data ) + local role = agent.role + local msg = MsgPack.unpack(data) + + local id = msg.id + local count = msg.count + + local itemData = csvdb["ItemCsv"][id] + if not itemData then return end + + local typ = math.floor((id-7000)/100) + local lv = (id-7000)%100 + + local own = role:getEquipCount(typ,lv) + if math.illegalNum(count, 1, own) then return end + + role:addEquip(typ, lv, -count) -- 删掉装备 + -- 发奖励 + local reward = itemData.sell_effect:toNumMap() + for k ,v in pairs(reward) do + reward[k] = v * count + end + role:award(reward) + SendPacket(actionCodes.Car_saleEquipRpc, MsgPack.pack({reward = reward})) + return true +end + return _M \ No newline at end of file diff --git a/src/actions/GmAction.lua b/src/actions/GmAction.lua index ec6d89d..e4aab3a 100644 --- a/src/actions/GmAction.lua +++ b/src/actions/GmAction.lua @@ -18,7 +18,9 @@ end function _M.hero(role, pms) local heroType = tonum(pms.pm1) - role:addHero({type = heroType}) + if not role:addHero({type = heroType}) then + return "失败" + end return "成功" end @@ -39,9 +41,9 @@ end function _M.get(role, pms) if pms.pm1 == "ALL" then - for id,data in pairs(csvdb["itemCsv"]) do - if data.type ~= 4 and data.type ~= 5 then - role:award({[id] = 10}) + for itemId = 1, 100 do + if csvdb["itemCsv"][itemId] then + role:award({[itemId] = 1000000}) end end else diff --git a/src/actions/HeroAction.lua b/src/actions/HeroAction.lua index 4ff1117..071d9f8 100644 --- a/src/actions/HeroAction.lua +++ b/src/actions/HeroAction.lua @@ -506,32 +506,36 @@ function _M.referEquipsRpc(agent, data) 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 - local equipSet = csvdb["equipCsv"][typ] - if not equipSet then return 4 end - local equipData = equipSet[level] - if not equipData then return 5 end + for typ = 1, 4 do -- 4件装备 + if equips[typ] and equips[typ] ~= 0 then + if role:getEquipCount(typ, equips[typ]) <= 0 then + return + end 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,isRefer=true}) - else - role:addEquip({type=typ,level=level,count=-1,isRefer=true}) - if ownLv > 0 then - role:addEquip({type=typ,level=ownLv,count=1,isRefer=true}) + local curEquip = hero:getProperty("equip") + for typ = 1, 4 do -- 4件装备 + if equips[typ] then + local cur = curEquip:getv(typ, 0) + if equips[typ] == 0 then + if cur ~= 0 then + role:addEquip(typ, cur, 1) -- 脱掉 + curEquip = curEquip:delk(typ) + end + else + if cur ~= equips[typ] then + if cur ~= 0 then + role:addEquip(typ, cur, 1) -- 脱掉 + end + role:addEquip(typ, equips[typ], -1) -- 穿上 + curEquip = curEquip:setv(typ, equips[typ]) + end end end - local x = hero:getProperty("equip"):setv(typ, level) - hero:updateProperty({field = "equip", value = x}) end + -- 更新角色 + hero:updateProperty({field = "equip", value = curEquip}) + SendPacket(actionCodes.Hero_referEquipsRpc, "") return true end diff --git a/src/actions/RoleAction.lua b/src/actions/RoleAction.lua index 765a314..01668ad 100644 --- a/src/actions/RoleAction.lua +++ b/src/actions/RoleAction.lua @@ -15,7 +15,6 @@ local tconcat = table.concat local httpc = require("http.httpc") local WAVE_HERO_NUMS = 150 -local WAVE_EQUIP_NUMS = 150 local function validName(name) name = string.upper(name) @@ -149,19 +148,10 @@ function _M.loginRpc( agent, data ) end end - response.wave = 3 + heroWave + response.wave = 2 + heroWave SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(response)) - local equipResp = {equipBag = {}} - for typ,set in pairs(role.equipBag) do - for level,equip in pairs(set) do - if not equipResp.equipBag[typ] then equipResp.equipBag[typ] = {} end - equipResp.equipBag[typ][level] = equip:data() - end - end - SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(equipResp)) - local runeResp = {runeBag = {}} for _,rune in pairs(role.runeBag) do runeResp.runeBag[rune:getProperty("uid")]=rune:data() @@ -169,7 +159,7 @@ function _M.loginRpc( agent, data ) SendPacket(actionCodes.Role_loginRpc, MsgPack.pack(runeResp)) local heroIndex = 1 - for index = 2, 1 + heroWave do + for index = 3, heroWave + 2 do local heroResponse = {heros = {}} for i = heroIndex, heroIndex + WAVE_HERO_NUMS do local heroId = heroIds[i] diff --git a/src/models/Role.lua b/src/models/Role.lua index f450b5d..07a7d8a 100644 --- a/src/models/Role.lua +++ b/src/models/Role.lua @@ -13,7 +13,6 @@ function Role:ctor( properties ) Role.super.ctor(self, properties) self.ignoreHeartbeat = false self.heros = {} - self.equipBag = {} self.runeBag = {} self.advData = nil end @@ -57,6 +56,8 @@ Role.schema = { potionBag = {"table", {}}, -- 营养剂背包 storyB = {"table", {}}, -- 剧情记录 + + equips = {"table", {}}, -- 装备简化下, 目前的设计足够支撑 -- {t = {l = c}} -- 接口设计好 底层扩展就重写~ } @@ -180,7 +181,8 @@ function Role:data() potionBag = self:getProperty("potionBag"), storyB = self:getProperty("storyB"), - + equips = self:getProperty("equips"), + } end diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index 2f9981d..bb8a445 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -62,6 +62,9 @@ function RolePlugin.bind(Role) local function _award(self, itemId, count, params) local pms = clone(params) local itemData = csvdb["itemCsv"][itemId] + if not itemData then -- 加一层保护 + return 0, {} + end local curType = itemData.type local change = {} -- 奖励被转化为了其他奖励 id = count @@ -81,7 +84,7 @@ function RolePlugin.bind(Role) [ItemType.EquipBase] = function() local typ = math.floor((itemId-7000)/100) local lv = (itemId-7000)%100 - self:addEquip({type = typ,level = lv,count = count}) + self:addEquip(typ, lv, count ,pms) end, [ItemType.Rune] = function() local typ = math.floor((itemId-2000)/100) @@ -285,8 +288,12 @@ function RolePlugin.bind(Role) 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 + if self:isHaveHero(heroType) then return end + if not csvdb["unitCsv"][heroType] then return false end + + local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) + redisproxy:sadd(string.format(R_HEROS, roleId), heroId) @@ -308,6 +315,16 @@ function RolePlugin.bind(Role) local bin = MsgPack.pack(heroResponse) SendPacket(actionCodes.Hero_loadInfos, bin) end + return true + end + + function Role:isHaveHero(heroType) + if not csvdb["unitCsv"][heroType] then return false end + for _, hero in pairs(self.heros) do + if hero:getProperty("type") == heroType then + return true + end + end end function Role:delHero(heroId) @@ -365,82 +382,40 @@ function RolePlugin.bind(Role) 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 + -- 放role 里面了 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 + function Role:addEquip(equipType, equipLv, count, pms) + pms = pms or {} + if count ~= count then return end + local equipCsv = (csvdb["equipCsv"][equipType] or {})[equipLv] + if not equipCsv then return end + local equips = self:getProperty("equips") + local curTypeEquips = equips[equipType] or {} + local curCount = (curTypeEquips[equipLv] or 0) + count + + -- 是否足够上层判断 + if curCount < 0 then + curCount = 0 + curTypeEquips[equipLv] = nil + else + curTypeEquips[equipLv] = curCount + end - if equip then - local originCount = equip:getProperty("count") - if isRefer 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 = count}) - if isRefer then - equip:updateProperty({field = "refer", value = equip:getProperty("refer")+params.count}) - end - self.equipBag[equipType][equipLv] = equip + if next(curTypeEquips) then + equips[equipType] = curTypeEquips + else + equips[equipType] = nil + end + + self:setProperty("equips", equips) + if not pms.notNotify then + self:changeUpdates({{type = "equips", field = {equipType, equipLv}, value = curCount, isOnlyToC = true}}) -- 通知客户端 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 + return (self:getProperty("equips")[typ] or {})[lv] or 0 end function Role:loadRunes() -- libgit2 0.21.2