AutoQueueBG/AutoQueueBG.lua
Roman Hergenreder 97221c70f0 Bugfix
2017-11-22 22:51:17 +01:00

344 lines
9.2 KiB
Lua

AutoQueueBG = LibStub("AceAddon-3.0"):NewAddon("AutoQueueBG", "AceConsole-3.0","AceEvent-3.0", "AceTimer-3.0")
local AutoQueueBG = _G.AutoQueueBG
local RANDOM_BG_INDEX = 1
local RAID_MARK_STAR = 1
local RAID_MARK_CONDOM = 2
local RAID_MARK_DIAMOND = 3
local RAID_MARK_TRIANGLE = 4
local RAID_MARK_MOON = 5
local RAID_MARK_SQUARE = 6
local RAID_MARK_CROSS = 7
local RAID_MARK_SKULL = 8
local SYMBOL_OF_KINGS_ID = 21177
local SDK_ID = 20217
local GSDK_ID = 25898
local GSDW_ID = 48938
local SDW_ID = 48936
local SDM_ID = 48932
local GSDM_ID = 48934
local CLASS_WARRIOR = 1
local CLASS_PALADIN = 2
local CLASS_HUNTER = 3
local CLASS_ROGUE = 4
local CLASS_PRIEST = 5
local CLASS_DEATHKNIGHT = 6
local CLASS_SHAMAN = 7
local CLASS_MAGE = 8
local CLASS_WARLOCK = 9
local QueuedMemberCount = QueuedMemberCount
local AQ_Enabled = AQ_Enabled
local AB_Enabled = AB_Enabled
local Frame = Frame
local Tick = Tick
local hasJoined = hasJoined
local messageSent = messageSent
local aMerchants = { ["Drix Finsterzang"] = 39, ["Jeeves"] = 13 }
local function OnUpdate(self, elapsed)
local doUpdate = (floor(Tick + elapsed) > floor(Tick))
Tick = Tick + elapsed
if doUpdate then
if AQ_Enabled then
AutoQueueBG:OnUpdate()
end
if AB_Enabled then
AutoQueueBG:BuyBuffMats()
end
end
end
function AutoQueueBG:CanQueue()
return (GetBattlefieldStatus(RANDOM_BG_INDEX) == "none")
end
function AutoQueueBG:IsQueued()
return (GetBattlefieldStatus(RANDOM_BG_INDEX) == "queued");
end
function AutoQueueBG:IsInBattleground()
return (GetBattlefieldStatus(RANDOM_BG_INDEX) == "active")
end
function AutoQueueBG:CanConfirm()
return (GetBattlefieldStatus(RANDOM_BG_INDEX) == "confirm")
end
function AutoQueueBG:CheckNPC(npcName)
TargetUnit(npcName)
if UnitExists("target") == 1
and GetUnitName("target") == npcName
and UnitIsFriend("target","player") == 1
and UnitIsDeadOrGhost("target") == nil
and CheckInteractDistance("target", 3) == 1 then
return true
end
return false
end
function AutoQueueBG:CheckRaidMember(raidMember)
if UnitExists(raidMember) == 1
and UnitIsFriend(raidMember,"player") == 1
and UnitIsDeadOrGhost(raidMember) == nil
and UnitInRange(raidMember) == 1 then
return true
end
return false
end
function AutoQueueBG:BuyBuffMats()
if not AB_Enabled then
return
end
local buyAmount = 200 - GetItemCount(SYMBOL_OF_KINGS_ID)
if buyAmount > 0 then
for npcName, itemSlot in pairs(aMerchants) do
if AutoQueueBG:CheckNPC(npcName) then
InteractUnit("target")
name, texture, price, quantity = GetMerchantItemInfo(itemSlot)
buyAmount = ceil(buyAmount / quantity)
BuyMerchantItem(itemSlot, buyAmount)
CloseMerchant()
return
end
end
end
end
function AutoQueueBG:IsPreperationTime()
local time = GetBattlefieldInstanceRunTime()
return time > 0 and time < 120000
end
function AutoQueueBG:OnUpdate()
if (not AQ_Enabled) then
return
end
if messageSent > 0 then
messageSent = messageSent - 1
end
if (AutoQueueBG:IsInBattleground()) then
if (GetBattlefieldInstanceExpiration() > 0) then
AutoQueueBG:LeaveBG()
elseif IsRaidLeader() == 1 then
AutoQueueBG:MarkPlayers()
end
if AutoQueueBG:IsPreperationTime() then
AutoQueueBG:BuffPlayers()
end
end
if AutoQueueBG:IsQueued() or AutoQueueBG:CanConfirm() then
if AutoQueueBG:GetQueueMemberCount() > QueuedMemberCount then
AutoQueueBG:LeaveBG()
elseif AutoQueueBG:CanConfirm() then
RequestBattlegroundInstanceInfo(RANDOM_BG_INDEX)
AcceptBattlefieldPort(RANDOM_BG_INDEX, 1)
StaticPopup_Hide("CONFIRM_BATTLEFIELD_ENTRY")
return
else
return
end
end
if(AutoQueueBG:CanQueue()) then
if (QueuedMemberCount > 1 and hasJoined and messageSent == 0) then
SendChatMessage("[AutoQueueBG] Bitte das aktuelle BG verlassen, damit wir als Gruppe anmelden können!", "PARTY", nil)
messageSent = 30
end
AutoQueueBG:QueueBG()
end
end
function AutoQueueBG:BuffDuration(unit, buffName)
name, rank, icon, count, debuffType, duration, expirationTime = UnitBuff(unit, buffName)
if expirationTime == nil then
return 0
else
return -1 * (GetTime() - expirationTime)
end
end
function AutoQueueBG:PlayerNeedsSDM(unit)
localizedClass, englishClass, classIndex = UnitClass(unit);
if classIndex == CLASS_WARRIOR or
classIndex == CLASS_DEATHKNIGHT or
classIndex == CLASS_ROGUE or
classIndex == CLASS_HUNTER then
return true
end
return false
end
function AutoQueueBG:BuffPlayer(unit, spellId)
TargetUnit(unit)
CastSpellByID(spellId)
TargetLastTarget()
end
function AutoQueueBG:BuffPlayers()
local aBuffs = {{GSDK,SDK},{GSDM,SDM},{GSDW,SDW}}
for i = 1, 40 do -- For each raid member
local unit = "raid" .. i
if AutoQueueBG:CheckRaidMember(unit) then
local spellName = GetSpellInfo(GSDK_ID)
if AutoQueueBG:BuffDuration(unit, spellName) < 1500 then
AutoQueueBG:BuffPlayer(unit, GSDK_ID)
return;
end
end
end
end
function AutoQueueBG:MarkPlayers()
if IsRaidLeader() == 1 then
local aMarks = {
["Kulaf"] = RAID_MARK_STAR,
["Jasna"] = RAID_MARK_CONDOM,
["Masimi"] = RAID_MARK_CONDOM,
["Alconex"] = RAID_MARK_TRIANGLE,
["Ytraqpolbeci"] = RAID_MARK_DIAMOND,
["Icebloqparty"] = RAID_MARK_DIAMOND,
["Zars"] = RAID_MARK_MOON,
["Miemiemiee"] = RAID_MARK_SQUARE,
["Amandela"] = RAID_MARK_CROSS,
["Chugian"] = RAID_MARK_MOON,
["Mev"] = RAID_MARK_MOON,
["Naiguh"] = RAID_MARK_MOON,
["Zalaras"] = RAID_MARK_TRIANGLE,
["Leiana"] = RAID_MARK_TRIANGLE,
["Seyjóu"] = RAID_MARK_SQUARE,
["Morcsi"] = RAID_MARK_SQUARE,
["Ravenson"] = RAID_MARK_DIAMOND,
["Gurtogg"] = RAID_MARK_CONDOM
}
for k, v in pairs(aMarks) do
AutoQueueBG:MarkPlayer(k, v)
end
end
end
function AutoQueueBG:IsMarkSet(mark)
for i = 1, 40 do -- For each raid member
local unit = "raid" .. i
if UnitExists(unit) then
if GetRaidTargetIndex(unit) == mark then
return true
end
end
end
return false
end
function AutoQueueBG:MarkPlayer(name, mark)
if UnitExists(name) and not AutoQueueBG:IsMarkSet(mark) then
SetRaidTarget(name, mark)
end
end
function AutoQueueBG:GetQueueMemberCount()
return (GetNumPartyMembers() + 1)
end
function AutoQueueBG:LeaveBG()
hasJoined = false
messageSent = 0
if AutoQueueBG:IsInBattleground() then
LeaveBattlefield()
else
AcceptBattlefieldPort(RANDOM_BG_INDEX, nil)
end
end
function AutoQueueBG:QueueBG()
if (IsPartyLeader() == nil or IsPartyLeader() == 1) then
RequestBattlegroundInstanceInfo(RANDOM_BG_INDEX)
QueuedMemberCount = AutoQueueBG:GetQueueMemberCount()
local joinAsGroup = (QueuedMemberCount == 1 and 0 or 1)
hasJoined = true
JoinBattlefield(RANDOM_BG_INDEX, joinAsGroup)
end
end
function AutoQueueBG:OnCommand(input)
local arg1, arg2 = AutoQueueBG:GetArgs(input, 2)
ChatType = id
if arg1 == nil then
print("[AutoQueueBG] Usage: /autoqueue [start|stop|status|autobuy]")
return
end
if string.lower(arg1) == "start" then
if AQ_Enabled then
print("AutoQueueBG ist bereits aktiv.")
else
AQ_Enabled = true
Tick = 0
print("AutoQueueBG aktiviert.")
end
elseif string.lower(arg1) == "stop" then
if not AQ_Enabled then
print("AutoQueueBG ist nicht aktiv.")
else
AQ_Enabled = false
-- Frame:SetScript("OnUpdate", nil)
print("AutoQueueBG deaktiviert.")
end
elseif string.lower(arg1) == "status" then
print("[AutoQueueBG] Status: " .. (AQ_Enabled and "Aktiviert" or "Deaktiviert"))
elseif string.lower(arg1) == "autobuy" then
AB_Enabled = not AB_Enabled
print("[AutoQueueBG] AutoBuy " .. (AB_Enabled and "Aktiviert" or "Deaktiviert"))
end
end
function AutoQueueBG:OnInitialize()
AQ_Enabled = false
AB_Enabled = true
Frame = CreateFrame("frame")
Frame:SetScript("OnUpdate", OnUpdate)
Tick = 0
hasJoined = false
messageSent = 0
QueuedMemberCount = 1
end
function string.starts(String,Start)
return string.sub(String,1,string.len(Start))==Start
end
function AutoQueueBG:OnSystemMessage(type, msg)
if (string.starts(msg, "Spieler in Warteschlange: ") and QueuedMemberCount > 1) then
local queued = string.match(msg, "%d+")
SendChatMessage("[AutoQueueBG] Als Gruppe angemeldet. Spieler: " .. queued .. "/20", "PARTY", nil)
end
end
function AutoQueueBG:OnEnable(first)
AutoQueueBG:RegisterEvent("CHAT_MSG_SYSTEM","OnSystemMessage")
AutoQueueBG:RegisterChatCommand("autoqueue", "OnCommand")
AutoQueueBG:RegisterChatCommand("aq", "OnCommand")
end
function AutoQueueBG:OnDisable()
AutoQueueBG:UnregisterAllEvents()
AutoQueueBG:UnregisterChatCommand("autoqueue")
AutoQueueBG:UnregisterChatCommand("aq")
end