Skip to content

Commit

Permalink
Detect players from chat events (#38)
Browse files Browse the repository at this point in the history
  • Loading branch information
haggen committed Oct 6, 2019
1 parent 5d29b79 commit 66b2213
Showing 1 changed file with 34 additions and 7 deletions.
41 changes: 34 additions & 7 deletions classic/Threatrack/API.lua
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,9 @@ local frame = CreateFrame("FRAME");

-- Listen to game events to try and derive player presence.
--
frame:RegisterEvent("CHAT_MSG_SAY");
frame:RegisterEvent("CHAT_MSG_YELL");
frame:RegisterEvent("CHAT_MSG_EMOTE");
frame:RegisterEvent("UPDATE_MOUSEOVER_UNIT");
frame:RegisterEvent("PLAYER_TARGET_CHANGED");
frame:RegisterEvent("COMBAT_LOG_EVENT_UNFILTERED");
Expand Down Expand Up @@ -171,20 +174,37 @@ local function IsCombatLogFlagsTypePlayer(flags)
return PLAYER == bit.band(flags, PLAYER);
end

-- ...
--
local function CreatePlayerDataFromPlayerGUID(guid)
local data = CreatePlayerData();
data.guid = guid;
_, data.class, _, data.race, data.sex, data.name = GetPlayerInfoByGUID(guid);
return data;
end

-- ...
--
local function CreatePlayerDataFromChatEvent(guid, language)
local data = CreatePlayerDataFromPlayerGUID(guid);
if (language == GetDefaultLanguage("player")) then
data.reaction = FRIENDLY;
else
data.reaction = HOSTILE;
end
return data;
end

-- Collect player data from combat log event source or target player.
--
local function CreatePlayerDataFromCombatLogEvent(guid, flags, spellName)
if (not IsCombatLogFlagsTypePlayer(flags)) then
return nil;
end

local data = CreatePlayerData();

data.guid = guid;
local data = CreatePlayerDataFromPlayerGUID(guid);
data.reaction = ReadCombatLogFlagsReaction(flags);

_, data.class, _, data.race, data.sex, data.name = GetPlayerInfoByGUID(guid);

if (spellName) then
data.estimatedLevel = ThreatrackData:GetSpellReqLevel(data.class, spellName);
end
Expand All @@ -194,7 +214,7 @@ end

-- Event handler.
--
local function OnEvent(_, event)
local function OnEvent(_, event, ...)
if (event == "PLAYER_TARGET_CHANGED") then
local data = CreatePlayerDataFromUnit("target");
if (data) then
Expand All @@ -208,7 +228,7 @@ local function OnEvent(_, event)
CallPlayerPresenceHandlers();
end
elseif (event == "COMBAT_LOG_EVENT_UNFILTERED") then
local _, action, _, sourceGuid, _, sourceFlags, _, targetGuid, _, targetFlags,
local _, _, _, sourceGuid, _, sourceFlags, _, targetGuid, _, targetFlags,
_, _, spellName, _, _, _, _, _, _, _, _ = CombatLogGetCurrentEventInfo();

local sourceData = CreatePlayerDataFromCombatLogEvent(sourceGuid, sourceFlags, spellName);
Expand All @@ -224,6 +244,13 @@ local function OnEvent(_, event)
if (sourceData or targetData) then
CallPlayerPresenceHandlers();
end
elseif (string.sub(event, 0, 8) == "CHAT_MSG") then
local _, _, language, _, _, _, _, _, _, _, _, guid = ...;
local data = CreatePlayerDataFromChatEvent(guid, language);
if (data) then
RegisterPlayerData(data);
CallPlayerPresenceHandlers();
end
end
end
frame:SetScript("OnEvent", OnEvent);
Expand Down

0 comments on commit 66b2213

Please sign in to comment.