Blame view

src/models/Capsule.lua 24.6 KB
555f745e   zhangqijia   feat: 一番赏
1
2
3
4
5
6
7
8
  --扭蛋机
  local MsgPack = MsgPack
  local Capsule = class("Capsule", require("shared.ModelBase"))
  
  function Capsule:ctor(properties)
      Capsule.super.ctor(self, properties)
  end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
9
10
11
12
13
14
15
  RewardType = {
      GOODS = 1,
      SPECIAL = 2,
      INCENTIVE = 3,
  }
  
  SpecialType = {
555f745e   zhangqijia   feat: 一番赏
16
17
18
19
20
      TOP = 1,
      CORE = 2,
      LAST = 3,
      JOKER = 4,
      KING = 5,
555f745e   zhangqijia   feat: 一番赏
21
22
23
24
25
26
27
  }
  
  CapsuleType = {
      PRIVATE = 0,
      PUBLIC = 1,
  }
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
28
29
  --[[
  --通知数据结构
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
30
  { [roleId] = { [good_id1] = { }, [good_id2] = { }, } }
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
31
32
  ]]--
  
555f745e   zhangqijia   feat: 一番赏
33
34
35
  Capsule.schema = {
      id = {"number", 0}, --扭蛋机key,配置读取
      room = {"number", 0}, --房间号, 配置读取
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
36
      name = {"string"},
555f745e   zhangqijia   feat: 一番赏
37
38
39
40
41
42
43
44
45
      typ = {"number", 1}, -- 1=共享,2=独享
      coin = {"number", 0}, --货币代号
      register = {"table", {}}, --人数 {["id"]=0}, 0 围观, 1 已报名
      record = {"table", {}}, --抽取记录  列表
      recordByRole = {"table", {}}, -- 抽取记录,hash, {roleid=record}
      rank = {"table", {}}, --排行
      goods = {"table", {}}, --奖励池
      specials = {"table", {}}, --特殊赏
      incentive = {"table", {}}, --激励奖
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
46
47
      incentiveRecord = {"table", {}}, --激励奖记录
      specialsRecord= {"table", {}}, --特殊赏领取记录
555f745e   zhangqijia   feat: 一番赏
48
49
50
51
52
      resetTimes = {"number", 0}, --每日一次手动重置的机会
      hideTime = {"number", 0} , --隐藏时间
      drawEndTime = {"number", 0}, --抽完时间
  }
  
555f745e   zhangqijia   feat: 一番赏
53
54
55
56
57
58
59
60
61
62
63
64
65
  function Capsule:getResetFields()
      return {
          id = self:getProperty("id"),
          room = self:getProperty("room"),
          typ = self:getProperty("typ"),
          coin = 0,
          register = {},
          record = {},
          recordByRole = {},
          rank = {},
          goods = {},
          specials = {},
          incentive = {},
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
66
          incentiveRecord = {},
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
67
          specialsRecord= {},
555f745e   zhangqijia   feat: 一番赏
68
          resetTimes = 0,
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
69
70
          hideTime = 0,
          drawEndTime = 0,
555f745e   zhangqijia   feat: 一番赏
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
      }
  end
  
  function Capsule:init()
      local id = self:getProperty("id")
      local room = self:getProperty("room")
      self:setProperties(self:getResetFields())
  
      local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room]
  
      --奖励池
      local goods_id = ichibankuji["goods_id"]
      local goods, specials, incentive = {}, {}, {}
      for _, data in pairs(csvdb["ichibankuji_goodsCsv"]) do
          for _, val in pairs(data) do
              if val.key == goods_id then
                  goods[goods_id..val.id] = clone(val)
              end
          end
      end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
91
      for _, v in pairs(goods) do
