-
Notifications
You must be signed in to change notification settings - Fork 0
/
Session.lua
96 lines (77 loc) · 2 KB
/
Session.lua
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
Session = {}
Session.__index = Session
function Session:new(db, interval)
local self = {}
setmetatable(self, Session)
self.db = db
self.db.lastCheckTime = nil
self.db.interval = interval
self.db.checkID = nil
self.db.checks = {}
return self
end
function Session:load(db)
local self = {}
setmetatable(self, Session)
self.db = db
return self
end
function Session:CheckHasEnded(time)
return time > self:GetNextCheckTime()
end
function Session:GetNextCheckTime()
local nextCheckTime
local interval = self.db.interval * 60
if self.db.lastCheckTime then
nextCheckTime = self.db.lastCheckTime + interval
else
nextCheckTime = self.db.startTime + interval
end
return nextCheckTime
end
function Session:NewCheckDB(time)
local id = timeToID(time)
self.db.checks[id] = {
time = time,
players = {},
}
return id, db
end
function Session:GetCheckDB(id)
return self.db.checks[id]
end
function Session:GetCurrentCheckDB()
if self.db.checkID then return self:GetCheckDB(self.db.checkID) end
end
function Session:AddPlayerToCurrentCheck(time, id, name, note)
local db = self:GetCurrentCheckDB()
if db then
db.players[id] = {
time = time,
name = name,
note = note,
}
end
end
function Session:PlayersForCurrentCheck()
local db = self:GetCurrentCheckDB()
if db then return db.players else return {} end
end
function Session:NewCheck(time, players, type)
local id, db = self:NewCheckDB(time)
self.db.checkID = id
for idx, player in ipairs(players) do
self:AddPlayerToCurrentCheck(time, player.id, player.name)
end
self.db.lastCheckTime = time
end
function Session:PeriodicCheck(time, players)
self:NewCheck(time, players, "PERIODIC")
end
function Session:InitialCheck(players)
self:NewCheck(self.db.startTime, players, "INITIAL")
end
function Session:End(time)
self.db.checkID = nil
self.db.endTime = time
end