Blame view

src/models/Activity.lua 10.7 KB
e51ff6d2   zhouhaihai   冒险~
1
  local Activity = class("Activity", require("shared.ModelBase"))
be4e8031   zhouhaihai   活动 拾荒
2
  local string_format = string.format
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
3
  
e51ff6d2   zhouhaihai   冒险~
4
  Activity.ActivityType = {
ea40710f   zhouhaihai   活动
5
  	Sign = 1,  -- 签到
706b517e   liuzujun   翻倍掉落活动,商城免费宝箱
6
  	DoubleDrop = 2, -- 双倍掉落
7f9f002d   liuzujun   循环周活动
7
8
9
10
  	DrawHero = 3,	--抽卡周
  	FoodSell = 4,	--贩卖周
  	OpenBox = 5,	--拆解周
  	AdvDraw = 6,	--拾荒抽周
e51ff6d2   zhouhaihai   冒险~
11
  }
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
12
  
9a1c54b2   zhouhaihai   活动
13
  
e51ff6d2   zhouhaihai   冒险~
14
15
16
17
  local function checkActivityType(activityType)
  	if type(activityType) == "string" then
  		activityType = Activity.ActivityType[activityType]
  	end
ea40710f   zhouhaihai   活动
18
  	return activityType or 0
e51ff6d2   zhouhaihai   冒险~
19
20
21
22
23
24
  end
  
  
  function Activity:ctor(properties)
  	Activity.super.ctor(self, properties)
  
9a1c54b2   zhouhaihai   活动
25
  	self._isOpen = {}
e51ff6d2   zhouhaihai   冒险~
26
  end
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
27
28
  
  
e51ff6d2   zhouhaihai   冒险~
29
  Activity.schema = {
be4e8031   zhouhaihai   活动 拾荒
30
  	actime 		= {"table", {}},  -- 最近检查某项活动的开始时间  {id = time}
7f9f002d   liuzujun   循环周活动
31
  	round		= {"table", {}},  -- 记录活动到了第几轮  {id = roundnum}
ea40710f   zhouhaihai   活动
32
  	act1		= {"table", {}},  -- {0 = day, 1= -1, 2 = -1} == 签到活动
7f9f002d   liuzujun   循环周活动
33
34
35
36
  	act3		= {"table", {}},  -- {0 = 抽卡次数, 1=1, 2=1}  抽卡周活动  1表示领取过该档位的奖励
  	act4		= {"table", {}},  -- {0 = 贩卖数量, 1=1, 2=1}  贩卖周活动  1表示领取过该档位的奖励
  	act5		= {"table", {}},  -- {0 = 拆解数量, 1=1, 2=1}  拆解周活动  1表示领取过该档位的奖励
  	act6		= {"table", {}},  -- {0 = 拾荒消耗远古金币数量, 1=1, 2=1}  拾荒周活动  1表示领取过该档位的奖励
e51ff6d2   zhouhaihai   冒险~
37
  }
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
38
  
e51ff6d2   zhouhaihai   冒险~
39
40
  function Activity:data()
  	return {
be4e8031   zhouhaihai   活动 拾荒
41
  		actime = self:getProperty("actime"),
7f9f002d   liuzujun   循环周活动
42
  		round = self:getProperty("round"),
ea40710f   zhouhaihai   活动
43
  		act1 = self:getProperty("act1"),
7f9f002d   liuzujun   循环周活动
44
45
46
47
  		act3 = self:getProperty("act3"),
  		act4 = self:getProperty("act4"),
  		act5 = self:getProperty("act5"),
  		act6 = self:getProperty("act6"),
e51ff6d2   zhouhaihai   冒险~
48
  	}
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
49
50
  end
  
e51ff6d2   zhouhaihai   冒险~
51
  
9a1c54b2   zhouhaihai   活动
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
  function Activity:updateProperty(params)
  	local type, default = table.unpack(self.schema[params.field])
  
  	if params.delta then
  		self:incrProperty(params.field, params.delta)
  		if not params.notNotify then
  			self.owner:notifyUpdateProperty(params.field, self:getProperty(params.field))
  		end
  		return true
  	end
  	if params.value then
  		self:setProperty(params.field, params.value)
  		if not params.notNotify then
  			self.owner:notifyUpdateProperty(params.field, self:getProperty(params.field))
  		end
  		return true
  	end
  	return false
  end
  
  
