Language:
Lua     Change language:
Pastebin: 72588
Author: Chardonnay
Subject: Stack/seller-weighted version of getItemSnapshotMedianBuyout
Created: 2007-08-22 15:58:23
Download and save
Toggle line numbers
1------------------------------------------------------------------------------- 
2-- Returns the current snapshot median for an item 
3------------------------------------------------------------------------------- 
4function getItemSnapshotMedianBuyout(itemKey, ahKey) 
5    if (not ahKey) then ahKey = Auctioneer.Util.GetAuctionKey() end 
6 
7    -- Try to get the value from the cache first. 
8    local unpacked 
9    local cache = getCacheForAHKey(ahKey, true
10    local packed = cache.snapshotMedians[itemKey] 
11    if (packed) then 
12        -- Use the cached value. 
13        --debugPrint("getItemSnapshotMedianBuyout: Cache hit - "..itemKey) 
14        unpacked = Auctioneer.Database.UnpackRecord(packed, MedianMetaData) 
15    else 
16        -- Not in the cache, we'll have to calculate it. 
17        --debugPrint("getItemSnapshotMedianBuyout: Cache miss - "..itemKey) 
18        unpacked = {} 
19 
20        -- Query the snapshot 
21        local auctions = Auctioneer.SnapshotDB.GetAuctionsForItem(itemKey, ahKey) 
22 
23        -- Set this flag to ignore own auctions 
24        local ignoreSelf = false 
25 
26        -- Group auctions by seller 
27        local auctionsBySeller = {} 
28        local totalVolume, numSellers = 0, 0 
29        for _, auction in pairs(auctions) do 
30            if not ignoreSelf or auction.owner ~= UnitName("player") then 
31                if auction.buyoutPrice and auction.buyoutPrice > 0 then 
32                    if not auctionsBySeller[auction.owner] then 
33                        auctionsBySeller[auction.owner] = {} 
34                        numSellers = numSellers + 1 
35                    end 
36                    -- cap stack size at 10 so that full stacks don't completely swamp 
37                    -- smaller auctions 
38                    local volume = math.min(10, auction.count) 
39                    for idx = 1, volume do 
40                        table.insert(auctionsBySeller[auction.owner], auction.buyoutPrice / auction.count) 
41                    end 
42                    totalVolume = totalVolume + volume 
43                end 
44            end 
45        end 
46 
47        -- Find the biggest seller 
48        local maxSellerVolume = 0 
49        for _, sellerAuctions in pairs(auctionsBySeller) do 
50            maxSellerVolume = math.max(maxSellerVolume, #sellerAuctions) 
51        end 
52 
53        -- If there are multiple sellers and more than a few auctions, don't allow a single seller to dominate 
54        if numSellers > 1 and totalVolume >= 10 then 
55            local shareCap = 2 
56            local volumeCap = totalVolume / numSellers * shareCap 
57            if maxSellerVolume > volumeCap then 
58                maxSellerVolume = (totalVolume - maxSellerVolume) / (numSellers - 1) * shareCap 
59                maxSellerVolume = math.max(1, math.floor(maxSellerVolume + 0.5)) 
60            end 
61        -- If there is only one seller, don't allow him to override the historical price (the two counts 
62        -- are compared in HSP calculations) 
63        elseif numSellers == 1 then 
64            maxSellerVolume = math.min(maxSellerVolume, Auctioneer.Core.Constants.MaxBuyoutHistorySize - 1
65        end 
66 
67        -- Add auctions from each seller to final buyout table 
68        local buyoutPrices = {} 
69        for _, sellerAuctions in pairs(auctionsBySeller) do 
70            -- Take the cheapest auctions from each seller - works against price fixers who 
71            -- post a ton of auctions at a high price to skew the median, and then undercut 
72            -- themselves in the hope that the lower price now looks "cheap" 
73            table.sort(sellerAuctions) 
74            local volume = math.min(#sellerAuctions, maxSellerVolume) 
75            for idx = 1, volume do 
76                table.insert(buyoutPrices, sellerAuctions[idx]) 
77            end 
78        end 
79 
80        -- After all that, we can calculate the median 
81        unpacked.median, unpacked.count = getMedian(buyoutPrices) 
82 
83        -- Cache the calculated values. 
84        cache.snapshotMedians[itemKey] = Auctioneer.Database.PackRecord(unpacked, MedianMetaData) 
85    end 
86 
87    return unpacked.median, unpacked.count 
88end 
89 
Download and save
Toggle line numbers
Thread:
Tip: Click the line numbers to toggle highliting on that line.

Paste followup:

Language:
Author:
Subject:


    Tabstop:     bigger biggest
Note: You can prefix a line with "@@@" to highlight it.