Advertisement
Guest User

junkit

a guest
Jun 7th, 2014
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 14.84 KB | None | 0 0
  1. require "Window"
  2. require "string"
  3. require "math"
  4. require "Sound"
  5. require "Item"
  6. require "Money"
  7. require "GameLib"
  8.  
  9. -- Sorted Pairs function, pass in the sort function
  10. local function spairs(t, order)
  11.     -- Collect keys
  12.     local keys = {}
  13.     for k in pairs(t) do keys[#keys + 1] = k end
  14.  
  15.     -- If order function given, sort by passing the table and keys a, b,
  16.     -- otherwise just sort keys
  17.     if order then
  18.         table.sort(keys, function(a,b) return order(t, a, b) end)
  19.     else
  20.         table.sort(keys)
  21.     end
  22.    
  23.     -- Return the iterator function
  24.     local i = 0
  25.     return function()
  26.         i = i + 1
  27.         if keys[i] then
  28.             return keys[i], t[keys[i]]
  29.         end
  30.     end
  31. end
  32.  
  33.  
  34. local function TableMerge(t1, t2)
  35.   for k,v in pairs(t2) do
  36.     if type(v) == "table" then
  37.       if type(t1[k] or false) == "table" then
  38.         TableMerge(t1[k] or {}, t2[k] or {})
  39.       else
  40.         t1[k] = v
  41.       end
  42.     else
  43.       t1[k] = v
  44.     end
  45.   end
  46.   return t1
  47. end
  48.  
  49. local JunkIt = {}
  50. local GeminiLocale, L
  51.  
  52. -----------------------------------------------------------------------------------------------
  53. -- Constants
  54. -----------------------------------------------------------------------------------------------
  55. -- Considered using ItemFamilyName and ItemCategoryName but unsure if localization would affect
  56.  
  57. local ItemFamily = {
  58.     Armor = 1,
  59.     Weapon = 2,
  60.     Bag = 5,
  61.     Ornamental = 15, -- Shield
  62.     Consumable = 16,
  63.     Reagent = 18, -- Crafting Reagents
  64.     Housing = 20, -- Housing according to inventory
  65.     --Housing = 22, -- What
  66.     QuestItem = 24,
  67.     Costume = 26,
  68.     Crafting = 27,
  69.     Schematic = 66,
  70. }
  71.  
  72. local ItemCategory = {
  73.     Junk = 94,
  74.     Schematic = 66,
  75. }
  76. local ktReverseQualityLookup = {}
  77.  
  78. function JunkIt:new(o)
  79.     o = o or {}
  80.     setmetatable(o, self)
  81.     self.__index = self
  82.  
  83.     o.config = {}
  84.     o.config.sellArmor = true
  85.     o.config.sellWeapons = true
  86.     o.config.sellCostumes = true
  87.     o.config.sellShields = true
  88.     o.config.keepSalvage = false
  89.     o.config.autoSell = false
  90.     o.config.sellHousing = false
  91.     o.config.autoRepair = false
  92.     o.config.showButton = true
  93.  
  94.     o.config.minSellQuality = Item.CodeEnumItemQuality.Average
  95.    
  96.     return o
  97. end
  98.  
  99. function JunkIt:Init()
  100.     Apollo.RegisterAddon(self, false, "", {"Vendor","Gemini:Locale-1.0"})
  101. end
  102.  
  103. ---------------------------------------------------------------------------------------------------
  104. -- JunkIt Load Functions
  105. ---------------------------------------------------------------------------------------------------
  106. function JunkIt:OnLoad()
  107.     -- Read in the file only once, presumably file IO is more costly
  108.     self.Xml = XmlDoc.CreateFromFile("JunkIt.xml")
  109.     self.Xml:RegisterCallback("OnDocLoaded", self)
  110.     GeminiLocale = Apollo.GetPackage("Gemini:Locale-1.0").tPackage
  111.     L = GeminiLocale:GetLocale("JunkIt", false)
  112. end
  113.  
  114. function JunkIt:OnDocLoaded()
  115.     -- Store reference to the Vendor addon
  116.     self.vendorAddon = Apollo.GetAddon("Vendor")
  117.  
  118.     -- Event thrown by opening the a Vendor window
  119.     Apollo.RegisterEventHandler("InvokeVendorWindow",   "OnInvokeVendorWindow", self)
  120.     -- Event thrown by closing the a Vendor window
  121.     Apollo.RegisterEventHandler("CloseVendorWindow",    "OnVendorClosed", self)
  122.  
  123.     -- Boolean to indicate options need configured
  124.     self.bConfigOptions = true
  125.    
  126.     -- Init ReverseLookup
  127.     for k,v in pairs(Item.CodeEnumItemQuality) do
  128.         ktReverseQualityLookup[v] = k
  129.     end
  130. end
  131.  
  132. function JunkIt:OnDependencyError(strDep, strError)
  133.     return false
  134. end
  135.  
  136. ---------------------------------------------------------------------------------------------------
  137. -- JunkIt EventHandlers
  138. ---------------------------------------------------------------------------------------------------
  139.  
  140.  
  141. -- Sets the selected options on the Options Frame, this is delayed to ensure both new users
  142. --  and people who have used the addon before see options as configured in the options pane.
  143. --  uses bool to indicate has already been done.
  144. function JunkIt:SetupJunkIt()
  145.     -- Load forms, parent to vendor addon
  146.     self.wndJunkOpts = Apollo.LoadForm(self.Xml, "OptionsContainer", self.vendorAddon.wndVendor:FindChild("BGFrame"), self)
  147.     GeminiLocale:TranslateWindow(L, self.wndJunkOpts)
  148.     self.wndOpt = Apollo.LoadForm(self.Xml, "VendorWindowOverlay", self.vendorAddon.wndVendor:FindChild("BGFrame"), self)
  149.     self.wndJunkButton = Apollo.LoadForm(self.Xml, "JunkButtonOverlay", self.vendorAddon.wndVendor, self)
  150.  
  151.     -- Show the Options button all the time
  152.     self.wndOpt:Show(true)
  153.    
  154.     -- Hide the Options Frame by default
  155.     self.wndJunkOpts:Show(false, true)
  156.  
  157.     -- Iterate through config options
  158.     for k,v in pairs(self.config) do
  159.         -- Boolean options are the checkboxes, so set the check appropriately
  160.         if (type(v) == "boolean") then
  161.             if self.wndJunkOpts:FindChild(k) ~= nil then
  162.                 self.wndJunkOpts:FindChild(k):SetCheck(v)
  163.             end
  164.         else -- Item Quality Type, set the ComboBox selected and the right color
  165.             self.wndJunkOpts:FindChild("QualityDropDown"):SetText(Apollo.GetString("CRB_" .. ktReverseQualityLookup[v]))
  166.             self.wndJunkOpts:FindChild("QualityDropDown"):SetNormalTextColor("ItemQuality_" .. ktReverseQualityLookup[v])
  167.         end
  168.     end
  169.     -- Show/Hide Sell button based on autosell config
  170.     self:SetButtonState()
  171.     -- Set boolean to indicate options have been set
  172.     self.bConfigOptions = false
  173. end
  174.  
  175. -- Event Handler for Vendor Window closing
  176. function JunkIt:OnVendorClosed()
  177.     -- If the button is not set unchecked it appears checked next time window is opened
  178.     self.wndOpt:FindChild("OptionsBtn"):SetCheck(false)
  179.     -- Hide Options window when vendor is closed, prevents options from being open next time vendor is opened
  180.     self.wndJunkOpts:Show(false)
  181.     -- If we are showing a submenu get rid of it
  182.     if self.wndSubMenu then
  183.         local wndPopout = self.wndSubMenu:GetData():GetParent():FindChild("QualityPopoutBtn")
  184.         if wndPopout ~= nil then
  185.             wndPopout:SetCheck(false)
  186.         end
  187.         self.wndSubMenu:Destroy()
  188.     end
  189. end
  190.  
  191. -- Event handler for Vendor window opening.
  192. function JunkIt:OnInvokeVendorWindow(unitArg)
  193.     -- Check and see if options pane needs to be synced, if so sync it
  194.     if self.bConfigOptions then
  195.         self:SetupJunkIt()
  196.     end
  197.  
  198.     local nItemsSold = nil
  199.     if self.config.autoSell then
  200.         nItemsSold = self:SellItems(true)
  201.     end
  202.  
  203.     local nItemsRepaired = nil
  204.     if self.config.autoRepair and IsRepairVendor(unitArg) then
  205.         nItemsRepaired = self:RepairItems(unitArg)
  206.     end
  207.  
  208.     -- Didn't do anything interesting, exit out.
  209.     if not nItemsSold and not nItemsRepaired then return end
  210.  
  211.     local strAlertMsg = "JunkIt"
  212.  
  213.     if nItemsSold then
  214.         strAlertMsg = string.format("%s: Sold %d Items", strAlertMsg, nItemsSold)
  215.     end
  216.  
  217.     if nItemsRepaired then
  218.         strAlertMsg = string.format("%s: Repaired %d Items", strAlertMsg, nItemsRepaired)
  219.     end
  220.     self.vendorAddon:ShowAlertMessageContainer(strAlertMsg, false)
  221. end
  222.  
  223. -----------------------------------------------------------------------------------------------
  224. -- Selling Functions
  225. -----------------------------------------------------------------------------------------------
  226. function JunkIt:SellItems(bAuto)
  227.     local tItemsToSell, itemCount = self:GetSellItems()
  228.     if tItemsToSell ~= nil and itemCount > 0 then
  229.         for _, v in ipairs(tItemsToSell) do
  230.             SellItemToVendorById(v:GetInventoryId(), v:GetStackCount())
  231.         end
  232.  
  233.         if not bAuto then
  234.             self.vendorAddon:ShowAlertMessageContainer("JunkIt: Sold " .. itemCount .. " items", false)
  235.         else
  236.             return itemCount
  237.         end
  238.     end
  239. end
  240.  
  241. function JunkIt:GetSellItems()
  242.     local invItems = GameLib.GetPlayerUnit():GetInventoryItems()
  243.     local sellItems = {}
  244.     local itemCount = 0
  245.     for _, v in ipairs(invItems) do
  246.         if self:IsSellable(v.itemInBag) then
  247.             table.insert(sellItems, v.itemInBag)
  248.             itemCount = itemCount + 1
  249.         end
  250.     end
  251.    
  252.     return sellItems, itemCount
  253. end
  254.  
  255. function JunkIt:IsSellable(item)
  256.     -- You can't sell items that don't exist or have no price
  257.     --if not item or not item:GetSellPrice() then return false end
  258.  
  259.     -- Determine if the item is junk, if it is, vend it!
  260.     if item:GetItemCategory() == ItemCategory.Junk then return true end
  261.     --if item:GetItemCategory() == ItemCategory.Schematic then return true end
  262.  
  263.     -- If item quality is set to inferior, keep all items
  264.     if self.config.minSellQuality == Item.CodeEnumItemQuality.Inferior then return false end
  265.  
  266.     -- If we are keeping salvagable items and this one is salvageable, then this isn't for sale
  267.     if self.config.keepSalvage and item:CanSalvage() then return false end
  268.  
  269.     -- Pull the itemFamily to reduce # of function calls
  270.     local itemFamily = item:GetItemFamily()
  271.    
  272.     --  Are we selling this type of item?
  273.     if ((itemFamily == ItemFamily.Armor and self.config.sellArmor) or
  274.         (itemFamily == ItemFamily.Weapon and self.config.sellWeapons) or
  275.         (itemFamily == ItemFamily.Ornamental and self.config.sellShields) or
  276.         (itemFamily == ItemFamily.Housing and self.config.sellHousing) or
  277.         (itemFamily == ItemFamily.Consumable and self.config.sellArmor) or
  278.         (itemFamily == ItemFamily.Schematic and self.config.sellArmor) or
  279.         (itemFamily == ItemFamily.Costume and self.config.sellCostumes)) then
  280.         -- Is it under our threshold?
  281.         if item:GetItemQuality() == Item.CodeEnumItemQuality.Inferior or self.config.minSellQuality > item:GetItemQuality() or item:GetItemCategory() == ItemCategory.Schematic then return true end
  282.     end
  283.  
  284.     -- Default is no, it is not sellable
  285.     return false
  286. end
  287.  
  288. ---------------------------------------------------------------------------------------------------
  289. -- Save/Restore Settings Functions
  290. ---------------------------------------------------------------------------------------------------
  291. function JunkIt:OnSave(eLevel)
  292.     if eLevel ~= GameLib.CodeEnumAddonSaveLevel.Character then return nil end
  293.     return self.config
  294. end
  295.  
  296. function JunkIt:OnRestore(eLevel, tData)
  297.     TableMerge(self.config, tData)
  298. end
  299.  
  300. -----------------------------------------------------------------------------------------------
  301. -- Repair Functions
  302. -----------------------------------------------------------------------------------------------
  303. function JunkIt:RepairItems(unitArg)
  304.     local nRepairedItems = self:CanRepair(unitArg)
  305.     if not nRepairedItems then return nil end
  306.     -- Perform Repair
  307.     RepairAllItemsVendor()
  308.     -- Reset Repair Tab information
  309.     self.vendorAddon:RefreshRepairTab()
  310.     -- return the number of items repaired
  311.     return nRepairedItems
  312. end
  313.  
  314. function JunkIt:CanRepair(unitArg)
  315.     local repairableItems = unitArg:GetRepairableItems()
  316.     local nCount = 0
  317.     if repairableItems ~= nil then
  318.         nCount = #repairableItems
  319.     end
  320.     if nCount == 0 then
  321.         return false
  322.     end
  323.     return nCount
  324. end
  325.  
  326. ---------------------------------------------------------------------------------------------------
  327. -- VendorWindow Functions
  328. ---------------------------------------------------------------------------------------------------
  329.  
  330. -- Handle the Options button, both show and hide
  331. function JunkIt:OnOptionsMenuToggle( wndHandler, wndControl, eMouseButton )
  332.     self.wndJunkOpts:FindChild("OptionsContainer"):Show(self.wndOpt:FindChild("OptionsBtn"):IsChecked())
  333. end
  334.  
  335. ---------------------------------------------------------------------------------------------------
  336. -- VendorWindowOverlay Functions
  337. ---------------------------------------------------------------------------------------------------
  338.  
  339. -- Handler for Options Close button
  340. function JunkIt:OnOptionsCloseClick( wndHandler, wndControl, eMouseButton )
  341.     self.wndOpt:FindChild("OptionsBtn"):SetCheck(false)
  342.     self:OnOptionsMenuToggle()
  343. end
  344.  
  345. -- Handler for Junk Button
  346. function JunkIt:OnJunkButtonClick( wndHandler, wndControl, eMouseButton )
  347.     self:SellItems()
  348. end
  349.  
  350. ---------------------------------------------------------------------------------------------------
  351. -- OptionsContainer Functions
  352. ---------------------------------------------------------------------------------------------------
  353.  
  354. function JunkIt:SetButtonState()
  355.     local showButton
  356.     if self.config.autoSell then
  357.         showButton = self.config.showButton
  358.     else
  359.         showButton = true
  360.     end
  361.  
  362.     self.wndJunkButton:Show(showButton)
  363.     self.wndJunkOpts:FindChild("showButton"):Show(self.config.autoSell)
  364. end
  365.  
  366. -- Handler for all of the checkboxes in the Options window
  367. function JunkIt:OnCheckboxChange( wndHandler, wndControl, eMouseButton )
  368.     local wndControlName = wndControl:GetName()
  369.     self.config[wndControlName] = wndControl:IsChecked()
  370.     -- Special cases, aren't they special?
  371.     -- If we turn on AutoSell, or change the showButton setting, honor it.
  372.     if wndControlName == "autoSell" then
  373.         wndControl:FindChild("showButton"):Show(self.config.autoSell)
  374.         self:SetButtonState()
  375.     elseif wndControlName == "showButton" then
  376.         self:SetButtonState()
  377.     end
  378. end
  379.  
  380. function JunkIt:OnQualityPopoutToggle( wndHandler, wndControl, eMouseButton )
  381.     if self.wndSubMenu and self.wndSubMenu:GetParent() == wndControl then
  382.         if not wndControl:IsChecked() and self.wndSubMenu then
  383.             self.wndSubMenu:Destroy()
  384.         end
  385.         return
  386.     end
  387.     if self.wndSubMenu then
  388.         self.wndSubMenu:GetData():GetParent():FindChild("QualityPopoutBtn"):SetCheck(false)
  389.         self.wndSubMenu:Destroy()
  390.     end
  391.     self.wndSubMenu = Apollo.LoadForm(self.Xml, "QualityMenu", wndControl, self)
  392.     local strBtnName = "sell" .. string.match(self.wndSubMenu:GetData():GetName(),"(.-)Container")
  393.     self.wndSubMenu:SetData(wndControl:GetParent():FindChild(strBtnName))
  394. end
  395.  
  396. function JunkIt:OnQualityDropdownToggle( wndHandler, wndControl, eMouseButton )
  397.     if self.wndSubMenu then
  398.         local wndPopout = self.wndSubMenu:GetData():GetParent():FindChild("QualityPopoutBtn")
  399.         if wndPopout ~= nil then
  400.             wndPopout:SetCheck(false)
  401.         end
  402.         self.wndSubMenu:Destroy()
  403.         if not wndControl:IsChecked() then
  404.             return
  405.         end
  406.     end
  407.     self.wndSubMenu = Apollo.LoadForm(self.Xml, "QualityMenu", wndControl, self)
  408.     self.wndSubMenu:SetData(wndControl)
  409.     self.wndSubMenu:SetAnchorPoints(0,1,1,0)
  410.     self.wndSubMenu:SetAnchorOffsets(-21, -26, 21, 246)
  411. end
  412.  
  413. ---------------------------------------------------------------------------------------------------
  414. -- QualityMenu Functions
  415. ---------------------------------------------------------------------------------------------------
  416.  
  417. function JunkIt:OnQualityBtnClicked( wndHandler, wndControl, eMouseButton )
  418.     self.wndSubMenu:GetData():SetText(wndControl:FindChild("QualityBtnTxt"):GetText())
  419.     self.wndSubMenu:GetData():SetNormalTextColor("ItemQuality_"..wndControl:GetName())
  420.     local wndPopout = self.wndSubMenu:GetData():GetParent():FindChild("QualityPopoutBtn")
  421.     if wndPopout ~= nil then
  422.         wndPopout:SetCheck(false)
  423.     else
  424.         self.wndSubMenu:GetData():SetCheck(false)
  425.     end
  426.  
  427.     local strWndName = wndControl:GetName()
  428.     for k,v in pairs(ktReverseQualityLookup) do
  429.         if v == strWndName then
  430.             self.config.minSellQuality = k
  431.             break
  432.         end
  433.     end
  434.  
  435.     self.wndSubMenu:Destroy()
  436.     if self.config.autoSell then
  437.         self:SellItems()
  438.     end
  439. end
  440.  
  441. ---------------------------------------------------------------------------------------------------
  442. -- JunkIt instance
  443. ---------------------------------------------------------------------------------------------------
  444. local JunkItInst = JunkIt:new()
  445. JunkItInst:Init()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement