forked from evl/auctionlite
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHistory.lua
More file actions
192 lines (168 loc) · 5.59 KB
/
History.lua
File metadata and controls
192 lines (168 loc) · 5.59 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
-------------------------------------------------------------------------------
-- History.lua
--
-- Track historical information about auction prices.
-------------------------------------------------------------------------------
local L = LibStub("AceLocale-3.0"):GetLocale("AuctionLite", false)
local MIN_TIME_BETWEEN_SCANS = 0;
local HALF_LIFE = 604800; -- 1 week
local INDEPENDENT_SCANS = 172800; -- 2 days
-- Retrieve historical price data for an item by id/suffix.
function AuctionLite:GetHistoricalPriceById(id, suffix)
local info = self.db.factionrealm.prices[id];
-- Check whether this item has sub-tables for each item suffix.
if info ~= nil then
if suffix == nil then
suffix = 0;
end
if suffix ~= 0 or info.suffix then
if suffix ~= 0 and info.suffix then
info = info[suffix];
else
info = nil;
end
end
end
-- Make sure we have the right format.
if info ~= nil then
self:ValidateHistoricalPrice(info);
end
return info;
end
-- Retrieve historical price data for an item.
function AuctionLite:GetHistoricalPrice(link)
local name, _, id, suffix = self:SplitLink(link);
local info = AuctionLite:GetHistoricalPriceById(id, suffix);
if info == nil then
-- Check to see whether we're using a database generated by v0.1,
-- which indexed by name instead of id. If so, migrate it.
info = self.db.factionrealm.prices[name];
if info ~= nil then
self:ValidateHistoricalPrice(info);
self:SetHistoricalPrice(link, info);
self.db.factionrealm.prices[name] = nil;
end
end
return info;
end
-- Set historical price data for an item.
function AuctionLite:SetHistoricalPrice(link, info)
local _, _, id, suffix = self:SplitLink(link);
if suffix == 0 then
-- This item has no suffix, so just use the id.
self.db.factionrealm.prices[id] = info;
else
-- This item has a suffix, so index by suffix as well.
local parent = self.db.factionrealm.prices[id];
if parent == nil or not parent.suffix then
parent = { suffix = true };
self.db.factionrealm.prices[id] = parent;
end
parent[suffix] = info;
end
end
-- Make sure that the price data structure is a valid one.
function AuctionLite:ValidateHistoricalPrice(info)
local field;
for _, field in ipairs({"price", "listings", "scans", "time", "items"}) do
if info[field] == nil then
info[field] = 0;
end
end
end
-- Update historical price data for an item given a price (per item) and
-- the number of listings seen in the latest scan.
function AuctionLite:UpdateHistoricalPrice(link, data)
if self.db.profile.storePrices then
-- Get the current data.
local info = self:GetHistoricalPrice(link)
-- If we have no data for this item, start a new one.
if info == nil then
info = { price = 0, listings = 0, scans = 0, time = 0, items = 0 };
self:SetHistoricalPrice(link, info);
end
-- Update the current data with our new data.
local time = time();
if info.time + MIN_TIME_BETWEEN_SCANS < time and data.listings > 0 then
local pastDiscountFactor = 0.5 ^ ((time - info.time) / HALF_LIFE);
local presentDiscountFactor = 1 - 0.5 ^ ((time - info.time) / INDEPENDENT_SCANS);
info.price = (data.price * data.listings * presentDiscountFactor +
info.price * info.listings * pastDiscountFactor) /
(data.listings * presentDiscountFactor +
info.listings * pastDiscountFactor);
info.listings = data.listings * presentDiscountFactor +
info.listings * pastDiscountFactor;
info.items = data.items * presentDiscountFactor + info.items * pastDiscountFactor;
info.scans = 1 * presentDiscountFactor + info.scans * pastDiscountFactor;
info.time = time;
end
end
end
-- Get saved item info for the sell frame.
function AuctionLite:GetSavedPrices(link)
local name, _, id, suffix = self:SplitLink(link);
local info = self.db.factionrealm.prefs[id];
-- Check whether this item has sub-tables for each item suffix.
if info ~= nil then
if suffix == nil then
suffix = 0;
end
if suffix ~= 0 or info.suffix then
if suffix ~= 0 and info.suffix then
info = info[suffix];
else
info = nil;
end
end
end
if info == nil then
info = {};
end
return info;
end
-- Set saved item info for the sell frame.
function AuctionLite:SetSavedPrices(link, info)
local _, _, id, suffix = self:SplitLink(link);
-- Set info to nil if there are no saved prices.
if info ~= nil then
local hasPrices = false;
for _, _ in pairs(info) do
hasPrices = true;
break;
end
if not hasPrices then
info = nil;
end
end
if suffix == 0 then
-- This item has no suffix, so just use the id.
self.db.factionrealm.prefs[id] = info;
else
-- This item has a suffix, so index by suffix as well.
local parent = self.db.factionrealm.prefs[id];
if parent == nil or not parent.suffix then
parent = { suffix = true };
self.db.factionrealm.prefs[id] = parent;
end
parent[suffix] = info;
end
end
-- Static popup warning for clearing data.
StaticPopupDialogs["AL_CLEAR_DATA"] = {
text = L["CLEAR_DATA_WARNING"],
button1 = L["Do it!"],
button2 = L["Cancel"],
OnAccept = function(self)
AuctionLite.db.factionrealm.prices = {};
AuctionLite:Print(L["Auction house data cleared."]);
collectgarbage("collect");
end,
showAlert = 1,
timeout = 0,
exclusive = 1,
hideOnEscape = 1
};
-- The user requested to clear all AH data.
function AuctionLite:ClearData()
StaticPopup_Show("AL_CLEAR_DATA");
end