init commit

This commit is contained in:
2025-05-31 10:13:27 +02:00
parent 3da26f025c
commit ada9afc48d
30 changed files with 3955 additions and 16 deletions

View File

@@ -0,0 +1,196 @@
resource.AddWorkshop( "3207278246" )
SquadMenu.blockDamage = SquadMenu.blockDamage or {}
SquadMenu.squads = SquadMenu.squads or {}
SquadMenu.lastSquadId = SquadMenu.lastSquadId or 0
--- Find a squad by it's ID.
function SquadMenu:GetSquad( id )
return self.squads[id]
end
--- Find and remove a squad by it's ID.
function SquadMenu:DeleteSquad( id )
local squad = self.squads[id]
if squad then
squad:Delete()
end
end
--- Remove a player id from join requests on all squads.
function SquadMenu:CleanupRequests( id, dontSync )
for _, squad in pairs( self.squads ) do
if squad.requestsById[id] then
squad.requestsById[id] = nil
if not dontSync then
squad:SyncRequests()
end
end
end
end
-- Updates the player name everywhere it appears.
function SquadMenu:UpdatePlayerName( ply )
if not IsValid( ply ) then return end
local id = self.GetPlayerId( ply )
local name = ply:Nick()
for _, squad in pairs( self.squads ) do
-- Update leader name
if squad.leaderId == id then
squad.leaderName = name
end
-- Update join request
if squad.requestsById[id] then
squad.requestsById[id] = name
squad:SyncRequests()
end
-- Update member name
if squad.membersById[id] then
squad.membersById[id] = name
squad:SyncWithMembers()
end
end
end
--- Create a new squad.
function SquadMenu:CreateSquad()
local id = self.lastSquadId + 1
self.lastSquadId = id
self.squads[id] = setmetatable( {
id = id,
name = "",
icon = "",
enableRings = false,
friendlyFire = false,
isPublic = false,
r = 255,
g = 255,
b = 255,
-- Members key-value table. Do not modify directly,
-- instead use squad:AddMember/squad:RemoveMember.
--
-- Each key is a player id from SquadMenu.GetPlayerId,
-- and each value is the player name.
membersById = {},
-- Join Requests key-value table.
--
-- Each key is a player id from SquadMenu.GetPlayerId,
-- and each value is the player name.
requestsById = {}
}, self.Squad )
self.PrintF( "Created squad #%d", id )
return self.squads[id]
end
-- Callbacks on FCVAR_REPLICATED cvars don't work clientside so we need this
cvars.AddChangeCallback( "squad_members_position", function()
SquadMenu.StartEvent( "squad_position_changed" )
net.Broadcast()
end, "changed_squad_members_position" )
--- Update player names on change
gameevent.Listen( "player_changename" )
hook.Add( "player_changename", "SquadMenu.UpdatePlayerName", function( data )
SquadMenu:UpdatePlayerName( Player( data.userid ) )
end )
--- Update player names on first spawn
hook.Add( "PlayerInitialSpawn", "SquadMenu.UpdatePlayerName", function( ply )
SquadMenu:UpdatePlayerName( ply )
end )
-- On disconnect, remove join requests and this player from their squad, if they have one
hook.Add( "PlayerDisconnected", "SquadMenu.PlayerCleanup", function( ply )
SquadMenu:CleanupRequests( SquadMenu.GetPlayerId( ply ) )
local squad = SquadMenu:GetSquad( ply:GetSquadID() )
if squad then
squad:RemoveMember( ply )
end
end )
--- Block damage between squad members
local blockDamage = SquadMenu.blockDamage
hook.Add( "PlayerShouldTakeDamage", "SquadMenu.BlockFriendlyFire", function( ply, attacker )
if not attacker.GetSquadID then return end
local id = ply:GetSquadID()
if id ~= -1 and ply ~= attacker and blockDamage[id] and id == attacker:GetSquadID() then
return false
end
end )
--- Chat commands and squad-only chat messages
local prefixes = {}
for _, prefix in ipairs( SquadMenu.CHAT_PREFIXES ) do
prefixes[prefix] = true
end
hook.Add( "PlayerSay", "SquadMenu.RemovePrefix", function( sender, text, _, channel )
-- Check for commands to open the menu
if text[1] == "!" then
text = string.lower( string.Trim( text ) )
if text == "!squad" or text == "!party" then
SquadMenu.StartEvent( "open_menu" )
net.Send( sender )
return ""
end
end
-- Check if this is supposed to be a members-only message
local isCustomChatSquadChannel = channel == "squad"
local parts = string.Explode( " ", text, false )
if not isCustomChatSquadChannel and ( not parts[1] or not prefixes[parts[1]] ) then return end
local id = sender:GetSquadID()
if id == -1 then
sender:ChatPrint( "You're not in a squad." )
return ""
end
if not isCustomChatSquadChannel then
table.remove( parts, 1 )
end
text = table.concat( parts, " " )
if text:len() == 0 then
sender:ChatPrint( "Please type a message to send to your squad members." )
return ""
end
local override = hook.Run( "SquadPlayerSay", sender, text )
if override ~= nil and ( not override or override == "" ) then return "" end
if type( override ) == "string" then text = override end
local members = SquadMenu:GetSquad( id ):GetActiveMembers()
SquadMenu.StartEvent( "members_chat", {
senderName = sender:Nick(),
text = text
} )
net.Send( members )
return ""
end, HOOK_HIGH )

