Blame view

src/models/Activity.lua 5.28 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,  -- 签到
e51ff6d2   zhouhaihai   冒险~
6
  }
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
7
  
9a1c54b2   zhouhaihai   活动
8
  
e51ff6d2   zhouhaihai   冒险~
9
10
11
12
  local function checkActivityType(activityType)
  	if type(activityType) == "string" then
  		activityType = Activity.ActivityType[activityType]
  	end
ea40710f   zhouhaihai   活动
13
  	return activityType or 0
e51ff6d2   zhouhaihai   冒险~
14
15
16
17
18
19
  end
  
  
  function Activity:ctor(properties)
  	Activity.super.ctor(self, properties)
  
9a1c54b2   zhouhaihai   活动
20
  	self._isOpen = {}
e51ff6d2   zhouhaihai   冒险~
21
  end
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
22
23
  
  
e51ff6d2   zhouhaihai   冒险~
24
  Activity.schema = {
be4e8031   zhouhaihai   活动 拾荒
25
  	actime 		= {"table", {}},  -- 最近检查某项活动的开始时间  {id = time}
ea40710f   zhouhaihai   活动
26
  	act1		= {"table", {}},  -- {0 = day, 1= -1, 2 = -1} == 签到活动
e51ff6d2   zhouhaihai   冒险~
27
  }
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
28
  
e51ff6d2   zhouhaihai   冒险~
29
30
  function Activity:data()
  	return {
be4e8031   zhouhaihai   活动 拾荒
31
  		actime = self:getProperty("actime"),
ea40710f   zhouhaihai   活动
32
  		act1 = self:getProperty("act1"),
e51ff6d2   zhouhaihai   冒险~
33
  	}
be83d162   zhouahaihai   登陆成功。 增加数据结构修正功能
34
35
  end
  
e51ff6d2   zhouhaihai   冒险~
36
  
9a1c54b2   zhouhaihai   活动
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
  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   活动
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
  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   活动
96
97
  function Activity:isOpen(activityType)
  	activityType = checkActivityType(activityType)
ea40710f   zhouhaihai   活动
98
99
100
101
102
103
104
  	return self._isOpen[activityType]
  end
  
  function Activity:getActData(actType)
  	actType = checkActivityType(actType)
  	return self:getProperty("act" .. actType)
  end
9a1c54b2   zhouhaihai   活动
105
  
ea40710f   zhouhaihai   活动
106
107
108
  function Activity:updateActData(actType, data, notNotify)
  	actType = checkActivityType(actType)
  	self:updateProperty({field = "act" .. actType,  value = data, notNotify = notNotify})
9a1c54b2   zhouhaihai   活动
109
110
  end
  
ea40710f   zhouhaihai   活动
111
  
9a1c54b2   zhouhaihai   活动
112
  -- 跨天刷新 --登录刷新
ea40710f   zhouhaihai   活动
113
114
  function Activity:checkActivityStatus(now, isCrossDay, notify)
  	self._isOpen = {}
be4e8031   zhouhaihai   活动 拾荒
115
  	local actime = self:getProperty("actime")
ea40710f   zhouhaihai   活动
116
  	local change = false
be4e8031   zhouhaihai   活动 拾荒
117
  	for actType, actData in pairs(csvdb["activity_ctrlCsv"]) do
ea40710f   zhouhaihai   活动
118
119
120
121
  		local isOpen, startTime = self:isOpenRaw(actType, now)
  		self._isOpen[actType] = isOpen
  
  		if isOpen then
be4e8031   zhouhaihai   活动 拾荒
122
  			if actime[actType] and actime[actType] == startTime then -- 还是之前的状态 开放中
ea40710f   zhouhaihai   活动
123
  			else  -- 重置
be4e8031   zhouhaihai   活动 拾荒
124
  				actime[actType] = startTime
ea40710f   zhouhaihai   活动
125
126
127
128
129
  				self:closeActivity(actType, notify, true)
  				self:initActivity(actType, isCrossDay, notify)
  				change = true
  			end
  		else
be4e8031   zhouhaihai   活动 拾荒
130
  			if actime[actType] then
ea40710f   zhouhaihai   活动
131
  				self:closeActivity(actType, notify)
be4e8031   zhouhaihai   活动 拾荒
132
  				actime[actType] = nil
ea40710f   zhouhaihai   活动
133
134
135
136
137
  				change = true
  			end
  		end
  	end
  	if change then
be4e8031   zhouhaihai   活动 拾荒
138
  		self:updateProperty({field = "actime", value = actime, notNotify = not notify})
ea40710f   zhouhaihai   活动
139
  	end
9a1c54b2   zhouhaihai   活动
140
141
  end
  
ea40710f   zhouhaihai   活动
142
  local activityFunc = {}
9a1c54b2   zhouhaihai   活动
143
  
ea40710f   zhouhaihai   活动
144
145
146
147
148
149
150
151
152
153
154
155
156
  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   活动 拾荒
157
  		local actData = csvdb["new_signInCsv"]
ea40710f   zhouhaihai   活动
158
159
160
161
162
163
  		if curData[0] > #actData then return end -- 满了就忽略了
  
  		-- 没满更新一下
  		self:updateActData(actType, curData, not notify)
  	end,
  }
9a1c54b2   zhouhaihai   活动
164
  
ea40710f   zhouhaihai   活动
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
  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
  	self:updateActData(actType, Activity.schema["act" .. actType][2], not notify or notUpdateAct)
  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   活动
186
187
  end
  
ea40710f   zhouhaihai   活动
188
  function Activity:checkActivity(notNotify, activityType, ...)
9a1c54b2   zhouhaihai   活动
189
  	if not activityType then return end
ea40710f   zhouhaihai   活动
190
191
192
  	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   活动
193
194
195
  	end
  end
  
e51ff6d2   zhouhaihai   冒险~
196
197
  
  return Activity