ea40710f   zhouhaihai   活动
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  function Activity:isOpenRaw(activityType, now)
  	activityType = checkActivityType(activityType)
  	local actData = csvdb["activity_ctrlCsv"][activityType]
  	if not actData then return end
  
  	if actData.time == "" then -- 关闭
  		return false
  	end
  
  	local st = 0
  	local et = 0
  	local now = skynet.timex()
  
  	if actData.ttype == 0 then -- 时间开放
  		local openTimes = actData.time:toArray(false, "=")
  		if openTimes[1] ~= "0" then
  			st = toUnixtime(openTimes[1]..string_format("%02x", RESET_TIME))
  		end
  		if openTimes[2] ~= "0" then
  			et = toUnixtime(openTimes[2]..string_format("%02x", RESET_TIME))
  		end
   	elseif actData.ttype == 1 then -- 周期开放
   		local openTimes = actData.time:toArray(true, "=")
   		local resetTime = toUnixtime(tostring(openTimes[1]) .. string_format("%02x", RESET_TIME))
   		local r = math.floor((now - resetTime) / (openTimes[3] * 86400))
   		st = resetTime + r * (openTimes[3] * 86400)
   		et = st + openTimes[2] * 86400
  	else
  		return
  	end
  
  	if now >= st and (et == 0 or now < et) then
  		return true, st
  	end
  	return false
  end
  
  -- 缓存开放
9a1c54b2   zhouhaihai   活动
111
112
  function Activity:isOpen(activityType)
  	activityType = checkActivityType(activityType)
ea40710f   zhouhaihai   活动
113
114
115
116
117
118
119
  	return self._isOpen[activityType]
  end
  
  function Activity:getActData(actType)
  	actType = checkActivityType(actType)
  	return self:getProperty("act" .. actType)
  end
9a1c54b2   zhouhaihai   活动
120
  
ea40710f   zhouhaihai   活动
121
122
123
  function Activity:updateActData(actType, data, notNotify)
  	actType = checkActivityType(actType)
  	self:updateProperty({field = "act" .. actType,  value = data, notNotify = notNotify})
9a1c54b2   zhouhaihai   活动
124
125
  end
  
ea40710f   zhouhaihai   活动
126
  
9a1c54b2   zhouhaihai   活动
127
  -- 跨天刷新 --登录刷新
ea40710f   zhouhaihai   活动
128
129
  function Activity:checkActivityStatus(now, isCrossDay, notify)
  	self._isOpen = {}
be4e8031   zhouhaihai   活动 拾荒
130
  	local actime = self:getProperty("actime")
ea40710f   zhouhaihai   活动
131
  	local change = false
be4e8031   zhouhaihai   活动 拾荒
132
  	for actType, actData in pairs(csvdb["activity_ctrlCsv"]) do
ea40710f   zhouhaihai   活动
133
134
135
136
  		local isOpen, startTime = self:isOpenRaw(actType, now)
  		self._isOpen[actType] = isOpen
  
  		if isOpen then
be4e8031   zhouhaihai   活动 拾荒
137
  			if actime[actType] and actime[actType] == startTime then -- 还是之前的状态 开放中
ea40710f   zhouhaihai   活动
138
  			else  -- 重置
be4e8031   zhouhaihai   活动 拾荒
139
  				actime[actType] = startTime
ea40710f   zhouhaihai   活动
140
141
142
143
144
  				self:closeActivity(actType, notify, true)
  				self:initActivity(actType, isCrossDay, notify)
  				change = true
  			end
  		else
be4e8031   zhouhaihai   活动 拾荒
145
  			if actime[actType] then
ea40710f   zhouhaihai   活动
146
  				self:closeActivity(actType, notify)
be4e8031   zhouhaihai   活动 拾荒
147
  				actime[actType] = nil
ea40710f   zhouhaihai   活动
148
149
150
151
152
  				change = true
  			end
  		end
  	end
  	if change then
be4e8031   zhouhaihai   活动 拾荒
153
  		self:updateProperty({field = "actime", value = actime, notNotify = not notify})
ea40710f   zhouhaihai   活动
154
  	end
9a1c54b2   zhouhaihai   活动
155
156
  end
  
ea40710f   zhouhaihai   活动
157
  local activityFunc = {}
9a1c54b2   zhouhaihai   活动
158
  
