ESOUI SVN TaosGroupUltimate

[/] [trunk/] [TaosGroupUltimate/] [logic/] [MapPingHandler.lua] - Rev 13

Go to most recent revision | Compare with Previous | Blame | View Log

--[[
        Addon: Taos Group Ultimate
        Author: TProg Taonnor
        Created by @Taonnor
]]--

--[[
        Global variables
]]--
local LOG_ACTIVE = false

local REFRESHRATE = 2000 -- ms; RegisterForUpdate is in miliseconds
local REFRESHTIME = 1 -- seconds; GetTimeStamp() difference is in seconds
local ACTION_BAR_ULTIMATE_SLOT_INDEX = ACTION_BAR_ULTIMATE_SLOT_INDEX + 1 -- 8, the constant is 7, but its wrong
local ABILITY_COEFFICIENT = 100000
local ULTIMATE_COEFFICIENT = 1000

--[[
        Class MapPingHandler
]]--
MapPingHandler = {}
MapPingHandler.__index = MapPingHandler

--[[
        Class Members
]]--
MapPingHandler.Name = "TGU-MapPingHandler"
MapPingHandler.LastMapPingTimestamp = GetTimeStamp() -- Timestamp is in seconds
MapPingHandler.IsMocked = false

--[[
        Called on map ping
]]--
function MapPingHandler.OnMapPing(eventCode, pingEventType, pingType, pingTag, offsetX, offsetY, isLocalPlayerOwner)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.OnMapPing")
        logDebug("pingType: " .. tostring(pingType), 
                 "pingTag: " .. tostring(pingTag), 
                 "offsetX: " .. tostring(offsetX), 
                 "offsetY: " .. tostring(offsetY))
    end
        
        if (pingType == MAP_PIN_TYPE_PING and
                MapPingHandler.IsPossiblePing(offsetX, offsetY)) then

        local abilityID = MapPingHandler.GetAbilityID(offsetX)
                local relativeUltimate = MapPingHandler.GetRelativeUltimate(offsetY)

        if (abilityID ~= -1 and relativeUltimate ~= -1) then
            local player = {}
            local playerName = ""
            local isPlayerDead = false

            if (MapPingHandler.IsMocked == false) then
                playerName = GetUnitName(pingTag)
                isPlayerDead = IsUnitDead(pingTag)
            else
                playerName = GetUnitName("player")
                isPlayerDead = IsUnitDead("player")
            end

            player.PingTag = pingTag
            player.GroupNumber = string.sub(pingTag, string.len("group") + 1)
            player.PlayerName = playerName
            player.IsPlayerDead = isPlayerDead
            player.AbilityID = abilityID
            player.UltimateName = GetAbilityName(abilityID)
            player.UltimateIcon = GetAbilityIcon(abilityID)
            player.RelativeUltimate = relativeUltimate

            if (LOG_ACTIVE) then 
                logDebug(player)
                logDebug("player.PingTag: " .. tostring(player.PingTag), 
                         "player.GroupNumber: " .. tostring(player.GroupNumber), 
                         "player.PlayerName: " .. tostring(player.PlayerName), 
                         "player.IsPlayerDead: " .. tostring(player.IsPlayerDead), 
                         "player.AbilityID: " .. tostring(player.AbilityID), 
                         "player.UltimateName: " .. tostring(player.UltimateName), 
                         "player.UltimateIcon: " .. tostring(player.UltimateIcon), 
                         "player.RelativeUltimate: " .. tostring(player.RelativeUltimate))
            end

            CALLBACK_MANAGER:FireCallbacks("TGU-MapPingChanged", player)
        end
    end
end

--[[
        Called on refresh of timer
]]--
function MapPingHandler.OnTimedUpdate(eventCode)
    if (LOG_ACTIVE) then logTrace("MapPingHandler.OnTimedUpdate") end

        if (IsUnitGrouped("player") == false and MapPingHandler.IsMocked == false) then return end -- only if player is in group and system is not mocked

        local current, max, effective_max = GetUnitPower("player", POWERTYPE_ULTIMATE)
    local currentTimestamp = GetTimeStamp() -- Timestamp is in seconds
        
        if ((currentTimestamp - MapPingHandler.LastMapPingTimestamp) > REFRESHTIME) then
                local abilityID = MapPingHandler.GetConfiguratedUltimate()
                local abilityCost = math.max(1, GetAbilityCost(abilityID))
                local relativeUltimate = math.floor((current / abilityCost) * 100)

                if (relativeUltimate > 100) then
                        relativeUltimate = 100
                end

                local abilityPing = abilityID / ABILITY_COEFFICIENT
                local ultimatePing = relativeUltimate / ULTIMATE_COEFFICIENT

        if (LOG_ACTIVE) then 
                    logDebug("abilityPing: " .. tostring(abilityPing), 
                     "ultimatePing: " .. tostring(ultimatePing))
        end
        
        if (MapPingHandler.IsMocked) then
            MapPingHandler.SendFakePings(abilityPing, ultimatePing)
        else
                    PingMap(MAP_PIN_TYPE_PING, MAP_TYPE_LOCATION_CENTERED, abilityPing, ultimatePing)
        end
                        
                MapPingHandler.LastMapPingTimestamp = GetTimeStamp()
        end
end

--[[
        Check if map ping is in possible range
]]--
function MapPingHandler.IsPossiblePing(offsetX, offsetY)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.IsPossiblePing")
        logDebug("offsetX: " .. tostring(offsetX), 
                 "offsetY: " .. tostring(offsetY))
    end

        local isValidPing = (offsetX ~= 0 or offsetY ~= 0)
        local isCorrectOffsetX = (offsetX >= 0.01 and offsetX <= 1.00)
        local isCorrectOffsetY = (offsetY >= 0.00 and offsetY <= 0.11)

    if (LOG_ACTIVE) then 
        logDebug("isValidPing: " .. tostring(isValidPing), 
                 "isCorrectOffsetX: " .. tostring(isCorrectOffsetX),
                 "isCorrectOffsetY: " .. tostring(isCorrectOffsetY))
    end

        return isValidPing and (isCorrectOffsetX and isCorrectOffsetY)
end

--[[
        Gets ability ID
]]--
function MapPingHandler.GetAbilityID(offset)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.GetAbilityID")
        logDebug("offset: " .. tostring(offset))
    end

    if (offset > 0) then
        local abilityID = math.floor((offset * ABILITY_COEFFICIENT) + 0.5)

        if (abilityID > 1000 and abilityID < 100000) then
            return abilityID
        else
            logError("abilityID is incorrect: " .. tostring(abilityID) .. "; offset: " .. tostring(offset))
            return -1
        end
    else
        logError("offset is incorrect: " .. tostring(offset))
        return -1
    end
end

--[[
        Gets relative ultimate
]]--
function MapPingHandler.GetRelativeUltimate(offset)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.GetRelativeUltimate")
        logDebug("offset: " .. tostring(offset))
    end

    if (offset >= 0) then
        local relativeUltimate = math.floor((offset * ULTIMATE_COEFFICIENT) + 0.5)

        if (relativeUltimate >= 0 and relativeUltimate <= 100) then
            return relativeUltimate
        else
            logError("relativeUltimate is incorrect: " .. tostring(relativeUltimate) .. "; offset: " .. tostring(offset))
            return -1
        end
    else
        logError("offset is incorrect: " .. tostring(offset))
        return -1
    end
end

--[[
        Gets configurated ultimate
]]--
function MapPingHandler.GetConfiguratedUltimate()
    if (LOG_ACTIVE) then logTrace("MapPingHandler.GetConfiguratedUltimate") end

    -- TODO: Dynamisch = Aktuelle Leiste; Fix = Eingestellte "Ultimate Gruppe"
        return GetSlotBoundId(ACTION_BAR_ULTIMATE_SLOT_INDEX)
end

--[[
        Sends fake pings for all group members
]]--
function MapPingHandler.SendFakePings(offsetX, offsetY)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.SendFakePings")
        logDebug("offsetX: " .. tostring(offsetX), 
                 "offsetY: " .. tostring(offsetY))
    end

    for i=1, 1, 1 do
        local eventCode = i
        local pingEventType = i
        local pingType = MAP_PIN_TYPE_PING
        local pingTag = "group" .. i
        local isLocalPlayerOwner = i == 1

        MapPingHandler.OnMapPing(eventCode, pingEventType, pingType, pingTag, offsetX, offsetY, isLocalPlayerOwner)
    end
end

--[[
        Initialize initializes MapPingHandler
]]--
function MapPingHandler.Initialize(isMocked)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.Initialize")
        logDebug("isMocked: " .. tostring(isMocked))
    end

    MapPingHandler.IsMocked = isMocked

        -- Register events
        EVENT_MANAGER:RegisterForEvent(MapPingHandler.Name, EVENT_MAP_PING, MapPingHandler.OnMapPing)
        
        -- Start timer
        EVENT_MANAGER:RegisterForUpdate(MapPingHandler.Name, REFRESHRATE, MapPingHandler.OnTimedUpdate)
end

Go to most recent revision | Compare with Previous | Blame