ESOUI SVN TaosGroupUltimate

[/] [trunk/] [TaosGroupUltimate/] [communication/] [Communicator.lua] - Rev 38

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 _logger = nil
local _ultimateHandler = nil

local ABILITY_COEFFICIENT = 100
local ULTIMATE_COEFFICIENT = 1000

--[[
        Table TGU_Communicator
]]--
TGU_Communicator = {}
TGU_Communicator.__index = TGU_Communicator

--[[
        Table Members
]]--
TGU_Communicator.Name = "TGU-Communicator"
TGU_Communicator.IsMocked = false
TGU_Communicator.IsLgsActive = false

--[[
        Called on data from LGS
]]--
function TGU_Communicator.OnUltimateReceived(unitTag, ultimateCurrent, ultimateCost, ultimateGroupId, isSelf)
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.OnUltimateReceived")
        _logger:logDebug("unitTag", unitTag)
        _logger:logDebug("ultimateCurrent", ultimateCurrent)
        _logger:logDebug("ultimateCost", ultimateCost)
        _logger:logDebug("ultimateGroupId", ultimateGroupId)
    end

        local relativeUltimate = math.floor((ultimateCurrent / ultimateCost) * 100)

        if (relativeUltimate > 100) then
                relativeUltimate = 100
        end

    CALLBACK_MANAGER:FireCallbacks(TGU_MAP_PING_CHANGED, unitTag, ultimateGroupId, relativeUltimate)
end

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

        local abilityPing = TGU_Communicator.GetAbilityPing(offsetX)
                local relativeUltimate = TGU_Communicator.GetRelativeUltimate(offsetY)

        if (abilityPing ~= -1 and relativeUltimate ~= -1) then
            CALLBACK_MANAGER:FireCallbacks(TGU_MAP_PING_CHANGED, pingTag, abilityPing, relativeUltimate)
        else
            _logger:logError("TGU_Communicator.OnMapPing, Ping invalid abilityPing: " .. tostring(abilityPing) .. "; relativeUltimate: " .. tostring(relativeUltimate))
        end
    end
end

--[[
        Called on refresh of timer
]]--
function TGU_Communicator.SendData(abilityGroup)
    if (LOG_ACTIVE) then _logger:logTrace("TGU_Communicator.SendData") end

    if (abilityGroup ~= nil) then
        local current, max, effective_max = GetUnitPower("player", POWERTYPE_ULTIMATE)
        local abilityCost = math.max(1, GetAbilityCost(abilityGroup.GroupAbilityId))

        -- Mocked
        if (TGU_Communicator.IsMocked) then
            TGU_Communicator.SendFakePings()
        -- LGS communication
        elseif (TGU_Communicator.IsLgsActive) then
            if (_ultimateHandler ~= nil) then
                _ultimateHandler:SetUltimateCost(abilityCost)
                _ultimateHandler:SetUltimageGroupId(abilityGroup.GroupAbilityPing)
            else
                _logger:logError("TGU_Communicator.SendData, _ultimateHandler is nil")
            end
        -- Standard communication
        else
            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 
                        _logger:logDebug("abilityGroup.GroupName", abilityGroup.GroupName)
                _logger:logDebug("relativeUltimate", relativeUltimate)
                _logger:logDebug("abilityPing", abilityPing)
                _logger:logDebug("ultimatePing", ultimatePing)
            end

                    PingMap(MAP_PIN_TYPE_PING, MAP_TYPE_LOCATION_CENTERED, abilityPing, ultimatePing)
        end
    else
        _logger:logError("TGU_Communicator.SendData, abilityGroup is nil.")
    end
end

--[[
        Check if map ping is in possible range
]]--
function TGU_Communicator.IsPossiblePing(offsetX, offsetY)
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.IsPossiblePing")
        _logger:logDebug("offsetX", offsetX)
        _logger:logDebug("offsetY", 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 
        _logger:logDebug("isValidPing", isValidPing)
        _logger:logDebug("isCorrectOffsetX", isCorrectOffsetX)
        _logger:logDebug("isCorrectOffsetY", isCorrectOffsetY)
    end

        return isValidPing and (isCorrectOffsetX and isCorrectOffsetY)
end

--[[
        Gets ability ID
]]--
function TGU_Communicator.GetAbilityPing(offset)
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.GetAbilityPing")
        _logger:logDebug("offset", 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
            _logger:logError("offset is incorrect: " .. tostring(abilityPing) .. "; offset: " .. tostring(offset))
            return -1
        end
    else
        _logger:logError("offset is incorrect: " .. tostring(offset))
        return -1
    end
end

--[[
        Gets relative ultimate
]]--
function TGU_Communicator.GetRelativeUltimate(offset)
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.GetRelativeUltimate")
        _logger:logDebug("offset", 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
            _logger:logError("relativeUltimate is incorrect: " .. tostring(relativeUltimate) .. "; offset: " .. tostring(offset))
            return -1
        end
    else
        _logger:logError("offset is incorrect: " .. tostring(offset))
        return -1
    end
end

--[[
        Sends fake pings for all group members
]]--
function TGU_Communicator.SendFakePings()
    if (LOG_ACTIVE) then  _logger:logTrace("TGU_Communicator.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

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

--[[
        Sets isLgsActive and updates communication type
]]--
function TGU_Communicator.SetIsLgsActive(isLgsActive)
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.SetIsLgsActive")
        _logger:logDebug("isLgsActive", isLgsActive)
    end

    if (isLgsActive ~= TGU_Communicator.IsLgsActive) then
        TGU_Communicator.IsLgsActive = isLgsActive
        TGU_Communicator.UpdateCommunicationType()
    end
end

--[[
        Updates communication type
]]--
function TGU_Communicator.UpdateCommunicationType()
    if (LOG_ACTIVE) then 
        _logger:logTrace("TGU_Communicator.UpdateCommunicationType")
    end

    -- Unregister events
    EVENT_MANAGER:UnRegisterForEvent(TGU_Communicator.Name, EVENT_MAP_PING)

    if (TGU_Communicator.IsLgsActive) then
        local LGS = LibStub:GetLibrary("LibGroupSocket")

        if (LGS ~= nil) then
            if (_ultimateHandler == nil) then
                _ultimateHandler = LGS:GetHandler(LGS.MESSAGE_TYPE_ULTIMATE)
            end

            _ultimateHandler:RegisterForUltimateChanges(TGU_Communicator.OnUltimateReceived)
            _ultimateHandler:Refresh()
        else
            _logger:logError("LGS not found. Please install LibGroupSocket. Activate default communication as fallback.")
            TGU_Communicator.SetIsLgsActive(false)
        end
    else
            -- Register events
            EVENT_MANAGER:RegisterForEvent(TGU_Communicator.Name, EVENT_MAP_PING, TGU_Communicator.OnMapPing)
    end
end

--[[
        Initialize initializes TGU_Communicator
]]--
function TGU_Communicator.Initialize(logger, isLgsActive, isMocked)
    if (LOG_ACTIVE) then 
        logger:logTrace("TGU_Communicator.Initialize")
        logger:logDebug("isLgsActive", isLgsActive)
        logger:logDebug("isMocked", isMocked)
    end

    _logger = logger

    TGU_Communicator.IsMocked = isMocked

    TGU_Communicator.SetIsLgsActive(isLgsActive)
    TGU_Communicator.UpdateCommunicationType()
end

Go to most recent revision | Compare with Previous | Blame