-
Notifications
You must be signed in to change notification settings - Fork 0
/
WereWatch.lua
210 lines (199 loc) · 7.55 KB
/
WereWatch.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
-- WereWatch
-- by gobbo (@gobbo1008)
WereWatch = {}
WereWatch.name = "WereWatch"
WereWatch.version = 1.3
function WereWatch:Initialize()
self.savedVariables = ZO_SavedVars:New("WereWatchSavedVariables", 1, nil, {})
self.werewolf = IsWerewolf()
local left = self.savedVariables.left
local top = self.savedVariables.top
if self.savedVariables.bestTime == nil then
self.savedVariables.bestTime = 0
end -- if
WereWatchUI:ClearAnchors()
WereWatchUI:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
WereWatch.evalOptions()
-- Here be LibAddonMenu dragons
local LAM = LibStub:GetLibrary("LibAddonMenu-2.0")
local panelData = {
type = "panel",
name = "WereWatch",
author = "gobbo",
version = "" .. WereWatch.version,
registerForRefresh = true,
}
LAM:RegisterAddonPanel("WereWatchOptions", panelData)
local optionsData = {
[1] = {
type = "checkbox",
name = "Enable timer",
tooltip = "Enables the timer UI element.",
getFunc = function() return WereWatch.savedVariables.optionsTimer end,
setFunc = function(value)
WereWatch.savedVariables.optionsTimer = value
WereWatch.evalOptions()
if not value then
WereWatchUI:SetHidden(true)
end
end,
},
[2] = {
type = "checkbox",
name = "Enable chat message",
tooltip = "Enables the chat message at the end of the werewolf run.",
getFunc = function() return WereWatch.savedVariables.optionsMessage end,
setFunc = function(value) WereWatch.savedVariables.optionsMessage = value end,
},
[3] = {
type = "checkbox",
name = "Force-show timer",
tooltip = "Force-shows the timer to allow you to place it where you want it. Will auto-hide on next transformation back to human.",
getFunc = function() return not WereWatchUI:IsHidden() end,
setFunc = function(value) WereWatchUI:SetHidden(not value) end,
},
[4] = {
type = "description",
-- width = "half",
reference = "WereWatchOptionsBestTime",
title = "Current best time: " .. WereWatch.ToMinSec(WereWatch.savedVariables.bestTime),
},
[5] = {
type = "button",
width = "half",
name = "Reset best time",
tooltip = "Resets your best time.",
func = function() WereWatch.savedVariables.bestTime = 0 end,
},
[6] = {
type = "button",
width = "half",
name = "Reset timer position",
tooltip = "Should you have accidentally moved the timer somwhere you can't reach it, this will reset it to the upper left corner of the screen.",
func = function()
WereWatchUI:ClearAnchors()
WereWatchUI:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, 0, 0)
end,
},
[7] = {
-- Helper control to refresh the best time label. Not sure if this is neccessary, but it works.
-- Please open an issue or a pull request if there is a better or easier way to do it, I'm new to this stuff
type = "custom",
refreshFunc = function()
WereWatchOptionsBestTime.data.title = "Current best time: " .. WereWatch.ToMinSec(WereWatch.savedVariables.bestTime)
WereWatchOptionsBestTime.UpdateValue(WereWatchOptionsBestTime)
end,
},
}
LAM:RegisterOptionControls("WereWatchOptions", optionsData)
end -- function
-- Load handler
function WereWatch.OnAddOnLoaded(event, addonName)
if addonName == WereWatch.name then
WereWatch:Initialize()
end -- if
end -- function
local timelastrun = 0
function WereWatch.OnUpdate(timerun)
if WereWatch.running then
if (timerun - timelastrun) >= 0.1 then
timelastrun = timerun
WereWatchUILabel:SetText(WereWatch.ToMinSec(GetTimeStamp() - WereWatch.startTime))
end -- if timerun - timelastrun
elseif ((timerun - timelastrun) >= 5) and ((timerun - timelastrun) <= 6) then
WereWatchUI:SetHidden(true)
end -- if WereWatch.running
end -- function
function WereWatch.OnIndicatorMoveStop()
WereWatch.savedVariables.left = WereWatchUI:GetLeft()
WereWatch.savedVariables.top = WereWatchUI:GetTop()
end -- function
function WereWatch.OnWerewolfStateChanged(eventCode, werewolf)
if werewolf ~= WereWatch.werewolf then
-- state changed from previous
WereWatch.werewolf = werewolf
if werewolf then
-- player turned into werewolf
-- start the stopwatch
WereWatch.startTime = GetTimeStamp()
WereWatch.running = true
if (WereWatch.savedVariables.optionsTimer) then
WereWatchUI:SetHidden(false)
end -- if
else
-- player turned into human again
-- stop the stopwatch
WereWatch.stopTime = GetTimeStamp()
WereWatch.running = false
-- calculate time difference
WereWatch.deltaTime = GetDiffBetweenTimeStamps(WereWatch.stopTime, WereWatch.startTime)
-- check for best time
if WereWatch.savedVariables.bestTime ~= nil then
if WereWatch.deltaTime > WereWatch.savedVariables.bestTime then
-- New best time!
if (WereWatch.savedVariables.optionsMessage) then
d("[WereWatch] You held your werewolf form for ".. WereWatch.ToMinSec(WereWatch.deltaTime) .. ". This beats your previous best time of " .. WereWatch.ToMinSec(WereWatch.savedVariables.bestTime) .. "!")
end
WereWatch.savedVariables.bestTime = WereWatch.deltaTime
elseif WereWatch.deltaTime <= WereWatch.savedVariables.bestTime then
-- No new best time.
if (WereWatch.savedVariables.optionsMessage) then
d("[WereWatch] You held your werewolf form for " .. WereWatch.ToMinSec(WereWatch.deltaTime) .. ". Your best time is " .. WereWatch.ToMinSec(WereWatch.savedVariables.bestTime) .. ".")
end
end -- if WereWatch.deltaTime
else
if (WereWatch.savedVariables.optionsMessage) then
d("[WereWatch] You held your werewolf form for ".. WereWatch.ToMinSec(WereWatch.deltaTime) .. ". No previous best time found.")
end
WereWatch.savedVariables.bestTime = WereWatch.deltaTime
end -- if WereWatch.savedVariables.bestTime
end -- if werewolf
end -- if werewolf ~= WereWatch.werewolf
end -- function
function WereWatch.OnLinkedWorldPositionChanged(eventCode)
if WereWatch.werewolf then
WereWatch.OnWerewolfStateChanged(eventCode, IsWerewolf())
end -- if
end -- function
function WereWatch.OnPlayerDead(eventCode)
if WereWatch.werewolf then
WereWatch.OnWerewolfStateChanged(eventCode, IsWerewolf())
end -- if
end -- function
function WereWatch.evalOptions()
if WereWatch.savedVariables.optionsTimer == nil then
WereWatch.savedVariables.optionsTimer = true
end
if WereWatch.savedVariables.optionsMessage == nil then
WereWatch.savedVariables.optionsMessage = true
end
end -- function
function WereWatch.ToMinSec(timestamp)
timestamp = math.floor(timestamp)
local minutes = math.floor(timestamp/60)
if minutes < 10 then
minutes = "0" .. minutes
end -- if
local seconds = timestamp % 60
if seconds < 10 then
seconds = "0" .. seconds
end -- if
return minutes .. ":" .. seconds
end -- function
SLASH_COMMANDS["/ww"] = function(arg)
if arg == "start" then
WereWatch.OnWerewolfStateChanged("debug", true)
elseif arg == "stop" then
WereWatch.OnWerewolfStateChanged("debug", false)
elseif arg == "pos" then
d("[WereWatch] Timer position: " .. WereWatchUI:GetLeft() .. ", " .. WereWatchUI:GetTop())
elseif arg == "best" then
d("[WereWatch] Your current best time is " .. WereWatch.ToMinSec(WereWatch.savedVariables.bestTime) .. ".")
elseif tonumber(arg) >= 0 then
WereWatch.savedVariables.bestTime = tonumber(arg)
end -- if
end -- function
EVENT_MANAGER:RegisterForEvent(WereWatch.name, EVENT_ADD_ON_LOADED, WereWatch.OnAddOnLoaded)
EVENT_MANAGER:RegisterForEvent(WereWatch.name, EVENT_WEREWOLF_STATE_CHANGED, WereWatch.OnWerewolfStateChanged)
EVENT_MANAGER:RegisterForEvent(WereWatch.name, EVENT_LINKED_WORLD_POSITION_CHANGED, WereWatch.OnLinkedWorldPositionChanged)
EVENT_MANAGER:RegisterForEvent(WereWatch.name, EVENT_PLAYER_DEAD, WereWatch.OnPlayerDead)