Skip to content

Commit

Permalink
Backport Code Refactor From Guildmates Branch
Browse files Browse the repository at this point in the history
  • Loading branch information
ACastanza committed Jun 3, 2024
1 parent 3080981 commit 44a8425
Show file tree
Hide file tree
Showing 2 changed files with 152 additions and 145 deletions.
2 changes: 1 addition & 1 deletion PvpAlerts.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2712,7 +2712,7 @@ function PVP:FullReset()
self.namesToDisplay = {}
self.onEffect = {}

self.friends = {}
self.potentialAllies = {}

self.attackSoundDelay = 0
self.friendSoundDelay = 0
Expand Down
295 changes: 151 additions & 144 deletions PvpAlerts_KOS_System.lua
Original file line number Diff line number Diff line change
Expand Up @@ -708,43 +708,35 @@ function PVP:AddCOOL(playerName, isSlashCommand)
self:PopulateReticleOverNamesBuffer()
end

function PVP:FindCOOLPlayer(unitName, unitAccName)
local unitId = 0
local newName = unitName
if next(self.idToName) ~= nil then
for k, v in pairs(self.idToName) do
if self.SV.playersDB[v] and self.SV.playersDB[v].unitAccName == unitAccName then
local hasPlayerNote = (self.SV.playerNotes[unitAccName] ~= nil)
if v ~= unitName then
self.SV.coolList[v] = unitAccName
self.SV.coolList[unitName] = nil
newName = v
end
if hasPlayerNote or not IsPlayerInGroup(v) then
unitId = k
end
break
end
end
end
function PVP:IsKOSOrFriend(playerName, cachedPlayerDbUpdates)
local playerDbRecord = cachedPlayerDbUpdates[playerName] or self.SV.playersDB[playerName]
if not playerDbRecord then return false end
local unitAccName = playerDbRecord.unitAccName
-- if GetRawUnitName(GetGroupLeaderUnitTag())==playerName then return "groupleader" end
if PVP:GetValidName(GetRawUnitName(GetGroupLeaderUnitTag())) == playerName then return "groupleader" end
if IsPlayerInGroup(playerName) then return "group" end
if self:IsAccNameInKOS(unitAccName) then return "KOS" end
if IsFriend(playerName) then return "friend" end

if unitId == 0 and next(self.playerNames) ~= nil then
for k, _ in pairs(self.playerNames) do
if self.SV.playersDB[k].unitAccName == unitAccName then
local hasPlayerNote = (self.SV.playerNotes[unitAccName] ~= nil)
if k ~= unitName then
self.SV.coolList[k] = unitAccName
self.SV.coolList[unitName] = nil
newName = k
end
if hasPlayerNote or not IsPlayerInGroup(k) then
unitId = 1234567890
end
break
end
end
if self:FindAccInCOOL(playerName, unitAccName) then return "cool" end

return false
end

function PVP:IsEmperor(playerName, currentCampaignActiveEmperor)
if currentCampaignActiveEmperor == "" or currentCampaignActiveEmperor == nil then return false end
if playerName == "" or playerName == nil then return false end
playerName = tostring(playerName)
if playerName == currentCampaignActiveEmperor .. "^Mx" then return true end
if playerName == currentCampaignActiveEmperor .. "^Fx" then return true end
return false
end

function PVP:IsAccNameInKOS(unitAccName)
for i = 1, #self.SV.KOSList do
if unitAccName == self.SV.KOSList[i].unitAccName then return true end
end
return unitId, newName
return false
end

function PVP:FindKOSPlayer(index)
Expand Down Expand Up @@ -796,35 +788,103 @@ function PVP:FindKOSPlayer(index)
return unitId
end

function PVP:IsKOSOrFriend(playerName, cachedPlayerDbUpdates)
local playerDbRecord = cachedPlayerDbUpdates[playerName] or self.SV.playersDB[playerName]
if not playerDbRecord then return false end
local unitAccName = playerDbRecord.unitAccName
-- if GetRawUnitName(GetGroupLeaderUnitTag())==playerName then return "groupleader" end
if PVP:GetValidName(GetRawUnitName(GetGroupLeaderUnitTag())) == playerName then return "groupleader" end
if IsPlayerInGroup(playerName) then return "group" end
if self:IsAccNameInKOS(unitAccName) then return "KOS" end
if IsFriend(playerName) then return "friend" end

if self:FindAccInCOOL(playerName, unitAccName) then return "cool" end
function PVP:FindCOOLPlayer(unitName, unitAccName)
local unitId = 0
local newName = unitName
if next(self.idToName) ~= nil then
for k, v in pairs(self.idToName) do
if self.SV.playersDB[v] and self.SV.playersDB[v].unitAccName == unitAccName then
local hasPlayerNote = (self.SV.playerNotes[unitAccName] ~= nil)
if v ~= unitName then
self.SV.coolList[v] = unitAccName
self.SV.coolList[unitName] = nil
newName = v
end
if hasPlayerNote or not IsPlayerInGroup(v) then
unitId = k
end
break
end
end
end

