Activity.lua
5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
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
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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
local Activity = class("Activity", require("shared.ModelBase"))
Activity.ActivityType = {
Sign = 1, -- 签到
}
local function checkActivityType(activityType)
if type(activityType) == "string" then
activityType = Activity.ActivityType[activityType]
end
return activityType or 0
end
function Activity:ctor(properties)
Activity.super.ctor(self, properties)
self._isOpen = {}
end
Activity.schema = {
ctime = {"table", {}}, -- 最近检查某项活动的开始时间 {id = time}
act1 = {"table", {}}, -- {0 = day, 1= -1, 2 = -1} == 签到活动
}
function Activity:data()
return {
act1 = self:getProperty("act1"),
}
end
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
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
-- 缓存开放
function Activity:isOpen(activityType)
activityType = checkActivityType(activityType)
return self._isOpen[activityType]
end
function Activity:getActData(actType)
actType = checkActivityType(actType)
return self:getProperty("act" .. actType)
end
function Activity:updateActData(actType, data, notNotify)
actType = checkActivityType(actType)
self:updateProperty({field = "act" .. actType, value = data, notNotify = notNotify})
end
-- 跨天刷新 --登录刷新
function Activity:checkActivityStatus(now, isCrossDay, notify)
self._isOpen = {}
local ctime = self:getProperty("ctime")
local change = false
for actType, actData in paris(csvdb["activity_ctrlCsv"]) do
local isOpen, startTime = self:isOpenRaw(actType, now)
self._isOpen[actType] = isOpen
if isOpen then
if ctime[actType] and ctime[actType] == startTime then -- 还是之前的状态 开放中
else -- 重置
ctime[actType] = startTime
self:closeActivity(actType, notify, true)
self:initActivity(actType, isCrossDay, notify)
change = true
end
else
if ctime[actType] then
self:closeActivity(actType, notify)
ctime[actType] = nil
change = true
end
end
end
if change then
self:setProperty("ctime", ctime)
end
end
local activityFunc = {}
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
local actData = csvdb["new_signlnCsv"]
if curData[0] > #actData then return end -- 满了就忽略了
-- 没满更新一下
self:updateActData(actType, curData, not notify)
end,
}
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
end
function Activity:checkActivity(notNotify, activityType, ...)
if not activityType then return end
if not self:isOpen(activityType) then return end
if activityFunc[activityType] and activityFunc[activityType]['check'] then
activityFunc[activityType]["check"](self, activityType, not notNotify, ...)
end
end
return Activity