ea40710f   zhouhaihai   活动
159
160
161
162
163
164
165
166
167
168
169
170
171
  activityFunc[Activity.ActivityType.Sign] = {
  	-- ["check"] = function(self, actType, notify) -- 检查
  	-- end,
  	["init"] = function(self, actType, isCrossDay, notify)
  		if not isCrossDay then
  			activityFunc[Activity.ActivityType.Sign]["crossDay"](self, actType, notify)
  		end
  	end,
  	-- ["close"] = function(self, actType, notify)
  	-- end,
  	["crossDay"] = function(self, actType, notify)
  		local curData = self:getActData(actType)
  		curData[0] = (curData[0] or 0) + 1
be4e8031   zhouhaihai   活动 拾荒
172
  		local actData = csvdb["new_signInCsv"]
ea40710f   zhouhaihai   活动
173
174
175
176
177
178
  		if curData[0] > #actData then return end -- 满了就忽略了
  
  		-- 没满更新一下
  		self:updateActData(actType, curData, not notify)
  	end,
  }
9a1c54b2   zhouhaihai   活动
179
  
7f9f002d   liuzujun   循环周活动
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
  --loop1:累计料理贩卖N次
  --loop2:累计招募N次
  --loop3:累计资助N次
  --loop4:时钟箱拆解N个
  function Activity:checkWeeklyAct(actType, notify, count)
  		local actInfoMap = {
  			[Activity.ActivityType.DrawHero] = {mailId = MailId.ActDrawCardReward, table = "activity_loop2Csv"},
  			[Activity.ActivityType.AdvDraw] = {mailId = MailId.ActAdvDrawReward, table = "activity_loop3Csv"},
  			[Activity.ActivityType.OpenBox] = {mailId = MailId.ActOpenBoxReward, table = "activity_loop4Csv"},
  			[Activity.ActivityType.FoodSell] = {mailId = MailId.ActSellFoodReward, table = "activity_loop1Csv"}
  		}
  		local info = actInfoMap[actType]
  		if not info then return end
  
  		local curData = self:getActData(actType)
  		local roundData = self:getProperty("round")
  		local curRound = roundData[actType] or 0
  		local ctrlData = csvdb["activity_ctrlCsv"][actType]
  		if not ctrlData then return end
  		if curRound >= ctrlData.condition then
  			return
  		end
  		curData[0] = (curData[0] or 0) + count
  		local totalCnt = 0
  		local finishCnt = 0
  		local maxCondition = 0
  		for k, cfg in pairs(csvdb[info.table] or {}) do
  			totalCnt = totalCnt + 1
  			if maxCondition < cfg.condition1 then
  				maxCondition = cfg.condition1
  			end
  			if not curData[cfg.id] and curData[0] >= cfg.condition1 then
  				if info.mailId then 
  					self.owner:sendMail(info.mailId, nil, cfg.reward, {cfg.condition1})
  					curData[cfg.id] = 1
  				end
  			end
  			if curData[cfg.id] then
  				finishCnt = finishCnt + 1
  			end
  		end
  		if totalCnt == finishCnt then
  			roundData[actType] = curRound + 1
  			for k,v in pairs(curData) do
  				if k == 0 then
  					curData[k] = curData[0] >= maxCondition and curData[0] - maxCondition or 0
  				else
  					curData[k] = nil
  				end
  			end
  			self:updateProperty({field = "round", value = roundData, notNotify = not notify})
  		end
  		self:updateActData(actType, curData, not notify)
  end
  
  -- 抽卡周
  activityFunc[Activity.ActivityType.DrawHero] = {
  	["check"] = function(self, actType, notify, count) -- 检查
  		self:checkWeeklyAct(actType, notify, count)
  	end,
  	["init"] = function(self, actType, isCrossDay, notify)
  		local roundData = self:getProperty("round")
  		roundData[actType] = 0
  		self:updateProperty({field = "round",  value = roundData, notNotify = not notify})
  	end,
  	-- ["close"] = function(self, actType, notify)
  	-- end,
  	["crossDay"] = function(self, actType, notify)
  		print("cross day draw card")
  		self.owner:sendMail(MailId.ActDrawCard)
  	end,
  }
  
  -- 售卖周
  activityFunc[Activity.ActivityType.FoodSell] = {
  	["check"] = function(self, actType, notify, count) -- 检查
  		self:checkWeeklyAct(actType, notify, count)
  	end,
  	["init"] = function(self, actType, isCrossDay, notify)
  		local roundData = self:getProperty("round")
  		roundData[actType] = 0
  		self:updateProperty({field = "round",  value = roundData, notNotify = not notify})
  	end,
  	-- ["close"] = function(self, actType, notify)
  	-- end,
  	["crossDay"] = function(self, actType, notify)
  		print("cross day sell food")
  		self.owner:sendMail(MailId.ActSellFood)
  	end,
  }
  
  -- 拾荒周
  activityFunc[Activity.ActivityType.AdvDraw] = {
  	["check"] = function(self, actType, notify, count) -- 检查
  		self:checkWeeklyAct(actType, notify, count)
  	end,
  	["init"] = function(self, actType, isCrossDay, notify)
  		local roundData = self:getProperty("round")
  		roundData[actType] = 0
  		self:updateProperty({field = "round",  value = roundData, notNotify = not notify})
  	end,
  	-- ["close"] = function(self, actType, notify)
  	-- end,
  	["crossDay"] = function(self, actType, notify)
  		print("cross day act adv cost")
  		self.owner:sendMail(MailId.ActAdvDraw)
  	end,
  }
  
  -- 拆解周
  activityFunc[Activity.ActivityType.OpenBox] = {
  	["check"] = function(self, actType, notify, count) -- 检查
  		self:checkWeeklyAct(actType, notify, count)
  	end,
  	["init"] = function(self, actType, isCrossDay, notify)
  		local roundData = self:getProperty("round")
  		roundData[actType] = 0
  		self:updateProperty({field = "round",  value = roundData, notNotify = not notify})
  	end,
  	-- ["close"] = function(self, actType, notify)
  	-- end,
  	["crossDay"] = function(self, actType, notify)
  		print("cross day open box")
  		self.owner:sendMail(MailId.ActOpenBox)
  	end,
  }
  
