Quantcast

Feature: /buygold command

Alar of Daggerspine [06-05-16 - 19:21]
Feature: /buygold command

Automatically uses resource to buy money satchel. You must manually open
the trader window
Filename
GarrisonCommander-Broker/ldb.lua
diff --git a/GarrisonCommander-Broker/ldb.lua b/GarrisonCommander-Broker/ldb.lua
index db3a2bf..ac81cd6 100644
--- a/GarrisonCommander-Broker/ldb.lua
+++ b/GarrisonCommander-Broker/ldb.lua
@@ -369,6 +369,7 @@ function addon:OnInitialized()
 	self:AddSlider("FREQUENCY",5,1,60,L["Update frequency"])
 	frequency=self:GetNumber("FREQUENCY",5)
 	self:ScheduleTimer("DelayedInit",1)
+	return self:ResBuyer()
 end
 function addon:ApplyFREQUENCY(value)
 	frequency=value
@@ -743,6 +744,69 @@ function dataobj:OldUpdate()
 	end
 	self.text=format("%s: %s (Tot: |cff00ff00%d|r) %s: %s",READY,ready,completed,NEXT,prox)
 end-- Resources rate: 144 a day
+local satchel_id=120146
+local satchel_name
+local satchel_link
+local satchel_index
+local button
+function addon:ResBuyer()
+	button=CreateFrame("Button",nil,UIParent,"SecureActionButtonTemplate")
+	button:SetAttribute("type1","item")
+	satchel_name,satchel_link=GetItemInfo(satchel_id)
+	button:SetAttribute("item",satchel_name)
+	self:AddChatCmd("Buygold","buygold",L["Use at trade merchant to buy multiple gold with resource"])
+
+end
+function addon:Buygold(args,...)
+	print(args,...)
+	if not args  or args=="" then
+		self:Print("Use /buygold <resource to use>")
+		self:Print("Example: /buygold 1000 will consume 1000 resourcesm i.e. acquire 20 " .. satchel_link)
+		return
+	end
+	local resources=math.floor((tonumber(strsplit(" ",args)) or 50)/50)
+	if not satchel_link then
+		satchel_name,satchel_link=GetItemInfo(satchel_id)
+		button:SetAttribute("item",satchel_name)
+	end
+	if not MerchantFrame:IsVisible() then
+		self:Print("Please open trader frame to buy " .. satchel_link)
+		return
+	end
+	satchel_index=nil
+	if IsMapGarrisonMap(GetCurrentMapAreaID()) then
+		for i=1,GetMerchantNumItems() do
+			local l=GetMerchantItemLink(i)
+			local id=self:GetItemID(l)
+			if id==satchel_id then
+				satchel_index=i
+				break
+			end
+		end
+	end
+	if not satchel_index then
+		self:Print("This trader is not the right one for " .. satchel_link)
+		return
+	end
+	self:Print("Buying " .. resources .. ' '  .. satchel_link)
+	local gold=GetMoney()
+	local buyer=function()
+		for i=1,resources do
+			BuyMerchantItem(satchel_index,1)
+			coroutine.yield(true)
+			coroutine.yield(true)
+			button:Click()
+			coroutine.yield(true)
+		end
+		while GetItemCount(satchel_id) > 0 do
+			button:Click()
+			coroutine.yield(true)
+			coroutine.yield(true)
+		end
+		addon:Print("You earned " .. GetMoneyString(GetMoney()-gold))
+	end
+	self:coroutineExecute(0.2,buyer)
+end
 local function convert(perc,numeric)
 	perc=max(0,min(10,perc))
 	if numeric then