ESOUI SVN TaosGroupUltimate

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

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 ACTION_BAR_ULTIMATE_SLOT_INDEX = ACTION_BAR_ULTIMATE_SLOT_INDEX + 1 -- 8, the constant is 7, but its wrong
local ABILITY_COEFFICIENT = 100
local ULTIMATE_COEFFICIENT = 1000

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

--[[
        Class Members
]]--
MapPingHandler.Name = "TGU-MapPingHandler"
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 abilityPing = MapPingHandler.GetAbilityPing(offsetX)
        local ultimateGroup = UltimateGroupHandler.GetUltimateGroupByAbilityPing(abilityPing)
                local relativeUltimate = MapPingHandler.GetRelativeUltimate(offsetY)

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

            if (MapPingHandler.IsMocked == false) then
                playerName = GetUnitName(pingTag)
                isPlayerDead = IsUnitDead(pingTag)
            else
                playerName = pingTag
                isPlayerDead = math.random() > 0.8
            end

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

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

            CALLBACK_MANAGER:FireCallbacks("TGU-MapPingChanged", player)
        else
            logError("MapPingHandler.OnMapPing, Ping invalid ultimateGroup: " .. tostring(ultimateGroup) .. "; relativeUltimate: " .. tostring(relativeUltimate))
        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 abilityGroup = UltimateGroupHandler.GetUltimateGroupByAbilityId(SettingsHandler.SavedVariables.StaticUltimateID)

    if (abilityGroup ~= nil) then
            local abilityCost = math.max(1, GetAbilityCost(abilityGroup.GroupAbilityId))
            local relativeUltimate = math.floor((current / abilityCost) * 100)

            if (relativeUltimate > 100) then
                    relativeUltimate = 100
            end

            local abilityPing = abilityGroup.GroupAbilityPing / ABILITY_COEFFICIENT

        local ultimatePing = 0.0001 -- Zero, if you send "0", the map ping will be invalid

        if (relativeUltimate > 0) then
                ultimatePing = relativeUltimate / ULTIMATE_COEFFICIENT
        end

        if (LOG_ACTIVE) then 
                    logDebug("abilityGroup.GroupName: " .. tostring(abilityGroup.GroupName), 
                     "relativeUltimate: " .. tostring(relativeUltimate), 
                     "abilityPing: " .. tostring(abilityPing), 
                     "ultimatePing: " .. tostring(ultimatePing))
        end

        if (MapPingHandler.IsMocked) then
            MapPingHandler.SendFakePings()
        else
                    PingMap(MAP_PIN_TYPE_PING, MAP_TYPE_LOCATION_CENTERED, abilityPing, ultimatePing)
        end
    else
        logError("MapPingHandler.OnTimedUpdate, abilityGroup is nil, change ultimate. StaticID: " .. tostring(SettingsHandler.SavedVariables.StaticUltimateID))
    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.009 and offsetX <= 0.30)
        local isCorrectOffsetY = (offsetY >= 0.000 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.GetAbilityPing(offset)
    if (LOG_ACTIVE) then 
        logTrace("MapPingHandler.GetAbilityPing")
        logDebug("offset: " .. tostring(offset))
    end

    if (offset > 0) then
        local abilityPing = math.floor((offset * ABILITY_COEFFICIENT) + 0.5)
        if (abilityPing >= 1 and abilityPing <= 29) then
            return abilityPing
        else
            logError("offset is incorrect: " .. tostring(abilityPing) .. "; 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

--[[
        Sends fake pings for all group members
]]--
function MapPingHandler.SendFakePings()
    if (LOG_ACTIVE) then  logTrace("MapPingHandler.SendFakePings") end

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

        local ultimateGroups = UltimateGroupHandler.GetUltimateGroups()
        local randomUltimateGroup = ultimateGroups[math.random(1, 29)]

        local offsetX = randomUltimateGroup.GroupAbilityPing / ABILITY_COEFFICIENT
        local offsetY = math.random(100) / ULTIMATE_COEFFICIENT

        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