Advertisement
Guest User

Auc-Stat-AuctionDB

a guest
Jul 22nd, 2014
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.87 KB | None | 0 0
  1. --[[
  2.     Auc-Stat-auctiondB - auctiondB price statistics module
  3.  
  4.     This is an Auctioneer statistics module that returns price data from
  5.     TradeSkillMaster_auctiondB addon.  You must have either The Undermine Journal
  6.     or TradeSkillMaster_auctiondB addon installed for this module to have any
  7.     effect.
  8.  
  9.     Copyright (c) 2011 Johnny C. Lam, 2012 Chris Hennick
  10.     All rights reserved.
  11.  
  12.     Redistribution and use in source and binary forms, with or without
  13.     modification, are permitted provided that the following conditions
  14.     are met:
  15.  
  16.     1. Redistributions of source code must retain the above copyright
  17.        notice, this list of conditions and the following disclaimer.
  18.     2. Redistributions in binary form must reproduce the above copyright
  19.        notice, this list of conditions and the following disclaimer in the
  20.        documentation and/or other materials provided with the distribution.
  21.  
  22.     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  23.     "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
  24.     TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  25.     PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS
  26.     BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27.     CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28.     SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29.     INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30.     CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31.     ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32.     POSSIBILITY OF SUCH DAMAGE.
  33.  --]]
  34. if not AucAdvanced then
  35. --  error("AucAdvanced not found!")
  36. return end
  37.  
  38. -- register this file with Ace Libraries
  39. local auctionDB = select(2, ...)
  40. auctionDB = LibStub("AceAddon-3.0"):NewAddon(auctionDB, "Auc-Stat-AuctionDB", "AceConsole-3.0")
  41.  
  42. local TSMAPI = _G.TSMAPI
  43.  
  44. local AceGUI = LibStub("AceGUI-3.0") -- load the AceGUI libraries
  45.  
  46. local libType, libName = "Stat", "AuctionDB"
  47. local lib, parent, private = AucAdvanced.NewModule(libType, libName)
  48.  
  49. if not lib then return end
  50. local aucPrint, decode, _, _, replicate, empty, get, set, default, debugPrint, fill, _TRANS = AucAdvanced.GetModuleLocals()
  51. local GetFaction = AucAdvanced.GetFaction
  52.  
  53. lib.Processors = {}
  54. lib.Processors.tooltip = function(callbackType, ...)
  55. --  private.ProcessTooltip(...)
  56. end
  57.  
  58. lib.Processors.config = function(callbackType, gui)
  59.     if private.SetupConfigGui then
  60.         private.SetupConfigGui(gui)
  61.     end
  62. end
  63.  
  64. lib.Processors.load = function(callbackType, addon)
  65.     -- check that this is our load message, and that our OnLoad function still exists
  66.     if addon == "auc-stat-auctiondb" and private.OnLoad then
  67.         private.OnLoad(addon)
  68.     end
  69. end
  70.  
  71. function lib.GetPrice(hyperlink, serverKey)
  72.     local array = lib.GetPriceArray(hyperlink, serverKey)
  73.     return array.price, array.stddev, array.seen
  74. end
  75.  
  76. function lib.GetPriceColumns()
  77.     return "Market Value", "Market Std Dev", "Seen Count"
  78. end
  79.  
  80. function lib.GetDay(t)
  81.     t = t or time()
  82.     return floor(t / (60*60*24))
  83. end
  84.  
  85. local array = {}
  86. local seen
  87. local confidence
  88. function lib.GetPriceArray(id, serverKey)
  89.     confidence = get("stat.auctiondb.confidence") or 1
  90.     seen = TSMAPI:GetData("seenCount", TSMAPI:GetItemID(id))
  91.     local price = TSMAPI:GetData("market", id)
  92.     local scans = TSMAPI:GetData("adbScans", id)
  93.     if not scans or not price then return end
  94.     local variance, n = 0, 0
  95.     local day = lib.GetDay()
  96.     local i, x
  97.     for i=0, 14 do
  98.         local data = scans[day-i]
  99.         if data then
  100.             if type(data) == "table" then
  101.                 for _, x in pairs(data) do
  102.                     variance = variance + (x-price)^2
  103.                     n = n + 1
  104.                 end
  105.             else
  106.                 variance = variance + (data-price)^2
  107.                 n = n + 1
  108.             end
  109.         end
  110.     end
  111.     wipe(array)
  112.     array.price = price
  113.     array.seen = seen
  114.     variance = variance * sqrt(seen / n) -- central limit theorem (simplifying assumption that scans are equal-size independent samples)
  115.     local minvariance = get("stat.auctiondb.minerrorpct")*.01*price
  116.     if variance < minvariance then variance = minvariance*price end
  117.     if variance <= 0 then return end
  118.     if n >= 2 then
  119.         array.stddev = sqrt(variance)/(confidence*(n-1))
  120.     end
  121.     return array
  122. end
  123.  
  124. local bellCurve = AucAdvanced.API.GenerateBellCurve()
  125. local mean, stddev
  126. local pdfcache = {} -- FIXME: should be cleared when settings change
  127. function lib.GetItemPDF(itemLink, serverKey)
  128.     local array = lib.GetPriceArray(itemLink, serverKey)
  129.     if not array then return end
  130.     mean = array.price
  131.     stddev = array.stddev
  132. --  print(string.format("%s: Mean %f, stddev %f\n", itemLink, mean, stddev))
  133.     -- Calculate the lower and upper bounds as +/- 3 standard deviations
  134.     if mean and stddev then
  135.         local lower, upper = (mean - 3 * stddev), (mean + 3 * stddev)
  136.         bellCurve:SetParameters(mean, stddev)
  137.         return bellCurve, lower, upper
  138.     else
  139.         return
  140.     end
  141. end
  142.  
  143. function lib.IsValidAlgorithm()
  144.     if not get("stat.auctiondb.enable") then return false end
  145.     if not TSMAPI then return false end
  146.     return true
  147. end
  148.  
  149. function private.OnLoad(addon)
  150.     TSMAPI = _G.TSMAPI
  151.     default("stat.auctiondb.enable", false)
  152.     default("stat.auctiondb.confidence", 1)
  153. --  default("stat.auctiondB.shockconfidence", 10)
  154. --  default("stat.auctiondb.fallbackconfidence", 2)
  155. --  default("stat.auctiondb.projection", -3.5)
  156. --  default("stat.auctiondb.regionagreement", 1)
  157. --  default("stat.auctiondB.maxz", 2) -- because this parameter is used to effectively apply median-centered Bollinger Bands
  158.     default("stat.auctiondb.minerrorpct", 1)
  159. --  default("stat.auctiondB.detectpriceshocks", true)
  160. --  default("stat.auctiondB.detectstddevshocks", false)
  161. --  default("stat.auctiondb.n", 492) -- 2 factions * 246 US realms as of 2012-09-17 (excludes Arena Pass)
  162.     private.OnLoad = nil -- only run this function once
  163. end
  164.  
  165. --~ function private.GetInfo(hyperlink, serverKey)
  166. --~     if not private.IsauctiondBLoaded() then return end
  167.  
  168. --~     local linkType, itemId, suffix, factor = decode(hyperlink)
  169. --~     if (linkType ~= "item") then return end
  170.  
  171. --~     local dta = TSMAPI:GetData(itemId, serverKey)
  172. --~     return dta
  173. --~ end
  174.  
  175. -- Localization via Auctioneer's Babylonian; from Auc-Advanced/CoreUtil.lua
  176. local Babylonian = LibStub("Babylonian")
  177. assert(Babylonian, "Babylonian is not installed")
  178. local babylonian = Babylonian(AucStatAuctionDBLocalizations)
  179. _TRANS = function (stringKey)
  180.     local locale = get("SelectedLocale")    -- locales are user choose-able
  181.     -- translated key or english Key or Raw Key
  182.     return babylonian(locale, stringKey) or babylonian[stringKey] or stringKey
  183. end
  184.  
  185. function private.SetupConfigGui(gui)
  186.     local id = gui:AddTab(lib.libName, lib.libType.." Modules")
  187.  
  188.     gui:AddHelp(id, "what auctiondb",
  189.         _TRANS('auctionDB_Help_auctionDB'),
  190.         _TRANS('auctionDB_Help_auctionDBAnswer')
  191.     )
  192.  
  193.     -- All options in here will be duplicated in the tooltip frame
  194.     local function addTooltipControls(id)
  195.         -- FIXME: cache needs to clear when settings change
  196.         gui:AddControl(id, "Header",     0,    _TRANS('auctionDB_Interface_auctionDBOptions'))
  197.         gui:AddControl(id, "Note",       0, 1, nil, nil, " ")
  198.         gui:AddControl(id, "Checkbox",   0, 1, "stat.auctiondb.enable", _TRANS('auctionDB_Interface_EnableauctionDB'))
  199.         gui:AddTip(id, _TRANS('auctionDB_HelpTooltip_EnableauctionDB'))
  200.         gui:AddControl(id, "WideSlider",    0, 1, "stat.auctiondb.minerrorpct", 0, 10, 0.5, _TRANS('auctiondB_Interface_MinErrorPercent') )
  201.         gui:AddTip(id, _TRANS('auctionDB_HelpTooltip_MinErrorPercent'))
  202.         gui:AddControl(id, "WideSlider",    0, 1, "stat.auctiondb.confidence", 1, 30, 1, _TRANS('auctionDB_Interface_Confidence') )
  203.         gui:AddTip(id, _TRANS('auctionDB_HelpTooltip_Confidence'))
  204.  
  205.     end
  206.  
  207.     local tooltipID = AucAdvanced.Settings.Gui.tooltipID
  208.  
  209.     addTooltipControls(id)
  210.     if tooltipID then addTooltipControls(tooltipID) end
  211.  
  212.     private.SetupConfigGui = nil -- only run once
  213. end
  214.  
  215. function private.IsauctionDBLoaded()
  216.     TSMAPI = _G.TSMAPI
  217.     return TSMAPI and true or false
  218. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement