WoWInterface SVN BattleBar

[/] [trunk/] [BattleBar.lua] - Rev 6

Compare with Previous | Blame | View Log

BB_VERSION = "0.3b";

-- Define the ADDON
BB = LibStub("AceAddon-3.0"):NewAddon("BattleBar", "AceConsole-3.0", "AceEvent-3.0");
LibStub("AceAddon-3.0"):EmbedLibrary(BB, "LibFuBarPlugin-3.0", true)
-- Define the Locale
local L = LibStub("AceLocale-3.0"):GetLocale("BattleBar");

-- Database Support
local AceDB = LibStub("AceDB-3.0");


-- Config Support
local AceConfig = LibStub("AceConfigDialog-3.0");

--------------------------- Debug Functions ------------------------------

function BB:DebugData(lots)
        BB.State.Scoreboard = {}
        
        BB.State.InBattleground = true;
        
        if (lots == true) then
                BB.State.Scoreboard[1] = { Name = "Narusegawa", Class = "Hunter", Faction = "Alliance", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[2] = { Name = "Voorije", Class = "Shaman", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[3] = { Name = "Deity", Class = "Warrior", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[4] = { Name = "Kasper", Class = "Mage", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[5] = { Name = "Moorije", Class = "Druid", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[6] = { Name = "Evriya", Class = "Warlock", Faction = "Alliance", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[7] = { Name = "Tiesto", Class = "Paladin", Faction = "Alliance", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[8] = { Name = "Speeb", Class = "Priest", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[9] = { Name = "Miriya", Class = "Rogue", Faction = "Alliance", KBS = "23", Dmg = "2353", Heal = "134" }
        else
                BB.State.Scoreboard[1] = { Name = "Narusegawa", Class = "Hunter", Faction = "Alliance", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[2] = { Name = "Voorije", Class = "Shaman", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
                BB.State.Scoreboard[3] = { Name = "Deity", Class = "Warrior", Faction = "Horde", KBS = "23", Dmg = "2353", Heal = "134" }
        end
end

function BB:Debug(message)
        if (BB.db.profile.debug == true) then
                BB:Print("DEBUG: "..message);
        end
end

--------------------------- Basic Addon Functionality ----------------------------------

function BB:OnInitialize()
        BB.db = AceDB:New("BB_Vars");
        BB.db:RegisterDefaults({
                profile = {
                                        debug = false,
                                        hidden = false,
                                        faction = "both",
                                        sortMode = "kbs",
                                        behaviour = {
                                                hideMode = false,
                                                rolloverMode = false
                                        },
                                        gui = {
                                                showSelf = true,
                                                showCount = 10
                                        }
                                  }
        });
        
        -- State Variables
        BB.State = {
                InQueue = false,
                InBattleground = false,
                CurrentBattleground = "",
                Scoreboard = {},
                Rollover = false
        }

        -- Initial Variables
        BB.TimeSinceLastUpdate = 0;
        BB.TimeSinceLastCheck = 0;
        
        BB.UpdateInterval = 1;
        BB.CheckInterval = 5;
        
        -- FuBar Settings
        if LibStub:GetLibrary("LibFuBarPlugin-Mod-3.0", true) then
                -- Create the FuBarPlugin bits.
                self:SetFuBarOption("GameTooltip", true)
                self:SetFuBarOption("hasNoColor", true)
                self:SetFuBarOption("cannotDetachTooltip", true)
                self:SetFuBarOption("hideWithoutStandby", true)
                self:SetFuBarOption("cannotDetachTooltip", true)
                --self:SetFuBarOption("iconPath", "Interface\\AddOns\\MicroManager\\Icon")      
        end
        
        -- Initialize the GUI
        BB:Gui_Init();
        
        -- Register Events
        BB:RegisterEvent("PLAYER_LOGIN");
        BB:RegisterEvent("PLAYER_ENTERING_WORLD"); -- For Checking Enter/Exit Battleground
        BB:RegisterEvent("ZONE_CHANGED_NEW_AREA"); -- For Checking Enter/Exit Battleground
        BB:RegisterEvent("UPDATE_WORLD_STATES"); -- For Parsing Battlfield Score
        BB:RegisterEvent("UPDATE_BATTLEFIELD_SCORE"); -- For Parsing Battlefield Score
end

function BB:OnEnable()
        self:Print("Loaded [ v"..BB_VERSION.." ]");
        BB:Gui_Update();
end

function BB:OnDisable()

end

function BB:OnUpdate(elapsed)
        BB.TimeSinceLastCheck = BB.TimeSinceLastCheck + elapsed;
        
        -- Periodically Check to see if we're in the BG's
        if (BB.TimeSinceLastCheck > BB.CheckInterval) then
                BB:BattlegroundCheck()
                BB:Gui_Update()
                
                BB.TimeSinceLastCheck = 0;
        end

        -- Update our info
        if (BB.State.InBattleground) then
                BB.TimeSinceLastUpdate = BB.TimeSinceLastUpdate + elapsed;      
                if (BB.TimeSinceLastUpdate > BB.UpdateInterval) then
                        BB:UpdateInfo();
                        
                        BB.TimeSinceLastUpdate = 0;
                end
        end
end

--------------------------- FuBar Plugin ---------------------------------------------------

function BB:OnUpdateFuBarText()
        self:SetFuBarText("Battle Bar");
end

function BB:OnUpdateFuBarTooltip()
        GameTooltip:AddLine("Battle Bar");
        GameTooltip:AddLine("Left Click - Open Config.");
        GameTooltip:AddLine("Shift & Left Click - Toggle Main Window.");
end

function BB:OnFuBarClick(button)
        if (button == "LeftButton") then
                if (IsShiftKeyDown()) then
                        BB:ToggleDialog();
                else
                        BB:ShowConfig();
                end
        end
end

--------------------------- Event Handling ------------------------------------------------

function BB:PLAYER_LOGIN()
        BB:BattlegroundCheck()
        BB:Gui_Update();
end

function BB:PLAYER_ENTERING_WORLD()
        BB:BattlegroundCheck()
        BB:Gui_Update();
end

function BB:ZONE_CHANGED_NEW_AREA()
        BB:BattlegroundCheck()
        BB:Gui_Update();
end

function BB:UPDATE_WORLD_STATES()
        BB:UpdateInfo()
end

function BB:UPDATE_BATTLEFIELD_SCORE()
        BB:UpdateInfo()
end

function BB:ToggleDialog()
        if (BB.db.profile.hidden == true) then
                BB:Print("Now Visible");
                BB.db.profile.hidden = false
        else
                BB.db.profile.hidden = true
                BB:Print("Now Hidden");
        end
        
        BB:Gui_Update();
end

--------------------------- Logic Functions -----------------------------------------------

local ClassConvert = 
{
        DRUID = "Druid",
        MAGE = "Mage",
        HUNTER = "Hunter",
        PRIEST = "Priest",
        PALADIN = "Paladin",
        ROGUE = "Rogue",
        SHAMAN = "Shaman",
        WARLOCK = "Warlock",
        WARRIOR = "Warrior"
}

local FactionConvert = 
{
        [0] = "Horde",
        [1] = "Alliance"
}

function BB:ShowConfig()
        --AceConfig:SetDefaultSize("BattleBar", 500, 550)
        AceConfig:Open("BattleBar", nil)
end

function BB:UpdateInfo()
        BB:Debug("Update Pong!");
        
        RequestBattlefieldScoreData();
        
        local member_list = {}
        local n = 0
        nscores = GetNumBattlefieldScores();
        
        
        qq = 1
        for n = 1,nscores,1 do
                local name, killingBlows, honorKills, deaths, honorGained, faction, rank, race, class, ct, dmg, heal = GetBattlefieldScore(n);
                member = {}
                member.Name = name
                member.Faction = FactionConvert[faction]
                member.Class = ClassConvert[ct]
                member.KBS = killingBlows
                member.Dmg = dmg
                member.Heal = heal
                
                if (member.Class == nil) then
                        BB:Debug("Error Converting '"..ct.."' / '"..class.."' to format.");
                end
                
                if (BB.db.profile.faction == "both") then
                        tinsert(member_list, member);
                else
                        if (BB.db.profile.faction == "alliance" and faction == 1) then
                                tinsert(member_list, member)
                        end
                        
                        if (BB.db.profile.faction == "horde" and faction == 0) then
                                tinsert(member_list, member)
                        end
                end
        end
        
        table.sort(member_list, function(a,b) return BB:SortScoreboard(a,b) end);
        
        BB.State.Scoreboard = member_list
        
        BB:Gui_Update();
end

function BB:SortScoreboard(a,b)
        local qq = nil;
        
        local sortMethod = BB.db.profile.sortMode;
        
        --BB:Debug("Sort Method: "..sortMethod);
        
        if (sortMethod == "kbs") then
                --BB:Debug("Sorting by Killing Blows");
                qq = a.KBS>b.KBS;
                if(a.KBS == b.KBS) then
                        qq = a.Dmg > b.Dmg
                end
        end
        
        if (sortMethod == "dmg") then
                --BB:Debug("Sorting by Damage Done");
                qq = a.Dmg>b.Dmg;
        end
        
        if (sortMethod == "heal") then
                --BB:Debug("Sorting by Healing Done");
                qq = a.Heal>b.Heal;
        end
        
        return qq;
end

function BB:Inside_Battlegrounds()
        for i=1, MAX_BATTLEFIELD_QUEUES do
           status, mapName, instanceID = GetBattlefieldStatus(i);
           --BGDebugMessage("DEBUG","status="..status.." mapName="..mapName.." id="..instanceID,"debug");
           if ( instanceID ~= 0 and status == "active") then
                   mapName = mapName.." "..instanceID;
                   return true, mapName
           end
        end
        --status="active"; 
        return false, ""
end

function BB:BattlegroundCheck()
        if (BB.State.InBattleground == true) then
                insBattle, insName = BB:Inside_Battlegrounds()
                if (not insBattle) then
                        -- We've left the battleground.
                        BB.State.InBattleground = insBattle
                        BB.State.CurrentBattleground = insName;
                        BB:Debug("Battleground Exit Detected.");                        
                else
                        BB.State.InBattleground = insBattle;
                        BB.State.CurrentBattleground = insName;
                end
        else
                insBattle, insName = BB:Inside_Battlegrounds()
                if (insBattle) then
                        -- We've entered the battleground.
                        BB.State.InBattleground = insBattle
                        BB.State.CurrentBattleground = insName;
                        BB:Debug("Battleground Enter Detected.");
                else
                        BB.State.InBattleground = insBattle;
                        BB.State.CurrentBattleground = insName;
                end
        end
end

Compare with Previous | Blame