-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCache.lua
More file actions
330 lines (289 loc) · 11.9 KB
/
Copy pathCache.lua
File metadata and controls
330 lines (289 loc) · 11.9 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
---@class BFBM
local BFBM = select(2, ...)
local L = BFBM.L
---@type AbstractFramework
local AF = _G.AbstractFramework
local SEND_INTERVAL = 99
local GetHotItem = C_BlackMarket.GetHotItem
local GetNumItems = C_BlackMarket.GetNumItems
local GetItemInfoByIndex = C_BlackMarket.GetItemInfoByIndex
local GetItemInfoInstant = C_Item.GetItemInfoInstant
local GetServerTime = GetServerTime
---------------------------------------------------------------------
-- scan and cache
---------------------------------------------------------------------
local lastSent
local function BLACK_MARKET_ITEM_UPDATE()
local numItems = GetNumItems()
if not numItems or numItems == 0 then
return
end
-- old
local lastScanned = {}
for itemID, t in pairs(BFBM.currentServerData.items) do
lastScanned[itemID] = {
numBids = t.numBids,
timeLeft = t.timeLeft,
}
end
wipe(BFBM.currentServerData.items)
local dataChanged
-- local hotMarketID = select(16, GetHotItem())
-- AUCTION_TIME_LEFT0 完成!
-- AUCTION_TIME_LEFT0_DETAIL 拍卖已结束。
-- AUCTION_TIME_LEFT1 短
-- AUCTION_TIME_LEFT1_DETAIL 少于30分钟
-- AUCTION_TIME_LEFT2 中
-- AUCTION_TIME_LEFT2_DETAIL 30分钟到2小时
-- AUCTION_TIME_LEFT3 长
-- AUCTION_TIME_LEFT3_DETAIL 2小时到12小时
-- AUCTION_TIME_LEFT4 非常长
-- AUCTION_TIME_LEFT4_DETAIL 大于12小时
for i = 1, numItems do
local name, texture, quantity, itemType, usable, level, levelType, sellerName, minBid, minIncrement, currBid, youHaveHighBid, numBids, timeLeft, link, marketID, quality = GetItemInfoByIndex(i)
local itemID = GetItemInfoInstant(link)
-- update favorite
if youHaveHighBid then
BFBM_DB.favorites[itemID] = true
BFBM.currentServerBids[itemID] = {currBid, GetServerTime()}
end
-- update BFBM_DB.data
BFBM.currentServerData.items[itemID] = {
-- itemID = itemID,
name = name,
texture = texture,
quantity = quantity,
quality = quality,
itemType = itemType,
-- usable = usable,
-- level = level,
-- levelType = levelType,
-- sellerName = sellerName,
-- minBid = minBid,
-- minIncrement = minIncrement,
currBid = currBid == 0 and minBid or currBid,
-- youHaveHighBid = youHaveHighBid,
numBids = numBids,
timeLeft = timeLeft,
link = link,
-- marketID = marketID,
-- isHot = marketID == hotMarketID,
}
-- check if data changed
if not lastScanned[itemID] or lastScanned[itemID].numBids ~= numBids or lastScanned[itemID].timeLeft ~= timeLeft then
dataChanged = true
end
end
-- update history
BFBM.UpdateHistoryCache(AF.player.realm, nil, BFBM.currentServerData.items)
if dataChanged then
-- NOTE: only update if data changed
BFBM.currentServerData.lastUpdate = GetServerTime()
-- current
BFBM.UpdateCurrentItems(AF.player.realm, true)
-- history
BFBM.UpdateHistoryItems()
-- favorites
BFBM.AlertFavorites(AF.player.realm, BFBM.currentServerData.items)
-- CN data
BFBM.UpdateDataUpload(AF.player.realm, BFBM.currentServerData.lastUpdate, BFBM.currentServerData.items)
end
if dataChanged or not lastSent or time() - lastSent >= SEND_INTERVAL then
lastSent = time()
-- send
BFBM.UpdateDataForSend()
BFBM.SendData("channel")
BFBM.SendData("guild")
BFBM.SendData("group")
end
end
BFBM:RegisterEvent("BLACK_MARKET_ITEM_UPDATE", AF.GetDelayedInvoker(0.5, BLACK_MARKET_ITEM_UPDATE))
---------------------------------------------------------------------
-- history
---------------------------------------------------------------------
function BFBM.UpdateHistoryCache(server, lastUpdate, items)
lastUpdate = lastUpdate or GetServerTime()
for itemID, t in pairs(items) do
local name = t.name
local texture = t.texture
local link = t.link
local quality = t.quality
local itemType = t.itemType
local currBid = t.currBid
local numBids = t.numBids
local timeLeft = t.timeLeft
if not BFBM_DB.data.items[itemID] then
BFBM_DB.data.items[itemID] = {
name = name,
texture = texture,
link = link,
quality = quality,
itemType = itemType,
history = {},
}
end
-- item history server
if not BFBM_DB.data.items[itemID].history[server] then
BFBM_DB.data.items[itemID].history[server] = {}
end
-- item history date
local day = AF.GetDateString(lastUpdate)
if not BFBM_DB.data.items[itemID].history[server][day] then
BFBM_DB.data.items[itemID].history[server][day] = {
bids = {},
finalBid = nil,
}
end
local changed
-- item history bids
if not BFBM_DB.data.items[itemID].history[server][day].bids[numBids] then
BFBM_DB.data.items[itemID].history[server][day].bids[numBids] = currBid
BFBM_DB.data.items[itemID].lastUpdate = lastUpdate
changed = true
end
-- item history final price
if not BFBM_DB.data.items[itemID].history[server][day].finalBid and (currBid >= BFBM.MAX_BID or timeLeft == 0) then
BFBM_DB.data.items[itemID].history[server][day].finalBid = currBid
changed = true
end
if changed then
AF.Fire("BFBM_ITEM_HISTORY_UPDATE", server, lastUpdate, itemID)
end
end
end
---------------------------------------------------------------------
-- update local cache
---------------------------------------------------------------------
function BFBM.UpdateLocalCache(server, lastUpdate, items)
-- update BFBM_DB.data.servers
if not BFBM_DB.data.servers[server] then
-- print("UpdateLocalCache: SAVE RECEIVED DATA")
BFBM_DB.data.servers[server] = {
items = items,
lastUpdate = lastUpdate,
}
elseif not BFBM_DB.data.servers[server].lastUpdate or BFBM_DB.data.servers[server].lastUpdate < lastUpdate then
-- print("UpdateLocalCache: UPDATE USING RECEIVED DATA")
BFBM_DB.data.servers[server].items = items
BFBM_DB.data.servers[server].lastUpdate = lastUpdate
else
-- print("UpdateLocalCache: RECEIVED DATA OLDER THAN LOCAL")
return
end
-- update BFBM_DB.data.items
BFBM.UpdateHistoryCache(server, lastUpdate, items)
-- current
BFBM.UpdateCurrentItems(server)
-- history
BFBM.UpdateHistoryItems()
-- send
BFBM.UpdateDataForSend()
-- favorites
BFBM.AlertFavorites(server, items)
-- chat alerts
BFBM.ShowChatAlerts(server)
-- CN data
BFBM.UpdateDataUpload(server, lastUpdate, items)
end
---------------------------------------------------------------------
-- alert favorites
---------------------------------------------------------------------
function BFBM.AlertFavorites(server, items)
if not BFBM_DB.config.priceChangeAlerts then return end
if not BFBM_DB.alert.servers[server] then
BFBM_DB.alert.servers[server] = {}
end
for itemID, t in pairs(items) do
if BFBM_DB.favorites[itemID] then
if BFBM_DB.myBids[server] and BFBM_DB.myBids[server][itemID] then
if BFBM_DB.myBids[server][itemID][1] < t.currBid then
BFBM_DB.myBids[server][itemID] = nil
-- be outbid
local currBid = AF.FormatMoney(t.currBid, nil, true, true)
local text = L["%s\nYou've been outbid!\nCurrent bid: %s\nServer: %s"]:format(t.link, currBid, server)
AF.ShowNotificationPopup(text, 60, t.texture, AF.GetSound("notification1"), 220, "LEFT", BFBM.OpenCurrentFrame, server, itemID)
end
elseif BFBM_DB.alert.servers[server][itemID] == nil then
-- first seen today
local currBid = AF.FormatMoney(t.currBid, nil, true, true)
local text = L["%s\nis on the Black Market!\nCurrent bid: %s\nServer: %s"]:format(t.link, currBid, server)
AF.ShowNotificationPopup(text, 60, t.texture, AF.GetSound("notification1"), 220, "LEFT", BFBM.OpenCurrentFrame, server, itemID)
elseif BFBM_DB.alert.servers[server][itemID] ~= t.numBids then
-- bid changed
local currBid = AF.FormatMoney(t.currBid, nil, true, true)
local text = L["%s\nThe bid price has changed!\nCurrent bid: %s\nServer: %s"]:format(t.link, currBid, server)
AF.ShowNotificationPopup(text, 60, t.texture, AF.GetSound("notification1"), 220, "LEFT", BFBM.OpenCurrentFrame, server, itemID)
end
BFBM_DB.alert.servers[server][itemID] = t.numBids
end
end
end
---------------------------------------------------------------------
-- chat alerts
---------------------------------------------------------------------
local lastAlerts = {}
function BFBM.ShowChatAlerts(server)
if BFBM_DB.config.chatAlerts == "all" or (BFBM_DB.config.chatAlerts == "current" and AF.IsConnectedRealm(server)) then
if BFBM_DB.config.chatAlertsInterval and BFBM_DB.config.chatAlertsInterval ~= 0 then
if lastAlerts[server] and time() - lastAlerts[server] < BFBM_DB.config.chatAlertsInterval then
return
end
lastAlerts[server] = time()
end
local serverName
if AF.IsConnectedRealm(server) then
serverName = AF.WrapTextInColor(server, "softlime")
else
serverName = AF.WrapTextInColor(server, "yellow_text")
end
local link = ("|c%s|Haddon:BFBlackMarket:%s|h%s|h|r"):format(AF.GetAccentColorHex(), server, L["here"])
AF.Print(L["%s data updated, click %s to view details!"]:format(serverName, link))
end
end
-- local link = ("|c%s|Haddon:BFBlackMarket:%s|h%s|h|r"):format(AF.GetAccentColorHex(), "影之哀伤", L["here"])
-- AF.Print(L["%s data updated, click %s to view details!"]:format("影之哀伤", link))
EventRegistry:RegisterCallback("SetItemRef", function(_, link)
local linkType, addonName, server = strsplit(":", link)
if linkType == "addon" and addonName == "BFBlackMarket" then
BFBM.OpenCurrentFrame(nil, nil, server)
end
end)
---------------------------------------------------------------------
-- data for upload
---------------------------------------------------------------------
local function GetBigFootClientVersion(wowProjectID)
wowProjectID = wowProjectID or WOW_PROJECT_ID
if wowProjectID == WOW_PROJECT_MAINLINE then -- 正式服
return 0
elseif wowProjectID == WOW_PROJECT_CLASSIC then -- 经典60
return 1
elseif wowProjectID == WOW_PROJECT_MISTS_CLASSIC then -- 熊猫人
return 2
elseif wowProjectID == WOW_PROJECT_WRATH_CLASSIC then -- 时光(WOW_PROJECT_WRATH_CLASSIC)
return 3
else -- TBC
return 4
end
end
function BFBM.UpdateDataUpload(server, lastUpdate, items)
if type(BFBM_DataUpload) ~= "table" then return end
BFBM_DataUpload[server] = {
Server = server,
LastUpdate = lastUpdate,
Items = {},
Version = BFBM.version,
ClientVersion = GetBigFootClientVersion(),
}
for itemID, t in pairs(items) do
tinsert(BFBM_DataUpload[server].Items, {
ID = itemID,
Name = t.name,
Type = t.itemType,
Quality = t.quality,
IconFileDataID = t.texture,
CurrentBid = t.currBid,
NumBids = t.numBids,
TimeLeft = t.timeLeft == 0 and 5 or t.timeLeft,
})
end
end