Gnome Runner addon

 avatar
unknown
lua
a year ago
10 kB
7
Indexable
-- Create a local table for your addon namespace
local GnomeRunner = {}

-- Add your variables to the table
GnomeRunner.frame = CreateFrame("Frame", "GnomeRunnerFrame")
GnomeRunner.flareSpellIDs = {
    30263, -- Red Smoke Flare
    30262, -- White Smoke Flare
    32812, -- Purple Smoke Flare
    30264  -- Green Smoke Flare
}

GnomeRunner.prizePot = 0
GnomeRunner.payoutSet = false
GnomeRunner.raceInProgress = false
GnomeRunner.raceStartTime = 0
GnomeRunner.elapsedTime = 0
GnomeRunner.raceName = "Race Event"
GnomeRunner.timerUpdateInterval = 30 -- Update every 30 seconds
GnomeRunner.countdownInProgress = false
GnomeRunner.countdownSeconds = 10
GnomeRunner.totalDeaths = 0
GnomeRunner.totalRacers = 0
GnomeRunner.totalGoldDistributed = 0

GnomeRunner.playerGUID = UnitGUID("player")

local function UpdateTimer()
    if GnomeRunner.raceInProgress then
        local currentTime = GetServerTime()
        GnomeRunner.elapsedTime = math.max(currentTime - GnomeRunner.raceStartTime, 60) -- Ensure at least 1 minute is displayed

        local minutesElapsed = math.floor(GnomeRunner.elapsedTime / 60)
        local secondsElapsed = GnomeRunner.elapsedTime % 60

        -- Check if the elapsed time is a multiple of 30 minutes
        if secondsElapsed == 0 and minutesElapsed > 0 and minutesElapsed % 30 == 0 then
            SendChatMessage("Elapsed Time: " .. minutesElapsed .. " minutes", "RAID")
        end
    end
end

local function EndRace()
    if GnomeRunner.raceInProgress then
        SendChatMessage("Thank you for coming to \"" .. GnomeRunner.raceName .. "\". This is the race's final stats:", "RAID")
        SendChatMessage("Deaths: " .. GnomeRunner.totalDeaths, "RAID")
        SendChatMessage("Total Elapsed Time: " .. math.floor(GnomeRunner.elapsedTime / 60) .. " minutes", "RAID")
        SendChatMessage("Total Racers: " .. GnomeRunner.totalRacers, "RAID")
        SendChatMessage("Total Gold Distributed: " .. GnomeRunner.prizePot .. " gold", "RAID")

        GnomeRunner.raceInProgress = false
        GnomeRunner.raceStartTime = 0
        GnomeRunner.elapsedTime = 0
        GnomeRunner.totalDeaths = 0
        GnomeRunner.totalRacers = 0
        GnomeRunner.totalGoldDistributed = 0
    else
        print("No race in progress.")
    end
end

local function CheckPlayer()
    local playerName = UnitName("player")

    local inRaid = IsInRaid()
    local _, instanceType, _, _, _, _, _, instanceMapID = GetInstanceInfo()

    if inRaid and instanceType == "raid" then
        GnomeRunner.totalRacers = 0  -- Reset totalRacers
        GnomeRunner.totalDeaths = 0  -- Reset totalDeaths

        for i = 1, GetNumGroupMembers() do
            local unit = "raid" .. i

            if UnitIsDeadOrGhost(unit) and UnitName(unit) == playerName then
                GnomeRunner.totalDeaths = GnomeRunner.totalDeaths + 1
                SendChatMessage(playerName .. " has died!", "RAID_WARNING")
            end

            GnomeRunner.totalRacers = GnomeRunner.totalRacers + 1
        end
    end
end

