Blame view

src/services/capsuled.lua 8.19 KB
555f745e   zhangqijia   feat: 一番赏
1
2
3
4
5
  require "ProtocolCode"
  require "shared.init"
  require "GlobalVar"
  require "RedisKeys"
  require "skynet.manager"
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
6
  skynet = require "skynet"
555f745e   zhangqijia   feat: 一番赏
7
8
9
10
11
12
13
14
15
16
17
18
  csvdb = require "shared.csvdata"
  redisproxy = require "shared.redisproxy"
  datacenter = require "skynet.datacenter"
  
  local capsules = {}
  local CMD = {}
  
  NotifyChangeType = {
      JOIN = 1,
      EXIT = 2,
      DRAW = 3,
      SPECIAL = 4,
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
19
20
21
22
23
24
25
  }
  
  skynet.register_protocol {
      name = "role",
      id = 101,
      pack = skynet.pack,
      unpack = skynet.unpack,
555f745e   zhangqijia   feat: 一番赏
26
27
  }
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
28
  local function rpcRole(roleId, funcName, ...)
555f745e   zhangqijia   feat: 一番赏
29
30
      local fields = ...
      local agent = datacenter.get("agent", roleId)
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
31
32
33
34
35
36
      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
555f745e   zhangqijia   feat: 一番赏
37
      else
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
38
39
40
41
42
43
          local roleCross = require("models.RoleCross")
          if funcName == "getProperties" then
              return false, roleCross.handle(funcName, roleId, fields)
          else
              return false, roleCross.handle(funcName, roleId, ...)
          end
555f745e   zhangqijia   feat: 一番赏
44
45
46
      end
  end
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
47
  function getNameByRoleId(roleId)
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
48
49
      local status, name = rpcRole(roleId, "getProperty", "name")
      return name
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
50
51
52
  
  end
  
555f745e   zhangqijia   feat: 一番赏
53
  function broadCastCapsule(roleId, capsuleId, broadInfo)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
54
55
56
      local capsule = capsules[capsuleId]
      if not capsule then print("not capsule :" .. capsuleId) return end
  
555f745e   zhangqijia   feat: 一番赏
57
58
59
60
61
62
63
64
65
66
      local register = capsule:getProperty("register") or {}
      if next(capsule) then
          for id, _ in pairs(register) do
              if id ~= roleId then
                  rpcRole(id, "SendPacket", actionCodes.Capsule_notifyChange, MsgPack.pack(broadInfo)) 	-- 通知对方
              end
          end
      end
  end
  
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
67
  function broadCastSpecial(roleId, capsuleId, broadInfo)
01ff9b4b   zhangqijia   fix: 一番赏 优化; 抽奖记录...
68
      if not broadInfo or not next(broadInfo) then return end
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
69
70
71
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return end
  
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
72
      local register = capsule:getProperty("register") or {}
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
73
74
75
      for id, _ in pairs(register) do
          if id ~= roleId then
              if broadInfo[id] then
da20e384   zhangqijia   feat: 一番赏 特殊赏 通知
76
                 rpcRole(id, "paySpecialReward", id, broadInfo[id])
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
77
78
79
              end
          end
      end
555f745e   zhangqijia   feat: 一番赏
80
81
  end
  
555f745e   zhangqijia   feat: 一番赏
82
  local function add(roleId, capsuleId)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
83
      local capsule = capsules[capsuleId]
da026be2   zhangqijia   fix: 一番赏 扭蛋机刷新机制 ...
84
      if not capsule then skynet.error("add not capsule: " .. capsuleId) return end
555f745e   zhangqijia   feat: 一番赏
85
86
      if next(capsule) then
          capsule:join(roleId)
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
87
          broadCastCapsule(roleId, capsuleId, {notifyType= NotifyChangeType.JOIN, roleId = roleId})
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
88
          return capsule:data(roleId)
555f745e   zhangqijia   feat: 一番赏
89
90
91
92
93
      end
      print("id 不存在: ".. capsuleId)
      return nil
  end
  
da026be2   zhangqijia   fix: 一番赏 扭蛋机刷新机制 ...
94
  local function capsuleRefreshing(now)
