Commit 7800f87953ed01455f1ae17004590c1da119e7c3
Merge branch 'cn/develop' into cn/publish/ts
Showing
12 changed files
with
1216 additions
and
3 deletions
Show diff stats
src/ProtocolCode.lua
| ... | ... | @@ -274,6 +274,15 @@ actionCodes = { |
| 274 | 274 | Seaport_taskRpc = 753, |
| 275 | 275 | Seaport_shopRpc = 754, |
| 276 | 276 | Seaport_resetRpc = 755, |
| 277 | + | |
| 278 | + Capsule_listRpc = 850, --扭蛋机列表 | |
| 279 | + Capsule_joinRpc = 851, --扭蛋机详情 | |
| 280 | + Capsule_registerRpc = 853, --报名扭蛋机 "报名"后,抽取按钮才会开放,未报名的玩家分在围观玩家中 | |
| 281 | + Capsule_drawRpc = 854, --抽扭蛋机 | |
| 282 | + Capsule_switchRoomRpc = 855, --切换扭蛋机房间 | |
| 283 | + Capsule_notifyChange = 856, -- 通知信息变动 | |
| 284 | + Capsule_payReward = 857, -- 特殊赏 奖励通知 | |
| 285 | + Capsule_exitRpc = 858, -- 退出 | |
| 277 | 286 | } |
| 278 | 287 | |
| 279 | 288 | rpcResponseBegin = 10000 | ... | ... |
src/RedisKeys.lua
| ... | ... | @@ -63,6 +63,15 @@ FRIEND_RECOMMEND = "friend:recommend" -- sort set ç™»å½•æŽ’åº èŽ·å–æŽ¨è好å |
| 63 | 63 | CHAT_OFFLINE = "chat:offline:%d" --消æ¯ç¦»çº¿ç¼“å˜ |
| 64 | 64 | |
| 65 | 65 | WORK_BATTLE_COUNT = "global:workbattle" -- 世界次数统计 |
| 66 | + | |
| 67 | +--Capsule æ‰è›‹æœº | |
| 68 | +CAPSULE_INFO= "capsule:info" --set | |
| 69 | +CAPSULE_PUBLIC = "capsule:%d:info" --hash | |
| 70 | + | |
| 71 | +CAPSULE_ID_ROLE = "role:%s:capsule:id" -- set | |
| 72 | +CAPSULE_ROLE = "role:%s:capsule:%d" --hash 独享 | |
| 73 | + | |
| 74 | + | |
| 66 | 75 | -- FRIEND_DINER_LIKE_KEY = "role:%d:diner:like" -- list |
| 67 | 76 | |
| 68 | 77 | -- UNION_SET = "global:union" | ... | ... |
| ... | ... | @@ -0,0 +1,132 @@ |
| 1 | +local ipairs = ipairs | |
| 2 | +local table = table | |
| 3 | +local math = math | |
| 4 | +local next = next | |
| 5 | +local string = string | |
| 6 | +local redisproxy = redisproxy | |
| 7 | +local MsgPack = MsgPack | |
| 8 | +local getRandomName = getRandomName | |
| 9 | +local mcast_util = mcast_util | |
| 10 | +local string_format = string.format | |
| 11 | +local tonumber = tonumber | |
| 12 | +local require = require | |
| 13 | + | |
| 14 | +local _M = {} | |
| 15 | + | |
| 16 | +function _M.listRpc(agent, data) | |
| 17 | + local role = agent.role | |
| 18 | + local msg = MsgPack.unpack(data) | |
| 19 | + local typ = msg.typ | |
| 20 | + local coin = msg.coin | |
| 21 | + | |
| 22 | + local capsules = {} | |
| 23 | + if typ == 1 then | |
| 24 | + local ret = skynet.call(agent.capsule_serv, "lua", "list", coin) | |
| 25 | + if next(ret) then | |
| 26 | + for k, v in pairs(ret) do | |
| 27 | + capsules[k] = v | |
| 28 | + end | |
| 29 | + end | |
| 30 | + elseif typ == 0 then | |
| 31 | + local ret = role:getCapsuleList(coin) | |
| 32 | + if next(ret) then | |
| 33 | + for k, v in pairs(ret) do | |
| 34 | + capsules[k] = v | |
| 35 | + end | |
| 36 | + end | |
| 37 | + end | |
| 38 | + | |
| 39 | + SendPacket(actionCodes.Capsule_listRpc, MsgPack.pack(capsules)) | |
| 40 | + return true | |
| 41 | +end | |
| 42 | + | |
| 43 | +function _M.joinRpc(agent, data) | |
| 44 | + local role = agent.role | |
| 45 | + local msg = MsgPack.unpack(data) | |
| 46 | + local roleId = role:getProperty("id") | |
| 47 | + local capsuleId = msg.capsule_id --如果刷新则需要传递当前扭蛋机id | |
| 48 | + local typ = msg.typ or 1 | |
| 49 | + | |
| 50 | + local ret = {} | |
| 51 | + if typ == 1 then | |
| 52 | + ret = skynet.call(agent.capsule_serv, "lua", "join", roleId, capsuleId) | |
| 53 | + elseif typ == 0 then | |
| 54 | + ret = role:joinCapsule() | |
| 55 | + end | |
| 56 | + SendPacket(actionCodes.Capsule_joinRpc, MsgPack.pack(ret)) | |
| 57 | + return true | |
| 58 | +end | |
| 59 | + | |
| 60 | +function _M.exitRpc(agent, data) | |
| 61 | + local role = agent.role | |
| 62 | + local msg = MsgPack.unpack(data) | |
| 63 | + local roleId = role:getProperty("id") | |
| 64 | + local capsuleId = msg.capsule_id --如果刷新则需要传递当前扭蛋机id | |
| 65 | + local typ = msg.typ or 1 | |
| 66 | + | |
| 67 | + local ret = {} | |
| 68 | + if typ == 1 then | |
| 69 | + ret = skynet.call(agent.capsule_serv, "lua", "exit", roleId, capsuleId) | |
| 70 | + elseif typ == 0 then | |
| 71 | + ret = role:exitCapsule() | |
| 72 | + end | |
| 73 | + | |
| 74 | + SendPacket(actionCodes.Capsule_exitRpc, MsgPack.pack(ret)) | |
| 75 | + return true | |
| 76 | +end | |
| 77 | + | |
| 78 | +function _M.registerRpc(agent, data) | |
| 79 | + local role = agent.role | |
| 80 | + local msg = MsgPack.unpack(data) | |
| 81 | + local roleId = role:getProperty("id") | |
| 82 | + local capsuleId = msg.capsule_id | |
| 83 | + | |
| 84 | + local ret = skynet.call(agent.capsule_serv, "lua", "register", roleId, capsuleId) | |
| 85 | + if not ret then return 1 end | |
| 86 | + SendPacket(actionCodes.Capsule_registerRpc, MsgPack.pack(ret)) | |
| 87 | + return true | |
| 88 | +end | |
| 89 | + | |
| 90 | +function _M.drawRpc(agent, data) | |
| 91 | + local role = agent.role | |
| 92 | + local msg = MsgPack.unpack(data) | |
| 93 | + local roleId = role:getProperty("id") | |
| 94 | + local capsuleId = msg.capsule_id | |
| 95 | + local typ = msg.typ --0=独享,1= 公开 | |
| 96 | + local full = msg.full -- 0=单次,1=十连抽 2=全收 | |
| 97 | + local cares = msg.cares | |
| 98 | + | |
| 99 | + local ret, reward, change, rewardByGoods, capsule | |
| 100 | + if typ == 1 then | |
| 101 | + ret, reward, rewardByGoods, capsule = skynet.call(agent.capsule_serv, "lua", "draw_capsule", roleId, capsuleId, full, cares) | |
| 102 | + else | |
| 103 | + ret, reward, rewardByGoods, capsule= role:drawCapsule(capsuleId, full, cares) | |
| 104 | + end | |
| 105 | + if ret < 4 then | |
| 106 | + return ret | |
| 107 | + end | |
| 108 | + | |
| 109 | + if ret == 4 then | |
| 110 | + SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack(reward)) | |
| 111 | + end | |
| 112 | + | |
| 113 | + if rewardByGoods and next(rewardByGoods) then | |
| 114 | + reward, change = role:award(reward, {log = {desc = "CapsuleReward", int1 = tonumber(capsuleId), int2 = roleId}}) | |
| 115 | + SendPacket(actionCodes.Capsule_drawRpc, MsgPack.pack({reward = rewardByGoods, capsule = capsule})) | |
| 116 | + else | |
| 117 | + return 5 | |
| 118 | + end | |
| 119 | + return true | |
| 120 | +end | |
| 121 | + | |
| 122 | +function _M.switchRoomRpc(agent, data) | |
| 123 | + local role = agent.role | |
| 124 | + local msg = MsgPack.unpack(data) | |
| 125 | + local roleId = role:getProperty("id") | |
| 126 | + | |
| 127 | + local ret = skynet.call(agent.capsule_serv, "lua", "switch_room", roleId) | |
| 128 | + SendPacket(actionCodes.Capsule_switchRoomRpc, MsgPack.pack(ret)) | |
| 129 | + return true | |
| 130 | +end | |
| 131 | + | |
| 132 | +return _M | |
| 0 | 133 | \ No newline at end of file | ... | ... |
src/agent.lua
| ... | ... | @@ -254,11 +254,12 @@ skynet.register_protocol { |
| 254 | 254 | } |
| 255 | 255 | |
| 256 | 256 | -- function CMD.start(gate, fd, ip) |
| 257 | -function CMD.start(session, source, gate, fd, ip, hotfixs) | |
| 257 | +function CMD.start(session, source, gate, capsuled, fd, ip, hotfixs) | |
| 258 | 258 | ignoreHeartbeat = false |
| 259 | 259 | |
| 260 | 260 | agentInfo.client_fd = fd |
| 261 | 261 | agentInfo.gate_serv = gate |
| 262 | + agentInfo.capsule_serv = capsuled | |
| 262 | 263 | agentInfo.ip = ip |
| 263 | 264 | |
| 264 | 265 | agent_util:reset() |
| ... | ... | @@ -282,6 +283,8 @@ function CMD.close() |
| 282 | 283 | mcast_util.usub_union() |
| 283 | 284 | local role = agentInfo.role |
| 284 | 285 | if not role then return end |
| 286 | + | |
| 287 | + skynet.call(agent.capsule_serv, "lua", "exit", role:getProperty("id")) | |
| 285 | 288 | role:log("onLogout", {logtime = skynet.timex()-role:getProperty("ltime")}) |
| 286 | 289 | role:mylog("logout", {int1 = skynet.timex()-role:getProperty("ltime")}) |
| 287 | 290 | role:onOfflineEvent() | ... | ... |
| ... | ... | @@ -0,0 +1,792 @@ |
| 1 | +--扭蛋机 | |
| 2 | +local MsgPack = MsgPack | |
| 3 | +local Capsule = class("Capsule", require("shared.ModelBase")) | |
| 4 | + | |
| 5 | +function Capsule:ctor(properties) | |
| 6 | + Capsule.super.ctor(self, properties) | |
| 7 | +end | |
| 8 | + | |
| 9 | +RewardType = { | |
| 10 | + GOODS = 1, | |
| 11 | + SPECIAL = 2, | |
| 12 | + INCENTIVE = 3, | |
| 13 | +} | |
| 14 | + | |
| 15 | +SpecialType = { | |
| 16 | + TOP = 1, | |
| 17 | + CORE = 2, | |
| 18 | + LAST = 3, | |
| 19 | + JOKER = 4, | |
| 20 | + KING = 5, | |
| 21 | +} | |
| 22 | + | |
| 23 | +CapsuleType = { | |
| 24 | + PRIVATE = 0, | |
| 25 | + PUBLIC = 1, | |
| 26 | +} | |
| 27 | + | |
| 28 | +--[[ | |
| 29 | +--通知数据结构 | |
| 30 | +{ [roleId] = { [good_id1] = { }, [good_id2] = { }, } } | |
| 31 | +]]-- | |
| 32 | + | |
| 33 | +Capsule.schema = { | |
| 34 | + id = {"number", 0}, --扭蛋机key,配置读取 | |
| 35 | + room = {"number", 0}, --房间号, 配置读取 | |
| 36 | + name = {"string"}, | |
| 37 | + typ = {"number", 1}, -- 1=共享,2=独享 | |
| 38 | + coin = {"number", 0}, --货币代号 | |
| 39 | + register = {"table", {}}, --人数 {["id"]=0}, 0 围观, 1 已报名 | |
| 40 | + record = {"table", {}}, --抽取记录 列表 | |
| 41 | + recordByRole = {"table", {}}, -- 抽取记录,hash, {roleid=record} | |
| 42 | + rank = {"table", {}}, --排行 | |
| 43 | + goods = {"table", {}}, --奖励池 | |
| 44 | + specials = {"table", {}}, --特殊赏 | |
| 45 | + incentive = {"table", {}}, --激励奖 | |
| 46 | + incentiveRecord = {"table", {}}, --激励奖记录 | |
| 47 | + specialsRecord= {"table", {}}, --特殊赏领取记录 | |
| 48 | + resetTimes = {"number", 0}, --每日一次手动重置的机会 | |
| 49 | + hideTime = {"number", 0} , --隐藏时间 | |
| 50 | + drawEndTime = {"number", 0}, --抽完时间 | |
| 51 | +} | |
| 52 | + | |
| 53 | +function Capsule:getResetFields() | |
| 54 | + return { | |
| 55 | + id = self:getProperty("id"), | |
| 56 | + room = self:getProperty("room"), | |
| 57 | + typ = self:getProperty("typ"), | |
| 58 | + coin = 0, | |
| 59 | + register = {}, | |
| 60 | + record = {}, | |
| 61 | + recordByRole = {}, | |
| 62 | + rank = {}, | |
| 63 | + goods = {}, | |
| 64 | + specials = {}, | |
| 65 | + incentive = {}, | |
| 66 | + incentiveRecord = {}, | |
| 67 | + specialsRecord= {}, | |
| 68 | + resetTimes = 0, | |
| 69 | + hideTime = 0, | |
| 70 | + drawEndTime = 0, | |
| 71 | + } | |
| 72 | +end | |
| 73 | + | |
| 74 | +function Capsule:init() | |
| 75 | + local id = self:getProperty("id") | |
| 76 | + local room = self:getProperty("room") | |
| 77 | + self:setProperties(self:getResetFields()) | |
| 78 | + | |
| 79 | + local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room] | |
| 80 | + | |
| 81 | + --奖励池 | |
| 82 | + local goods_id = ichibankuji["goods_id"] | |
| 83 | + local goods, specials, incentive = {}, {}, {} | |
| 84 | + for _, data in pairs(csvdb["ichibankuji_goodsCsv"]) do | |
| 85 | + for _, val in pairs(data) do | |
| 86 | + if val.key == goods_id then | |
| 87 | + goods[goods_id..val.id] = clone(val) | |
| 88 | + end | |
| 89 | + end | |
| 90 | + end | |
| 91 | + for _, v in pairs(goods) do | |
| 92 | + v.weight = (v.weight or 0) * v.amount | |
| 93 | + end | |
| 94 | + | |
| 95 | + --特殊赏 | |
| 96 | + local special_ids = ichibankuji["special_id"] | |
| 97 | + if special_ids ~= "" then | |
| 98 | + for _, special_id in ipairs(special_ids:toArray(true, "=")) do | |
| 99 | + local val = csvdb["ichibankuji_specialCsv"][special_id] | |
| 100 | + if type(val.type) == "number" then | |
| 101 | + specials[special_id] = {np= 1, amount = val.amount, award = val.award, quality = tonumber(val.type), showIndex = val.showIndex} | |
| 102 | + elseif type(val.type) == "string" then | |
| 103 | + local pos = val.type:find("=") | |
| 104 | + if pos then | |
| 105 | + for k, v in pairs(val.type:toNumMap()) do | |
| 106 | + specials[special_id] = {np= v, amount = val.amount, award = val.award, quality = k, showIndex = val.showIndex} | |
| 107 | + end | |
| 108 | + else | |
| 109 | + specials[special_id] = {np= 1, amount = val.amount, award = val.award, quality = tonumber(val.type), showIndex = val.showIndex} | |
| 110 | + end | |
| 111 | + end | |
| 112 | + end | |
| 113 | + end | |
| 114 | + | |
| 115 | + --激励奖 | |
| 116 | + local incentive_ids = ichibankuji["incentive_id"] | |
| 117 | + if incentive_ids ~= "" then | |
| 118 | + for _, incentive_id in ipairs(incentive_ids:toArray(true, "=")) do | |
| 119 | + local val = csvdb["ichibankuji_incentiveCsv"][incentive_id] | |
| 120 | + if type(val.type) == "number" then | |
| 121 | + incentive["last"] = {np=val.type, award = val.award} | |
| 122 | + elseif type(val.type) == "string" then | |
| 123 | + for k, v in pairs(val.type:toNumMap()) do | |
| 124 | + if k == 2 then | |
| 125 | + incentive["amount"] = {np= v, award = val.award} | |
| 126 | + elseif k==3 then | |
| 127 | + incentive["probabilities"] = {np= v, award = val.award} | |
| 128 | + end | |
| 129 | + end | |
| 130 | + end | |
| 131 | + end | |
| 132 | + end | |
| 133 | + --货币类型 | |
| 134 | + local coin = ichibankuji["token"]:toArray(true, "=") | |
| 135 | + self:setProperties({coin = coin[1] or 0, hideTime = ichibankuji.hide_time, goods = goods, specials = specials, incentive = incentive}) | |
| 136 | +end | |
| 137 | + | |
| 138 | +function Capsule:isShow() | |
| 139 | + if skynet.timex() >= self:getProperty("hideTime") then | |
| 140 | + return false | |
| 141 | + end | |
| 142 | + return true | |
| 143 | +end | |
| 144 | + | |
| 145 | +function Capsule:refreshing(now) | |
| 146 | + local id = self:getProperty("id") | |
| 147 | + local room = self:getProperty("room") | |
| 148 | + local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room] | |
| 149 | + local reset = tostring(ichibankuji.reset) | |
| 150 | + | |
| 151 | + if reset == "0" then | |
| 152 | + return false | |
| 153 | + elseif reset == "1" then | |
| 154 | + if self:getProperty("resetTimes") == 1 then | |
| 155 | + return true | |
| 156 | + end | |
| 157 | + return false | |
| 158 | + else | |
| 159 | + local resetArr = reset:toArray(true, "=") | |
| 160 | + if not next(resetArr) then return false end | |
| 161 | + | |
| 162 | + if resetArr[1] == "2" then | |
| 163 | + if self:getGoodsAmount() > 0 then return false end | |
| 164 | + | |
| 165 | + local drawEndTime = self:getProperty("drawEndTime") or 0 | |
| 166 | + if drawEndTime == 0 then return false end | |
| 167 | + | |
| 168 | + if now - drawEndTime >= resetArr[2] then | |
| 169 | + return true | |
| 170 | + end | |
| 171 | + return false | |
| 172 | + | |
| 173 | + elseif resetArr[1] == "3" then | |
| 174 | + | |
| 175 | + elseif resetArr[1] == "4" then | |
| 176 | + if now >= resetArr[2] then return true end | |
| 177 | + end | |
| 178 | + end | |
| 179 | + | |
| 180 | + return false | |
| 181 | +end | |
| 182 | + | |
| 183 | +function Capsule:getOnlineCount() | |
| 184 | + local register = self:getProperty("register") or {} | |
| 185 | + local reg, onlookers = 0, 0 | |
| 186 | + for _, v in pairs(register) do | |
| 187 | + if v == 1 then | |
| 188 | + reg = reg + 1 | |
| 189 | + else | |
| 190 | + onlookers = onlookers + 1 | |
| 191 | + end | |
| 192 | + end | |
| 193 | + return {[0]=onlookers, [1]=reg, [2] = reg+onlookers} | |
| 194 | +end | |
| 195 | + | |
| 196 | +function Capsule:join(roleId) | |
| 197 | + --一个房间最多人数 TODO | |
| 198 | + local register = self:getProperty("register") or {} | |
| 199 | + register[roleId] = 0 | |
| 200 | + self:setProperty("register", register) | |
| 201 | + return self:data(roleId) | |
| 202 | +end | |
| 203 | + | |
| 204 | +function Capsule:getRegisterByRoleId(roleId) | |
| 205 | + local register = self:getProperty("register") or {} | |
| 206 | + return register[roleId] or 0 | |
| 207 | +end | |
| 208 | + | |
| 209 | +function Capsule:isRegister(roleId) | |
| 210 | + return self:getRegisterByRoleId(roleId) == 1 | |
| 211 | +end | |
| 212 | + | |
| 213 | +function Capsule:register(roleId) | |
| 214 | + local register = self:getProperty("register") or {} | |
| 215 | + register[roleId] = 1 | |
| 216 | + self:setProperty("register", register) | |
| 217 | + return self:data(roleId) | |
| 218 | +end | |
| 219 | + | |
| 220 | +function Capsule:exit(roleId) | |
| 221 | + local register = self:getProperty("register") or {} | |
| 222 | + if next(register) then | |
| 223 | + register[roleId] = nil | |
| 224 | + return true | |
| 225 | + end | |
| 226 | + return false | |
| 227 | +end | |
| 228 | + | |
| 229 | +function Capsule:confirmed(cares) | |
| 230 | + local goods = self:getProperty("goods") or {} | |
| 231 | + local specials = self:getProperty("specials") or {} | |
| 232 | + local change = {} | |
| 233 | + for k, v in pairs(cares) do | |
| 234 | + if v.typ == 1 then | |
| 235 | + if goods[k] and goods[k].amount ~= v.count then | |
| 236 | + change[k] = goods[k].amount | |
| 237 | + end | |
| 238 | + else | |
| 239 | + if specials[k] and specials[k].amount ~= v.count then | |
| 240 | + change[k] = specials[k].amount | |
| 241 | + end | |
| 242 | + end | |
| 243 | + end | |
| 244 | + return change | |
| 245 | +end | |
| 246 | + | |
| 247 | +function Capsule:getGoodsAmount() | |
| 248 | + local goods = self:getProperty("goods") or {} | |
| 249 | + local amount = 0 | |
| 250 | + for _, v in pairs(goods) do | |
| 251 | + amount = amount + v.amount | |
| 252 | + end | |
| 253 | + return amount | |
| 254 | +end | |
| 255 | + | |
| 256 | +function Capsule:getSpecialByType(typ) | |
| 257 | + local specials = self:getProperty("specials") or {} | |
| 258 | + for k, v in pairs(specials) do | |
| 259 | + if v.quality == typ then | |
| 260 | + return k, v | |
| 261 | + end | |
| 262 | + end | |
| 263 | + return nil | |
| 264 | +end | |
| 265 | + | |
| 266 | +function Capsule:checkSpecialFlag(typ) | |
| 267 | + local spKey, special = self:getSpecialByType(typ) | |
| 268 | + if not special then return nil end | |
| 269 | + | |
| 270 | + if special["amount"] <= 0 then return nil end | |
| 271 | + return spKey, special | |
| 272 | +end | |
| 273 | + | |
| 274 | +local function getSpecialRoleNotify(rewardRecord, count, award, spKey, typ, now) | |
| 275 | + local rewardByRole = {} | |
| 276 | + while(count > 0 and next(rewardRecord)) do | |
| 277 | + local roleId = math.randWeight(rewardRecord, "amount") | |
| 278 | + if roleId then | |
| 279 | + | |
| 280 | + local tmp = rewardRecord[roleId] | |
| 281 | + tmp["amount"] = tmp["amount"] - 1 | |
| 282 | + | |
| 283 | + if tmp["amount"] <= 0 then rewardRecord[roleId] = nil end | |
| 284 | + | |
| 285 | + tmp = rewardByRole[roleId] | |
| 286 | + if not tmp then | |
| 287 | + local name = getNameByRoleId(roleId) | |
| 288 | + tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now} | |
| 289 | + else | |
| 290 | + if not tmp[spKey] then | |
| 291 | + local name = getNameByRoleId(roleId) | |
| 292 | + tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now} | |
| 293 | + else | |
| 294 | + tmp[spKey].amount = tmp[spKey].amount + 1 | |
| 295 | + end | |
| 296 | + end | |
| 297 | + rewardByRole[roleId] = tmp | |
| 298 | + | |
| 299 | + count = count - 1 | |
| 300 | + end | |
| 301 | + end | |
| 302 | + return rewardByRole, count | |
| 303 | +end | |
| 304 | + | |
| 305 | +function Capsule:getTop(record) | |
| 306 | + local spKey, special = self:checkSpecialFlag(SpecialType.TOP) | |
| 307 | + if not special then return nil end | |
| 308 | + local specials = self:getProperty("specials") or {} | |
| 309 | + local specialsRecord = self:getProperty("specialsRecord") or {} | |
| 310 | + | |
| 311 | + if #record < special["np"] then return nil end | |
| 312 | + local topRecord = {} | |
| 313 | + local count = special["np"] | |
| 314 | + for _, v in ipairs(record) do | |
| 315 | + if count <= 0 then break end | |
| 316 | + | |
| 317 | + local tmpCount = 0 | |
| 318 | + if count >= v.amount then | |
| 319 | + count = count - v.amount | |
| 320 | + tmpCount = v.amount | |
| 321 | + else | |
| 322 | + tmpCount = count | |
| 323 | + count = 0 | |
| 324 | + end | |
| 325 | + | |
| 326 | + if not topRecord[v.roleId]then | |
| 327 | + topRecord[v.roleId] = {amount = v.amount } | |
| 328 | + else | |
| 329 | + topRecord[v.roleId] = {amount = (topRecord[v.roleId]["amount"] or 0) + tmpCount} | |
| 330 | + end | |
| 331 | + end | |
| 332 | + | |
| 333 | + local rewardByRole, count = getSpecialRoleNotify(topRecord, special["amount"], special["award"], spKey, SpecialType.TOP) | |
| 334 | + | |
| 335 | + special["amount"] = count | |
| 336 | + specials[spKey] = special | |
| 337 | + specialsRecord[SpecialType.TOP] = rewardByRole | |
| 338 | + self:setProperties({specialsRecord = specialsRecord, specials = specials}) | |
| 339 | + return rewardByRole | |
| 340 | + | |
| 341 | +end | |
| 342 | + | |
| 343 | +--TODO | |
| 344 | +function Capsule:getCore(record) | |
| 345 | + local spKey, special = self:checkSpecialFlag(SpecialType.CORE) | |
| 346 | + if not special then return nil end | |
| 347 | + | |
| 348 | + local specials = self:getProperty("specials") or {} | |
| 349 | + local specialsRecord = self:getProperty("specialsRecord") or {} | |
| 350 | + | |
| 351 | + if self:getGoodsAmount() > 0 then return nil end | |
| 352 | + | |
| 353 | + local np = special["np"] | |
| 354 | + if np > #record then return nil end | |
| 355 | + | |
| 356 | + | |
| 357 | + local left = math.ceil((np - #record)/2) or 0 | |
| 358 | + local count = np | |
| 359 | + local roleRecord = {} | |
| 360 | + for i, v in ipairs(record) do | |
| 361 | + if count <= 0 then break end | |
| 362 | + if i > left then | |
| 363 | + local tmpCount = 0 | |
| 364 | + if count >= v.amount then | |
| 365 | + count = count - v.amount | |
| 366 | + tmpCount = v.amount | |
| 367 | + else | |
| 368 | + tmpCount = count | |
| 369 | + count = 0 | |
| 370 | + end | |
| 371 | + | |
| 372 | + if not roleRecord[v.roleId]then | |
| 373 | + roleRecord[v.roleId] = {amount = v.amount } | |
| 374 | + else | |
| 375 | + roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount} | |
| 376 | + end | |
| 377 | + end | |
| 378 | + | |
| 379 | + end | |
| 380 | + | |
| 381 | + local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.CORE) | |
| 382 | + | |
| 383 | + special["amount"] = count | |
| 384 | + specials[spKey] = special | |
| 385 | + specialsRecord[SpecialType.CORE] = rewardByRole | |
| 386 | + self:setProperties({specialsRecord = specialsRecord, specials = specials}) | |
| 387 | + return rewardByRole | |
| 388 | +end | |
| 389 | + | |
| 390 | +function Capsule:getLast(record) | |
| 391 | + local spKey, special = self:checkSpecialFlag(SpecialType.LAST) | |
| 392 | + if not special then return nil end | |
| 393 | + | |
| 394 | + local specials = self:getProperty("specials") or {} | |
| 395 | + local specialsRecord = self:getProperty("specialsRecord") or {} | |
| 396 | + | |
| 397 | + if self:getGoodsAmount() > 0 then return nil end | |
| 398 | + | |
| 399 | + table.sort(record, function(a, b) return a.create_time > b.create_time end) | |
| 400 | + | |
| 401 | + local np = special["np"] | |
| 402 | + local count = np | |
| 403 | + local roleRecord = {} | |
| 404 | + for _, v in ipairs(record) do | |
| 405 | + if count <= 0 then break end | |
| 406 | + | |
| 407 | + local tmpCount = 0 | |
| 408 | + if count >= v.amount then | |
| 409 | + count = count - v.amount | |
| 410 | + tmpCount = v.amount | |
| 411 | + else | |
| 412 | + tmpCount = count | |
| 413 | + count = 0 | |
| 414 | + end | |
| 415 | + | |
| 416 | + if not roleRecord[v.roleId]then | |
| 417 | + roleRecord[v.roleId] = {amount = v.amount } | |
| 418 | + else | |
| 419 | + roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount} | |
| 420 | + end | |
| 421 | + end | |
| 422 | + | |
| 423 | + local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.LAST) | |
| 424 | + | |
| 425 | + special["amount"] = count | |
| 426 | + specials[spKey] = special | |
| 427 | + specialsRecord[SpecialType.LAST] = rewardByRole | |
| 428 | + self:setProperties({specialsRecord = specialsRecord, specials = specials}) | |
| 429 | + return rewardByRole | |
| 430 | +end | |
| 431 | + | |
| 432 | +function Capsule:getJoker(record) | |
| 433 | + local spKey, special = self:checkSpecialFlag(SpecialType.JOKER) | |
| 434 | + if not special then return nil end | |
| 435 | + | |
| 436 | + local specials = self:getProperty("specials") or {} | |
| 437 | + local specialsRecord = self:getProperty("specialsRecord") or {} | |
| 438 | + | |
| 439 | + if self:getGoodsAmount() > 0 then return nil end | |
| 440 | + | |
| 441 | + local roleRecord = {} | |
| 442 | + for _, v in ipairs(record) do | |
| 443 | + if not roleRecord[v.roleId]then | |
| 444 | + roleRecord[v.roleId] = {amount = v.amount } | |
| 445 | + else | |
| 446 | + roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount} | |
| 447 | + end | |
| 448 | + end | |
| 449 | + | |
| 450 | + local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.JOKER) | |
| 451 | + | |
| 452 | + special["amount"] = count | |
| 453 | + specials[spKey] = special | |
| 454 | + specialsRecord[SpecialType.JOKER] = rewardByRole | |
| 455 | + self:setProperties({specialsRecord = specialsRecord, specials = specials}) | |
| 456 | + return rewardByRole | |
| 457 | +end | |
| 458 | + | |
| 459 | +function Capsule:getKing(record) | |
| 460 | + local spKey, special = self:checkSpecialFlag(SpecialType.KING) | |
| 461 | + if not special then return nil end | |
| 462 | + | |
| 463 | + local specials = self:getProperty("specials") or {} | |
| 464 | + local specialsRecord = self:getProperty("specialsRecord") or {} | |
| 465 | + | |
| 466 | + if self:getGoodsAmount() > 0 then return nil end | |
| 467 | + | |
| 468 | + local roleRecord = {} | |
| 469 | + for _, v in ipairs(record) do | |
| 470 | + if not roleRecord[v.roleId]then | |
| 471 | + roleRecord[v.roleId] = {amount = v.amount } | |
| 472 | + else | |
| 473 | + roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount} | |
| 474 | + end | |
| 475 | + end | |
| 476 | + | |
| 477 | + local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.KING) | |
| 478 | + special["amount"] = count | |
| 479 | + specials[spKey] = special | |
| 480 | + specialsRecord[SpecialType.KING] = rewardByRole | |
| 481 | + self:setProperties({specialsRecord = specialsRecord, specials = specials}) | |
| 482 | + return rewardByRole | |
| 483 | +end | |
| 484 | + | |
| 485 | +local rewardToNtyFunc = function(notify, tmpReward) | |
| 486 | + for key, val in pairs(tmpReward or {}) do | |
| 487 | + if not notify[key] then | |
| 488 | + notify[key] = val | |
| 489 | + else | |
| 490 | + for k, v in pairs(val) do | |
| 491 | + if not notify[key][k] then | |
| 492 | + notify[key][k] = v | |
| 493 | + else | |
| 494 | + notify[key][k] = notify[key][k].amount + v.amount | |
| 495 | + end | |
| 496 | + end | |
| 497 | + | |
| 498 | + end | |
| 499 | + end | |
| 500 | +end | |
| 501 | + | |
| 502 | +function Capsule:checkSpecialReward( now) | |
| 503 | + local specials = self:getProperty("specials") or {} | |
| 504 | + if not next(specials) then return nil end | |
| 505 | + local record = self:getProperty("record") or {} | |
| 506 | + | |
| 507 | + if not next(record) then return nil end | |
| 508 | + table.sort(record, function(a, b) return a.create_time < b.create_time end ) | |
| 509 | + | |
| 510 | + | |
| 511 | + local notify = self:getTop(record) or {} | |
| 512 | + | |
| 513 | + local coreReward = self:getCore(record) | |
| 514 | + rewardToNtyFunc(notify, coreReward) | |
| 515 | + | |
| 516 | + local lastReward = self:getLast(record) | |
| 517 | + rewardToNtyFunc(notify, lastReward) | |
| 518 | + | |
| 519 | + local jokerReward = self:getJoker(record) | |
| 520 | + rewardToNtyFunc(notify, jokerReward) | |
| 521 | + | |
| 522 | + local kingReward = self:getKing(record) | |
| 523 | + rewardToNtyFunc(notify, kingReward) | |
| 524 | + | |
| 525 | + --广播出去TODO | |
| 526 | + --rpcRole(roleId, "paySpecialReward", RewardTYpe.JOKER, jokerReward.award) | |
| 527 | + return notify | |
| 528 | +end | |
| 529 | + | |
| 530 | +function Capsule:checkIncentive(roleId, name, now) | |
| 531 | + local goods = self:getProperty("goods") or {} | |
| 532 | + local recordByRole = self:getProperty("recordByRole") or {} | |
| 533 | + local roleRecord = recordByRole[roleId] or {} | |
| 534 | + local incentiveRecord = self:getProperty("incentiveRecord") or {} | |
| 535 | + local incentiveByRole = incentiveRecord[roleId] or {} | |
| 536 | + local incentive = self:getProperty("incentive") | |
| 537 | + | |
| 538 | + | |
| 539 | + local notify = {} | |
| 540 | + -- 最后一抽 TODO | |
| 541 | + if incentive["last"] then | |
| 542 | + local last = true | |
| 543 | + for k, v in pairs(goods) do | |
| 544 | + if v and v.amount then | |
| 545 | + last = false | |
| 546 | + break | |
| 547 | + end | |
| 548 | + end | |
| 549 | + if last then | |
| 550 | + notify["last"] = {name = name, good_id = "last", typ = RewardType.INCENTIVE, award = incentive["last"]["award"], amount = 1, quality = 1, create_time= now} | |
| 551 | + end | |
| 552 | + end | |
| 553 | + | |
| 554 | + --次数 | |
| 555 | + if incentive["amount"] then | |
| 556 | + local amount = 0 | |
| 557 | + for _, v in pairs(roleRecord) do | |
| 558 | + if (v.calculated or 0) == 0 then | |
| 559 | + amount = amount + v.amount | |
| 560 | + end | |
| 561 | + end | |
| 562 | + | |
| 563 | + local count = math.floor(amount / incentive["amount"]["np"]) | |
| 564 | + local tmpCount = count * incentive["amount"]["np"] | |
| 565 | + notify["amount"] = {name = name, good_id = "amount", typ = RewardType.INCENTIVE, award = incentive["amount"]["award"], amount = count, quality = 2, create_time= now} | |
| 566 | + | |
| 567 | + --填充v.calculated 字段,标识已经用于每x抽的计算中。 | |
| 568 | + for _, v in pairs(roleRecord) do | |
| 569 | + if tmpCount <= 0 then break end | |
| 570 | + | |
| 571 | + v.calculated = v.calculated or 0 | |
| 572 | + if v.calculated ~= v.amount then | |
| 573 | + if tmpCount <= v.amount then | |
| 574 | + v.calculated = tmpCount | |
| 575 | + tmpCount = 0 | |
| 576 | + else | |
| 577 | + v.calculated = v.amount | |
| 578 | + tmpCount = tmpCount - v.amount | |
| 579 | + end | |
| 580 | + end | |
| 581 | + | |
| 582 | + end | |
| 583 | + end | |
| 584 | + | |
| 585 | + --概率 | |
| 586 | + if incentive["probabilities"] then | |
| 587 | + local probabilities = math.randomInt(1, 100) | |
| 588 | + if probabilities <= incentive["probabilities"]["np"] then | |
| 589 | + notify["probabilities"] = {name = name, good_id = "probabilities", typ = RewardType.INCENTIVE, award = incentive["probabilities"]["award"], amount = 1, quality = 3, create_time= now} | |
| 590 | + | |
| 591 | + end | |
| 592 | + end | |
| 593 | + table.insert(incentiveByRole, notify) | |
| 594 | + incentiveRecord[roleId] = incentiveByRole | |
| 595 | + self:setProperty("incentiveRecord", incentiveRecord) | |
| 596 | + | |
| 597 | + return notify | |
| 598 | +end | |
| 599 | + | |
| 600 | +function Capsule:drawByCount(roleId, count) | |
| 601 | + if count <= 0 then return nil end | |
| 602 | + | |
| 603 | + local goods = self:getProperty("goods") or {} | |
| 604 | + local record = self:getProperty("record") or {} | |
| 605 | + local recordByRole = self:getProperty("recordByRole") or {} | |
| 606 | + local roleRecord = recordByRole[roleId] or {} | |
| 607 | + | |
| 608 | + local id = self:getProperty("id") | |
| 609 | + local room = self:getProperty("room") | |
| 610 | + local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room] | |
| 611 | + local goods_id = ichibankuji["goods_id"] | |
| 612 | + local now = skynet.timex() | |
| 613 | + | |
| 614 | + --奖励, 通知信息 | |
| 615 | + local notify= {} | |
| 616 | + notify[roleId] = {} | |
| 617 | + | |
| 618 | + local name = getNameByRoleId(roleId) | |
| 619 | + while (goods and next(goods) and count > 0) do | |
| 620 | + local good_id = math.randWeight(goods, "weight") | |
| 621 | + if good_id then | |
| 622 | + local good = goods[good_id] or {} | |
| 623 | + if good and good.amount > 0 then | |
| 624 | + good.amount = good.amount - 1 | |
| 625 | + | |
| 626 | + --插入记录 | |
| 627 | + local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = 1, quality = good.quality, create_time= now} | |
| 628 | + table.insert(record, tmpNotify) | |
| 629 | + | |
| 630 | + --作为奖励记录+通知 | |
| 631 | + if not notify[roleId][good_id] then | |
| 632 | + notify[roleId][good_id] = tmpNotify | |
| 633 | + else | |
| 634 | + notify[roleId][good_id].amount = notify[roleId][good_id].amount + 1 | |
| 635 | + end | |
| 636 | + | |
| 637 | + --记录角色的抽奖记录 计算激励奖需要用到 | |
| 638 | + if not roleRecord[good_id] then | |
| 639 | + roleRecord[good_id] = tmpNotify | |
| 640 | + else | |
| 641 | + roleRecord[good_id].amount = roleRecord[good_id].amount + 1 | |
| 642 | + end | |
| 643 | + | |
| 644 | + good.weight = good.weight - csvdb["ichibankuji_goodsCsv"][goods_id][good.id].weight | |
| 645 | + count = count - 1 | |
| 646 | + end | |
| 647 | + end | |
| 648 | + | |
| 649 | + end | |
| 650 | + recordByRole[roleId] = roleRecord | |
| 651 | + self:setProperties({recordByRole = recordByRole, record = record, goods = goods}) | |
| 652 | + | |
| 653 | + local tmpNotify = self:checkIncentive(roleId, name, now) | |
| 654 | + for k, v in pairs(tmpNotify) do | |
| 655 | + if not notify[roleId][k] then | |
| 656 | + notify[roleId][k] = v | |
| 657 | + else | |
| 658 | + notify[roleId][k].amount = notify[roleId][k].amount + v.amount | |
| 659 | + end | |
| 660 | + end | |
| 661 | + | |
| 662 | + local speciNotify = self:checkSpecialReward(now) | |
| 663 | + rewardToNtyFunc(notify, speciNotify) | |
| 664 | + | |
| 665 | + local reward, rewardByGoods = {}, {} | |
| 666 | + for key, val in pairs(notify) do | |
| 667 | + if key == roleId then | |
| 668 | + for k, v in pairs(val) do | |
| 669 | + for id, count in pairs(v.award:toNumMap()) do | |
| 670 | + reward[id] = (reward[id] or 0) + count | |
| 671 | + end | |
| 672 | + rewardByGoods[k] = v | |
| 673 | + end | |
| 674 | + end | |
| 675 | + | |
| 676 | + end | |
| 677 | + return reward, rewardByGoods, notify | |
| 678 | +end | |
| 679 | + | |
| 680 | +function Capsule:drawAll(roleId) | |
| 681 | + local goods = self:getProperty("goods") or {} | |
| 682 | + local record = self:getProperty("record") or {} | |
| 683 | + local recordByRole = self:getProperty("recordByRole") or {} | |
| 684 | + local roleRecord = recordByRole[roleId] or {} | |
| 685 | + local now = skynet.timex() | |
| 686 | + | |
| 687 | + local name = getNameByRoleId(roleId) | |
| 688 | + local notify = {} | |
| 689 | + notify[roleId] = {} | |
| 690 | + for good_id, good in pairs(goods) do | |
| 691 | + if good.amount > 0 then | |
| 692 | + --插入记录 | |
| 693 | + local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = good.amount, quality = good.quality, create_time = now} | |
| 694 | + table.insert(record, notify) | |
| 695 | + | |
| 696 | + --作为奖励记录+通知 | |
| 697 | + if not notify[roleId][good_id] then | |
| 698 | + notify[roleId][good_id] = tmpNotify | |
| 699 | + else | |
| 700 | + notify[roleId][good_id].amount = notify[roleId][good_id].amount + good.award | |
| 701 | + end | |
| 702 | + | |
| 703 | + --记录角色的抽奖记录 | |
| 704 | + if not roleRecord[good_id] then | |
| 705 | + roleRecord[good_id] = notify | |
| 706 | + else | |
| 707 | + roleRecord[good_id].amount = roleRecord[good_id].amount + good.amount | |
| 708 | + end | |
| 709 | + | |
| 710 | + good.amount = 0 | |
| 711 | + end | |
| 712 | + | |
| 713 | + end | |
| 714 | + recordByRole[roleId] = roleRecord | |
| 715 | + self:setProperties({recordByRole = recordByRole, record = record, goods = goods}) | |
| 716 | + | |
| 717 | + local tmpNotify = self:checkIncentive(roleId, name, now) | |
| 718 | + for k, v in pairs(tmpNotify) do | |
| 719 | + if not notify[roleId][k] then | |
| 720 | + notify[roleId][k] = v | |
| 721 | + else | |
| 722 | + notify[roleId][k].amount = notify[roleId][k].amount + v.amount | |
| 723 | + end | |
| 724 | + end | |
| 725 | + | |
| 726 | + local speciNotify = self:checkSpecialReward(now) | |
| 727 | + rewardToNtyFunc(notify, speciNotify) | |
| 728 | + | |
| 729 | + local reward, rewardByGoods = {}, {} | |
| 730 | + for key, val in pairs(notify) do | |
| 731 | + if key == roleId then | |
| 732 | + for k, v in pairs(val) do | |
| 733 | + for id, count in pairs(v.award:toNumMap()) do | |
| 734 | + reward[id] = (reward[id] or 0) + count | |
| 735 | + end | |
| 736 | + rewardByGoods[k] = v | |
| 737 | + end | |
| 738 | + end | |
| 739 | + | |
| 740 | + end | |
| 741 | + return reward, rewardByGoods, notify | |
| 742 | +end | |
| 743 | + | |
| 744 | +--@param | |
| 745 | +--[[ | |
| 746 | +@roleId | |
| 747 | +@typ 0=独享,1=公开 | |
| 748 | +@cares 关注{k=v} | |
| 749 | +]]-- | |
| 750 | + | |
| 751 | +function Capsule:draw(roleId, full, cares) | |
| 752 | + if self:getGoodsAmount() == 0 then return 2 end | |
| 753 | + if self:getProperty("typ") == 1 then | |
| 754 | + --是否报名 | |
| 755 | + if self:isRegister(roleId) == false then return 3 end | |
| 756 | + | |
| 757 | + --关注的奖品的数量发生了变化 | |
| 758 | + if cares then | |
| 759 | + local change = self:confirmed(cares) | |
| 760 | + if next(change) then return 4, change end | |
| 761 | + end | |
| 762 | + end | |
| 763 | + | |
| 764 | + if full == 0 then | |
| 765 | + return 5, self:drawByCount(roleId, 1) | |
| 766 | + elseif full == 1 then | |
| 767 | + return 5, self:drawByCount(roleId, 10) | |
| 768 | + elseif full == 2 then | |
| 769 | + return 5, self:drawAll(roleId) | |
| 770 | + end | |
| 771 | +end | |
| 772 | + | |
| 773 | + | |
| 774 | +function Capsule:data(roleId) | |
| 775 | + return { | |
| 776 | + id = self:getProperty("id"), | |
| 777 | + room = self:getProperty("room"), | |
| 778 | + typ = self:getProperty("typ"), | |
| 779 | + name = self:getProperty("name"), | |
| 780 | + coin = self:getProperty("coin"), | |
| 781 | + onlineCount = self:getOnlineCount(), | |
| 782 | + playerStatus = self:getRegisterByRoleId(roleId), | |
| 783 | + record = self:getProperty("record"), | |
| 784 | + rank = self:getProperty("rank"), | |
| 785 | + goods = self:getProperty("goods"), | |
| 786 | + specials = self:getProperty("specials"), | |
| 787 | + incentive = self:getProperty("incentive"), | |
| 788 | + specialsRecord= self:getProperty("specialsRecord"), | |
| 789 | + } | |
| 790 | +end | |
| 791 | + | |
| 792 | +return Capsule | |
| 0 | 793 | \ No newline at end of file | ... | ... |
src/models/Role.lua
src/models/RoleCross.lua
| ... | ... | @@ -210,6 +210,12 @@ RoleCross.bind = function (Role) |
| 210 | 210 | return "成功" |
| 211 | 211 | end |
| 212 | 212 | |
| 213 | + function Role:paySpecialReward(typ, reward) | |
| 214 | + local tmpReward, _= self:award(reward, {log = {desc = "specialsReward", int1=self:getKey(), int2 = typ}}) | |
| 215 | + SendPacket(actionCodes.Capsule_payReward, MsgPack.pack({reward=tmpReward})) | |
| 216 | + return "reward" | |
| 217 | + end | |
| 218 | + | |
| 213 | 219 | end |
| 214 | 220 | |
| 215 | 221 | |
| ... | ... | @@ -370,6 +376,11 @@ function CMD.redPTag(roleId, tag, pms) |
| 370 | 376 | end |
| 371 | 377 | end |
| 372 | 378 | |
| 379 | +function CMD.paySpecialReward(roleId, typ, reward) | |
| 380 | + local role = require("models.Role").new({key = string.format("%s",roleId), id = roleId}) | |
| 381 | + role:award(reward, {log = {desc = "specialsReward", int1=self:getKey(), int2 = typ}}) | |
| 382 | +end | |
| 383 | + | |
| 373 | 384 | RoleCross.handle = function(cmd, roleId, ...) |
| 374 | 385 | SRole = SRole or require("models.Role") |
| 375 | 386 | if CMD[cmd] then | ... | ... |
src/models/RoleLog.lua
src/models/RolePlugin.lua
| ... | ... | @@ -19,6 +19,7 @@ function RolePlugin.bind(Role) |
| 19 | 19 | --self:loadRoleIncre() |
| 20 | 20 | self:loadFriends() |
| 21 | 21 | self:loadSparks() |
| 22 | + self:loadCapsules() | |
| 22 | 23 | end |
| 23 | 24 | |
| 24 | 25 | function Role:reloadWhenLogin() |
| ... | ... | @@ -821,6 +822,41 @@ function RolePlugin.bind(Role) |
| 821 | 822 | end |
| 822 | 823 | end |
| 823 | 824 | |
| 825 | + function Role:loadCapsules() | |
| 826 | + local now = skynet.timex() | |
| 827 | + local roleId = self:getProperty("id") | |
| 828 | + local res = redisproxy:smembers(CAPSULE_ID_ROLE:format(roleId)) or {} | |
| 829 | + for _, key in pairs(res) do | |
| 830 | + local capsule = require("models.Capsule").new({ key = CAPSULE_ROLE:format(roleId,tonumber(key))}) | |
| 831 | + capsule:load() | |
| 832 | + if capsule:isShow() then | |
| 833 | + if capsule:refreshing(now) then | |
| 834 | + capsule:init() | |
| 835 | + capsule:create() | |
| 836 | + end | |
| 837 | + self.capsules[key] = capsule | |
| 838 | + else | |
| 839 | + redisproxy:del(CAPSULE_ROLE:format(roleId, tonumber(key))) | |
| 840 | + redisproxy:srem(CAPSULE_ID_ROLE, key) | |
| 841 | + end | |
| 842 | + end | |
| 843 | + | |
| 844 | + for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do | |
| 845 | + for _, val in pairs(data) do | |
| 846 | + if val.typ == 0 then | |
| 847 | + local key = val.id..val.room | |
| 848 | + if not self.capsules[key] then | |
| 849 | + local capsule = require("models.Capsule").new({ key = CAPSULE_ROLE:format(roleId, tonumber(key)), id= val.id, room = val.room, typ = 0, name=val.name}) | |
| 850 | + capsule:init() | |
| 851 | + capsule:create() | |
| 852 | + self.capsules[key] = capsule | |
| 853 | + redisproxy.sadd(CAPSULE_ID_ROLE:format(roleId), key) | |
| 854 | + end | |
| 855 | + end | |
| 856 | + end | |
| 857 | + end | |
| 858 | + end | |
| 859 | + | |
| 824 | 860 | -- 0 为操作成功 |
| 825 | 861 | function Role:addRune(params) |
| 826 | 862 | if params.type and params.id then |
| ... | ... | @@ -1995,6 +2031,7 @@ function RolePlugin.bind(Role) |
| 1995 | 2031 | function Role:onRecoverTimer(now) |
| 1996 | 2032 | self:updateTimeReset(now, true) |
| 1997 | 2033 | self:checkNewEvent(now) |
| 2034 | + self:checkCapsule(now) | |
| 1998 | 2035 | self:saveRoleData(now) |
| 1999 | 2036 | end |
| 2000 | 2037 | |
| ... | ... | @@ -3116,7 +3153,39 @@ function RolePlugin.bind(Role) |
| 3116 | 3153 | return itemRandomOccupy |
| 3117 | 3154 | end |
| 3118 | 3155 | |
| 3156 | + function Role:getCapsuleList(coin) | |
| 3157 | + local capsules = {} | |
| 3158 | + for k, v in pairs(self.capsules) do | |
| 3159 | + if v:getProperty("coin") == coin then | |
| 3160 | + local onlineCount= v:getOnlineCount() | |
| 3161 | + capsules[k] = {id=v:getProperty("id"), room=v:getProperty("room"), typ=v:getProperty("typ"), people=onlineCount[2], coin= v:getProperty("coin")} | |
| 3162 | + end | |
| 3163 | + end | |
| 3164 | + return capsules | |
| 3165 | + end | |
| 3166 | + | |
| 3167 | + function Role:drawCapsule(capsuleId, full, cares) | |
| 3168 | + local capsule = self.capsules[capsuleId] or {} | |
| 3169 | + if next(capsule) then | |
| 3170 | + local roleId = self:getProperty("id") | |
| 3171 | + return capsule:draw(roleId, full, cares) | |
| 3172 | + end | |
| 3173 | + return 1 | |
| 3174 | + end | |
| 3175 | + | |
| 3176 | + function Role:joinCapsule(capsuleId) | |
| 3177 | + local capsule = self.capsules[capsuleId] or {} | |
| 3178 | + return capsule:data() | |
| 3179 | + end | |
| 3119 | 3180 | |
| 3181 | + function Role:checkCapsule(now) | |
| 3182 | + for _, v in pairs(self.capsules) do | |
| 3183 | + if v:refreshing(now) then | |
| 3184 | + v:init() | |
| 3185 | + v:create() | |
| 3186 | + end | |
| 3187 | + end | |
| 3188 | + end | |
| 3120 | 3189 | end |
| 3121 | 3190 | |
| 3122 | 3191 | return RolePlugin |
| 3123 | 3192 | \ No newline at end of file | ... | ... |
src/services/agent_ctrl.lua
| ... | ... | @@ -188,7 +188,7 @@ function _M:query_agent(fd, uid, isQueue) |
| 188 | 188 | end |
| 189 | 189 | |
| 190 | 190 | local agent = get_a(pack) |
| 191 | - local ok = pcall(skynet.call, agent, "lua", "start", gate_serv, fd, self.f2i[fd]) | |
| 191 | + local ok = pcall(skynet.call, agent, "lua", "start", gate_serv, capsuled, fd, self.f2i[fd]) | |
| 192 | 192 | if not ok then |
| 193 | 193 | query_agent_response(fd, {ret = "INNER_ERROR"}) |
| 194 | 194 | return |
| ... | ... | @@ -223,7 +223,7 @@ function _M:query_agent(fd, uid, isQueue) |
| 223 | 223 | end |
| 224 | 224 | end |
| 225 | 225 | |
| 226 | - local ok = pcall(skynet.call, agent, "lua", "start", gate_serv, fd, self.f2i[fd], hotfixList) | |
| 226 | + local ok = pcall(skynet.call, agent, "lua", "start", gate_serv, capsuled, fd, self.f2i[fd], hotfixList) | |
| 227 | 227 | if not ok then |
| 228 | 228 | self.factory:push(agent) |
| 229 | 229 | query_agent_response(fd, {ret = "INNER_ERROR"}) | ... | ... |
| ... | ... | @@ -0,0 +1,181 @@ |
| 1 | +require "ProtocolCode" | |
| 2 | +require "shared.init" | |
| 3 | +require "GlobalVar" | |
| 4 | +require "RedisKeys" | |
| 5 | +require "skynet.manager" | |
| 6 | +skynet = require "skynet" | |
| 7 | +csvdb = require "shared.csvdata" | |
| 8 | +redisproxy = require "shared.redisproxy" | |
| 9 | +datacenter = require "skynet.datacenter" | |
| 10 | + | |
| 11 | +local capsules = {} | |
| 12 | +local CMD = {} | |
| 13 | + | |
| 14 | +NotifyChangeType = { | |
| 15 | + JOIN = 1, | |
| 16 | + EXIT = 2, | |
| 17 | + DRAW = 3, | |
| 18 | + SPECIAL = 4, | |
| 19 | + INCENTIVE = 5, | |
| 20 | +} | |
| 21 | + | |
| 22 | +local function rpcRole(roleId, funcName, ...) | |
| 23 | + local fields = ... | |
| 24 | + local agent = datacenter.get("agent", roleId) | |
| 25 | + local roleCross = require("models.RoleCross") | |
| 26 | + if funcName == "getProperties" then | |
| 27 | + return roleCross.handle(funcName, roleId, fields) | |
| 28 | + else | |
| 29 | + return roleCross.handle(funcName, roleId, ...) | |
| 30 | + end | |
| 31 | +end | |
| 32 | + | |
| 33 | +function getNameByRoleId(roleId) | |
| 34 | + return rpcRole(roleId, "getProperty", "name") | |
| 35 | + | |
| 36 | +end | |
| 37 | + | |
| 38 | +function broadCastCapsule(roleId, capsuleId, broadInfo) | |
| 39 | + local capsule = capsules[capsuleId] or {} | |
| 40 | + local register = capsule:getProperty("register") or {} | |
| 41 | + if next(capsule) then | |
| 42 | + for id, _ in pairs(register) do | |
| 43 | + if id ~= roleId then | |
| 44 | + rpcRole(id, "SendPacket", actionCodes.Capsule_notifyChange, MsgPack.pack(broadInfo)) -- 通知对方 | |
| 45 | + end | |
| 46 | + end | |
| 47 | + end | |
| 48 | +end | |
| 49 | + | |
| 50 | +local function getCapsuleId(id, room) | |
| 51 | + return string.format("%d%d", id, room) | |
| 52 | +end | |
| 53 | + | |
| 54 | + | |
| 55 | +local function add(roleId, capsuleId) | |
| 56 | + local capsule = capsules[capsuleId] or {} | |
| 57 | + if next(capsule) then | |
| 58 | + capsule:join(roleId) | |
| 59 | + broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.JOIN, roleId = roleId}) | |
| 60 | + return capsule:data(roleId) | |
| 61 | + end | |
| 62 | + print("id 不存在: ".. capsuleId) | |
| 63 | + return nil | |
| 64 | +end | |
| 65 | + | |
| 66 | +local function capsuleRefreshing() | |
| 67 | + for _, v in pairs(capsules) do | |
| 68 | + if v:refreshing() then | |
| 69 | + v:init() | |
| 70 | + v:create() | |
| 71 | + end | |
| 72 | + end | |
| 73 | +end | |
| 74 | + | |
| 75 | +--扭蛋机刷新 | |
| 76 | +local function check_capsules() | |
| 77 | + pcall(capsuleRefreshing) | |
| 78 | + skynet.timeout(60, check_capsules) | |
| 79 | +end | |
| 80 | + | |
| 81 | +function CMD.start() | |
| 82 | + local now = skynet.timex() | |
| 83 | + local res = redisproxy:smembers(CAPSULE_INFO) or {} | |
| 84 | + for _, key in pairs(res) do | |
| 85 | + local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key)}) | |
| 86 | + capsule:load() | |
| 87 | + if capsule:isShow() then | |
| 88 | + if capsule:refreshing(now) then | |
| 89 | + capsule:init() | |
| 90 | + capsule:create() | |
| 91 | + end | |
| 92 | + capsules[key] = capsule | |
| 93 | + else | |
| 94 | + redisproxy:del(CAPSULE_PUBLIC:format(key)) | |
| 95 | + redisproxy:srem(CAPSULE_INFO, key) | |
| 96 | + end | |
| 97 | + end | |
| 98 | + | |
| 99 | + for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do | |
| 100 | + for _, val in ipairs(data) do | |
| 101 | + if val.type == 1 then | |
| 102 | + local key = val.id..val.room | |
| 103 | + if not capsules[key] then | |
| 104 | + local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name}) | |
| 105 | + capsule:init() | |
| 106 | + capsule:create() | |
| 107 | + capsules[key] = capsule | |
| 108 | + redisproxy:sadd(CAPSULE_INFO, key) | |
| 109 | + end | |
| 110 | + end | |
| 111 | + end | |
| 112 | + end | |
| 113 | + | |
| 114 | + check_capsules() | |
| 115 | +end | |
| 116 | + | |
| 117 | +function CMD.list(coin) | |
| 118 | + local tmpCapsules = {} | |
| 119 | + for k, v in pairs(capsules) do | |
| 120 | + if v:getProperty("coin") == coin then | |
| 121 | + local onlineCount= v:getOnlineCount() | |
| 122 | + tmpCapsules[k] = {id=v:getProperty("id"), room=v:getProperty("room"), typ=v:getProperty("typ"), people=onlineCount[2], coin= v:getProperty("coin")} | |
| 123 | + end | |
| 124 | + end | |
| 125 | + return tmpCapsules | |
| 126 | +end | |
| 127 | + | |
| 128 | +function CMD.join(roleId, capsuleId) | |
| 129 | + if capsuleId then | |
| 130 | + return add(roleId, capsuleId) | |
| 131 | + end | |
| 132 | + return nil | |
| 133 | +end | |
| 134 | + | |
| 135 | + | |
| 136 | +function CMD.exit(roleId, capsuleId) | |
| 137 | + if capsuleId then | |
| 138 | + local capsule = capsules[capsuleId] or {} | |
| 139 | + if next(capsule) then | |
| 140 | + capsule:exit(roleId) | |
| 141 | + broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.EXIT, roleId = roleId}) | |
| 142 | + end | |
| 143 | + else | |
| 144 | + for _, capsule in pairs(capsules) do | |
| 145 | + if next(capsule) then | |
| 146 | + capsule:exit(roleId) | |
| 147 | + broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.EXIT, roleId = roleId}) | |
| 148 | + end | |
| 149 | + end | |
| 150 | + end | |
| 151 | +end | |
| 152 | + | |
| 153 | +function CMD.draw_capsule(roleId, capsuleId, full, cares) | |
| 154 | + local capsule = capsules[capsuleId] or {} | |
| 155 | + if next(capsule) then | |
| 156 | + local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, cares) | |
| 157 | + --if ret > 4 then | |
| 158 | + -- broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.DRAW, roleId = roleId, notify = notify}) | |
| 159 | + --end | |
| 160 | + return ret, reward, rewardByGoods, capsule:data(roleId) | |
| 161 | + end | |
| 162 | + return 1 | |
| 163 | +end | |
| 164 | + | |
| 165 | +function CMD.register(roleId, capsuleId) | |
| 166 | + local capsule = capsules[capsuleId] or {} | |
| 167 | + if next(capsule) then | |
| 168 | + return capsule:register(roleId) | |
| 169 | + end | |
| 170 | + return nil | |
| 171 | +end | |
| 172 | + | |
| 173 | +skynet.start(function() | |
| 174 | + skynet.dispatch("lua", function(session, address, cmd, ...) | |
| 175 | + local f = CMD[string.lower(cmd)] | |
| 176 | + skynet.ret(skynet.pack(f(...))) | |
| 177 | + end) | |
| 178 | + | |
| 179 | + skynet.register("capsuled") | |
| 180 | + globalCsv = csvdb["GlobalDefineCsv"] | |
| 181 | +end) | |
| 0 | 182 | \ No newline at end of file | ... | ... |
src/services/watchdog.lua
| ... | ... | @@ -66,6 +66,8 @@ function CMD.start(conf) |
| 66 | 66 | skynet.call(gate_serv, "lua", "open" , conf) |
| 67 | 67 | |
| 68 | 68 | skynet.call(pvpd, "lua", "start") |
| 69 | + | |
| 70 | + skynet.call(capsuled, "lua", "start") | |
| 69 | 71 | -- 开启agent状态检测定时器 |
| 70 | 72 | check_agent_status() |
| 71 | 73 | -- 创建广播服务 |
| ... | ... | @@ -126,4 +128,5 @@ skynet.start(function() |
| 126 | 128 | skynet.newservice("services/chated") |
| 127 | 129 | -- 网关服务 |
| 128 | 130 | gate_serv = skynet.newservice("gate") |
| 131 | + capsuled = skynet.newservice("services/capsuled") | |
| 129 | 132 | end) | ... | ... |