ea40710f   zhouhaihai   活动
307
308
309
310
311
312
313
314
315
316
  function Activity:initActivity(actType, isCrossDay, notify)
  	if activityFunc[actType] and activityFunc[actType]['close'] then
  		activityFunc[actType]["init"](self, actType, isCrossDay, notify)
  	end
  end
  
  function Activity:closeActivity(actType, notify, notUpdateAct)
  	if activityFunc[actType] and activityFunc[actType]['close'] then
  		activityFunc[actType]["close"](self, actType, notify)
  	end
7f9f002d   liuzujun   循环周活动
317
318
319
  	if Activity.schema["act".. actType] then
  		self:updateActData(actType, Activity.schema["act" .. actType][2], not notify or notUpdateAct)
  	end
ea40710f   zhouhaihai   活动
320
321
322
323
324
325
326
327
328
329
  end
  
  function Activity:refreshDailyData(notify)
  	for actType, status in pairs(self._isOpen) do
  		if status then
  			if activityFunc[actType] and activityFunc[actType]['crossDay'] then
  				activityFunc[actType]["crossDay"](self, actType, notify)
  			end
  		end
  	end
9a1c54b2   zhouhaihai   活动
330
331
  end
  
ea40710f   zhouhaihai   活动
332
  function Activity:checkActivity(notNotify, activityType, ...)
9a1c54b2   zhouhaihai   活动
333
  	if not activityType then return end
ea40710f   zhouhaihai   活动
334
335
336
  	if not self:isOpen(activityType) then return end
  	if activityFunc[activityType] and activityFunc[activityType]['check'] then
  		activityFunc[activityType]["check"](self, activityType, not notNotify, ...)
9a1c54b2   zhouhaihai   活动
337
338
339
  	end
  end
  
706b517e   liuzujun   翻倍掉落活动,商城免费宝箱
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
  -- 获取此次挂机掉落翻倍时长
  function Activity:getActHangDoubleTime(lastTs, nowTs)
  	local type = "DoubleDrop"
  	local actId = checkActivityType(type)
  	local isOpen = self:isOpen(type)
  	local openTs = self:getProperty("actime")[actId] or 0
  	local timeNow = skynet.timex()
  	lastTs =  math.max(lastTs, openTs)
  	if isOpen then
  		if nowTs > openTs then
  			return nowTs - lastTs
  		else
  			return 0
  		end
  	end
  	return 0
  end
  
e51ff6d2   zhouhaihai   冒险~
358
  
706b517e   liuzujun   翻倍掉落活动,商城免费宝箱
359
  return Activity