555f745e   zhangqijia   feat: 一番赏
95
      for _, v in pairs(capsules) do
f2f84157   zhangqijia   fix: 一番赏 修复king赏bug
96
          if v:refreshing(now) then
da026be2   zhangqijia   fix: 一番赏 扭蛋机刷新机制 ...
97
98
99
              print("刷新机器looooook....")
              print(v:getProperty("id"))
              print(v:getProperty("room"))
555f745e   zhangqijia   feat: 一番赏
100
101
102
103
104
105
106
107
              v:init()
              v:create()
          end
      end
  end
  
  --扭蛋机刷新
  local function check_capsules()
da026be2   zhangqijia   fix: 一番赏 扭蛋机刷新机制 ...
108
109
110
      local now = skynet.timex()
      pcall(capsuleRefreshing, now)
      skynet.timeout(100, check_capsules)
555f745e   zhangqijia   feat: 一番赏
111
112
  end
  
6304b5ad   zhangqijia   feat: 一番赏 增加重置扭蛋机指令
113
114
115
116
117
118
119
120
121
122
123
124
  function CMD.reset()
      local now = skynet.timex()
      local res = redisproxy:smembers(CAPSULE_INFO) or {}
      for _, key in pairs(res) do
          redisproxy:del(CAPSULE_PUBLIC:format(key))
          redisproxy:srem(CAPSULE_INFO, key)
      end
  
      for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do
          for _, val in ipairs(data) do
              if val.type == 1 then
                  local key = val.id..val.room
078bdb2f   zhangqijia   fix: 一番赏 特殊赏的抽奖记录...
125
126
127
128
129
                  local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name})
                  capsule:init()
                  capsule:create()
                  capsules[key] = capsule
                  redisproxy:sadd(CAPSULE_INFO, key)
6304b5ad   zhangqijia   feat: 一番赏 增加重置扭蛋机指令
130
131
132
133
134
              end
          end
      end
  end
  
555f745e   zhangqijia   feat: 一番赏
135
136
137
138
  function CMD.start()
      local now = skynet.timex()
      local res = redisproxy:smembers(CAPSULE_INFO) or {}
      for _, key in pairs(res) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
139
          local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key)})
555f745e   zhangqijia   feat: 一番赏
140
141
142
143
144
145
146
147
          capsule:load()
          if capsule:isShow() then
              if capsule:refreshing(now) then
                  capsule:init()
                  capsule:create()
              end
              capsules[key] = capsule
          else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
148
              redisproxy:del(CAPSULE_PUBLIC:format(key))
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
149
              redisproxy:srem(CAPSULE_INFO, key)
555f745e   zhangqijia   feat: 一番赏
150
151
152
153
154
155
          end
      end
  
      for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do
          for _, val in ipairs(data) do
              if val.type == 1 then
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
156
                  local key = val.id..val.room
555f745e   zhangqijia   feat: 一番赏
157
                  if not capsules[key] then
f2f84157   zhangqijia   fix: 一番赏 修复king赏bug
158
159
160
161
162
163
164
165
                      if now < val.hide_time then
                          local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name})
                          capsule:init()
                          capsule:create()
  
                          capsules[key] = capsule
                          redisproxy:sadd(CAPSULE_INFO, key)
                      end
555f745e   zhangqijia   feat: 一番赏
166
167
168
169
170
171
172
173
                  end
              end
          end
      end
  
      check_capsules()
  end
  
639bbdff   zhangqijia   fix: 一番赏 获取扭蛋机列表,...
174
  function CMD.list(id)
555f745e   zhangqijia   feat: 一番赏
175
176
      local tmpCapsules = {}
      for k, v in pairs(capsules) do
639bbdff   zhangqijia   fix: 一番赏 获取扭蛋机列表,...
177
          if v:getProperty("id") == id then