function GnomeRunner.frame.OnEvent(self, event, ...)
    if event == "RAID_ROSTER_UPDATE" then
        CheckPlayer()
    elseif event == "UNIT_SPELLCAST_SUCCEEDED" then
        local unit, _, _, _, spellID = ...

        if unit == "player" then
            local spellName, _, _, _, _, _, _, spellID = UnitCastingInfo("player")

            if spellName then
                for _, flareSpellID in ipairs(GnomeRunner.flareSpellIDs) do
                    if flareSpellID == spellID then
                        local playerName = UnitName("player")

                        SendChatMessage(playerName .. " used a flare!", "RAID")
                        SendChatMessage(playerName .. " used a flare!", "RAID_WARNING")

                        return
                    end
                end
            end
        else
            local playerName = UnitName(unit)

            -- Check if the event is for a friendly player
            if UnitIsPlayer(unit) and UnitInRaid(unit) then
                local isAssistant = false
                local isLeader = false

                for i = 1, GetNumGroupMembers() do
                    local _, _, subgroup, _, _, _, _, _, _, _, _, isAssistant, isLeader = GetRaidRosterInfo(i)
                    if isAssistant or isLeader then
                        -- Ignore assistants and raid leaders
                        return
                    end
                end

                local buffs = {}
                for i = 1, 40 do
                    local _, _, _, _, _, _, _, _, _, spellId = UnitAura(unit, i, "HELPFUL")

                    if spellId then
                        local spellName = GetSpellInfo(spellId)
                        table.insert(buffs, spellName)
                    else
                        break
                    end
                end

                local debuffs = {}
                for i = 1, 40 do
                    local _, _, _, _, _, _, _, _, _, spellId = UnitAura(unit, i, "HARMFUL")

                    if spellId then
                        local spellName = GetSpellInfo(spellId)
                        table.insert(debuffs, spellName)
                    else
                        break
                    end
                end

                if #buffs > 0 then
                    SendChatMessage(playerName .. " gained buffs: " .. table.concat(buffs, ", "), "RAID")
                end
                if #debuffs > 0 then
                    SendChatMessage(playerName .. " gained debuffs: " .. table.concat(debuffs, ", "), "RAID")
                end
            end
        end
    elseif event == "UNIT_AURA" then
        local unit = ...
        local playerName = UnitName(unit)

        -- Check if the event is for a friendly player
        if UnitIsPlayer(unit) and UnitInRaid(unit) then
            local isAssistant = false
            local isLeader = false

            for i = 1, GetNumGroupMembers() do
                local _, _, subgroup, _, _, _, _, _, _, _, _, isAssistant, isLeader = GetRaidRosterInfo(i)
                if isAssistant or isLeader then
                    -- Ignore assistants and raid leaders
                    return
                end
            end

            local buffs = {}
            for i = 1, 40 do
                local _, _, _, _, _, _, _, _, _, spellId = UnitAura(unit, i, "HELPFUL")

                if spellId then
                    local spellName = GetSpellInfo(spellId)
                    table.insert(buffs, spellName)
                else
                    break
                end
            end

            local debuffs = {}
            for i = 1, 40 do
                local _, _, _, _, _, _, _, _, _, spellId = UnitAura(unit, i, "HARMFUL")

                if spellId then
                    local spellName = GetSpellInfo(spellId)
                    table.insert(debuffs, spellName)
                else
                    break
                end
            end

            if #buffs > 0 then
                SendChatMessage(playerName .. " gained buffs: " .. table.concat(buffs, ", "), "RAID")
            end
            if #debuffs > 0 then
                SendChatMessage(playerName .. " gained debuffs: " .. table.concat(debuffs, ", "), "RAID")
            end
        end
    end
end

GnomeRunner.frame:RegisterEvent("UNIT_SPELLCAST_SUCCEEDED")
GnomeRunner.frame:RegisterEvent("UNIT_AURA")
GnomeRunner.frame:RegisterEvent("RAID_ROSTER_UPDATE")
GnomeRunner.frame:SetScript("OnEvent", GnomeRunner.frame.OnEvent)

SLASH_START_RACE1 = "/race"
SlashCmdList["START_RACE"] = function(msg)

    if msg == "" then
        if not IsInRaid() then
            print("You are not in a raid. You must be in a raid to use /race.")
            return
        end

        if not GnomeRunner.payoutSet or GnomeRunner.raceName == "Race Event" then
            print("Payout or Race Name not set. Use /payout and /racename to set the prize pot and race name.")
            return
        end

        if not GnomeRunner.raceInProgress then
            print("Starting the race!")
            GnomeRunner.raceInProgress = true
            GnomeRunner.raceStartTime = GetServerTime()
            GnomeRunner.countdownInProgress = true
            C_Timer.NewTicker(1, function()
                if GnomeRunner.countdownInProgress then
                    GnomeRunner.countdownSeconds = GnomeRunner.countdownSeconds - 1
                    if GnomeRunner.countdownSeconds > 0 then
                        SendChatMessage("Race starting in " .. GnomeRunner.countdownSeconds .. " seconds!", "RAID_WARNING")
                    else
                        SendChatMessage("GO GO GO! The race has started for \"" .. GnomeRunner.raceName .. "\"!", "RAID_WARNING")
                        GnomeRunner.countdownInProgress = false
                        GnomeRunner.countdownSeconds = 10
                    end
                end
            end)

            -- Add the following line to update the timer every 30 seconds
            C_Timer.NewTicker(GnomeRunner.timerUpdateInterval, UpdateTimer)
        else
            print("Race is already in progress.")
        end
    else
        print("Invalid command. Usage: /race")
    end
end

SLASH_SET_PAYOUT1 = "/payout"
SlashCmdList["SET_PAYOUT"] = function(msg)
    local amount = tonumber(string.match(msg:lower(), "%d+"))
    if amount and amount > 0 then
        GnomeRunner.prizePot = amount
        GnomeRunner.payoutSet = true
        print("Prize pot set to " .. amount .. " gold. You can now use /race.")
        SendChatMessage("The prize pot for the race has been set to " .. amount .. " gold!", "RAID_WARNING")
    else
        print("Invalid command. Usage: /payout gold amount")
    end
end

SLASH_SET_RACENAME1 = "/racename"
SlashCmdList["SET_RACENAME"] = function(msg)
    if msg ~= "" then
        GnomeRunner.raceName = msg
        SendChatMessage("Welcome to " .. GnomeRunner.raceName .. "! The race will start shortly.", "RAID_WARNING")
        print("Race name set to: " .. GnomeRunner.raceName)
    else
        print("Invalid command. Usage: /racename <message>")
    end
end

SLASH_END_RACE1 = "/endrace"
SlashCmdList["END_RACE"] = function()
    EndRace()
end
Editor is loading...
Leave a Comment