return false
if unitId == 0 and next(self.playerNames) ~= nil then
for k, _ in pairs(self.playerNames) do
if self.SV.playersDB[k].unitAccName == unitAccName then
local hasPlayerNote = (self.SV.playerNotes[unitAccName] ~= nil)
if k ~= unitName then
self.SV.coolList[k] = unitAccName
self.SV.coolList[unitName] = nil
newName = k
end
if hasPlayerNote or not IsPlayerInGroup(k) then
unitId = 1234567890
end
break
end
end
end
return unitId, newName
end

function PVP:IsEmperor(playerName, currentCampaignActiveEmperor)
if currentCampaignActiveEmperor == "" or currentCampaignActiveEmperor == nil then return false end
if playerName == "" or playerName == nil then return false end
playerName = tostring(playerName)
if playerName == currentCampaignActiveEmperor .. "^Mx" then return true end
if playerName == currentCampaignActiveEmperor .. "^Fx" then return true end
return false
end
function PVP:FindPotentialAllies(includeFriends)
local currentTime = GetFrameTimeMilliseconds()

function PVP:IsAccNameInKOS(unitAccName)
for i = 1, #self.SV.KOSList do
if unitAccName == self.SV.KOSList[i].unitAccName then return true end
for k, v in pairs(self.idToName) do
local playerDbRecord = self.SV.playersDB[v]
if playerDbRecord then
local isCool = self:FindAccInCOOL(v, playerDbRecord.unitAccName)
local isPlayerGrouped = IsPlayerInGroup(v)
local playerNote = self.SV.playerNotes[playerDbRecord.unitAccName]
local hasPlayerNote = (playerNote ~= nil) and (playerNote ~= "")
local isFriend = IsFriend(v)
if hasPlayerNote or ((not isPlayerGrouped) and (isCool or isFriend)) then
self.potentialAllies[v] = {
currentTime = currentTime,
unitAccName = playerDbRecord.unitAccName,
unitAlliance = playerDbRecord.unitAlliance,
isPlayerGrouped = isPlayerGrouped,
isFriend = isFriend,
isCool = isCool,
playerNote = hasPlayerNote and playerNote or nil,
isResurrect = false
}
end
end
end

for k, _ in pairs(self.playerNames) do
if not self.potentialAllies[k] then
local playerDbRecord = self.SV.playersDB[k]
if playerDbRecord then
local isPlayerGrouped = IsPlayerInGroup(k)
local isCool = self:FindAccInCOOL(k, playerDbRecord.unitAccName)
local playerNote = self.SV.playerNotes[playerDbRecord.unitAccName]
local hasPlayerNote = (playerNote ~= nil) and (playerNote ~= "")
local isFriend = IsFriend(k)
if hasPlayerNote or ((not isPlayerGrouped) and (isCool or isFriend)) then
self.potentialAllies[k] = {
currentTime = currentTime,
unitAccName = playerDbRecord.unitAccName,
unitAlliance = playerDbRecord.unitAlliance,
isPlayerGrouped = isPlayerGrouped,
isFriend = isFriend,
isCool = isCool,
playerNote = hasPlayerNote and playerNote or nil,
isResurrect = false
}
end
end
end
end

-- Update resurrect status for displayed names
for i = 1, #self.namesToDisplay do
local name = self.namesToDisplay[i]
if self.potentialAllies[name] and self.namesToDisplay[i].isResurrect then
self.potentialAllies[name].isResurrect = true
end
end
return false
end

function PVP:PopulateKOSBuffer()
Expand Down Expand Up @@ -910,29 +970,38 @@ function PVP:PopulateKOSBuffer()
CheckActive()
end

self:FindFriends()
self:FindPotentialAllies(true, true)

