-
Notifications
You must be signed in to change notification settings - Fork 8
/
MasterMerchant_Util.lua
311 lines (272 loc) · 9.55 KB
/
MasterMerchant_Util.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
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
-- MasterMerchant Utility Functions File
-- Last Updated September 15, 2014
-- Written August 2014 by Dan Stone (@khaibit) - [email protected]
-- Extended February 2015 by Chris Lasswell (@Philgo68) - [email protected]
-- Released under terms in license accompanying this file.
-- Distribution without license is prohibited!
-- |H0:item:69359:96:50:26848:96:50:0:0:0:0:0:0:0:0:0:19:0:0:0:0:0|h|h AUTGuild 1058 days
local internal = _G["LibGuildStore_Internal"]
-- Gap values for Shell sort
MasterMerchant.shellGaps = {
1391376, 463792, 198768, 86961, 33936, 13776, 4592, 1968, 861, 336, 112, 48, 21, 7, 3, 1
}
function MasterMerchant:ssup(inputTable, numElements)
for _, gapVal in ipairs(MasterMerchant.shellGaps) do
for i = gapVal + 1, numElements do
local tableVal = inputTable[i]
for j = i - gapVal, 1, -gapVal do
local testVal = inputTable[j]
if not (tableVal < testVal) then break end
inputTable[i] = testVal;
i = j
end
inputTable[i] = tableVal
end
end
return inputTable
end
function MasterMerchant:ssdown(inputTable, numElements)
for _, gapVal in ipairs(MasterMerchant.shellGaps) do
for i = gapVal + 1, numElements do
local tableVal = inputTable[i]
for j = i - gapVal, 1, -gapVal do
local testVal = inputTable[j]
if not (tableVal > testVal) then break end
inputTable[i] = testVal;
i = j
end
inputTable[i] = tableVal
end
end
return inputTable
end
-- Lua's table.sort function uses quicksort. Here I implement
-- Shellsort in Lua for better memory efficiency.
-- (http://en.wikipedia.org/wiki/Shellsort)
function MasterMerchant.shellSort(inputTable, comparison, numElements)
numElements = numElements or #inputTable
for _, gapVal in ipairs(MasterMerchant.shellGaps) do
for i = gapVal + 1, numElements do
local tableVal = inputTable[i]
for j = i - gapVal, 1, -gapVal do
local testVal = inputTable[j]
if not comparison(tableVal, testVal) then break end
inputTable[i] = testVal
i = j
end
inputTable[i] = tableVal
end
end
return inputTable
end
-- MM has no data for |H1:item:86987:363:50:0:0:0:0:0:0:0:0:0:0:0:1:3:0:1:0:400:0|h|h
function MasterMerchant.concat(...)
local theString = MM_STRING_EMPTY
for i = 1, select('#', ...) do
local option = select(i, ...)
if option ~= nil and option ~= MM_STRING_EMPTY then
theString = theString .. tostring(option) .. MM_STRING_SEPARATOR_SPACE
end
end
theString = zo_strgsub(theString, '^%s*(.-)%s*$', '%1')
return theString
end
function MasterMerchant.concatTooltip(...)
local theString = MM_STRING_EMPTY
for i = 1, select('#', ...) do
local option = select(i, ...)
if option ~= nil and option ~= MM_STRING_EMPTY then
theString = theString .. tostring(option)
end
end
return theString
end
function MasterMerchant.ShowChildren(control, startNum, endNum)
local numChildren = zo_min(control:GetNumChildren(), endNum)
local numStart = zo_min(startNum, numChildren)
for i = numStart, numChildren do
local child = control:GetChild(i)
if child and child.GetName and child.GetText then
d(i .. ') ' .. child:GetName() .. ' - ' .. child:GetText())
elseif child and child.GetName then
d(i .. ') ' .. child:GetName())
elseif child and child.GetText then
d(i .. ') - ' .. child:GetText())
end
if child then
MasterMerchant.ShowChildren(child, 1, 100)
end
end
end
function MasterMerchant.GetItemLinePrice(itemLink)
if itemLink then
local tipStats = MasterMerchant:GetTooltipStats(itemLink, true, false)
if tipStats.avgPrice then
return tipStats.avgPrice
end
end
return 0
end
function MasterMerchant:playSounds(lastIndex)
local index, value = next(SOUNDS, lastIndex)
if index then
d(index)
PlaySound(value)
zo_callLater(function()
local LEQ = LibExecutionQueue:new()
LEQ:continueWith(function() self:playSounds(index) end, nil)
end, 2000)
end
end
-- the result as a string.
-- ||cffffff38||r
-- ||u0:6%:currency:||u
-- ||t80%:80%:/esoui/art/currency/gold_mipmap.dds||t
-- '|r |t16:16:EsoUI/Art/currency/currency_gold.dds|t'
local function IsValueInteger(value)
return value % 2 == 0
end
function MasterMerchant.LocalizedNumber(amount)
local function IsValueInteger(value)
return value % 2 == 0
end
local function comma_value(amount)
local formatted = tostring(amount)
local k
repeat
formatted, k = zo_strgsub(formatted, "^(-?%d+)(%d%d%d)", "%1" .. GetString(SK_THOUSANDS_SEP) .. "%2")
until k == 0
return formatted
end
amount = amount or 0
local applyFormatting = MasterMerchant.systemSavedVariables.trimDecimals or amount > 100 or IsValueInteger(amount)
if IsInGamepadPreferredMode() or applyFormatting then
return comma_value(zo_floor(amount))
end
return comma_value(zo_roundToNearest(amount, 0.01))
end
function MasterMerchant:GetFullPriceOrProfit(dispPrice, quantity)
local _, _, expectedProfit = GetTradingHousePostPriceInfo(dispPrice)
if MasterMerchant.systemSavedVariables.showFullPrice then
if MasterMerchant.systemSavedVariables.showUnitPrice and quantity > 0 then
return dispPrice / (quantity or 1)
end
return dispPrice
else
if MasterMerchant.systemSavedVariables.showUnitPrice and quantity > 0 then
return expectedProfit / (quantity or 1)
end
return expectedProfit
end
end
local function GetTimeAgo(timestamp)
local secsSince = GetTimeStamp() - timestamp
local formatedTime = nil
if secsSince < ZO_ONE_DAY_IN_SECONDS then
formatedTime = ZO_FormatDurationAgo(secsSince)
else
formatedTime = zo_strformat(GetString(SK_TIME_DAYS), zo_floor(secsSince / ZO_ONE_DAY_IN_SECONDS))
end
return formatedTime
end
local MM_DATE_FORMATS = {
[MM_MONTH_DAY_FORMAT] = "<<1>>/<<2>>",
[MM_DAY_MONTH_FORMAT] = "<<2>>/<<1>>",
[MM_MONTH_DAY_YEAR_FORMAT] = "<<1>>/<<2>>/<<3>>",
[MM_YEAR_MONTH_DAY_FORMAT] = "<<3>>/<<1>>/<<2>>",
[MM_DAY_MONTH_YEAR_FORMAT] = "<<2>>/<<1>>/<<3>>",
}
local function GetDateFormattedString(month, day, yearString)
local dateFormat = MM_DATE_FORMATS[MasterMerchant.systemSavedVariables.dateFormatMonthDay]
return zo_strformat(dateFormat, month, day, yearString)
end
local function GetTimeDateString(timestamp)
local timeData = os.date("*t", timestamp)
local month, day, hour, minute, year = timeData.month, timeData.day, timeData.hour, timeData.min, timeData.year
local postMeridiem = hour >= 12
local yearString = string.sub(tostring(year), 3, 4)
local meridiemString = MasterMerchant.systemSavedVariables.useTwentyFourHourTime and "" or (postMeridiem and "PM" or "AM")
if not MasterMerchant.systemSavedVariables.useTwentyFourHourTime then
hour = hour > 12 and hour - 12 or hour
end
minute = minute < 10 and "0" .. tostring(minute) or tostring(minute)
return string.format("%s %s:%s%s", GetDateFormattedString(month, day, yearString), hour, minute, meridiemString)
end
-- Create a textual representation of a time interval
function MasterMerchant.TextTimeSince(timestamp)
if MasterMerchant.systemSavedVariables.useFormatedTime then
return GetTimeDateString(timestamp)
else
return GetTimeAgo(timestamp)
end
end
function MasterMerchant:BuildTableFromString(str)
local t = {}
local function helper(line)
if line ~= MM_STRING_EMPTY then
t[line] = true
end
return MM_STRING_EMPTY
end
helper((str:gsub("(.-)\r?\n", helper)))
if next(t) then return t end
end
-- A utility function to grab all the keys of the sound table
-- to populate the options dropdown
function MasterMerchant:SoundKeys()
local keyList = {}
for i = 1, #self.alertSounds do table.insert(keyList, self.alertSounds[i].name) end
return keyList
end
-- A utility function to find the key associated with a given value in
-- the sounds table. Best we can do is a linear search unfortunately,
-- but it's a small table.
function MasterMerchant:SearchSounds(sound)
for _, theSound in ipairs(self.alertSounds) do
if theSound.sound == sound then return theSound.name end
end
-- If we hit this point, we didn't find what we were looking for
return nil
end
-- Same as searchSounds, above, but compares names instead of sounds.
function MasterMerchant:SearchSoundNames(name)
for _, theSound in ipairs(self.alertSounds) do
if theSound.name == name then return theSound.sound end
end
end
local function IsNewSale(str)
return str:sub(1, 1) == '3'
end
function MasterMerchant:ShouldUseSale(salesId)
local isNewSale = IsNewSale(salesId)
local shouldUseSale = (isNewSale and not MasterMerchant.systemSavedVariables.useID64FormatedSales) or MasterMerchant.systemSavedVariables.useID64FormatedSales
return shouldUseSale
end
function MasterMerchant:GetIndexedData(dataTable, itemId, itemIndex, salesId)
return dataTable[itemId][itemIndex]['sales'][salesId]
end
function MasterMerchant:IsSalesDataValid(dataTable, itemId, itemIndex, salesId)
if not dataTable[itemId] then
return false
end
if not dataTable[itemId][itemIndex] then
return false
end
if not dataTable[itemId][itemIndex]['sales'] then
return false
end
if not dataTable[itemId][itemIndex]['sales'][salesId] then
return false
end
return true
end
-- /script d({ZO_ColorDef.FloatsToHex(0.84, 0.71, 0.15, 1)})
function MasterMerchant:DecimalToRGB(rDecimal, gDecimal, bDecimal, aDecimal)
-- Treat nil values as 0.0
rDecimal = rDecimal or 0.0
gDecimal = gDecimal or 0.0
bDecimal = bDecimal or 0.0
aDecimal = aDecimal or 0.0
local r, g, b, a = ZO_ColorDef.FloatsToRGBA(rDecimal, gDecimal, bDecimal, aDecimal)
return r, g, b, a
end