This repository was archived by the owner on Nov 6, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracking.lua
More file actions
275 lines (203 loc) · 10.1 KB
/
Tracking.lua
File metadata and controls
275 lines (203 loc) · 10.1 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
----------------------------------------------------------------------------------------------------------------------
-- This program is free software: you can redistribute it and/or modify
-- it under the terms of the GNU General Public License as published by
-- the Free Software Foundation, either version 3 of the License, or
-- (at your option) any later version.
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
-- You should have received a copy of the GNU General Public License
-- along with this program. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------------------------------------------------------------------------------------
local addonName, CLL = ...
if not CLL then return end
-- Initialise Tracking environment
local Tracking = {}
Tracking.results = {}
-- Upvalues
local ChatMsg = CLL.Output.Print
local DebugMsg = CLL.Debug.Print
local match = string.match
local tostring = tostring
local pairs = pairs
local select = select
local type = type
local math_abs = math.abs
local L = CLL.L
local wipe = wipe
local GetLocale = GetLocale
local GetCoinTextureString = GetCoinTextureString
local GetCurrencyInfo = GetCurrencyInfo
local GetMoney = GetMoney
-- Locals and constants
local MODULE = "Tracking"
local LOOT_STRING = L["You receive item: "]
local CURRENCY_STRING = L["You receive currency: "]
local currentGoldValue = 0
local currentOrderResources = 0
-- Parses a chat message and extracts the obtained item or currency info (if available)
-- @param msg The captured chat message
-- @return id, amount, type, link Some info about the item or currency gained in the given message
local function ParseChatMsg(str)
-- DebugMsg(MODULE, "ParseChatMsg with string " .. tostring(str) .. " -> isItem = " .. tostring(isItem) .. ", isCurrency = " .. tostring(isCurrency) .. ", type = " .. tostring(type))
if not str:match(LOOT_STRING) and not str:match(CURRENCY_STRING) then return end
-- Extract item/currency ID and amount (if one was given)
local id, amount = str:match("item:(%d+)"), str:match(".*" .. "x(%d+)")
local type = "INVALID"
if id then type = "item"
else -- Maybe it's a currency?
id, amount = str:match("currency:(%d+)"), str:match(".*" .. "x(%d*)")
if id then type = "currency" end
end
amount = (amount and tonumber(amount)) or 1 -- Explicit typecasting is done here to allow direct calculations and validation before adding it later
local link = str:match("\124c.*\124r") -- This stores much more information and can be used to retrieve bonuses etc. later (if needed)
if type ~= "INVALID" then -- Can proceed as planned (with hopefully correct values)
return id, amount, type, link
end
-- ... and in case there's something wrong...
DebugMsg(MODULE, "Error while parsing string '" .. tostring(str) .. "' - is neither item nor currency?")
end
-- Adds a piece of loot (item or currency) as a result for the currently active scan, or the last one is none is in progress
local function AddLoot(id, amount, type, link)
DebugMsg(MODULE, "AddLoot with link - " .. tostring(link))
local results = CLL.Tracking.results
local entry = results[link]
-- Add to existing entry, if one exists (create one otherwise)
if entry then -- This item or currency has been scanned before -> increase its total amount if it matches the other data
if entry.type ~= type or entry.id ~= id then -- Something isn't right -> Stop here to be safe
DebugMsg("Mismatch in type or id for link - " .. tostring(link))
return
end
-- Finally, increase the amount
amount = amount or 0 -- Just to make sure
entry.count = entry.count + 1
entry.amount = entry.amount + tonumber(amount)
else -- Create a new entry (no validation is necessary)
results[link] = { id = id, type = type, amount = amount, count = 1 }
end
DebugMsg(MODULE, "Updated entry for type = " .. tostring(type) .. ", id = " .. tostring(id) .. ", with amount = " .. tostring(amount) .. ", count = " .. tostring(entry and entry.count or 1))
-- Store the updated info in the shared table
CLL.Tracking.results = results
end
-- Event handlers (TODO: Move elsewhere?)
local function OnChatMsg(...)
local args = { ... }
local msg = args[2]
-- Extract loot info (TODO: Gold isn't recognized, I believe)
local id, amount, type, link = ParseChatMsg(msg)
DebugMsg(MODULE, "Parsing link " .. tostring(link))
DebugMsg(MODULE, "Extracted type = " .. tostring(type) .. ", id = " .. tostring(id) .. ", amount = " .. tostring(amount))
-- Add loot to the current tracking process
AddLoot(id, amount, type, tostring(link))
end
-- Start tracking for item and currency updates
function Tracking.Start()
if CLL.Tracking.isActive then -- Already tracking
DebugMsg(MODULE, "Tracking is already in progress!")
return
end
if not CLL.SettingsDB.profile.settings.core.isEnabled then -- Tracking is disabled (TODO: Logging is a more accurate name after the rework)
DebugMsg(MODULE, "Stopped logging because Tracking is disabled")
return
end
currentGoldValue = GetMoney()
currentOrderResources = select(2, GetCurrencyInfo(1220)) or 0
DebugMsg(MODULE, "Tracking started with " .. GetCoinTextureString(currentGoldValue) .. " & " .. currentOrderResources .. " OR. Registering for events...")
-- Re-start the scan and scrub any previous results
CLL.Tracking.isActive = true
wipe(CLL.Tracking.results)
-- Register for spell casts
-- Register for loot containers
-- Register for special loot toast windows
-- GOLD? (not from salvage, but still...)
ContainerLootLogger:RegisterEvent("CHAT_MSG_LOOT", OnChatMsg)
ContainerLootLogger:RegisterEvent("CHAT_MSG_CURRENCY", OnChatMsg)
-- ContainerLootLogger:RegisterEvent(key, eventHandler)
--TotalAP.Debug("Registered for event = " .. key)
end
-- Helper function to make up for the # operator's shortcomings in hash tables
local function count(t)
local c = 0
for k,v in pairs(t) do
c = c + 1
end
return c
end
-- Stop tracking and store the results
function Tracking.Stop(container)
if not CLL.SettingsDB.profile.settings.core.isEnabled then -- Tracking is disabled (TODO: Logging is a more accurate name after the rework)
DebugMsg(MODULE, "No results to store, because Tracking is disabled")
return
end
local oldGoldValue = currentGoldValue
local oldOrderResources = currentOrderResources
currentGoldValue = GetMoney()
currentOrderResources = select(2, GetCurrencyInfo(1220)) or 0
DebugMsg(MODULE, "Tracking stopped with " .. GetCoinTextureString(currentGoldValue) .. " & " .. currentOrderResources .. " OR. Unregistering events...")
local goldChange = currentGoldValue - oldGoldValue
if goldChange > 0 then -- Update DB entry (TODO: Parse chat is still necessary to get all the rewards?)
ChatMsg("Gold earned: " .. GetCoinTextureString(goldChange))
local goldEntry = { amount = goldChange, type = "gold", count = 1 }
CLL.DB.AddEntry("GOLD", goldEntry, container)
else
if goldChange < 0 then
DebugMsg(MODULE, "Negative gold change while tracking is in progress - you're doing it wrong!")
end
end
-- TODO: Generalize this to work with all items/currency (PS, BOS, etc.? - let user set what to track)
local orderResourcesChange = currentOrderResources - oldOrderResources
if type(orderResourcesChange) == "number" and orderResourcesChange ~= 0 then -- Update DB entry
ChatMsg("OR spent: " .. math_abs(orderResourcesChange) .. " |TInterface\\Icons\\inv_orderhall_orderresources:12|t") -- TODO: Util functions to format this?
local orderResourcesEntry = { amount = orderResourcesChange, type = "currency", count = 1 }
CLL.DB.AddEntry("ORDER_RESOURCES", orderResourcesEntry, container)
else
-- if orderResourcesChange < 0 then
-- DebugMsg(MODULE, "Negative order resources change while tracking is in progress - you're doing it wrong!")
-- end
end
CLL.Tracking.isActive = false
-- Unregister all previously registered events
ContainerLootLogger:UnregisterEvent("CHAT_MSG_LOOT")
ContainerLootLogger:UnregisterEvent("CHAT_MSG_CURRENCY")
-- Print results of the latest scan
--Tracking.PrintResults() -- TODO: Manually only, to avoid spam
-- Update DB with current results
container = container or "UNKNOWN_CONTAINER" -- TODO: Actual opening detection is not added yet, so this is using the fallback mechanism for everything
local clientLocale = GetLocale()
local fqcn = CLL.GetFQCN()
-- Increment container counter if there are any results
if count(CLL.Tracking.results) > 0 then
DebugMsg(MODULE, "Saving results to DB... container = " .. container .. ", clientLocale = " .. clientLocale .. ", fqcn = " .. fqcn)
for k, v in pairs(CLL.Tracking.results) do -- Add individual loot entry to the DB and increase statistics according to its amount/count
DebugMsg(MODULE, "Attempting to add entry for key = " .. k)
CLL.DB.AddEntry(k, v, container)
end
CLL.DB.AddOpening(container) -- TODO. This is inaccurate, count in the Detection module and simply add the total count here...
return
end
DebugMsg(MODULE, "Results are empty; nothing will be saved")
end
-- Return the results of the latest tracking process (only if no scan is currently active)
function Tracking.GetResults()
if CLL.Tracking.isActive then -- Tracking is currently in process and the new results aren't yet available
DebugMsg("Tracking is still in process. Results aren't updated yet!")
return
end
return CLL.Tracking.results
end
-- Print out the currently saved results (this is also possible while a scan is still ongoing)
function Tracking.PrintResults()
ChatMsg("-------------------------")
ChatMsg("Tracking results: ") -- TODO: L
for k, v in pairs(CLL.Tracking.results) do -- List this entry and all the saved data
ChatMsg(tostring(k) .. ": " .. tostring(v.amount) .. " (" .. tostring(v.type) .. " with ID " .. tostring(v.id) .. ")")
end
ChatMsg("-------------------------")
end
-- Returns the status of the tracking process
function Tracking.IsActive()
return CLL.Tracking.isActive
end
-- Add module to shared environment
CLL.Tracking = Tracking