555f745e   zhangqijia   feat: 一番赏
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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
137
          v.weight = (v.weight or 0) * v.amount
      end
  
      --特殊赏
      local special_ids = ichibankuji["special_id"]
      if special_ids ~= "" then
          for _, special_id in ipairs(special_ids:toArray(true, "=")) do
              local val = csvdb["ichibankuji_specialCsv"][special_id]
              if type(val.type) == "number" then
                  specials[special_id] = {np= 1, amount = val.amount, award = val.award, quality = tonumber(val.type), showIndex = val.showIndex}
              elseif type(val.type) == "string" then
                  local pos = val.type:find("=")
                  if pos then
                      for k, v in pairs(val.type:toNumMap()) do
                          specials[special_id] = {np= v, amount = val.amount, award = val.award, quality = k, showIndex = val.showIndex}
                      end
                  else
                      specials[special_id] = {np= 1, amount = val.amount, award = val.award, quality = tonumber(val.type), showIndex = val.showIndex}
                  end
              end
          end
      end
  
      --激励奖
      local incentive_ids = ichibankuji["incentive_id"]
      if incentive_ids ~= "" then
          for _, incentive_id in ipairs(incentive_ids:toArray(true, "=")) do
              local val = csvdb["ichibankuji_incentiveCsv"][incentive_id]
              if type(val.type) == "number" then
                  incentive["last"] = {np=val.type, award = val.award}
              elseif type(val.type) == "string" then
                  for k, v in pairs(val.type:toNumMap()) do
                      if k == 2 then
                          incentive["amount"] = {np= v, award = val.award}
                      elseif k==3 then
                          incentive["probabilities"] = {np= v, award = val.award}
                      end
                  end
              end
          end
      end
      --货币类型
      local coin = ichibankuji["token"]:toArray(true, "=")
      self:setProperties({coin = coin[1] or 0, hideTime = ichibankuji.hide_time, goods = goods, specials = specials, incentive = incentive})
  end
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
  function Capsule:isShow()
      if skynet.timex() >= self:getProperty("hideTime") then
          return false
      end
      return true
  end
  
  function Capsule:refreshing(now)
      local id = self:getProperty("id")
      local room = self:getProperty("room")
      local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room]
      local reset = tostring(ichibankuji.reset)
  
      if reset == "0" then
          return false
      elseif reset == "1" then
          if self:getProperty("resetTimes") == 1 then
              return true
          end
          return false
      else
          local resetArr = reset:toArray(true, "=")
          if not next(resetArr) then return false end
  
          if resetArr[1] == "2" then
              if self:getGoodsAmount() > 0 then return false end
  
              local drawEndTime = self:getProperty("drawEndTime") or 0
              if drawEndTime == 0 then return false end
  
              if now - drawEndTime >= resetArr[2] then
                  return true
              end
              return false
  
          elseif resetArr[1] == "3" then
  
          elseif resetArr[1] == "4" then
              if now >= resetArr[2] then return true end
          end
      end
  
      return false
  end
  
555f745e   zhangqijia   feat: 一番赏
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
  function Capsule:getOnlineCount()
      local register = self:getProperty("register") or {}
      local reg, onlookers = 0, 0
      for _, v in pairs(register) do
          if v == 1 then
              reg = reg + 1
          else
              onlookers = onlookers + 1
          end
      end
      return {[0]=onlookers, [1]=reg, [2] = reg+onlookers}
  end
  
  function Capsule:join(roleId)
      --一个房间最多人数 TODO
      local register = self:getProperty("register") or {}
      register[roleId] = 0
      self:setProperty("register", register)
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
201
      return self:data(roleId)
555f745e   zhangqijia   feat: 一番赏
202
203
  end
  
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
204
  function Capsule:getRegisterByRoleId(roleId)
555f745e   zhangqijia   feat: 一番赏
205
      local register = self:getProperty("register") or {}
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
206
207
208
209
210
      return register[roleId] or 0
  end
  
  function Capsule:isRegister(roleId)
      return self:getRegisterByRoleId(roleId) == 1
555f745e   zhangqijia   feat: 一番赏
211
212
213
214
  end
  
  function Capsule:register(roleId)
      local register = self:getProperty("register") or {}
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
215
216
217
      register[roleId] = 1
      self:setProperty("register", register)
      return self:data(roleId)
