-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathHonorAssistHourlyCalculator.lua
69 lines (56 loc) · 2.2 KB
/
HonorAssistHourlyCalculator.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
local addonName, addonTable = ...
HonorAssist = addonTable
local hourlyData = {}
local totalHonor = 0
local totalKills = 0
local startTimeEpoch = nil
function HonorAssist:LoadHourlySettings()
hourlyData = {}
totalHonor = 0
startTimeEpoch = HonorAssist:GetPreviousHourTimeEpoch()
end
function HonorAssist:GetHourlyStartTimeEpoch()
return startTimeEpoch
end
-- Passing in dishonorable kill information will probably not work as expected. We don't really care about dishonorable kills right now.
-- Mainly because we would rather not test what a dishonorable kill event looks like due to the in-game ramifications.
function HonorAssist:AddToHourlyDatabase(honorGained, timeKilledUtc)
local timeKilledEpoch = HonorAssist:DatabaseTimeUtcToEpochTime(timeKilledUtc)
-- If there is no or negative honor then return.
if (honorGained <= 0) then
return totalHonor
end
-- If time killed is less than the hour we are currently tracking then ignore.
if (timeKilledEpoch < startTimeEpoch - (60 * 60)) then
return totalHonor
end
-- Instantiate table for time if it doesn't exist.
if (hourlyData[timeKilledEpoch] == nil) then
hourlyData[timeKilledEpoch] = {}
end
table.insert(hourlyData[timeKilledEpoch], { Honor = honorGained })
totalHonor = totalHonor + honorGained
HonorAssist:UpdateHourlyHonor(totalHonor)
return totalHonor
end
-- Update start time and remove data that has fallen out of the previous hour window.
function HonorAssist:RecalculateHourlyData()
startTimeEpoch = HonorAssist:GetPreviousHourTimeEpoch()
local deleteData = {}
-- Inefficient but I'm sleepy.
-- TODO: Make this O(n), not 2 * O(n). Stop requiring an extra table to store values to be deleted.
-- Goes through list the first time and removes out of date honor from the totals.
for timeKilledEpoch, honorEntries in pairs(hourlyData) do
if (timeKilledEpoch < startTimeEpoch) then
for index, honor in pairs(honorEntries) do
totalHonor = totalHonor - honor.Honor
end
table.insert(deleteData, timeKilledEpoch)
end
end
-- Goes thorugh list the second time and sets those keys to nil to remove from table.
for _, timeKilledEpoch in pairs(deleteData) do
hourlyData[timeKilledEpoch] = nil
end
HonorAssist:UpdateHourlyHonor(totalHonor)
end