555f745e   zhangqijia   feat: 一番赏
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
              local onlineCount= v:getOnlineCount()
              tmpCapsules[k] = {id=v:getProperty("id"), room=v:getProperty("room"), typ=v:getProperty("typ"), people=onlineCount[2], coin= v:getProperty("coin")}
          end
      end
      return tmpCapsules
  end
  
  function CMD.join(roleId, capsuleId)
      if capsuleId then
          return add(roleId, capsuleId)
      end
      return nil
  end
  
  
  function CMD.exit(roleId, capsuleId)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
194
      if capsuleId then
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
195
196
197
198
199
          local capsule = capsules[capsuleId]
          if not capsule then skynet.error("not capsule: " .. capsuleId) return end
  
          capsule:exit(roleId)
          broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.EXIT, roleId = roleId})
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
200
      else
652ef8f7   zhangqijia   fix: 一番赏 修复广播caps...
201
          for id, capsule in pairs(capsules) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
202
203
              if next(capsule) then
                  capsule:exit(roleId)
652ef8f7   zhangqijia   fix: 一番赏 修复广播caps...
204
                  broadCastCapsule(roleId, id, {notifyType = NotifyChangeType.EXIT, roleId = roleId})
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
205
206
              end
          end
555f745e   zhangqijia   feat: 一番赏
207
208
209
      end
  end
  
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
210
  function CMD.draw_capsule(roleId, capsuleId, full, drawsNum, cares)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
211
212
213
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return 2 end
  
01ff9b4b   zhangqijia   fix: 一番赏 优化; 抽奖记录...
214
      local ret, drawReward = capsule:draw(roleId, full, drawsNum, cares)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
215
216
      if ret > 5 then
          --broadCastCapsule(roleId, capsuleId, {notifyType = NotifyChangeType.DRAW, notify = notify})
01ff9b4b   zhangqijia   fix: 一番赏 优化; 抽奖记录...
217
          broadCastSpecial(roleId, capsuleId, drawReward["specials"])
555f745e   zhangqijia   feat: 一番赏
218
      end
01ff9b4b   zhangqijia   fix: 一番赏 优化; 抽奖记录...
219
220
      drawReward["specials"] = nil
      return ret, drawReward, capsule:data(roleId)
555f745e   zhangqijia   feat: 一番赏
221
222
  end
  
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
223
  function CMD.register(roleId, capsuleId)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
224
225
226
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
  
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
227
228
229
230
      return capsule:register(roleId)
  end
  
  function CMD.goods_stock(capsuleId)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
231
232
233
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return 0 end
  
0eb317fb   zhangqijia   fix: 一番赏奖励抽奖的bug与...
234
235
236
      return capsule:getGoodsAmount(), capsule:getProperty("token")
  end
  
0a421d34   zhangqijia   fix: 一番赏奖励 刷新协议缺少...
237
  function CMD.capsule_data(roleId, capsuleId)
7f21f75b   zhangqijia   fix: 一番赏 增加rank,刷...
238
239
240
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
  
0a421d34   zhangqijia   fix: 一番赏奖励 刷新协议缺少...
241
      return capsule:data(roleId)
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
242
243
  end
  
01ff9b4b   zhangqijia   fix: 一番赏 优化; 抽奖记录...
244
245
246
247
248
249
250
  function CMD.page_record(capsuleId, up, idx)
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
  
      return capsule:pageRecord(up, idx)
  end
  
da20e384   zhangqijia   feat: 一番赏 特殊赏 通知
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  function CMD.get_special_nty(roleId, capsuleId)
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
  
      return capsule:getSpecialNotify(roleId)
  end
  
  function CMD.clear_special_nty(roleId, capsuleId, good_ids)
      local capsule = capsules[capsuleId]
      if not capsule then skynet.error("not capsule: " .. capsuleId) return nil end
  
      return capsule:clearSpecialNty(roleId, good_ids)
  end
  
555f745e   zhangqijia   feat: 一番赏
265
266
267
268
269
270
271
  skynet.start(function()
      skynet.dispatch("lua", function(session, address, cmd, ...)
          local f = CMD[string.lower(cmd)]
          skynet.ret(skynet.pack(f(...)))
      end)
  
      skynet.register("capsuled")
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
272
      globalCsv = csvdb["GlobalDefineCsv"]
555f745e   zhangqijia   feat: 一番赏
273
  end)