555f745e   zhangqijia   feat: 一番赏
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
  end
  
  function Capsule:exit(roleId)
      local register = self:getProperty("register") or {}
      if next(register) then
          register[roleId] = nil
          return true
      end
      return false
  end
  
  function Capsule:confirmed(cares)
      local goods = self:getProperty("goods") or {}
      local specials = self:getProperty("specials") or {}
      local change = {}
      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
              end
          else
              if specials[k] and specials[k].amount ~= v.count then
                  change[k] = specials[k].amount
              end
          end
      end
      return change
  end
  
  function Capsule:getGoodsAmount()
      local goods = self:getProperty("goods") or {}
      local amount = 0
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
250
      for _, v in pairs(goods) do
555f745e   zhangqijia   feat: 一番赏
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
          amount = amount + v.amount
      end
      return amount
  end
  
  function Capsule:getSpecialByType(typ)
      local specials = self:getProperty("specials") or {}
      for k, v in pairs(specials) do
          if v.quality == typ then
              return k, v
          end
      end
      return nil
  end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
266
267
  function Capsule:checkSpecialFlag(typ)
      local spKey, special = self:getSpecialByType(typ)
555f745e   zhangqijia   feat: 一番赏
268
269
      if not special then return nil end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
270
271
272
      if special["amount"] <= 0 then return nil end
      return spKey, special
  end
555f745e   zhangqijia   feat: 一番赏
273
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
274
275
276
277
278
  local function getSpecialRoleNotify(rewardRecord, count, award, spKey, typ, now)
      local rewardByRole = {}
      while(count > 0 and next(rewardRecord)) do
          local roleId = math.randWeight(rewardRecord, "amount")
          if roleId then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
279
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
280
281
282
283
              local tmp = rewardRecord[roleId]
              tmp["amount"] = tmp["amount"] - 1
  
              if tmp["amount"] <= 0 then rewardRecord[roleId] = nil end
555f745e   zhangqijia   feat: 一番赏
284
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
285
286
              tmp = rewardByRole[roleId]
              if not tmp  then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
287
288
                  local name = getNameByRoleId(roleId)
                  tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now}
555f745e   zhangqijia   feat: 一番赏
289
              else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
290
                  if not tmp[spKey] then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
291
292
                      local name = getNameByRoleId(roleId)
                      tmp[spKey] = {name = name, good_id = spKey, typ = RewardType.SPECIAL, award = award, amount = 1, quality = typ, create_time= now}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
293
294
295
                  else
                      tmp[spKey].amount = tmp[spKey].amount + 1
                  end
555f745e   zhangqijia   feat: 一番赏
296
              end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
297
              rewardByRole[roleId] = tmp
555f745e   zhangqijia   feat: 一番赏
298
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
299
300
301
302
303
              count = count - 1
          end
      end
      return rewardByRole, count
  end
555f745e   zhangqijia   feat: 一番赏
304
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
305
306
307
308
309
  function Capsule:getTop(record)
      local spKey, special = self:checkSpecialFlag(SpecialType.TOP)
      if not special then return nil end
      local specials = self:getProperty("specials") or {}
      local specialsRecord = self:getProperty("specialsRecord") or {}
555f745e   zhangqijia   feat: 一番赏
310
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
311
312
313
314
315
      if #record < special["np"] then return  nil end
      local topRecord = {}
      local count = special["np"]
      for _, v in ipairs(record) do
          if count <= 0 then break end
555f745e   zhangqijia   feat: 一番赏
316
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
317
318
319
320
321
322
323
          local tmpCount = 0
          if count >= v.amount then
              count = count - v.amount
              tmpCount = v.amount
          else
              tmpCount = count
              count = 0
555f745e   zhangqijia   feat: 一番赏
324
325
          end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
326
327
328
329
330
          if not topRecord[v.roleId]then
              topRecord[v.roleId]  = {amount = v.amount }
          else
              topRecord[v.roleId] = {amount = (topRecord[v.roleId]["amount"] or 0) + tmpCount}
          end
555f745e   zhangqijia   feat: 一番赏
331
332
      end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
