Raised This Month: $ Target: $400
 0% 

How to assign players to teams with config and commands.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
CBUltimate
New Member
Join Date: Apr 2017
Old 04-18-2017 , 11:36   How to assign players to teams with config and commands.
Reply With Quote #1

Hi, I'm quite new to sourcemod, recently I want to host a simple pug match for my friends to have a match. I'm learning to write a plugin to assign players into teams based on their steam id.

I need help on reading/writing config files to check what team the steam id suppose to be assign to and also make a command to assign specific steam id to a team. I just need some guides to do it.

This is a config file I made, teams.cfg.
Code:
// Teams: 0 = No team, 1 = Spectate, 2 = T, 3  = CT.

"assingedPlayers"
{
	"STEAM_0:1:11111111" "2" //example
}
This is a plugin code I found and modified it for myself.
PHP Code:
int Connections[20]; 
int ConnectionCount 0

ArrayList g_AssignedPlayers;

new 
Handle:assignCMD INVALID_HANDLE;

public 
OnPluginStart() 

    
HookEvent("player_activate"PlayerActivateEventHookMode_Post); 
    
assignCMD CreateConVar("sm_assign""3""Set player's team, 1 for Spectate, 2 for T, 3 for CT."FCVAR_NOTIFY); //This is temporary for assigning players into the team I want. Will be removed for config and set team command.


public 
OnClientPostAdminCheck(int client) { 
    
Connections[ConnectionCount] = client
    
ConnectionCount++; 


public 
Action PlayerActivate(Handle event, const char[]useridbool dontBroadcast) { 
    for(
int isizeof(Connections); i++) { 
        if(
Connections[i]) { 
            if (!
IsClientSourceTV(Connections[i])){
                new 
String:auth[64]; 
                
GetClientAuthString(Connections[i], authsizeof(auth)); 
                
PrintToServer("Player join: SteamID: %s"auth);
                
ChangeClientTeam(Connections[i], GetConVarInt(assignCMD)); 
                
Connections[i] = false
            } else {
                
Connections[i] = false;
            }
        } 
    } 

Thanks in advance!~
CBUltimate is offline
ambn
Veteran Member
Join Date: Feb 2015
Location: Fun servers
Old 04-18-2017 , 14:24   Re: How to assign players to teams with config and commands.
Reply With Quote #2

Use keyvalues set section name with steamid and a NULL_STRING for team number then just jump to the key to get needed info , also remomber to rewind and keyvalue to file as well.
__________________
ambn is offline
CBUltimate
New Member
Join Date: Apr 2017
Old 04-19-2017 , 10:55   Re: How to assign players to teams with config and commands.
Reply With Quote #3

Thanks for the help! I looked up everything about KeyValues and got it working!
Here's the code if anyone needs it, if there's anything wrong, reply me.

PHP Code:
#pragma semicolon 1
#include <sourcemod>
#define PLUGIN_VERSION    "1.0"

int Connections[20]; 
int ConnectionCount 0

new 
Handle:assignCMD INVALID_HANDLE;

public 
OnPluginStart() 

    
HookEvent("player_activate"PlayerActivateEventHookMode_Post); 
    
assignCMD CreateConVar("sm_autoassign""1""Set player's team, 1 for Spectate, 2 for T, 3 for CT."FCVAR_NOTIFY);
    
RegServerCmd("sm_assignid"AssignTeam"Set client index to team 1 for Spectate, 2 for T, 3 for CT.");


public 
Action AssignTeam(int args)
{
    
char arg[128];
    
char full[256];

    
GetCmdArgString(fullsizeof(full));

    
int clientId = -1;
    
int teamId = -1;

    if (
args == 2){
        
GetCmdArg(1argsizeof(arg));
        
clientId StringToInt(arg);
        
GetCmdArg(2argsizeof(arg));
        
teamId StringToInt(arg);
        
PrintToServer("Assigning client %d to %s"clientIdteamId);

        
ChangeClientTeam(clientIdteamId);
        new 
String:auth[64]; 
        
GetClientAuthId(clientIdAuthId_SteamID64authsizeof(auth), true);
        if (!
IsFakeClient(clientId)){
            
KeyValues kv = new KeyValues("assingedPlayers");
            
kv.ImportFromFile("/addons/sourcemod/configs/teamassigner/teams.cfg");
            
kv.JumpToKey(authtrue);
            
kv.SetNum("team"teamId);
            
kv.Rewind();
            
kv.ExportToFile("/addons/sourcemod/configs/teamassigner/teams.cfg");
            
delete kv;
        }

        return 
Plugin_Continue;
    } else {
        return 
Plugin_Handled;
    }
}

public 
OnClientPostAdminCheck(int client) { 
    
Connections[ConnectionCount] = client
    
ConnectionCount++; 


int GetTeamFromSteamID(const char[] steamid, const char[] steamname)
{
    
int finalTeam GetConVarInt(assignCMD);
    
KeyValues kv = new KeyValues("assingedPlayers");
    
kv.ImportFromFile("/addons/sourcemod/configs/teamassigner/teams.cfg");
    if (
kv.JumpToKey(steamid)){
        
finalTeam kv.GetNum("team"0);
        
kv.SetString("nick"steamname);
        
kv.Rewind();
        
kv.ExportToFile("/addons/sourcemod/configs/teamassigner/teams.cfg");
        
PrintToServer("Data found.");
        
delete kv;
    } else {
        
kv.JumpToKey(steamidtrue);
        
kv.SetString("nick"steamname);
        
kv.SetNum("team"GetConVarInt(assignCMD));
        
PrintToServer("Data created.");
        
kv.Rewind();
        
kv.ExportToFile("/addons/sourcemod/configs/teamassigner/teams.cfg");
        
delete kv;
    }
    return 
finalTeam;
}

public 
Action PlayerActivate(Handle event, const char[]useridbool dontBroadcast) { 
    for(
int isizeof(Connections); i++) { 
        if(
Connections[i]) { 
            if (!
IsClientSourceTV(Connections[i])){
                new 
String:auth[64]; 
                new 
String:name[64]; 
                
GetClientAuthId(Connections[i], AuthId_SteamID64authsizeof(auth), true);
                
GetClientName(Connections[i], namesizeof(name));
                
PrintToServer("Player join: SteamID: %s"auth);
                
PrintToServer("Player Name: %s"name);
                if (!
IsFakeClient(Connections[i])){
                    
ChangeClientTeam(Connections[i], GetTeamFromSteamID(authname)); 
                }
                
Connections[i] = false
            } else {
                
Connections[i] = false;
            }
        } 
    } 

CBUltimate is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 08:38.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode