-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.lua
executable file
·186 lines (142 loc) · 5.68 KB
/
Core.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
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
local f = CreateFrame("Frame")
SLASH_DEATHCOUNTER1 = "/deathcounter"
SLASH_DEATHCOUNTER2 = "/dcr"
local defaults = {
deaths = 0,
deathsToday = 0,
lastDeath = time(),
lastDayDied = date("%d"),
guildAnnounce = false,
usePopupAlert = false,
}
function f:OnEvent(event, addOnName)
if addOnName == "Death_Counter" then
--print("Death Counter Loaded")
FADeathCounterDB = FADeathCounterDB or CopyTable(defaults)
self:InitializeOptions()
end
if event == "PLAYER_DEAD" then
currentTime = time()
deathTimeString = self:CalculateTimesinceDeath(FADeathCounterDB.lastDeath, currentTime)
self:RecordDeathTime(currentTime)
self:CountDeath()
self:CountDeathsToday()
self:PrintDeaths(deathTimeString)
self:AnnounceToGuild(deathTimeString)
end
end
f:RegisterEvent("ADDON_LOADED")
f:RegisterEvent("PLAYER_DEAD")
f:SetScript("OnEvent", f.OnEvent)
function f:InitializeOptions()
self.panel = CreateFrame("Frame", "FADeathCounterOptionsPanel", UIParent)
self.panel.name = "Death Counter"
local cb = CreateFrame("CheckButton", "FADeathCounterUsePopupAlert", self.panel, "InterfaceOptionsCheckButtonTemplate")
cb:SetPoint("TOPLEFT", 20, -20)
_G[cb:GetName() .. "Text"]:SetText("Use a pop up alert")
cb:HookScript("OnClick", function(_, btn, down)
FADeathCounterDB.usePopupAlert = cb:GetChecked()
end)
cb:SetChecked(FADeathCounterDB.usePopupAlert)
local cbGuildAnnounce = CreateFrame("CheckButton", "FADeathCounterGuildAnnounce", self.panel, "InterfaceOptionsCheckButtonTemplate")
cbGuildAnnounce:SetPoint("TOPLEFT", 20, -60)
_G[cbGuildAnnounce:GetName() .. "Text"]:SetText("Announce Deaths to Your Guild")
cbGuildAnnounce:HookScript("OnClick", function(_, btn, down)
FADeathCounterDB.guildAnnounce = cbGuildAnnounce:GetChecked()
end)
cbGuildAnnounce:SetChecked(FADeathCounterDB.guildAnnounce)
local category = Settings.RegisterCanvasLayoutCategory(self.panel, self.panel.name)
Settings.RegisterAddOnCategory(category)
end
function f:CountDeath()
FADeathCounterDB.lastDeath = time()
FADeathCounterDB.deaths = FADeathCounterDB.deaths + 1
end
function f:CountDeathsToday()
today = date("%d")
if (today ~= FADeathCounterDB.lastDayDied) then
FADeathCounterDB.lastDayDied = today
FADeathCounterDB.deathsToday = 0
end
FADeathCounterDB.deathsToday = FADeathCounterDB.deathsToday + 1
end
function f:PrintDeaths(deathTimeString)
print("You have died " .. FADeathCounterDB.deaths .. " times!")
print("You died " .. FADeathCounterDB.deathsToday .. " times today.")
if (FADeathCounterDB.deaths == 1) then
useText = "Started Tracking "
else
useText = "Last death was "
end
messageText = useText .. deathTimeString .. "ago"
print(messageText)
if (FADeathCounterDB.usePopupAlert == true) then
message("You died " .. FADeathCounterDB.deaths .. " times.\n\n" .. messageText)
end
end
function f:RecordDeathTime(currentTime)
FADeathCounterDB.lastDeath = currentTime
end
function f:CalculateTimesinceDeath(lastDiedTimeStamp, currentTimeStamp)
secondsInAMinute = 60
secondsInAnHour = secondsInAMinute * 60
secondsInADay = secondsInAnHour * 24
returnText = ""
diff = currentTimeStamp - lastDiedTimeStamp
daysAgo = math.floor(diff / secondsInADay)
if (daysAgo >= 1) then
returnText = daysAgo .. " days "
diff = diff - (secondsInADay * daysAgo)
end
hoursAgo = math.floor(diff / secondsInAnHour)
if (hoursAgo >= 1) then
returnText = returnText .. hoursAgo .. " hours "
diff = diff - (secondsInAnHour * hoursAgo)
end
minutesAgo = math.floor(diff / secondsInAMinute)
if (minutesAgo >= 1) then
returnText = returnText .. minutesAgo .. " minutes "
diff = diff - (secondsInAMinute * minutesAgo)
end
returnText = returnText .. diff .. " seconds "
return returnText
end
function f:ShouldAnnounceToGuild()
shouldAnnounce = false
if (FADeathCounterDB.guildAnnounce and IsInGuild()) then
shouldAnnounce = true
end
return shouldAnnounce
end
function f:AnnounceToGuild(deathTimeString)
if (f:ShouldAnnounceToGuild() == true) then
playerName = f:GetPlayerName()
SendChatMessage(playerName .. " just died! They died " .. FADeathCounterDB.deathsToday .. " times today. " .. playerName .. "'s last death was " .. deathTimeString .. "ago.", "GUILD")
end
end
function f:GetPlayerName()
playerName, playerRealm = UnitName("player")
return playerName
end
SlashCmdList.DEATHCOUNTER = function(msg)
if msg == "config" then
InterfaceOptionsFrame_OpenToCategory(f.panel)
end
if msg == "help" then
print("/dcr -- Show your death count stats")
print("/dcr help -- Show this help message")
print("You can use /deathcounter instead of /dcr")
print("For support, go to https://github.com/FinalAsgard/WoW-Death_Counter")
end
if msg == "reset confirm" then
FADeathCounterDB = CopyTable(defaults)
print("DB has been reset to default values")
end
if msg == "" or msg == nil then
print("You have died " .. FADeathCounterDB.deaths .. " times.")
print("You died " .. FADeathCounterDB.deathsToday .. " times today.")
if (FADeathCounterDB.deaths > 0) then
print("Last died " .. f:CalculateTimesinceDeath(FADeathCounterDB.lastDeath, time()))
end
end
end