diff --git a/src/ProtocolCode.lua b/src/ProtocolCode.lua index 59683ef..2c22204 100644 --- a/src/ProtocolCode.lua +++ b/src/ProtocolCode.lua @@ -279,10 +279,11 @@ actionCodes = { Capsule_joinRpc = 851, --扭蛋机详情 Capsule_registerRpc = 853, --报名扭蛋机 "报名"后,抽取按钮才会开放,未报名的玩家分在围观玩家中 Capsule_drawRpc = 854, --抽扭蛋机 - Capsule_switchRoomRpc = 855, --切换扭蛋机房间 + --Capsule_switchRoomRpc = 855, --切换扭蛋机房间 Capsule_notifyChange = 856, -- 通知信息变动 Capsule_payReward = 857, -- 特殊赏 奖励通知 Capsule_exitRpc = 858, -- 退出 + Capsule_getDataRpc = 859, --获取扭蛋机信息 } rpcResponseBegin = 10000 diff --git a/src/actions/CapsuleAction.lua b/src/actions/CapsuleAction.lua index 6620a97..f95b5ae 100644 --- a/src/actions/CapsuleAction.lua +++ b/src/actions/CapsuleAction.lua @@ -93,40 +93,83 @@ function _M.drawRpc(agent, data) local roleId = role:getProperty("id") local capsuleId = msg.capsule_id local typ = msg.typ --0=独享,1= 公开 - local full = msg.full -- 0=单次,1=十连抽 2=全收 + local full = msg.full or 0-- 0=单次,1=十连抽 2=全收 local cares = msg.cares - local ret, reward, change, rewardByGoods, capsule + local ret, token, reward, change, rewardByGoods, capsule + + --检查库存 + if typ == 1 then + ret, token = skynet.call(agent.capsule_serv, "lua", "goods_stock", capsuleId) + else + ret, token = role:goodStock(capsuleId) + end + if ret == 0 then skynet.error("零库存 " .. capsuleId) return 1 end + + --检查余额 + if not next(token) then skynet.error("代币未配置 " .. capsuleId) return 1 end + local cost, drawsNum = {}, 0 + if full == 0 then + drawsNum = 1 + elseif full == 1 then + if ret < 10 then + drawsNum = ret + else + drawsNum = 10 + end + elseif full == 2 then + drawsNum = ret + end + cost[token[1]] = drawsNum * token[2] + + --if not role:checkItemEnough(cost) then return 2 end + --role:costItems(cost, {log = {desc = "CapsuleCoinCost", int1 = token[1], int2 = cost[token[1]]}}) + + --开始抽奖 if typ == 1 then ret, reward, rewardByGoods, capsule = skynet.call(agent.capsule_serv, "lua", "draw_capsule", roleId, capsuleId, full, cares) else ret, reward, rewardByGoods, capsule= role:drawCapsule(capsuleId, full, cares) end - if ret < 4 then + if ret < 5 then return ret end - if ret == 4 then - SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack(reward)) + dump(reward) + dump(rewardByGoods) + + if ret == 5 then + SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack({change = reward, capsule = capsule})) + return true end + -- rewardByGoods是抽到的扭蛋信息,reward是抽扭蛋后获得的所有奖励信息。 if rewardByGoods and next(rewardByGoods) then reward, change = role:award(reward, {log = {desc = "CapsuleReward", int1 = tonumber(capsuleId), int2 = roleId}}) SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack({reward = rewardByGoods, capsule = capsule})) else - return 5 + return ret end return true end -function _M.switchRoomRpc(agent, data) +function _M.getData(agent, data) local role = agent.role local msg = MsgPack.unpack(data) - local roleId = role:getProperty("id") + local capsuleId = msg.capsule_id + local typ = msg.typ --0=独享,1= 公开 - local ret = skynet.call(agent.capsule_serv, "lua", "switch_room", roleId) - SendPacket(actionCodes.Capsule_switchRoomRpc, MsgPack.pack(ret)) + local capsule + if typ == 1 then + capsule = skynet.call(agent.capsule_serv, "lua", "capsule_data", capsuleId) + else + capsule = role:getCapsuleData(capsuleId) + end + if not capsule then return 1 end + + SendPacket(actionCodes.Capsule_getDataRpc, MsgPack.pack({capsule = capsule})) return true + end return _M \ No newline at end of file diff --git a/src/models/Capsule.lua b/src/models/Capsule.lua index fab0a3e..29e8433 100644 --- a/src/models/Capsule.lua +++ b/src/models/Capsule.lua @@ -36,6 +36,7 @@ Capsule.schema = { name = {"string"}, typ = {"number", 1}, -- 1=共享,2=独享 coin = {"number", 0}, --货币代号 + token = {"table", {}}, --抽一次,货币=消耗 register = {"table", {}}, --人数 {["id"]=0}, 0 围观, 1 已报名 record = {"table", {}}, --抽取记录 列表 recordByRole = {"table", {}}, -- 抽取记录,hash, {roleid=record} @@ -56,6 +57,7 @@ function Capsule:getResetFields() room = self:getProperty("room"), typ = self:getProperty("typ"), coin = 0, + token = {}, register = {}, record = {}, recordByRole = {}, @@ -132,7 +134,7 @@ function Capsule:init() end --货币类型 local coin = ichibankuji["token"]:toArray(true, "=") - self:setProperties({coin = coin[1] or 0, hideTime = ichibankuji.hide_time, goods = goods, specials = specials, incentive = incentive}) + self:setProperties({coin = coin[1] or 0, token = coin, hideTime = ichibankuji.hide_time, goods = goods, specials = specials, incentive = incentive}) end function Capsule:isShow() @@ -233,11 +235,11 @@ function Capsule:confirmed(cares) for k, v in pairs(cares) do if v.typ == 1 then if goods[k] and goods[k].amount ~= v.count then - change[k] = goods[k].amount + change[k] = {typ=1, count = goods[k].amount} end else if specials[k] and specials[k].amount ~= v.count then - change[k] = specials[k].amount + change[k] = {typ=1, count = specials[k].amount} end end end @@ -282,8 +284,8 @@ local function getSpecialRoleNotify(rewardRecord, count, award, spKey, typ, now) if tmp["amount"] <= 0 then rewardRecord[roleId] = nil end - tmp = rewardByRole[roleId] - if not tmp then + tmp = rewardByRole[roleId] or {} + if not next(tmp) then local name = getNameByRoleId(roleId) tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now} else @@ -372,7 +374,7 @@ function Capsule:getCore(record) if not roleRecord[v.roleId]then roleRecord[v.roleId] = {amount = v.amount } else - roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount} + roleRecord[v.roleId] = {amount = (roleRecord[v.roleId]["amount"] or 0) + tmpCount} end end @@ -416,7 +418,7 @@ function Capsule:getLast(record) if not roleRecord[v.roleId]then roleRecord[v.roleId] = {amount = v.amount } else - roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount} + roleRecord[v.roleId] = {amount = (roleRecord[v.roleId]["amount"] or 0) + tmpCount} end end @@ -443,7 +445,7 @@ function Capsule:getJoker(record) if not roleRecord[v.roleId]then roleRecord[v.roleId] = {amount = v.amount } else - roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount} + roleRecord[v.roleId] = {amount = (roleRecord[v.roleId]["amount"] or 0) + v.amount} end end @@ -470,7 +472,7 @@ function Capsule:getKing(record) if not roleRecord[v.roleId]then roleRecord[v.roleId] = {amount = v.amount } else - roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount} + roleRecord[v.roleId] = {amount = (roleRecord[v.roleId]["amount"] or 0) + v.amount} end end @@ -522,8 +524,7 @@ function Capsule:checkSpecialReward( now) local kingReward = self:getKing(record) rewardToNtyFunc(notify, kingReward) - --广播出去TODO - --rpcRole(roleId, "paySpecialReward", RewardTYpe.JOKER, jokerReward.award) + return notify end @@ -562,7 +563,7 @@ function Capsule:checkIncentive(roleId, name, now) local count = math.floor(amount / incentive["amount"]["np"]) local tmpCount = count * incentive["amount"]["np"] - notify["amount"] = {name = name, good_id = "amount", typ = RewardType.INCENTIVE, award = incentive["amount"]["award"], amount = count, quality = 2, create_time= now} + notify["amount"] = {name = name, roleId= roleId, good_id = "amount", typ = RewardType.INCENTIVE, award = incentive["amount"]["award"], amount = count, quality = 2, create_time= now} --填充v.calculated 字段,标识已经用于每x抽的计算中。 for _, v in pairs(roleRecord) do @@ -594,7 +595,8 @@ function Capsule:checkIncentive(roleId, name, now) incentiveRecord[roleId] = incentiveByRole self:setProperty("incentiveRecord", incentiveRecord) - return notify + --TODO 先屏蔽 + return {} end function Capsule:drawByCount(roleId, count) @@ -618,32 +620,32 @@ function Capsule:drawByCount(roleId, count) local name = getNameByRoleId(roleId) while (goods and next(goods) and count > 0) do local good_id = math.randWeight(goods, "weight") - if good_id then - local good = goods[good_id] or {} - if good and good.amount > 0 then - good.amount = good.amount - 1 - - --插入记录 - local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = 1, quality = good.quality, create_time= now} - table.insert(record, tmpNotify) - - --作为奖励记录+通知 - if not notify[roleId][good_id] then - notify[roleId][good_id] = tmpNotify - else - notify[roleId][good_id].amount = notify[roleId][good_id].amount + 1 - end + if not good_id then break end - --记录角色的抽奖记录 计算激励奖需要用到 - if not roleRecord[good_id] then - roleRecord[good_id] = tmpNotify - else - roleRecord[good_id].amount = roleRecord[good_id].amount + 1 - end + local good = goods[good_id] or {} + if good and good.amount > 0 then + good.amount = good.amount - 1 + + --插入记录 + local tmpNotify = {roleId = roleId, name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = 1, quality = good.quality, create_time= now} + table.insert(record, tmpNotify) + + --作为奖励记录+通知 + if not notify[roleId][good_id] then + notify[roleId][good_id] = tmpNotify + else + notify[roleId][good_id].amount = notify[roleId][good_id].amount + 1 + end - good.weight = good.weight - csvdb["ichibankuji_goodsCsv"][goods_id][good.id].weight - count = count - 1 + --记录角色的抽奖记录 计算激励奖需要用到 + if not roleRecord[good_id] then + roleRecord[good_id] = tmpNotify + else + roleRecord[good_id].amount = roleRecord[good_id].amount + 1 end + + good.weight = good.weight - csvdb["ichibankuji_goodsCsv"][goods_id][good.id].weight + count = count - 1 end end @@ -690,8 +692,8 @@ function Capsule:drawAll(roleId) for good_id, good in pairs(goods) do if good.amount > 0 then --插入记录 - local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = good.amount, quality = good.quality, create_time = now} - table.insert(record, notify) + local tmpNotify = {roleId = roleId, name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = good.amount, quality = good.quality, create_time = now} + table.insert(record, tmpNotify) --作为奖励记录+通知 if not notify[roleId][good_id] then @@ -702,7 +704,7 @@ function Capsule:drawAll(roleId) --记录角色的抽奖记录 if not roleRecord[good_id] then - roleRecord[good_id] = notify + roleRecord[good_id] = tmpNotify else roleRecord[good_id].amount = roleRecord[good_id].amount + good.amount end @@ -749,24 +751,23 @@ end ]]-- function Capsule:draw(roleId, full, cares) - if self:getGoodsAmount() == 0 then return 2 end if self:getProperty("typ") == 1 then --是否报名 - if self:isRegister(roleId) == false then return 3 end + if self:isRegister(roleId) == false then return 4 end --关注的奖品的数量发生了变化 if cares then local change = self:confirmed(cares) - if next(change) then return 4, change end + if next(change) then return 5, change end end end if full == 0 then - return 5, self:drawByCount(roleId, 1) + return 6, self:drawByCount(roleId, 1) elseif full == 1 then - return 5, self:drawByCount(roleId, 10) + return 6, self:drawByCount(roleId, 10) elseif full == 2 then - return 5, self:drawAll(roleId) + return 6, self:drawAll(roleId) end end diff --git a/src/models/RoleCross.lua b/src/models/RoleCross.lua index 5639619..a3d14be 100644 --- a/src/models/RoleCross.lua +++ b/src/models/RoleCross.lua @@ -210,10 +210,22 @@ RoleCross.bind = function (Role) return "成功" end - function Role:paySpecialReward(typ, reward) - local tmpReward, _= self:award(reward, {log = {desc = "specialsReward", int1=self:getKey(), int2 = typ}}) - SendPacket(actionCodes.Capsule_payReward, MsgPack.pack({reward=tmpReward})) - return "reward" + function Role:paySpecialReward(roleId, notify) + if notify and next(notify) then + local reward = {} + for key, val in pairs(notify) do + if key == roleId then + for k, v in pairs(val) do + for id, count in pairs(v.award:toNumMap()) do + reward[id] = (reward[id] or 0) + count + end + end + end + + end + local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) + role:award(reward, {log = {desc = "CapsuleReward", int1=roleId}}) + end end end @@ -376,9 +388,22 @@ function CMD.redPTag(roleId, tag, pms) end end -function CMD.paySpecialReward(roleId, typ, reward) - local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) - role:award(reward, {log = {desc = "specialsReward", int1=self:getKey(), int2 = typ}}) +function CMD.paySpecialReward(roleId, notify) + if notify and next(notify) then + local reward = {} + for key, val in pairs(notify) do + if key == roleId then + for k, v in pairs(val) do + for id, count in pairs(v.award:toNumMap()) do + reward[id] = (reward[id] or 0) + count + end + end + end + end + local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) + role:award(reward, {log = {desc = "CapsuleReward", int1=roleId}}) + end + end RoleCross.handle = function(cmd, roleId, ...) diff --git a/src/models/RoleLog.lua b/src/models/RoleLog.lua index ccea51b..72f6c76 100644 --- a/src/models/RoleLog.lua +++ b/src/models/RoleLog.lua @@ -161,7 +161,7 @@ local ItemReason = { returner = 1410, -- 回归者奖励 CapsuleReward = 1411, --扭蛋机奖励 - specialsReward = 1412, --特殊赏 + CapsuleCoinCost = 1413, --抽扭蛋机消耗 } diff --git a/src/models/RolePlugin.lua b/src/models/RolePlugin.lua index 30a0c43..095092c 100644 --- a/src/models/RolePlugin.lua +++ b/src/models/RolePlugin.lua @@ -3170,7 +3170,7 @@ function RolePlugin.bind(Role) local roleId = self:getProperty("id") return capsule:draw(roleId, full, cares) end - return 1 + return 3 end function Role:joinCapsule(capsuleId) @@ -3186,6 +3186,16 @@ function RolePlugin.bind(Role) end end end + + function Role:goodStock(capsuleId) + local capsule = self.capsules[capsuleId] or {} + return capsule:getGoodsAmount(), capsule:getProperty("token") + end + + function Role:getCapsuleData(capsuleId) + local capsule = self.capsules[capsuleId] or {} + return capsule:data() + end end return RolePlugin \ No newline at end of file diff --git a/src/services/capsuled.lua b/src/services/capsuled.lua index 31bb2ad..f50ec67 100644 --- a/src/services/capsuled.lua +++ b/src/services/capsuled.lua @@ -16,22 +16,37 @@ NotifyChangeType = { EXIT = 2, DRAW = 3, SPECIAL = 4, - INCENTIVE = 5, +} + +skynet.register_protocol { + name = "role", + id = 101, + pack = skynet.pack, + unpack = skynet.unpack, } local function rpcRole(roleId, funcName, ...) local fields = ... local agent = datacenter.get("agent", roleId) - local roleCross = require("models.RoleCross") - if funcName == "getProperties" then - return roleCross.handle(funcName, roleId, fields) + if agent and agent.serv then + if funcName == "getProperties" then + return true, skynet.call(agent.serv, "role", funcName, fields) + else + return true, skynet.call(agent.serv, "role", funcName, ...) + end else - return roleCross.handle(funcName, roleId, ...) + local roleCross = require("models.RoleCross") + if funcName == "getProperties" then + return false, roleCross.handle(funcName, roleId, fields) + else + return false, roleCross.handle(funcName, roleId, ...) + end end end function getNameByRoleId(roleId) - return rpcRole(roleId, "getProperty", "name") + local status, name = rpcRole(roleId, "getProperty", "name") + return name end @@ -47,16 +62,23 @@ function broadCastCapsule(roleId, capsuleId, broadInfo) end end -local function getCapsuleId(id, room) - return string.format("%d%d", id, room) +function broadCastSpecial(roleId, capsuleId, broadInfo) + local capsule = capsules[capsuleId] or {} + local register = capsule:getProperty("register") or {} + if next(capsule) then + for id, _ in pairs(register) do + if id ~= roleId then + rpcRole(id, "paySpecialReward", id, broadInfo) + end + end + end end - local function add(roleId, capsuleId) local capsule = capsules[capsuleId] or {} if next(capsule) then capsule:join(roleId) - broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.JOIN, roleId = roleId}) + broadCastCapsule(roleId, capsuleId, {notifyType= NotifyChangeType.JOIN, roleId = roleId}) return capsule:data(roleId) end print("id 不存在: ".. capsuleId) @@ -138,36 +160,44 @@ function CMD.exit(roleId, capsuleId) local capsule = capsules[capsuleId] or {} if next(capsule) then capsule:exit(roleId) - broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.EXIT, roleId = roleId}) + broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId}) end else for _, capsule in pairs(capsules) do if next(capsule) then capsule:exit(roleId) - broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.EXIT, roleId = roleId}) + broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId}) end end end end -function CMD.draw_capsule(roleId, capsuleId, full, cares) +function CMD.draw_capsule(roleId, capsuleId, full, drawsNum, cares) local capsule = capsules[capsuleId] or {} if next(capsule) then - local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, cares) - --if ret > 4 then - -- broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.DRAW, roleId = roleId, notify = notify}) - --end + local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, drawsNum, cares) + if ret > 5 then + --broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.DRAW, notify = notify}) + broadCastSpecial(roleId, capsuleId, notify) + end return ret, reward, rewardByGoods, capsule:data(roleId) end - return 1 + return 2 end function CMD.register(roleId, capsuleId) local capsule = capsules[capsuleId] or {} - if next(capsule) then - return capsule:register(roleId) - end - return nil + return capsule:register(roleId) +end + +function CMD.goods_stock(capsuleId) + local capsule = capsules[capsuleId] or {} + return capsule:getGoodsAmount(), capsule:getProperty("token") +end + +function CMD.capsule_data(capsuleId) + local capsule = capsules[capsuleId] or {} + return capsule:data() end skynet.start(function() -- libgit2 0.21.2