Skip to content

Commit

Permalink
Merge pull request #1 from StyledStrike/refactor-1
Browse files Browse the repository at this point in the history
Major Refactor
  • Loading branch information
StyledStrike authored May 20, 2024
2 parents ab45a79 + bb89ba4 commit 216ad15
Show file tree
Hide file tree
Showing 17 changed files with 647 additions and 500 deletions.
15 changes: 10 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ You can check a Player's squad by calling this function:
-- Available both on SERVER and CLIENT.
-- Will be -1 if this player is not in a squad.
local id = Player:GetSquadID()
```

You can use this function to get a specific squad instance:

-- You can use this function to get the squad instance.
```lua
local squad = SquadMenu:GetSquad( id )

--[[
Expand All @@ -32,12 +35,14 @@ local squad = SquadMenu:GetSquad( id )
squad.enableRings - boolean
squad.friendlyFire - boolean
squad.isPublic - boolean
]]

squad.members - Player[]
-- You can get the player entities that are part of the squad with:
local players = squad:GetActiveMembers()

Please do not modify squad.members directly.
Check out squad:AddMember and squad:RemoveMember if you need to.
]]
-- "p" represents a player Entity or a string you can get from SquadMenu.GetPlayerId:
squad:AddMember( p )
squad:RemoveMember( p, reason ) -- reason is a number from SquadMenu.LEAVE_REASON_*
```

You can also filter the squad name before it's assigned by using the `ShouldAllowSquadName` hook **on the server**.
Expand Down
125 changes: 66 additions & 59 deletions lua/autorun/sh_squad_menu.lua
Original file line number Diff line number Diff line change
@@ -1,38 +1,41 @@
SquadMenu = {
-- Chat prefixes that allow messaging squad members only
CHAT_PREFIXES = { "/s", "!s", "/p", "!pchat" },
SquadMenu = SquadMenu or {}

-- Primary color used for the UI theme
THEME_COLOR = Color( 34, 52, 142 ),
if CLIENT then
-- Settings file
SquadMenu.DATA_FILE = "squad_menu.json"
end

-- Max. length of a squad name
MAX_NAME_LENGTH = 30,
-- Chat prefixes that allow messaging squad members only
SquadMenu.CHAT_PREFIXES = { "/s", "!s", "/p", "!pchat" }

-- Settings file
DATA_FILE = "squad_menu.json",

-- Size limit for JSON data
MAX_JSON_SIZE = 49152, -- 48 kibibytes

-- Used on net.WriteUInt for the command ID
COMMAND_SIZE = 3,

-- Command IDs (Max. ID when COMMAND_SIZE = 3 is 7)
BROADCAST_EVENT = 0,
SQUAD_LIST = 1,
SETUP_SQUAD = 2,
JOIN_SQUAD = 3,
LEAVE_SQUAD = 4,
ACCEPT_REQUESTS = 5,
REQUESTS_LIST = 6,
KICK = 7,

-- Reasons given when a member is removed from a squad
LEAVE_REASON_DELETED = 0,
LEAVE_REASON_LEFT = 1,
LEAVE_REASON_KICKED = 2
}
-- Primary color used for the UI theme
SquadMenu.THEME_COLOR = Color( 34, 52, 142 )

-- Max. length of a squad name
SquadMenu.MAX_NAME_LENGTH = 30

-- Size limit for JSON data
SquadMenu.MAX_JSON_SIZE = 49152 -- 48 kibibytes

-- Used on net.WriteUInt for the command ID
SquadMenu.COMMAND_SIZE = 3

-- Command IDs (Max. ID when COMMAND_SIZE = 3 is 7)
SquadMenu.BROADCAST_EVENT = 0
SquadMenu.SQUAD_LIST = 1
SquadMenu.SETUP_SQUAD = 2
SquadMenu.JOIN_SQUAD = 3
SquadMenu.LEAVE_SQUAD = 4
SquadMenu.ACCEPT_REQUESTS = 5
SquadMenu.REQUESTS_LIST = 6
SquadMenu.KICK = 7

-- Reasons given when a member is removed from a squad
SquadMenu.LEAVE_REASON_DELETED = 0
SquadMenu.LEAVE_REASON_LEFT = 1
SquadMenu.LEAVE_REASON_KICKED = 2

-- Server settings
CreateConVar(
"squad_max_members",
"10",
Expand Down Expand Up @@ -88,17 +91,35 @@ function SquadMenu.GetShowCreationMessage()
return cvar and cvar:GetInt() or 1
end

function SquadMenu.AllPlayersBySteamID()
local all = player.GetHumans()
function SquadMenu.GetPlayerId( ply )
if ply:IsBot() then
return "BOT_" .. ply:AccountID()
end

return ply:SteamID()
end

local PID = SquadMenu.GetPlayerId

function SquadMenu.AllPlayersById()
local all = player.GetAll()
local byId = {}

for _, p in ipairs( all ) do
byId[p:SteamID()] = p
for _, ply in ipairs( all ) do
byId[PID( ply )] = ply
end

return byId
end

function SquadMenu.FindPlayerById( id )
local all = player.GetAll()

for _, ply in ipairs( all ) do
if id == PID( ply ) then return ply end
end
end

function SquadMenu.ValidateNumber( n, default, min, max )
return math.Clamp( tonumber( n ) or default, min, max )
end
Expand Down Expand Up @@ -130,12 +151,13 @@ function SquadMenu.WriteTable( t )
local data = util.Compress( SquadMenu.TableToJSON( t ) )
local bytes = #data

net.WriteUInt( bytes, 16 )

if bytes > SquadMenu.MAX_JSON_SIZE then
SquadMenu.PrintF( "Tried to write JSON that was too big! (%d/%d)", bytes, SquadMenu.MAX_JSON_SIZE )
return
end

net.WriteUInt( bytes, 16 )
net.WriteData( data )
end

Expand All @@ -152,24 +174,19 @@ function SquadMenu.ReadTable()
end

if SERVER then
function SquadMenu.StartEvent( event, data )
data = data or {}
data.event = event

SquadMenu.StartCommand( SquadMenu.BROADCAST_EVENT )
net.WriteString( SquadMenu.TableToJSON( data ) )
end

resource.AddWorkshop( "3207278246" )
util.AddNetworkString( "squad_menu.command" )
-- Shared files
include( "squad_menu/player.lua" )
AddCSLuaFile( "squad_menu/player.lua" )

-- Server files
include( "squad_menu/server/main.lua" )
include( "squad_menu/server/squad.lua" )
include( "squad_menu/server/network.lua" )

-- Client files
AddCSLuaFile( "squad_menu/client/theme.lua" )
AddCSLuaFile( "squad_menu/client/main.lua" )
AddCSLuaFile( "squad_menu/client/config.lua" )
AddCSLuaFile( "squad_menu/client/menu.lua" )
AddCSLuaFile( "squad_menu/client/hud.lua" )

Expand All @@ -179,27 +196,17 @@ if SERVER then
end

if CLIENT then
function SquadMenu.GetLanguageText( id )
return language.GetPhrase( "squad_menu." .. id ):Trim()
end

function SquadMenu.ChatPrint( ... )
chat.AddText( SquadMenu.THEME_COLOR, "[" .. SquadMenu.GetLanguageText( "title" ) .. "] ", Color( 255, 255, 255 ), ... )
end
-- Shared files
include( "squad_menu/player.lua" )

-- Client files
include( "squad_menu/client/theme.lua" )
include( "squad_menu/client/main.lua" )
include( "squad_menu/client/config.lua" )
include( "squad_menu/client/menu.lua" )
include( "squad_menu/client/hud.lua" )

include( "squad_menu/client/vgui/member_status.lua" )
include( "squad_menu/client/vgui/squad_line.lua" )
include( "squad_menu/client/vgui/tabbed_frame.lua" )
end

local PlayerMeta = FindMetaTable( "Player" )

function PlayerMeta:GetSquadID()
return self:GetNWInt( "squad_menu.id", -1 )
end
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ E2Helper.Descriptions["doesSquadExist(n)"] = "Returns 1 if the given ID points t
E2Helper.Descriptions["getSquadName(n)"] = "Finds a squad by it's ID and returns the name. Returns a empty string if the squad does not exist."
E2Helper.Descriptions["getSquadColor(n)"] = "Finds a squad by it's ID and returns the color. Returns vec(0) if the squad does not exist."
E2Helper.Descriptions["getSquadMemberCount(n)"] = "Returns the number of members in a squad, or 0 if the squad does not exist."
E2Helper.Descriptions["getSquadMembers(n)"] = "Returns an array of players in the squad associated with the squad ID, or a empty array if the squad does not exist."
E2Helper.Descriptions["getSquadMembers(n)"] = "Returns an array of active players associated with the squad ID, or a empty array if the squad does not exist."
E2Helper.Descriptions["getAllSquadIDs()"] = "Returns an array of all available squad IDs."
14 changes: 5 additions & 9 deletions lua/entities/gmod_wire_expression2/core/custom/squad_menu.lua
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,17 @@ end

e2function number getSquadMemberCount( number id )
local squad = SquadMenu:GetSquad( id )
return squad and #squad.members or 0
if not squad then return 0 end

local _, count = squad:GetActiveMembers()
return count
end

e2function array getSquadMembers( number id )
local squad = SquadMenu:GetSquad( id )
if not squad then return {} end

local members = {}

for _, ply in ipairs( squad.members ) do
if IsValid( ply ) then
members[#members + 1] = ply
end
end

local members = squad:GetActiveMembers()
return members
end

Expand Down
60 changes: 60 additions & 0 deletions lua/squad_menu/client/config.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
local Config = SquadMenu.Config or {}

SquadMenu.Config = Config

function Config:Reset()
self.showMembers = true
self.showRings = true
self.showHalos = false
self.enableSounds = true

self.nameDistance = 3000
self.haloDistance = 8000
end

function Config:Load()
self:Reset()

local data = file.Read( SquadMenu.DATA_FILE, "DATA" )
if not data then return end

data = SquadMenu.JSONToTable( data )

self.showMembers = data.showMembers == true
self.showRings = data.showRings == true
self.showHalos = data.showHalos == true
self.enableSounds = data.enableSounds == true

self.nameDistance = SquadMenu.ValidateNumber( data.nameDistance, 3000, 500, 50000 )
self.haloDistance = SquadMenu.ValidateNumber( data.haloDistance, 8000, 500, 50000 )
end

function Config:Save( immediate )
if not immediate then
-- avoid spamming the file system
timer.Remove( "SquadMenu.SaveConfigDelay" )
timer.Create( "SquadMenu.SaveConfigDelay", 0.5, 1, function()
self:Save( true )
end )

return
end

local path = SquadMenu.DATA_FILE

local data = SquadMenu.TableToJSON( {
showMembers = self.showMembers,
showRings = self.showRings,
showHalos = self.showHalos,
enableSounds = self.enableSounds
} )

SquadMenu.PrintF( "%s: writing %s", path, string.NiceSize( string.len( data ) ) )
file.Write( path, data )

if SquadMenu.mySquad then
SquadMenu:UpdateMembersHUD()
end
end

Config:Load()
19 changes: 9 additions & 10 deletions lua/squad_menu/client/hud.lua
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ function SquadMenu:UpdateMembersHUD()

s.childH = childH
s.childOffset = childOffset

s:SetSize( w, h )

local position = math.Clamp( SquadMenu.GetMembersPosition(), 1, 9 )
Expand Down Expand Up @@ -115,8 +114,7 @@ end

function SquadMenu:AddMemberToHUD( member )
member.panel = vgui.Create( "Squad_MemberInfo", self.membersPanel )
member.panel:SetPlayer( member.id, member.name )
member.panel.squad = squad
member.panel:SetPlayer( member.id, member.name, squad )

self.membersPanel:InvalidateLayout()
end
Expand Down Expand Up @@ -168,6 +166,7 @@ local Clamp = math.Clamp
local SetMaterial = surface.SetMaterial
local DrawTexturedRect = surface.DrawTexturedRect
local LocalPlayer = LocalPlayer
local PID = SquadMenu.GetPlayerId

do
local Start3D2D = cam.Start3D2D
Expand All @@ -181,7 +180,7 @@ do
SquadMenu.DrawRing = function( ply, flags )
if flags == 1 then return end
if ply == LocalPlayer() then return end
if not squad.membersById[ply:SteamID()] then return end
if not squad.membersById[PID( ply )] then return end

local pos = ply:GetPos()
local mult = Clamp( pos:DistToSqr( EyePos() ) / ringMaxDist, 0, 1 )
Expand All @@ -199,16 +198,16 @@ end

----------

local AllPlayersBySteamID = SquadMenu.AllPlayersBySteamID
local AllPlayersById = SquadMenu.AllPlayersById

SquadMenu.DrawHalos = function()
local origin = EyePos()
local me = LocalPlayer()
local players = AllPlayersBySteamID()
local byId = AllPlayersById()
local i, t, dist = 0, {}

for _, member in ipairs( squad.members ) do
local ply = players[member.id]
local ply = byId[member.id]

if ply and ply ~= me then
dist = origin:DistToSqr( ply:EyePos() )
Expand All @@ -234,7 +233,7 @@ SquadMenu.HideTargetInfo = function()
local ply = trace.Entity
if not ply:IsPlayer() then return end

if squad.membersById[ply:SteamID()] and EyePos():DistToSqr( ply:EyePos() ) < nameDistance then
if squad.membersById[PID( ply )] and EyePos():DistToSqr( ply:EyePos() ) < nameDistance then
return false
end
end
Expand Down Expand Up @@ -273,11 +272,11 @@ SquadMenu.DrawMemberTags = function()

local origin = EyePos()
local me = LocalPlayer()
local players = AllPlayersBySteamID()
local byId = AllPlayersById()
local dist

for _, member in ipairs( squad.members ) do
local ply = players[member.id]
local ply = byId[member.id]

if ply and ply ~= me and not ply:IsDormant() then
dist = origin:DistToSqr( ply:EyePos() )
Expand Down
Loading

0 comments on commit 216ad15

Please sign in to comment.