333
334
335
336
337
338
339
340
      local rewardByRole, count = getSpecialRoleNotify(topRecord, special["amount"], special["award"], spKey, SpecialType.TOP)
  
      special["amount"] = count
      specials[spKey] = special
      specialsRecord[SpecialType.TOP] = rewardByRole
      self:setProperties({specialsRecord = specialsRecord, specials = specials})
      return rewardByRole
  
555f745e   zhangqijia   feat: 一番赏
341
342
343
344
  end
  
  --TODO
  function Capsule:getCore(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
345
346
      local spKey, special = self:checkSpecialFlag(SpecialType.CORE)
      if not special then return nil end
555f745e   zhangqijia   feat: 一番赏
347
348
  
      local specials = self:getProperty("specials") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
349
350
351
      local specialsRecord = self:getProperty("specialsRecord") or {}
  
      if self:getGoodsAmount() > 0 then return nil end
555f745e   zhangqijia   feat: 一番赏
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
  
      local np = special["np"]
      if np > #record then return nil end
  
  
      local left = math.ceil((np - #record)/2) or 0
      local count = np
      local roleRecord = {}
      for i, v in ipairs(record) do
          if count <= 0 then break end
          if i > left then
              local tmpCount = 0
              if count >= v.amount then
                  count = count - v.amount
                  tmpCount = v.amount
              else
                  tmpCount = count
                  count = 0
              end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
372
373
              if not roleRecord[v.roleId]then
                  roleRecord[v.roleId]  = {amount = v.amount }
555f745e   zhangqijia   feat: 一番赏
374
              else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
375
                  roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount}
555f745e   zhangqijia   feat: 一番赏
376
377
378
379
380
              end
          end
  
      end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
381
      local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.CORE)
555f745e   zhangqijia   feat: 一番赏
382
  
555f745e   zhangqijia   feat: 一番赏
383
384
      special["amount"] = count
      specials[spKey] = special
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
385
386
387
      specialsRecord[SpecialType.CORE] = rewardByRole
      self:setProperties({specialsRecord = specialsRecord, specials = specials})
      return rewardByRole
555f745e   zhangqijia   feat: 一番赏
388
389
390
  end
  
  function Capsule:getLast(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
391
392
      local spKey, special = self:checkSpecialFlag(SpecialType.LAST)
      if not special then return nil end
555f745e   zhangqijia   feat: 一番赏
393
394
  
      local specials = self:getProperty("specials") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
395
      local specialsRecord = self:getProperty("specialsRecord") or {}
555f745e   zhangqijia   feat: 一番赏
396
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
397
      if self:getGoodsAmount() > 0 then return nil end
555f745e   zhangqijia   feat: 一番赏
398
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
399
      table.sort(record, function(a, b) return a.create_time > b.create_time end)
555f745e   zhangqijia   feat: 一番赏
400
401
402
  
      local np = special["np"]
      local count = np
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
403
      local roleRecord = {}
555f745e   zhangqijia   feat: 一番赏
404
405
406
407
408
409
410
411
412
413
414
415
      for _, v in ipairs(record) do
          if count <= 0 then break end
  
          local tmpCount = 0
          if count >= v.amount then
              count = count - v.amount
              tmpCount = v.amount
          else
              tmpCount = count
              count = 0
          end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
416
417
          if not roleRecord[v.roleId]then
              roleRecord[v.roleId]  = {amount = v.amount }
555f745e   zhangqijia   feat: 一番赏
418
          else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
419
              roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + tmpCount}
555f745e   zhangqijia   feat: 一番赏
420
421
422
          end
      end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
423
      local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.LAST)
555f745e   zhangqijia   feat: 一番赏
424
  
555f745e   zhangqijia   feat: 一番赏
425
426
      special["amount"] = count
      specials[spKey] = special
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
427
428
429
      specialsRecord[SpecialType.LAST] = rewardByRole
      self:setProperties({specialsRecord = specialsRecord, specials = specials})
      return rewardByRole
