-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSamisFishTracker.lua
More file actions
348 lines (286 loc) · 10.3 KB
/
SamisFishTracker.lua
File metadata and controls
348 lines (286 loc) · 10.3 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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
local SFT = SamisFishTrackerAddon
local visibility = SFT.constants.visibility
local averageRateUpdateName = SFT.name .. "AverageRate"
local function iterateThroughEntireBag()
local bagId = BAG_BACKPACK
local slotIndex = ZO_GetNextBagSlotIndex(bagId, 0)
local newLinks = {}
while slotIndex do
slotIndex = ZO_GetNextBagSlotIndex(bagId, slotIndex)
local itemLink = GetItemLink(bagId, slotIndex, 1)
if itemLink and GetItemLinkItemType(itemLink) == ITEMTYPE_FISH then
newLinks[slotIndex] = itemLink
end
end
SFT.utils.cacheFish(newLinks)
end
function SFT.ConfigureAverageRateAutoUpdate()
local intervalSeconds = tonumber(SFT.savedVariables.averageRateUpdateIntervalSeconds) or 1
intervalSeconds = math.max(1, math.min(60, intervalSeconds))
SFT.savedVariables.averageRateUpdateIntervalSeconds = intervalSeconds
EVENT_MANAGER:UnregisterForUpdate(averageRateUpdateName)
if SFT.savedVariables.averageRateAutoUpdateEnabled == false then
return
end
EVENT_MANAGER:RegisterForUpdate(averageRateUpdateName, intervalSeconds * 1000, function()
SFT.UpdateAverageRateLabel()
end)
end
function SFT.RestorePosition()
local left = SFT.savedVariables.left
local top = SFT.savedVariables.top
if left == nil or top == nil then
return
end
SamisFishTrackerControl:ClearAnchors()
SamisFishTrackerControl:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
end
function SFT.RestoreFilletPosition()
local left = SFT.savedVariables.filletLeft
local top = SFT.savedVariables.filletTop
if left == nil or top == nil then
return
end
SamisFilletTrackerControl:ClearAnchors()
SamisFilletTrackerControl:SetAnchor(TOPLEFT, GuiRoot, TOPLEFT, left, top)
end
function SFT.ApplyVisibilitySetting()
if SFT.savedVariables.visibility == visibility.SHOW then
SamisFishTrackerControl:SetHidden(false)
else
SamisFishTrackerControl:SetHidden(true)
end
end
function SFT.OnInventoryUpdate(eventCode, itemSoundCategory)
SFT.RefreshTotals()
SFT.RefreshStorageLabels()
-- Sync session count to actual bag count so it reflects current inventory
SFT.UpdateFishCount(SFT.total_bag)
SFT.savedVariables.amount = SFT.total_bag
-- Refresh again after 2 seconds to account for any inventory state settling delay
zo_callLater(function()
SFT.RefreshTotals()
SFT.RefreshStorageLabels()
SFT.UpdateFishCount(SFT.total_bag)
SFT.savedVariables.amount = SFT.total_bag
end, 2000)
end
function SFT.Initialize()
SFT.fishamount = 0
SFT.filletCountTotal = 0
SFT.filletsSinceRoe = 0
SFT.lastRoeFillets = 0
SFT.lastRoeRatePercent = 0
SFT.total_roe_found = 0
SFT.sessionStartTime = os.time()
SFT.averageRateCatchHistory = {}
EVENT_MANAGER:RegisterForEvent(SFT.name, EVENT_LOOT_RECEIVED, SFT.LootReceivedEvent)
EVENT_MANAGER:RegisterForEvent(SFT.name, EVENT_CLOSE_BANK, SFT.UpdateTotal)
EVENT_MANAGER:RegisterForEvent(SFT.name, EVENT_INVENTORY_ITEM_USED, SFT.OnInventoryUpdate)
EVENT_MANAGER:RegisterForEvent(SFT.name, EVENT_INVENTORY_SINGLE_SLOT_UPDATE, SFT.OnInventorySlotUpdate)
SFT.savedVariables = ZO_SavedVars:NewAccountWide("SamisFishTrackerSavedVariables", 1, nil, {
amount = 0,
visibility = visibility.HIDE,
roeRate = SFT.constants.roeRate,
enableRoeTracking = true,
filletCountTotal = 0,
filletsSinceRoe = 0,
lastRoeFillets = 0,
lastRoeRatePercent = 0,
autoSyncRoeRate = false,
totalRoeFound = 0,
showAverageRate = true,
averageRateAutoUpdateEnabled = true,
averageRateUpdateIntervalSeconds = 1,
averageRateUseRollingWindow = true,
averageRateRollingWindowSeconds = 300,
})
SFT.filletCountTotal = tonumber(SFT.savedVariables.filletCountTotal) or 0
SFT.filletsSinceRoe = tonumber(SFT.savedVariables.filletsSinceRoe) or 0
SFT.lastRoeFillets = tonumber(SFT.savedVariables.lastRoeFillets) or 0
SFT.lastRoeRatePercent = tonumber(SFT.savedVariables.lastRoeRatePercent) or 0
SFT.total_roe_found = tonumber(SFT.savedVariables.totalRoeFound) or 0
SamisFishTrackerControl:SetHandler("OnMoveStop", function()
SFT.savedVariables.left = SamisFishTrackerControl:GetLeft()
SFT.savedVariables.top = SamisFishTrackerControl:GetTop()
end)
SFT.RestoreFilletPosition()
SamisFilletTrackerControl:SetHandler("OnMoveStop", function()
SFT.savedVariables.filletLeft = SamisFilletTrackerControl:GetLeft()
SFT.savedVariables.filletTop = SamisFilletTrackerControl:GetTop()
end)
SFT.InitializeBackground()
SFT.settingsInit()
SFT.RefreshTotals()
SFT.UpdateFishCount(SFT.savedVariables.amount or 0)
SFT.RefreshStorageLabels()
SFT.ApplyVisibilitySetting()
SFT.RestorePosition()
SFT.RegisterSlashCommands()
SFT.ConfigureAverageRateAutoUpdate()
iterateThroughEntireBag()
end
function SFT.LootReceivedEvent(_, _, itemLink, quantity, _, _, self)
if not self then
return
end
if not SFT.IsTrackableFish(itemLink) then
return
end
local amount = quantity or 1
SFT.RecordFishCatch(amount)
SFT.UpdateFishAmount(amount)
SFT.savedVariables.amount = SFT.fishamount
SFT.UpdateAverageRateLabel(true)
end
function SFT.RegisterFilletCount(amount)
if SFT.IsRoeTrackingEnabled and not SFT.IsRoeTrackingEnabled() then
return
end
local increment = amount or 0
if increment <= 0 then
return
end
SFT.filletCountTotal = (SFT.filletCountTotal or 0) + increment
SFT.filletsSinceRoe = (SFT.filletsSinceRoe or 0) + increment
local totalFillets = SFT.filletCountTotal or 0
local totalRoeFound = SFT.total_roe_found or 0
if totalFillets > 0 then
SFT.lastRoeRatePercent = (totalRoeFound / totalFillets) * 100
else
SFT.lastRoeRatePercent = 0
end
if SFT.savedVariables.autoSyncRoeRate then
SFT.savedVariables.roeRate = SFT.lastRoeRatePercent / 100
end
SFT.savedVariables.filletCountTotal = SFT.filletCountTotal
SFT.savedVariables.filletsSinceRoe = SFT.filletsSinceRoe
SFT.savedVariables.lastRoeRatePercent = SFT.lastRoeRatePercent
if SFT.UpdateFilletStatsLabel then
SFT.UpdateFilletStatsLabel()
end
end
function SFT.OnPerfectRoeFound(amount)
if SFT.IsRoeTrackingEnabled and not SFT.IsRoeTrackingEnabled() then
return
end
local increment = amount or 0
if increment <= 0 then
return
end
SFT.total_roe_found = (SFT.total_roe_found or 0) + increment
local filletsSinceRoe = SFT.filletsSinceRoe or 0
if filletsSinceRoe > 0 then
SFT.lastRoeFillets = filletsSinceRoe
SFT.lastRoeRatePercent = (1 / filletsSinceRoe) * 100
else
SFT.lastRoeFillets = 0
end
SFT.filletsSinceRoe = 0
local totalFillets = SFT.filletCountTotal or 0
if totalFillets > 0 then
SFT.lastRoeRatePercent = (SFT.total_roe_found / totalFillets) * 100
else
SFT.lastRoeRatePercent = 0
end
if SFT.savedVariables.autoSyncRoeRate then
SFT.savedVariables.roeRate = SFT.lastRoeRatePercent / 100
end
SFT.savedVariables.totalRoeFound = SFT.total_roe_found
SFT.savedVariables.filletsSinceRoe = SFT.filletsSinceRoe
SFT.savedVariables.lastRoeFillets = SFT.lastRoeFillets
SFT.savedVariables.lastRoeRatePercent = SFT.lastRoeRatePercent
SFT.RefreshTotals()
SFT.RefreshStorageLabels()
if SFT.UpdateFilletStatsLabel then
SFT.UpdateFilletStatsLabel()
end
end
function SFT.ResetRoeFilletTracking()
if SFT.IsRoeTrackingEnabled and not SFT.IsRoeTrackingEnabled() then
return
end
SFT.filletCountTotal = 0
SFT.filletsSinceRoe = 0
SFT.lastRoeFillets = 0
SFT.lastRoeRatePercent = 0
SFT.total_roe_found = 0
SFT.savedVariables.filletCountTotal = 0
SFT.savedVariables.filletsSinceRoe = 0
SFT.savedVariables.lastRoeFillets = 0
SFT.savedVariables.lastRoeRatePercent = 0
SFT.savedVariables.totalRoeFound = 0
SFT.RefreshTotals()
SFT.RefreshStorageLabels()
end
function SFT.ApplyLastRoeRateToRoeRateSetting()
if SFT.IsRoeTrackingEnabled and not SFT.IsRoeTrackingEnabled() then
return
end
local percentValue = tonumber(SFT.lastRoeRatePercent) or 0
local rateValue = percentValue / 100
local clampedRate = math.max(0.0001, rateValue)
SFT.savedVariables.roeRate = clampedRate
SFT.UpdateFishCount(SFT.fishamount)
SFT.RefreshStorageLabels()
end
local function handleInventoryAddition(bagId, slotIndex, stackCountChange)
if bagId == BAG_BACKPACK and stackCountChange > 0 then
iterateThroughEntireBag()
end
local itemLink = GetItemLink(bagId, slotIndex)
if (not itemLink or itemLink == "") and SFT.utils.getCachedFishItemLink(slotIndex) ~= nil then
itemLink = SFT.utils.getCachedFishItemLink(slotIndex)
end
if not itemLink or itemLink == "" then
return
end
local itemId = GetItemLinkItemId(itemLink)
if bagId == BAG_VIRTUAL and itemId == SFT.constants.perfectRoeItemId and stackCountChange > 0 then
SFT.OnPerfectRoeFound(stackCountChange)
return
end
end
function SFT.OnInventorySlotUpdate(eventCode, bagId, slotIndex, isNewItem, itemSoundCategory, updateReason,
stackCountChange)
if bagId ~= BAG_BACKPACK and bagId ~= BAG_SUBSCRIBER_BANK and bagId ~= BAG_VIRTUAL then
return
end
if stackCountChange > 0 then
handleInventoryAddition(bagId, slotIndex, stackCountChange)
return
end
if not stackCountChange or stackCountChange == 0 then
return
end
local itemLink = GetItemLink(bagId, slotIndex)
if (not itemLink or itemLink == "") and SFT.utils.getCachedFishItemLink(slotIndex) ~= nil then
itemLink = SFT.utils.getCachedFishItemLink(slotIndex)
end
if not itemLink or itemLink == "" then
return
end
local isFish = GetItemLinkItemType(itemLink) == ITEMTYPE_FISH
if isFish and stackCountChange < 0 then
SFT.RegisterFilletCount(-stackCountChange)
end
SFT.utils.cacheItemLink(bagId, slotIndex, itemLink)
end
function SFT.OnAddOnLoaded(_, addonName)
if addonName ~= SFT.name then
return
end
SFT.Initialize()
EVENT_MANAGER:UnregisterForEvent(SFT.name, EVENT_ADD_ON_LOADED)
ZO_PreHookHandler(RETICLE.interact, "OnEffectivelyShown", SFT.ManageInteraction)
ZO_PreHookHandler(RETICLE.interact, "OnHide", SFT.ManageInteraction)
end
function SFT.ManageInteraction()
if SFT.savedVariables.visibility ~= visibility.AUTO then
return
end
local action, _, _, _, additionalInfo = GetGameCameraInteractableActionInfo()
local shouldShow = action and additionalInfo == ADDITIONAL_INTERACT_INFO_FISHING_NODE
SamisFishTrackerControl:SetHidden(not shouldShow)
end
EVENT_MANAGER:RegisterForEvent(SFT.name, EVENT_ADD_ON_LOADED, SFT.OnAddOnLoaded)