Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move player finders to a proper place & optimize them #2161

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 3 additions & 47 deletions garrysmod/lua/includes/extensions/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -261,53 +261,9 @@ function meta:HasGodMode()

end

-- These are totally in the wrong place.
function player.GetByAccountID( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:AccountID() == ID ) then
return players[i]
end
end

return false
end

function player.GetByUniqueID( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:UniqueID() == ID ) then
return players[i]
end
end

return false
end

function player.GetBySteamID( ID )
ID = string.upper( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:SteamID() == ID ) then
return players[i]
end
end

return false
end

function player.GetBySteamID64( ID )
ID = tostring( ID )
local players = player.GetAll()
for i = 1, #players do
if ( players[i]:SteamID64() == ID ) then
return players[i]
end
end

return false
end

--[[---------------------------------------------------------
Iterator for players
-----------------------------------------------------------]]
local inext = ipairs( {} )
local PlayerCache = nil

Expand Down
143 changes: 143 additions & 0 deletions garrysmod/lua/includes/extensions/player_finders.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@

--[[---------------------------------------------------------
Name: GetByAccountID( ID )
Desc: Attempts to get the player by the AccountID
-----------------------------------------------------------]]
local AccountIDMap = {}

function player.GetByAccountID( ID )

ID = tostring( ID )
local ply = AccountIDMap[ID]

if ( ply ) then
return ply
end

return false

end

--[[---------------------------------------------------------
Name: GetByUniqueID( ID )
Desc: Attempts to get the player by the UniqueID
-----------------------------------------------------------]]
local UniqueIDMap = {}

function player.GetByUniqueID( ID )

ID = tostring( ID )
local ply = UniqueIDMap[ID]

if ( ply ) then
return ply
end

return false

end

--[[---------------------------------------------------------
Name: GetBySteamID( ID )
Desc: Attempts to get the player by the SteamID
-----------------------------------------------------------]]
local SteamIDMap = {}

function player.GetBySteamID( ID )

ID = string.upper( tostring( ID ) )
local ply = SteamIDMap[ID]

if ( ply ) then
return ply
end

return false

end

--[[---------------------------------------------------------
Name: GetBySteamID64( ID )
Desc: Attempts to get the player by the SteamID64
-----------------------------------------------------------]]
local SteamID64Map = {}

function player.GetBySteamID64( ID )

ID = tostring( ID )
local ply = SteamID64Map[ID]

if ( ply ) then
return ply
end

return false

end

--[[---------------------------------------------------------
Manage the maps
-----------------------------------------------------------]]
hook.Add( "OnEntityCreated", "PlayerFinders", function( ent )

if ( not ent:IsPlayer() ) then
return
end

local ply = ent

--
-- Store in the maps
--
local AID = tostring( ply:AccountID() )
local UID = tostring( ply:UniqueID() )
local SID = ply:SteamID()
local S64 = ply:SteamID64()

AccountIDMap[AID] = ply
AccountIDMap[ply] = AID

UniqueIDMap[UID] = ply
UniqueIDMap[ply] = UID

SteamIDMap[SID] = ply
SteamIDMap[ply] = SID

SteamID64Map[S64] = ply
SteamID64Map[ply] = S64

end )

hook.Add( "EntityRemoved", "PlayerFinders", function( ent, fullUpdate )

if ( fullUpdate ) then
return
end

if ( not ent:IsPlayer() ) then
return
end

local ply = ent

--
-- Clear from the maps
--
local AID = AccountIDMap[ply]
local UID = UniqueIDMap[ply]
local SID = SteamIDMap[ply]
local S64 = SteamID64Map[ply]

AccountIDMap[AID] = nil
AccountIDMap[ply] = nil

UniqueIDMap[UID] = nil
UniqueIDMap[ply] = nil

SteamIDMap[SID] = nil
SteamIDMap[ply] = nil

SteamID64Map[S64] = nil
SteamID64Map[ply] = nil

end )
1 change: 1 addition & 0 deletions garrysmod/lua/includes/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ include ( "extensions/entity.lua" )
include ( "extensions/ents.lua" )
include ( "extensions/math.lua" )
include ( "extensions/player.lua" )
include ( "extensions/player_finders.lua" )
include ( "extensions/player_auth.lua" )
include ( "extensions/string.lua" )
include ( "extensions/table.lua" )
Expand Down
1 change: 1 addition & 0 deletions garrysmod/lua/send.txt
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ lua\includes\extensions\net.lua
lua\includes\extensions\math.lua
lua\includes\extensions\math\ease.lua
lua\includes\extensions\player.lua
lua\includes\extensions\player_finders.lua
lua\includes\extensions\player_auth.lua
lua\includes\extensions\string.lua
lua\includes\extensions\table.lua
Expand Down