From ad484303cb54fc675d96a74be7c853e067641e9f Mon Sep 17 00:00:00 2001 From: gaofengduan <782277855@qq.com> Date: Wed, 3 Jul 2019 20:59:28 +0800 Subject: [PATCH] add rune --- .luacheckrc | 2 +- src/RedisKeys.lua | 4 ++-- src/actions/GmAction.lua | 7 +++++++ src/models/Equip.lua | 20 +++++++++++++++++++- src/models/RolePlugin.lua | 96 ++++++++++++++++++++++++++++++++++++++---------------------------------------------------------- src/models/Rune.lua | 54 +++++++++++++++++++++++++++++++++++++++++++++++------- 6 files changed, 114 insertions(+), 69 deletions(-) diff --git a/.luacheckrc b/.luacheckrc index d1ede57..3ef248d 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,6 +1,6 @@ std = "max" globals = { - "R_RUNE_ROOT", + "R_RUNEIDS", "R_EQUIP_ROOT", "R_EQUIP", "R_RUNE", diff --git a/src/RedisKeys.lua b/src/RedisKeys.lua index dd849c0..19be3d3 100644 --- a/src/RedisKeys.lua +++ b/src/RedisKeys.lua @@ -6,8 +6,8 @@ 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 +R_RUNEIDS = "role:%d:runeIds" -- 玩家拥有符文自增id +R_RUNE = "role:%d:rune:%d" -- 符文详细信息 -- -- role -- R_FARM_KEY = "role:%d:farm" diff --git a/src/actions/GmAction.lua b/src/actions/GmAction.lua index fe79b1d..b0abb1d 100644 --- a/src/actions/GmAction.lua +++ b/src/actions/GmAction.lua @@ -30,6 +30,13 @@ function _M.equip(role, pms) return "成功" end +function _M.rune(role, pms) + local typ = tonum(pms.pm1) + local id = tonum(pms.pm2) + local result = role:addRune({type = typ,id = id}) + return result +end + function _M.get(role, pms) local itemId = tonum(pms.pm1) if not csvdb["itemCsv"][itemId] then diff --git a/src/models/Equip.lua b/src/models/Equip.lua index 048ff4c..d4a1c8f 100644 --- a/src/models/Equip.lua +++ b/src/models/Equip.lua @@ -11,6 +11,24 @@ function Equip:ctor( properties ) Equip.super.ctor(self, properties) end +function Equip:notifyUpdateProperty(field, newValue, oldValue) + local datas = { + key = field, + newValue = newValue, + oldValue = oldValue, + } + self:notifyUpdateProperties(datas) +end + +function Equip:notifyUpdateProperties(params) + local updateData = { + type = self:getProperty("type"), + level = self:getProperty("level"), + datas = params + } + SendPacket(actionCodes.Role_updateEquip, MsgPack.pack(updateData)) +end + function Equip:updateProperty(params) if not params.field or (not params.delta and not params.value) then return @@ -20,7 +38,7 @@ function Equip:updateProperty(params) elseif params.value then self:setProperty(params.field, params.value) end - SendPacket(actionCodes.Role_updateEquip, MsgPack.pack(self:data())) + self:notifyUpdateProperties(self:data()) end function Equip:data() diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index a66c876..3bbeab3 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -433,72 +433,52 @@ function RolePlugin.bind(Role) 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) + 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 + end end end + -- 0 为操作成功 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 params.type and params.id then + local set = csvdb["runeCsv"][params.type] + if not set then return 2 end + local data = set[params.id] + 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 heroInfo = { + key = string.format(R_RUNE, roleId, runeUid), + uid = runeUid, + type = params.type, + id = params.id, + } + + local newRune = require("models.Rune").new(heroInfo) + newRune:create() + newRune:generateAttrs() + newRune.owner = self + self.runeBag[runeUid] = newRune - if rune then - count = rune:getProperty("count")+count + return 0 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() + return 1 end - rune:updateProperty({field = "count", value = math.min(count,0)}) - self.runeBag[runeId][runeLv] = rune end function Role:getRuneCount(id,lv) diff --git a/src/models/Rune.lua b/src/models/Rune.lua index c5c5842..d4e7412 100644 --- a/src/models/Rune.lua +++ b/src/models/Rune.lua @@ -1,9 +1,11 @@ local Rune = class("Rune", require("shared.ModelBase")) Rune.schema = { + uid = {"number"}, -- 唯一自增id + type = {"number"}, -- 装备位置 id = {"number"}, level = {"number", 0}, -- 等级 - count = {"number", 0}, -- 数量 refer = {"number", 0}, -- 已装备英雄id + attrs = {"string",""} -- 基础属性值 id=value } function Rune:ctor( properties ) @@ -21,7 +23,7 @@ end function Rune:notifyUpdateProperties(params) local updateData = { - id = self:getProperty("id"), + uid = self:getProperty("uid"), datas = params } SendPacket(actionCodes.Role_updateRune, MsgPack.pack(updateData)) @@ -36,16 +38,54 @@ function Rune:updateProperty(params) 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) + self:notifyUpdateProperties(self:data()) +end + +-- types 类型=权重"2=100 3=100 4=100" value最大值=最小值 "50=100 50=100 100=200" +local function getRandomValue(types,values) + local typeMap = types:toNumMap() + local valueArry = values:toArray() + if table.nums(typeMap) ~= #valueArry then return nil end + local typ,value + local typTab,weightSum = {},0 + for t,w in pairs(typeMap) do + weightSum = weightSum + w + table.insert(typTab,{t=t,w=weightSum}) + end + + local tk + local tmp = math.random(1,weightSum) + for k,v in ipairs(typTab) do + if v.w > tmp then + typ = v.t + tk = k + break + end + end + + local range = valueArry[tk]:toArray(true,"=") + value = math.random(range[1],range[2]) + print("typ "..typ.." value "..value) + return typ,value +end + +function Rune:generateAttrs() + local runeData = csvdb["runeCsv"][self:getProperty("type")][self:getProperty("id")] + local attrs = "" + attrs:setv(1,20) + attrs:setv(getRandomValue(runeData.attr1,runeData.range1)) + attrs:setv(getRandomValue(runeData.attr2,runeData.range2)) + print(attrs) + self:setProperty("attrs","2=30") end function Rune:data() return { + uid = self:getProperty("uid"), + type = self:getProperty("type"), id = self:getProperty("id"), - level = self:getProperty("level"), - count = self:getProperty("count"), + refer = self:getProperty("refer"), + attrs = self:getProperty("attrs"), } end -- libgit2 0.21.2