if next(self.friends) ~= nil and (mode == 1 or mode == 2) then
for rawName, v in pairs(self.friends) do
local playerNote
local accName = self.SV.playersDB[rawName].unitAccName
playerNote = self.SV.playerNotes[accName]
if playerNote then playerNote = PVP:Colorize("- " .. playerNote, 'C5C29F') else playerNote = "" end
if not self.KOSNamesList[accName] then
local friendIcon, resurrectIcon
if v.isFriend then
friendIcon = self:GetFriendIcon()
if next(self.potentialAllies) ~= nil then
for rawName, v in pairs(self.potentialAllies) do
local isAlly = v.unitAlliance == self.allianceOfPlayer
if (mode == 2 and isAlly) or (mode == 3 and not isAlly) or mode == 1 then
local playerNoteToken
if v.playerNote then
playerNoteToken = PVP:Colorize("- " .. v.playerNote, 'C5C29F')
else
friendIcon = self:GetCoolIcon()
playerNoteToken = ""
end
if v.isResurrect then
resurrectIcon = self:GetResurrectIcon()
else
resurrectIcon = ""
if not self.KOSNamesList[v.unitAccName] then
local importantIcon, resurrectIcon
if v.isFriend then
importantIcon = self:GetFriendIcon()
elseif v.isCool then
importantIcon = self:GetCoolIcon()
else
importantIcon = ""
end
if v.isResurrect then
resurrectIcon = self:GetResurrectIcon()
else
resurrectIcon = ""
end
PVP_KOS_Text:AddMessage(self:GetFormattedClassNameLink(rawName, self:NameToAllianceColor(rawName)) ..
self:GetFormattedAccountNameLink(v.unitAccName, "40BB40") ..
resurrectIcon ..
importantIcon .. playerNoteToken)
self.KOSNamesList[v.unitAccName] = true
end
PVP_KOS_Text:AddMessage(self:GetFormattedClassNameLink(rawName, self:NameToAllianceColor(rawName)) ..
self:GetFormattedAccountNameLink(accName, "40BB40") .. friendIcon .. resurrectIcon .. playerNote)
self.KOSNamesList[accName] = true
end
end
end
Expand All @@ -943,8 +1012,9 @@ function PVP:PopulateKOSBuffer()
local rawName = self.SV.KOSList[i].unitName
local accName = self.SV.KOSList[i].unitAccName
local ally = self.SV.playersDB[rawName].unitAlliance == self.allianceOfPlayer
local isResurrect, playerNote
local isActive = PVP.kosActivityList.activeChars[accName]
local isResurrect, playerNote

playerNote = self.SV.playerNotes[accName]
if playerNote then playerNote = PVP:Colorize("- " .. playerNote, 'C5C29F') else playerNote = "" end

Expand Down Expand Up @@ -992,7 +1062,8 @@ function PVP:PopulateKOSBuffer()
end
else
PVP_KOS_Text:AddMessage(self:GetFormattedClassNameLink(rawName, self:NameToAllianceColor(rawName, true),
nil, true) .. self:GetFormattedAccountNameLink(accName, "3F3F3F") .. playerNote)
nil, true) ..
self:GetFormattedAccountNameLink(accName, "3F3F3F") .. playerNote)
end
end
end
Expand All @@ -1019,70 +1090,6 @@ function PVP:PopulateKOSBuffer()
end
end

function PVP:FindFriends()
local currentTime = GetFrameTimeMilliseconds()
local foundNames = {}
if next(self.idToName) ~= nil then
for k, v in pairs(self.idToName) do
local playerDbRecord = self.SV.playersDB[v]
if playerDbRecord then
local cool = self:FindAccInCOOL(v, playerDbRecord.unitAccName)
local hasPlayerNote = (self.SV.playerNotes[playerDbRecord.unitAccName] ~= nil)
if hasPlayerNote or (not IsPlayerInGroup(v)) and (IsFriend(v) or cool) then
if not self.friends[v] and self.SV.playKOSSound and PVP.SV.KOSmode ~= 3 then
if currentTime - self.friendSoundDelay > 2000 then
PlaySound(SOUNDS.CROWN_CRATES_CARD_FLIPPING)
end
PlaySound(SOUNDS.CROWN_CRATES_CARD_FLIPPING)
self.friendSoundDelay = currentTime
end
if IsFriend(v) then
self.friends[v] = { currentTime = currentTime, isFriend = true, isResurrect = false }
else
self.friends[v] = { currentTime = currentTime, isFriend = false, isResurrect = false }
end
foundNames[v] = true
end
end
end
end

if next(self.playerNames) ~= nil then
for k, _ in pairs(self.playerNames) do
if not foundNames[k] then
local playerDbRecord = self.SV.playersDB[k]
local cool = self:FindAccInCOOL(k, playerDbRecord.unitAccName)
local hasPlayerNote = (self.SV.playerNotes[playerDbRecord.unitAccName] ~= nil)
if hasPlayerNote or (not IsPlayerInGroup(k)) and (IsFriend(k) or cool) then
if not self.friends[k] and self.SV.playKOSSound and PVP.SV.KOSmode ~= 3 then
if currentTime - self.friendSoundDelay > 2000 then
PlaySound(SOUNDS.CROWN_CRATES_CARD_FLIPPING)
end
PlaySound(SOUNDS.CROWN_CRATES_CARD_FLIPPING)
self.friendSoundDelay = currentTime
end
if IsFriend(k) then
self.friends[k] = { currentTime = currentTime, isFriend = true, isResurrect = false }
else
self.friends[k] = { currentTime = currentTime, isFriend = false, isResurrect = false }
end
end
end
end
end

for i = 1, #self.namesToDisplay do
if self.friends[self.namesToDisplay[i]] and self.namesToDisplay[i].isResurrect then
self.friends[self.namesToDisplay[i]].isResurrect = true
end
end


for k, v in pairs(self.friends) do
if v.currentTime ~= currentTime then self.friends[k] = nil end
end
end

function PVP:SetKOSSliderPosition()
local mode = PVP.SV.KOSmode
local control = PVP_KOS_ControlFrame
Expand Down

0 comments on commit 44a8425

Please sign in to comment.