View File

@@ -0,0 +1,182 @@
util.AddNetworkString( "squad_menu.command" )
function SquadMenu.StartEvent( event, data )
data = data or {}
data.event = event
SquadMenu.StartCommand( SquadMenu.BROADCAST_EVENT )
SquadMenu.WriteTable( data )
end
local commands = {}
local PID = SquadMenu.GetPlayerId
commands[SquadMenu.SQUAD_LIST] = function( ply )
local data = {}
for _, squad in pairs( SquadMenu.squads ) do
data[#data + 1] = squad:GetBasicInfo()
end
SquadMenu.StartCommand( SquadMenu.SQUAD_LIST )
SquadMenu.WriteTable( data )
net.Send( ply )
end
commands[SquadMenu.SETUP_SQUAD] = function( ply )
local squadId = ply:GetSquadID()
local plyId = PID( ply )
local data = SquadMenu.ReadTable()
if type( data.name ) == "string" then
local shouldAllow, name = hook.Run( "ShouldAllowSquadName", data.name, ply )
if shouldAllow == false then
data.name = name or "?"
end
end
-- Update existing squad if this ply is the leader.
if squadId ~= -1 then
local squad = SquadMenu:GetSquad( squadId )
if not squad then return end
if squad.leaderId ~= plyId then return end
squad:SetBasicInfo( data )
squad:SyncWithMembers()
-- Litterally all I need is that lol
hook.Run("SquadMenu_SquadUpdated", squadId, ply, ply:SteamID())
SquadMenu.PrintF( "Edited squad #%d for %s", squadId, ply:SteamID() )
SquadMenu.StartEvent( "squad_created", { id = squadId } )
net.Broadcast()
return
end
local squad = SquadMenu:CreateSquad()
squad:SetBasicInfo( data )
squad:SetLeader( ply )
squad:AddMember( ply )
SquadMenu.StartEvent( "squad_created", {
id = squadId,
name = squad.name,
leaderName = squad.leaderName,
r = squad.r,
g = squad.g,
b = squad.b
} )
net.Broadcast()
end
commands[SquadMenu.JOIN_SQUAD] = function( ply )
local squadId = net.ReadUInt( 16 )
local squad = SquadMenu:GetSquad( squadId )
if squad then
squad:RequestToJoin( ply )
end
end
commands[SquadMenu.LEAVE_SQUAD] = function( ply )
local squadId = ply:GetSquadID()
if squadId == -1 then return end
local squad = SquadMenu:GetSquad( squadId )
if squad then
squad:RemoveMember( ply, SquadMenu.LEAVE_REASON_LEFT )
end
end
commands[SquadMenu.ACCEPT_REQUESTS] = function( ply )
local squadId = ply:GetSquadID()
if squadId == -1 then return end
local squad = SquadMenu:GetSquad( squadId )
local ids = SquadMenu.ReadTable()
if squad and squad.leaderId == PID( ply ) then
squad:AcceptRequests( ids )
end
end
commands[SquadMenu.KICK] = function( ply )
local squadId = ply:GetSquadID()
if squadId == -1 then return end
local plyId = PID( ply )
local squad = SquadMenu:GetSquad( squadId )
if squad and squad.leaderId == plyId then
local targetId = net.ReadString()
if targetId == plyId then return end
local byId = SquadMenu.AllPlayersById()
if not byId[targetId] then return end
squad:RemoveMember( byId[targetId], SquadMenu.LEAVE_REASON_KICKED )
end
end
commands[SquadMenu.PING] = function( ply )
local squadId = ply:GetSquadID()
if squadId == -1 then return end
local squad = SquadMenu:GetSquad( squadId )
if not squad then return end
local pos = net.ReadVector()
local members, count = squad:GetActiveMembers()
if count == 0 then return end
SquadMenu.StartCommand( SquadMenu.PING )
net.WriteVector( pos )
net.WriteString( ply:Nick() )
net.WriteString( ply:SteamID() )
net.Send( members )
end
-- Safeguard against spam
local cooldowns = {
[SquadMenu.SQUAD_LIST] = { interval = 0.5, players = {} },
[SquadMenu.SETUP_SQUAD] = { interval = 1, players = {} },
[SquadMenu.JOIN_SQUAD] = { interval = 0.1, players = {} },
[SquadMenu.LEAVE_SQUAD] = { interval = 1, players = {} },
[SquadMenu.ACCEPT_REQUESTS] = { interval = 0.2, players = {} },
[SquadMenu.KICK] = { interval = 0.1, players = {} },
[SquadMenu.PING] = { interval = 0.5, players = {} }
}
net.Receive( "squad_menu.command", function( _, ply )
local id = ply:SteamID()
local cmd = net.ReadUInt( SquadMenu.COMMAND_SIZE )
if not commands[cmd] then
SquadMenu.PrintF( "%s <%s> sent a unknown network command! (%d)", ply:Nick(), id, cmd )
return
end
local t = RealTime()
local players = cooldowns[cmd].players
if players[id] and players[id] > t then
SquadMenu.PrintF( "%s <%s> sent network commands too fast!", ply:Nick(), id )
return
end
players[id] = t + cooldowns[cmd].interval
commands[cmd]( ply )
end )
hook.Add( "PlayerDisconnected", "SquadMenu.NetCleanup", function( ply )
local id = ply:SteamID()
for _, c in pairs( cooldowns ) do
c.players[id] = nil
end
end )

View File

@@ -0,0 +1,278 @@
local IsValid = IsValid
local PID = SquadMenu.GetPlayerId
local FindByPID = SquadMenu.FindPlayerById
local Squad = SquadMenu.Squad or {}
SquadMenu.Squad = Squad
Squad.__index = Squad
--- Set the leader of this squad. `ply` can either be
--- a player entity, a ID string that came from `SquadMenu.GetPlayerId`,
--- or `nil` if you want to unset the current squad leader.
function Squad:SetLeader( ply, name )
if type( ply ) == "string" then
self.leaderId = ply -- this is a player ID
self.leaderName = name or "?"
elseif IsValid( ply ) then
self.leaderId = PID( ply )
self.leaderName = ply:Nick()
else
self.leaderId = nil
self.leaderName = nil
SquadMenu.PrintF( "Removed leader from squad #%d", self.id )
return
end
SquadMenu.PrintF( "New leader for squad #%d: %s <%s>", self.id, self.leaderName, self.leaderId )
end
--- Get a table containing a list of active squad members.
--- Returns an array of player entities that are currently on the server.
function Squad:GetActiveMembers()
local members, count = {}, 0
local byId = SquadMenu.AllPlayersById()
for id, _ in pairs( self.membersById ) do
if byId[id] then
count = count + 1
members[count] = byId[id]
end
end
return members, count
end
--- Get a ready-to-be-stringified table containing details from this squad.
function Squad:GetBasicInfo()
local info = {
id = self.id,
name = self.name,
icon = self.icon,
members = {},
enableRings = self.enableRings,
friendlyFire = self.friendlyFire,
isPublic = self.isPublic,
r = self.r,
g = self.g,
b = self.b
}
if self.leaderId then
info.leaderId = self.leaderId
info.leaderName = self.leaderName
end
local count = 0
for id, name in pairs( self.membersById ) do
count = count + 1
info.members[count] = { id, name }
end
return info
end
local ValidateString = SquadMenu.ValidateString
local ValidateNumber = SquadMenu.ValidateNumber
--- Set the details of this squad using a table.
function Squad:SetBasicInfo( info )
if SquadMenu.GetForceFriendlyFire() then
info.friendlyFire = true
end
self.name = ValidateString( info.name, "Unnamed", SquadMenu.MAX_NAME_LENGTH )
self.icon = ValidateString( info.icon, "icon16/flag_blue.png", 256 )
self.enableRings = info.enableRings == true
self.friendlyFire = info.friendlyFire == true
self.isPublic = info.isPublic == true
self.r = ValidateNumber( info.r, 255, 0, 255 )
self.g = ValidateNumber( info.g, 255, 0, 255 )
self.b = ValidateNumber( info.b, 255, 0, 255 )
SquadMenu.blockDamage[self.id] = Either( self.friendlyFire, nil, true )
end
--- Send details and a list of members to all squad members.
--- You should never set `immediate` to `true` unless you know what you're doing.
function Squad:SyncWithMembers( immediate )
if not immediate then
-- avoid spamming the networking system
timer.Remove( "SquadMenu.Sync" .. self.id )
timer.Create( "SquadMenu.Sync" .. self.id, 0.5, 1, function()
self:SyncWithMembers( true )
end )
return
end
local members, count = self:GetActiveMembers()
if count == 0 then return end
local data = self:GetBasicInfo()
SquadMenu.StartCommand( SquadMenu.SETUP_SQUAD )
SquadMenu.WriteTable( data )
net.Send( members )
end
--- Send the requests list to the squad leader.
function Squad:SyncRequests()
local leader = FindByPID( self.leaderId )
if not IsValid( leader ) then return end
SquadMenu.StartCommand( SquadMenu.REQUESTS_LIST )
SquadMenu.WriteTable( self.requestsById )
net.Send( leader )
end
--- Turns `p` into a id and player entity depending on what `p` is.
local function ParsePlayerArg( p )
if type( p ) == "string" then
return p, FindByPID( p )
end
return PID( p ), p
end
--- Add a player as a new member.
--- `p` can be a player id from `SquadMenu.GetPlayerId` or a player entity.
function Squad:AddMember( p )
local id, ply = ParsePlayerArg( p )
if self.membersById[id] then return end
local count = table.Count( self.membersById )
if count >= SquadMenu.GetMemberLimit() then return end
local name = id
if IsValid( ply ) then
local oldSquad = SquadMenu:GetSquad( ply:GetSquadID() )
if oldSquad then
oldSquad:RemoveMember( ply, SquadMenu.LEAVE_REASON_LEFT )
end
ply:SetNWInt( "squad_menu.id", self.id )
name = ply:Nick()
end
self.membersById[id] = name
self:SyncWithMembers()
-- We don't send the requests list to the squad leaders (2nd "true" parameter)
-- because "player_joined_squad" will already tell them to remove
-- this player from their own copies of the join requests list.
SquadMenu:CleanupRequests( id, true )
SquadMenu.StartEvent( "player_joined_squad", {
squadId = self.id,
playerId = id
} )
net.Broadcast()
hook.Run( "SquadMenu_OnJoinedSquad", self.id, ply, id )
end
--- Remove a player from this squad's members list.
--- `ply` can be a player id from `SquadMenu.GetPlayerId` or a player entity.
--- `reasonId` can be `nil` or one of the values from `SquadMenu.LEAVE_REASON_*`.
function Squad:RemoveMember( p, reasonId )
local id, ply = ParsePlayerArg( p )
if not self.membersById[id] then return end
if id == self.leaderId then
pcall( hook.Run, "SquadMenu_OnLeftSquad", self.id, ply, id )
self:Delete()
return
end
self.membersById[id] = nil
self:SyncWithMembers()
if not IsValid( ply ) then return end
ply:SetNWInt( "squad_menu.id", -1 )
if reasonId ~= nil then
SquadMenu.StartCommand( SquadMenu.LEAVE_SQUAD )
net.WriteUInt( reasonId, 3 )
net.Send( ply )
end
hook.Run( "SquadMenu_OnLeftSquad", self.id, ply, id )
end
--- Add a player to the list of players that
--- requested to join and notify the squad leader.
function Squad:RequestToJoin( ply )
if self.isPublic then
self:AddMember( ply )
return
end
local plyId = PID( ply )
if self.requestsById[plyId] then return end
self.requestsById[plyId] = ply:Nick()
self:SyncRequests()
end
--- Accept all the join requests from a list of player IDs.
function Squad:AcceptRequests( ids )
if not table.IsSequential( ids ) then return end
local limit = SquadMenu.GetMemberLimit()
local count = table.Count( self.membersById )
for _, id in ipairs( ids ) do
if count >= limit then
break
elseif self.requestsById[id] then
count = count + 1
self.requestsById[id] = nil
self:AddMember( id )
end
end
end
--- Remove all members from this squad and delete it.
function Squad:Delete()
timer.Remove( "SquadMenu.Sync" .. self.id )
local members, count = self:GetActiveMembers()
if count > 0 then
for _, ply in ipairs( members ) do
ply:SetNWInt( "squad_menu.id", -1 )
end
SquadMenu.StartCommand( SquadMenu.LEAVE_SQUAD )
net.WriteUInt( SquadMenu.LEAVE_REASON_DELETED, 3 )
net.Send( members )
end
self.membersById = nil
self.requestsById = nil
local id = self.id
SquadMenu.squads[id] = nil
SquadMenu.blockDamage[id] = nil
SquadMenu.PrintF( "Deleted squad #%d", id )
SquadMenu.StartEvent( "squad_deleted", { id = id } )
net.Broadcast()
end