From 312b9db56e705df16efa6f205cec67c802103358 Mon Sep 17 00:00:00 2001 From: zhouahaihai Date: Tue, 5 Mar 2019 10:23:46 +0800 Subject: [PATCH] 背包 --- src/ProtocolCode.lua | 3 +++ src/actions/HeroAction.lua | 21 +++++++++++++++++++++ src/actions/RoleAction.lua | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/models/Role.lua | 7 ++++++- src/models/RolePlugin.lua | 118 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-------- src/utils/MathUtil.lua | 11 +++++++++++ 6 files changed, 205 insertions(+), 9 deletions(-) diff --git a/src/ProtocolCode.lua b/src/ProtocolCode.lua index d5025ca..85bf29f 100644 --- a/src/ProtocolCode.lua +++ b/src/ProtocolCode.lua @@ -21,6 +21,8 @@ actionCodes = { Role_updateItems = 107, Role_changeUpdate = 108, Role_pipelining = 109, + Role_saleItemRpc = 110, + Role_openItemRpc = 111, Adv_startAdvRpc = 151, Adv_roleFormatRpc = 152, @@ -43,6 +45,7 @@ actionCodes = { Hero_loveItemRpc = 212, Hero_loveTaskRpc = 213, Hero_changeSkinRpc = 214, + Hero_createHeroRpc = 215, Hang_startRpc = 251, Hang_checkRpc = 252, diff --git a/src/actions/HeroAction.lua b/src/actions/HeroAction.lua index 41066db..07ae6c5 100644 --- a/src/actions/HeroAction.lua +++ b/src/actions/HeroAction.lua @@ -404,4 +404,25 @@ function _M.changeSkinRpc(agent, data) return true end +function _M.createHeroRpc(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local heroType = msg.heroType + local unitData = csvdb["unitCsv"][heroType] + if not unitData then return end + local cost = globalCsv.unit_fragment_cost[unitData["rare"]] + if not cost then return end + if role:getItemCount(heroType) < cost then return end + + for _, hero in pairs(role.heros) do + if hero:getProperty("type") == heroType then return end + end + + role:costItems({[heroType] = cost}, {}) + role:award({[heroType + ItemStartId.Hero] = 1}, {}) + + SendPacket(actionCodes.Hero_createHeroRpc, "") + return true +end + return _M \ No newline at end of file diff --git a/src/actions/RoleAction.lua b/src/actions/RoleAction.lua index 4a1edd0..58d6360 100644 --- a/src/actions/RoleAction.lua +++ b/src/actions/RoleAction.lua @@ -246,4 +246,58 @@ function _M.syncTimeRpc(agent, data) return true end +function _M.saleItemRpc(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local itemId = msg.itemId + local count = msg.count + if math.illegalNum(count, 1, role:getItemCount(itemId)) then return end + local itemData = csvdb["itemCsv"][itemId] + if itemData.sell_effect == "" then return end + local sellEffect = itemData.sell_effect:toArray(true, "=") + + role:costItems({[itemId] = count}, {}) + local reward = role:award({[sellEffect[1]] = sellEffect[2] * count}, {}) + + SendPacket(actionCodes.Role_saleItemRpc, MsgPack.pack({reward = reward})) + return true +end + +function _M.openItemRpc(agent, data) + local role = agent.role + local msg = MsgPack.unpack(data) + local itemId = msg.itemId + local count = msg.count + if math.illegalNum(count, 1, role:getItemCount(itemId)) then return end + local itemData = csvdb["itemCsv"][itemId] + if itemData.use_type ~= 2 then return end + local randomData = csvdb["item_randomCsv"][tonumber(itemData.use_effect)] + if not randomData then return end + + local reward = randomData.gift:toNumMap() + for _id, _count in pairs(reward) do + reward[_id] = _count * count + end + if randomData.random_num > 0 and randomData.random_gift ~= "" then + for i = 1, count do + local pool = {} + for _, temp in ipairs(randomData.random_gift:toArray()) do + table.insert(pool, temp:toArray(true, "=")) + end + local needCount = math.min(#pool, randomData.random_num) + for j = 1, needCount do + local idx = math.randWeight(pool, 3) + reward[pool[idx][1]] = (reward[pool[idx][1]] or 0) + pool[idx][2] + table.remove(pool, idx) + end + end + end + role:costItems({[itemId] = count}, {}) + reward = role:award(reward, {}) + + SendPacket(actionCodes.Role_openItemRpc, MsgPack.pack({reward = reward})) + return true +end + + return _M \ No newline at end of file diff --git a/src/models/Role.lua b/src/models/Role.lua index 057a31c..18b2047 100644 --- a/src/models/Role.lua +++ b/src/models/Role.lua @@ -28,12 +28,15 @@ Role.schema = { ctime = {"number", skynet.timex()}, -- 创建时间 ignoreMt = {"number", 0}, -- 忽略维护拦截 sversion = {"number", globalCsv.StructVersion or 0}, -- 重整数据版本 - + diamond = {"number", 0}, + reDiamond = {"number", 0}, -- roleInfo level = {"number", 0}, items = {"string", ""}, loveStatus = {"string", ""}, --统计角色的最高 好感度等级 类型相关 -- type=loveL type=loveL + bagLimit = {"table", globalCsv.store_limit_max}, + --冒险相关 advPass = {"string", ""}, -- 通关记录 advItems = {"string", ""}, -- 冒险临时背包 @@ -147,6 +150,8 @@ function Role:data() level = self:getProperty("level"), items = self:getProperty("items"):toNumMap(), loveStatus = self:getProperty("loveStatus"):toNumMap(), + diamond = self:getAllDiamond(), + bagLimit = self:getProperty("bagLimit"), advPass = self:getProperty("advPass"), advInfo = self:getProperty("advInfo"), diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index 7be86d4..18391a3 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -35,6 +35,29 @@ function RolePlugin.bind(Role) end + local function checkItemCount(self, itemId, count) + local itemData = csvdb["itemCsv"][itemId] + -- 种类 类型数量限制 + local page = globalCsv.store_type[itemData.type] + local limit = self:getProperty("bagLimit")[page] + 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 + curCount = curCount + 1 + if curCount >= limit then + count = 0 + break + end + end + end + end + -- 其他 + + return count + end local function _award(self, itemId, count, params) local itemData = csvdb["itemCsv"][itemId] @@ -49,13 +72,16 @@ function RolePlugin.bind(Role) end end, } - - if itemTypeAward[curType] then - itemTypeAward[curType]() - else - params.itemId = itemId - params.count = count - self:addItem(params) + -- 对数量筛查 + local count = checkItemCount(self, itemId, count) + if count ~= 0 then + if itemTypeAward[curType] then + itemTypeAward[curType]() + else + params.itemId = itemId + params.count = count + self:addItem(params) + end end return count, change -- count 刷新实际发放的奖励个数 change 物品实际奖励与当前id 不符 就发生转换 而不实际发奖 @@ -83,7 +109,9 @@ function RolePlugin.bind(Role) end table.insert(allChange, {form = {[itemId] = count}, to = cr}) else - reward[itemId] = (reward[itemId] or 0) + count + if count > 0 then + reward[itemId] = (reward[itemId] or 0) + count + end end end @@ -92,6 +120,10 @@ function RolePlugin.bind(Role) function Role:addItem(params) params = params or {} + if params.itemId == ItemId.Diamond then + self:gainDiamond(params) + return + end local items = self:getProperty("items") local origin = items:getv(params.itemId, 0) local nums = origin+params.count @@ -119,18 +151,88 @@ function RolePlugin.bind(Role) end function Role:costItems(itemCountT, params) + if itemCountT[ItemId.Diamond] then --优先扣除钻石 + local pms = clone(params or {}) + pms.count = itemCountT[ItemId.Diamond] + if not self:costDiamond(pms) then + return + end + itemCountT[ItemId.Diamond] = nil + end for itemId, count in pairs(itemCountT) do local pms = clone(params or {}) pms.itemId = itemId pms.count = - count self:addItem(pms) end + return true end function Role:getItemCount(itemId) + if itemId == ItemId.Diamond then + return self:getAllDiamond() + end return self:getProperty("items"):getv(itemId, 0) end + function Role:getAllDiamond() + return self:getProperty("diamond") + self:getProperty("reDiamond") + end + + function Role:gainDiamond(params) + if not params or type(params) ~= "table" then return false end + local count = tonum(params.count) + if isnan(count) then + return false + end + local origind = self:getProperty("diamond") + local originr = self:getProperty("reDiamond") + local origin = origind + originr + if params.isRecharge then + self:incrProperty("reDiamond", count) + else + self:incrProperty("diamond", count) + end + self:notifyUpdateProperty("diamond", self:getAllDiamond()) + return true + end + + function Role:costDiamond(params) + if not params or type(params) ~= "table" then return false end + local count = tonum(params.count) + if isnan(count) then + return false + end + if count <= 0 then + return false + end + local origind = self:getProperty("diamond") + local originr = self:getProperty("reDiamond") + local origin = origind + originr + + if origin < 0 then + return false + end + if origin < count then + return false + end + local last = count + local costFirst = {"diamond", "reDiamond"} + if params.isRecharge then + costFirst = {"reDiamond", "diamond"} + end + last = math.max(last - self:getProperty(costFirst[1]), 0) + if last < count then + self:incrProperty(costFirst[1], last - count) + end + if last > 0 then + self:incrProperty(costFirst[2], -last) + end + + self:notifyUpdateProperty("diamond", self:getAllDiamond()) + return true + end + function Role:addHero(params) local roleId = self:getProperty("id") local heroId = tonum(redisproxy:hincrby(string.format(R_INCR, roleId), "hero", 1)) diff --git a/src/utils/MathUtil.lua b/src/utils/MathUtil.lua index 8a8b3bd..9815a41 100644 --- a/src/utils/MathUtil.lua +++ b/src/utils/MathUtil.lua @@ -39,4 +39,15 @@ function math.randWeight(dataset, field) end return nil +end + +function math.illegalNum(num, min, max) + local illegal = true + for i = min, max do + if num == i then + illegal = false + break + end + end + return illegal end \ No newline at end of file -- libgit2 0.21.2