555f745e   zhangqijia   feat: 一番赏
430
431
432
  end
  
  function Capsule:getJoker(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
433
434
      local spKey, special = self:checkSpecialFlag(SpecialType.JOKER)
      if not special then return nil end
555f745e   zhangqijia   feat: 一番赏
435
436
  
      local specials = self:getProperty("specials") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
437
438
439
      local specialsRecord = self:getProperty("specialsRecord") or {}
  
      if self:getGoodsAmount() > 0 then return nil end
555f745e   zhangqijia   feat: 一番赏
440
441
442
  
      local roleRecord = {}
      for _, v in ipairs(record) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
443
444
          if not roleRecord[v.roleId]then
              roleRecord[v.roleId]  = {amount = v.amount }
555f745e   zhangqijia   feat: 一番赏
445
          else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
446
              roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount}
555f745e   zhangqijia   feat: 一番赏
447
448
449
          end
      end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
450
      local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.JOKER)
555f745e   zhangqijia   feat: 一番赏
451
  
555f745e   zhangqijia   feat: 一番赏
452
453
      special["amount"] = count
      specials[spKey] = special
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
454
455
456
      specialsRecord[SpecialType.JOKER] = rewardByRole
      self:setProperties({specialsRecord = specialsRecord, specials = specials})
      return rewardByRole
