Skip to content

Commit

Permalink
convert to use steamids for normal players
Browse files Browse the repository at this point in the history
  • Loading branch information
plally committed Dec 11, 2024
1 parent 01ac7da commit faee778
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 6 deletions.
21 changes: 18 additions & 3 deletions lua/mapvote/client/modules/map_vote.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,21 @@ function MapVote.CancelVote()
hook.Run( "MapVote_VoteCancelled" )
end

---@param ply Player
---@param mapIndex number
---@param voteMult number
function MapVote.ChangeVote( ply, mapIndex, voteMult )
if not IsValid( ply ) then return end
if not IsValid( MapVote.Panel ) then return end
MapVote.Panel.voteArea:SetVote( ply, mapIndex, voteMult )

---@type string|Player
local identifier = ply:SteamID64()
if ply:IsBot() then
-- dont use steamid object for bots to allow testing UI locally
identifier = ply
end

MapVote.Panel.voteArea:SetVote( identifier, mapIndex, voteMult )
end

function MapVote.StartVote( maps, endTime )
Expand Down Expand Up @@ -115,8 +126,12 @@ hook.Add( "Tick", "MapVote_RequestState", function()
end )

gameevent.Listen( "player_disconnect" )
hook.Add( "player_disconnect", "MapVote_RemoveVote", function()
hook.Add( "player_disconnect", "MapVote_RemoveVote", function( data )
if not IsValid( MapVote.Panel ) then return end

MapVote.Panel.voteArea:RemoveInvalidVotes()
if data.networkid == "BOT" then
MapVote.Panel.voteArea:RemoveInvalidVotes()
else
MapVote.Panel.voteArea:RemoveVote( data.networkid )
end
end )
15 changes: 12 additions & 3 deletions lua/mapvote/client/vgui/mapvote_panel.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,22 @@ end

function PANEL:RemoveInvalidVotes()
for identifier, vote in pairs( self.votes ) do
if not identifier or not IsValid( identifier ) then
local invalidPlayer = (type( identifier ) == "Player") and not IsValid( identifier )
if not identifier or invalidPlayer then
self:removeVote( vote )
self.votes[identifier] = nil
end
end
end

function PANEL:RemoveVote( identifier )
local vote = self.votes[identifier]
if not vote then return end

self:removeVote( vote )
self.votes[identifier] = nil
end

function PANEL:removeVote( oldVote, removePanel )
if removePanel == nil then
removePanel = true
Expand Down Expand Up @@ -173,15 +182,15 @@ function PANEL:GetPositionRelativeToSelf( mapPanel )
end

---@param identifier string|Player
---@return Player
---@return Player|nil
---@private
function PANEL:GetPlayerFromIdentifier( identifier )
if type( identifier ) == "string" then
return player.GetBySteamID64( identifier )
elseif type( identifier ) == "Player" then
return identifier
end
return identifier
return nil
end

local createdFonts = {}
Expand Down

0 comments on commit faee778

Please sign in to comment.