Blame view

src/services/capsuled.lua 5.03 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
19
20
21
  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,
      INCENTIVE = 5,
  }
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
22
  local function rpcRole(roleId, funcName, ...)
555f745e   zhangqijia   feat: 一番赏
23
24
      local fields = ...
      local agent = datacenter.get("agent", roleId)
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
25
26
27
      local roleCross = require("models.RoleCross")
      if funcName == "getProperties" then
          return roleCross.handle(funcName, roleId, fields)
555f745e   zhangqijia   feat: 一番赏
28
      else
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
29
          return roleCross.handle(funcName, roleId, ...)
555f745e   zhangqijia   feat: 一番赏
30
31
32
      end
  end
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
33
34
35
36
37
  function getNameByRoleId(roleId)
      return rpcRole(roleId, "getProperty", "name")
  
  end
  
555f745e   zhangqijia   feat: 一番赏
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  function broadCastCapsule(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, "SendPacket", actionCodes.Capsule_notifyChange, MsgPack.pack(broadInfo)) 	-- 通知对方
              end
          end
      end
  end
  
  local function getCapsuleId(id, room)
      return string.format("%d%d", id, room)
  end
  
  
  local function add(roleId, capsuleId)
555f745e   zhangqijia   feat: 一番赏
56
57
58
59
      local capsule = capsules[capsuleId] or {}
      if next(capsule) then
          capsule:join(roleId)
          broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.JOIN, roleId = roleId})
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
60
          return capsule:data(roleId)
555f745e   zhangqijia   feat: 一番赏
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
      end
      print("id 不存在: ".. capsuleId)
      return nil
  end
  
  local function capsuleRefreshing()
      for _, v in pairs(capsules) do
          if v:refreshing() then
              v:init()
              v:create()
          end
      end
  end
  
  --扭蛋机刷新
  local function check_capsules()
      pcall(capsuleRefreshing)
      skynet.timeout(60, check_capsules)
  end
  
  function CMD.start()
      local now = skynet.timex()
      local res = redisproxy:smembers(CAPSULE_INFO) or {}
      for _, key in pairs(res) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
85
          local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key)})
555f745e   zhangqijia   feat: 一番赏
86
87
88
89
90
91
92
93
          capsule:load()
          if capsule:isShow() then
              if capsule:refreshing(now) then
                  capsule:init()
                  capsule:create()
              end
              capsules[key] = capsule
          else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
94
              redisproxy:del(CAPSULE_PUBLIC:format(key))
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
95
              redisproxy:srem(CAPSULE_INFO, key)
555f745e   zhangqijia   feat: 一番赏
96
97
98
99
100
101
          end
      end
  
      for _, data in pairs(csvdb["ichibankuji_mainCsv"]) do
          for _, val in ipairs(data) do
              if val.type == 1 then
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
102
                  local key = val.id..val.room
555f745e   zhangqijia   feat: 一番赏
103
                  if not capsules[key] then
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
104
                      local capsule = require("models.Capsule").new({ key = CAPSULE_PUBLIC:format(key), id= val.id, room = val.room, typ = 1, name=val.name})
555f745e   zhangqijia   feat: 一番赏
105
106
107
                      capsule:init()
                      capsule:create()
                      capsules[key] = capsule
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
108
                      redisproxy:sadd(CAPSULE_INFO, key)
555f745e   zhangqijia   feat: 一番赏
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
                  end
              end
          end
      end
  
      check_capsules()
  end
  
  function CMD.list(coin)
      local tmpCapsules = {}
      for k, v in pairs(capsules) do
          if v:getProperty("coin") == coin then
              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: 一番赏增加通知信息的数据...
137
138
139
140
141
142
143
144
145
146
147
148
149
      if capsuleId then
          local capsule = capsules[capsuleId] or {}
          if next(capsule) then
              capsule:exit(roleId)
              broadCastCapsule(roleId, capsuleId, {changeType = 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})
              end
          end
555f745e   zhangqijia   feat: 一番赏
150
151
152
      end
  end
  
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
153
  function CMD.draw_capsule(roleId, capsuleId, full, cares)
555f745e   zhangqijia   feat: 一番赏
154
155
      local capsule = capsules[capsuleId] or {}
      if next(capsule) then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
156
          local ret, reward, rewardByGoods, notify = capsule:draw(roleId, full, cares)
555f745e   zhangqijia   feat: 一番赏
157
158
159
          --if ret > 4 then
          --    broadCastCapsule(roleId, capsuleId, {changeType = NotifyChangeType.DRAW, roleId = roleId, notify = notify})
          --end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
160
          return ret, reward, rewardByGoods, capsule:data(roleId)
555f745e   zhangqijia   feat: 一番赏
161
      end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
162
      return 1
555f745e   zhangqijia   feat: 一番赏
163
164
  end
  
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
165
166
167
168
169
170
171
172
  function CMD.register(roleId, capsuleId)
      local capsule = capsules[capsuleId] or {}
      if next(capsule) then
          return capsule:register(roleId)
      end
      return nil
  end
  
555f745e   zhangqijia   feat: 一番赏
173
174
175
176
177
178
179
  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: 一番赏奖励中加入得奖者的...
180
      globalCsv = csvdb["GlobalDefineCsv"]
555f745e   zhangqijia   feat: 一番赏
181
  end)