555f745e   zhangqijia   feat: 一番赏
457
458
459
  end
  
  function Capsule:getKing(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
460
461
      local spKey, special = self:checkSpecialFlag(SpecialType.KING)
      if not special then return nil end
555f745e   zhangqijia   feat: 一番赏
462
463
  
      local specials = self:getProperty("specials") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
464
      local specialsRecord = self:getProperty("specialsRecord") or {}
555f745e   zhangqijia   feat: 一番赏
465
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
466
      if self:getGoodsAmount() > 0 then return nil end
555f745e   zhangqijia   feat: 一番赏
467
468
469
  
      local roleRecord = {}
      for _, v in ipairs(record) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
470
471
          if not roleRecord[v.roleId]then
              roleRecord[v.roleId]  = {amount = v.amount }
555f745e   zhangqijia   feat: 一番赏
472
          else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
473
              roleRecord[v.roleId] = {amount = (roleRecord[v.rol_id]["amount"] or 0) + v.amount}
555f745e   zhangqijia   feat: 一番赏
474
475
476
          end
      end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
477
478
479
480
481
482
483
      local rewardByRole, count = getSpecialRoleNotify(roleRecord, special["amount"], special["award"], SpecialType.KING)
      special["amount"] = count
      specials[spKey] = special
      specialsRecord[SpecialType.KING] = rewardByRole
      self:setProperties({specialsRecord = specialsRecord, specials = specials})
      return rewardByRole
  end
555f745e   zhangqijia   feat: 一番赏
484
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
485
486
487
488
489
490
491
492
493
494
495
496
  local rewardToNtyFunc = function(notify, tmpReward)
      for key, val in pairs(tmpReward or {}) do
          if not notify[key] then
              notify[key] = val
          else
              for k, v in pairs(val) do
                  if not notify[key][k] then
                      notify[key][k] = v
                  else
                      notify[key][k] = notify[key][k].amount + v.amount
                  end
              end
555f745e   zhangqijia   feat: 一番赏
497
  
555f745e   zhangqijia   feat: 一番赏
498
499
          end
      end
555f745e   zhangqijia   feat: 一番赏
500
501
  end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
502
  function Capsule:checkSpecialReward( now)
555f745e   zhangqijia   feat: 一番赏
503
504
505
      local specials = self:getProperty("specials") or {}
      if not next(specials) then return nil end
      local record =  self:getProperty("record") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
506
  
555f745e   zhangqijia   feat: 一番赏
507
508
      if not next(record) then return nil end
      table.sort(record, function(a, b) return a.create_time < b.create_time  end )
555f745e   zhangqijia   feat: 一番赏
509
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
510
511
  
      local notify = self:getTop(record) or {}
555f745e   zhangqijia   feat: 一番赏
512
513
  
      local coreReward = self:getCore(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
514
515
      rewardToNtyFunc(notify, coreReward)
  
555f745e   zhangqijia   feat: 一番赏
516
      local lastReward = self:getLast(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
517
518
      rewardToNtyFunc(notify, lastReward)
  
555f745e   zhangqijia   feat: 一番赏
519
      local jokerReward = self:getJoker(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
520
521
      rewardToNtyFunc(notify, jokerReward)
  
555f745e   zhangqijia   feat: 一番赏
522
      local kingReward = self:getKing(record)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
523
      rewardToNtyFunc(notify, kingReward)
555f745e   zhangqijia   feat: 一番赏
524
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
525
526
      --广播出去TODO
      --rpcRole(roleId, "paySpecialReward", RewardTYpe.JOKER, jokerReward.award)
555f745e   zhangqijia   feat: 一番赏
527
528
529
      return notify
  end
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
530
  function Capsule:checkIncentive(roleId, name, now)
555f745e   zhangqijia   feat: 一番赏
531
      local goods = self:getProperty("goods") or {}
555f745e   zhangqijia   feat: 一番赏
532
533
      local recordByRole = self:getProperty("recordByRole") or {}
      local roleRecord = recordByRole[roleId] or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
534
535
      local incentiveRecord = self:getProperty("incentiveRecord") or {}
      local incentiveByRole = incentiveRecord[roleId] or {}
555f745e   zhangqijia   feat: 一番赏
536
537
538
      local incentive = self:getProperty("incentive")
  
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
539
      local notify = {}
555f745e   zhangqijia   feat: 一番赏
540
541
542
543
544
545
546
547
548
      -- 最后一抽 TODO
      if incentive["last"] then
          local last = true
          for k, v in pairs(goods) do
              if v and v.amount then
                  last = false
                  break
              end
          end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
549
          if last then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
550
               notify["last"] = {name = name, good_id = "last", typ = RewardType.INCENTIVE, award = incentive["last"]["award"], amount = 1, quality = 1, create_time= now}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
551
          end
555f745e   zhangqijia   feat: 一番赏
552
553
554
555
556
557
558
559
560
561
562
563
564
      end
  
      --次数
      if incentive["amount"] then
          local amount = 0
          for _, v in pairs(roleRecord) do
              if (v.calculated or 0) == 0 then
                  amount = amount + v.amount
              end
          end
  
          local count = math.floor(amount / incentive["amount"]["np"])
          local tmpCount = count * incentive["amount"]["np"]
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
565
          notify["amount"] = {name = name, good_id = "amount", typ = RewardType.INCENTIVE, award = incentive["amount"]["award"], amount = count, quality = 2, create_time= now}
555f745e   zhangqijia   feat: 一番赏
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
  
          --填充v.calculated 字段,标识已经用于每x抽的计算中。
          for _, v in pairs(roleRecord) do
              if tmpCount <= 0 then break end
  
              v.calculated = v.calculated or 0
              if v.calculated ~= v.amount then
                  if tmpCount <= v.amount then
                      v.calculated = tmpCount
                      tmpCount = 0
                  else
                      v.calculated = v.amount
                      tmpCount = tmpCount - v.amount
                  end
              end
  
          end
      end
  
      --概率
      if incentive["probabilities"] then
          local probabilities = math.randomInt(1, 100)
          if probabilities <= incentive["probabilities"]["np"] then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
589
              notify["probabilities"] = {name = name, good_id = "probabilities", typ = RewardType.INCENTIVE, award = incentive["probabilities"]["award"], amount = 1, quality = 3, create_time= now}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
590
  
555f745e   zhangqijia   feat: 一番赏
591
592
          end
      end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
593
594
595
      table.insert(incentiveByRole, notify)
      incentiveRecord[roleId] = incentiveByRole
      self:setProperty("incentiveRecord", incentiveRecord)
555f745e   zhangqijia   feat: 一番赏
596
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
597
      return notify
555f745e   zhangqijia   feat: 一番赏
598
599
600
601
602
603
604
605
  end
  
  function Capsule:drawByCount(roleId, count)
      if count <= 0 then return nil end
  
      local goods = self:getProperty("goods") or {}
      local record = self:getProperty("record") or {}
      local recordByRole = self:getProperty("recordByRole") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
606
      local roleRecord = recordByRole[roleId] or {}
555f745e   zhangqijia   feat: 一番赏
607
608
609
610
611
  
      local id = self:getProperty("id")
      local room = self:getProperty("room")
      local ichibankuji = csvdb["ichibankuji_mainCsv"][id][room]
      local goods_id = ichibankuji["goods_id"]
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
612
      local now = skynet.timex()
555f745e   zhangqijia   feat: 一番赏
613
614
  
      --奖励, 通知信息
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
615
616
617
      local  notify= {}
      notify[roleId] = {}
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
618
      local name = getNameByRoleId(roleId)
555f745e   zhangqijia   feat: 一番赏
619
620
621
622
      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 {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
623
              if good and good.amount > 0 then
555f745e   zhangqijia   feat: 一番赏
624
                  good.amount = good.amount - 1
555f745e   zhangqijia   feat: 一番赏
625
626
  
                  --插入记录
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
627
                  local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = 1, quality = good.quality, create_time= now}
555f745e   zhangqijia   feat: 一番赏
628
                  table.insert(record, tmpNotify)
555f745e   zhangqijia   feat: 一番赏
629
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
630
631
632
                  --作为奖励记录+通知
                  if not notify[roleId][good_id] then
                      notify[roleId][good_id] = tmpNotify
555f745e   zhangqijia   feat: 一番赏
633
                  else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
634
                      notify[roleId][good_id].amount = notify[roleId][good_id].amount + 1
555f745e   zhangqijia   feat: 一番赏
635
636
                  end
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
637
638
639
640
641
642
643
644
                  --记录角色的抽奖记录 计算激励奖需要用到
                  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
555f745e   zhangqijia   feat: 一番赏
645
646
647
648
649
                  count = count - 1
              end
          end
  
      end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
650
651
      recordByRole[roleId] = roleRecord
      self:setProperties({recordByRole = recordByRole, record = record, goods = goods})
555f745e   zhangqijia   feat: 一番赏
652
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
653
      local tmpNotify = self:checkIncentive(roleId, name, now)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
654
655
656
657
658
659
      for k, v in pairs(tmpNotify) do
          if not notify[roleId][k] then
              notify[roleId][k] = v
          else
              notify[roleId][k].amount = notify[roleId][k].amount + v.amount
          end
555f745e   zhangqijia   feat: 一番赏
660
      end
555f745e   zhangqijia   feat: 一番赏
661
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
662
663
      local speciNotify = self:checkSpecialReward(now)
      rewardToNtyFunc(notify, speciNotify)
555f745e   zhangqijia   feat: 一番赏
664
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
665
      local reward, rewardByGoods = {}, {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
666
667
      for key, val in pairs(notify) do
          if key == roleId then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
668
              for k, v in pairs(val) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
669
670
671
                  for id, count in pairs(v.award:toNumMap()) do
                      reward[id] = (reward[id] or 0) + count
                  end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
672
                  rewardByGoods[k] = v
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
673
674
              end
          end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
675
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
676
      end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
677
      return reward, rewardByGoods, notify
555f745e   zhangqijia   feat: 一番赏
678
679
680
681
682
683
  end
  
  function Capsule:drawAll(roleId)
      local goods = self:getProperty("goods") or {}
      local record = self:getProperty("record") or {}
      local recordByRole = self:getProperty("recordByRole") or {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
684
685
      local roleRecord = recordByRole[roleId] or {}
      local now = skynet.timex()
555f745e   zhangqijia   feat: 一番赏
686
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
687
      local name = getNameByRoleId(roleId)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
688
689
      local notify = {}
      notify[roleId] = {}
555f745e   zhangqijia   feat: 一番赏
690
      for good_id, good in pairs(goods) do
555f745e   zhangqijia   feat: 一番赏
691
          if good.amount > 0 then
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
692
              --插入记录
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
693
              local tmpNotify = {name= name, good_id = good_id, typ = RewardType.GOODS, award = good.award, amount = good.amount, quality = good.quality, create_time = now}
555f745e   zhangqijia   feat: 一番赏
694
695
              table.insert(record, notify)
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
696
697
698
              --作为奖励记录+通知
              if not notify[roleId][good_id] then
                  notify[roleId][good_id] = tmpNotify
555f745e   zhangqijia   feat: 一番赏
699
              else
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
700
                  notify[roleId][good_id].amount = notify[roleId][good_id].amount + good.award
555f745e   zhangqijia   feat: 一番赏
701
              end
555f745e   zhangqijia   feat: 一番赏
702
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
703
704
705
706
707
              --记录角色的抽奖记录
              if not roleRecord[good_id] then
                  roleRecord[good_id] = notify
              else
                  roleRecord[good_id].amount = roleRecord[good_id].amount + good.amount
555f745e   zhangqijia   feat: 一番赏
708
              end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
709
710
  
              good.amount = 0
555f745e   zhangqijia   feat: 一番赏
711
712
713
          end
  
      end
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
714
715
      recordByRole[roleId] = roleRecord
      self:setProperties({recordByRole = recordByRole, record = record, goods = goods})
555f745e   zhangqijia   feat: 一番赏
716
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
717
      local tmpNotify = self:checkIncentive(roleId, name, now)
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
718
719
720
721
722
723
      for k, v in pairs(tmpNotify) do
          if not notify[roleId][k] then
              notify[roleId][k] = v
          else
              notify[roleId][k].amount = notify[roleId][k].amount + v.amount
          end
555f745e   zhangqijia   feat: 一番赏
724
      end
555f745e   zhangqijia   feat: 一番赏
725
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
726
727
      local speciNotify = self:checkSpecialReward(now)
      rewardToNtyFunc(notify, speciNotify)
555f745e   zhangqijia   feat: 一番赏
728
  
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
729
      local reward, rewardByGoods = {}, {}
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
730
731
      for key, val in pairs(notify) do
          if key == roleId then
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
732
              for k, v in pairs(val) do
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
733
734
735
                  for id, count in pairs(v.award:toNumMap()) do
                      reward[id] = (reward[id] or 0) + count
                  end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
736
                  rewardByGoods[k] = v
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
737
738
              end
          end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
739
  
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
740
      end
cd2a7a67   zhangqijia   fix: 一番赏奖励中加入得奖者的...
741
      return reward, rewardByGoods, notify
555f745e   zhangqijia   feat: 一番赏
742
743
744
745
746
  end
  
  --@param
  --[[
  @roleId
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
747
  @typ 0=独享,1=公开
555f745e   zhangqijia   feat: 一番赏
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
  @cares 关注{k=v}
  ]]--
  
  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 cares then
              local change = self:confirmed(cares)
              if next(change) then return 4, change end
          end
      end
  
      if full == 0 then
          return 5, self:drawByCount(roleId, 1)
      elseif full == 1 then
          return 5, self:drawByCount(roleId, 10)
      elseif full == 2 then
          return 5, self:drawAll(roleId)
      end
  end
  
  
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
774
  function Capsule:data(roleId)
555f745e   zhangqijia   feat: 一番赏
775
776
777
778
779
780
781
      return {
          id = self:getProperty("id"),
          room = self:getProperty("room"),
          typ = self:getProperty("typ"),
          name = self:getProperty("name"),
          coin = self:getProperty("coin"),
          onlineCount = self:getOnlineCount(),
f382b4f2   zhangqijia   fix: 一番赏的抽奖bug
782
          playerStatus = self:getRegisterByRoleId(roleId),
555f745e   zhangqijia   feat: 一番赏
783
          record = self:getProperty("record"),
555f745e   zhangqijia   feat: 一番赏
784
785
786
787
          rank = self:getProperty("rank"),
          goods = self:getProperty("goods"),
          specials = self:getProperty("specials"),
          incentive = self:getProperty("incentive"),
d10a9a36   zhangqijia   fix: 一番赏增加通知信息的数据...
788
          specialsRecord= self:getProperty("specialsRecord"),
555f745e   zhangqijia   feat: 一番赏
789
790
791
792
      }
  end
  
  return Capsule