Team Join Control

A place for requesting new Source.Python plugins to be made for your server.

Please request only one plugin per thread.
[+35]Jumpman
Junior Member
Posts: 14
Joined: Tue Jul 24, 2018 9:26 am

Team Join Control

Postby [+35]Jumpman » Tue May 18, 2021 8:55 am

Hi

Can someone make a script that forces you to autojoin instead of selecting a team when initially joining ?

No matter what you choose CT / T it should do the same like if you choose auto and throw you over to the team with the lowest player, after map start when the first player chooses team it should throw the player over to a random team so the team to join is not the same after each map change.
User avatar
VinciT
Senior Member
Posts: 315
Joined: Thu Dec 18, 2014 2:41 am

Re: Team Join Control

Postby VinciT » Tue May 18, 2021 8:18 pm

Hi Jumpman, give this a shot:

Syntax: Select all

# ../force_team/force_team.py

# Python
from contextlib import suppress
from random import choice

# Source.Python
from commands import CommandReturn
from commands.client import ClientCommand
from events import Event
from filters.players import PlayerIter
from listeners import OnLevelInit
from players.entity import Player


# Put players in a team as soon as they close the MOTD? (True/False)
PICK_TEAM_AFTER_MOTD = False
# Prevent players from joining the spectator team? (True/False)
DISABLE_MANUAL_SPECTATE = False
# Prevent players from changing their team if they are already a terrorist or
# counter-terrorist? (True/False)
SELECT_TEAM_ONCE = False


# Dictionary used to keep track of player numbers in each team.
teams = {2: 0, 3: 0}


class PlayerFT(Player):
"""Extended Player class."""

def __init__(self, index, caching=True):
"""Initializes the object."""
super().__init__(index, caching)
self.is_team_assigned = False


def load():
"""Called when the plugin gets loaded."""
# If there are players on the server while the plugin is being loaded, make
# sure to count how many players each team has.
for player in PlayerIter():
try:
teams[player.team] += 1
except KeyError:
# The player's team is either 0 (unassigned) or 1 (spectator), and
# we don't need to keep track of those.
continue


@OnLevelInit
def on_level_init(map_name):
"""Called when a new map starts."""
# Reset the dictionary values, just in case.
teams.update({2: 0, 3: 0})


@Event('player_team')
def player_team(event):
"""Called when a player changes teams."""
new_team = event.get_int('team')
old_team = event.get_int('oldteam')

with suppress(KeyError):
teams[old_team] -= 1

with suppress(KeyError):
teams[new_team] += 1

if SELECT_TEAM_ONCE:
player = PlayerFT.from_userid(event['userid'])
player.is_team_assigned = True if new_team in (2, 3) else False


@ClientCommand('joingame')
def joingame_command(command, index):
"""Command the player uses to close the MOTD."""
if PICK_TEAM_AFTER_MOTD:
PlayerFT(index).client_command('jointeam')
return CommandReturn.BLOCK


@ClientCommand('jointeam')
def jointeam_command(command, index):
"""Command the player uses to select and join a team."""
player = PlayerFT(index)

try:
# 0: Auto Assign
# 1: Spectate
# 2: Terrorist
# 3: Counter-Terrorist
chosen_team = int(command[1])
except (IndexError, ValueError):
# IndexError: player used the 'jointeam' command without any arguments.
# ValueError: incorrect argument type was passed (not a number).
chosen_team = 0

# Is the player trying to become a spectator?
if chosen_team == 1 and not DISABLE_MANUAL_SPECTATE:
player.team = chosen_team
return CommandReturn.BLOCK

# Was the player already assigned to a team?
if player.is_team_assigned:
# Don't go further.
return CommandReturn.BLOCK

t_num, ct_num = teams.values()
# Are the teams balanced numbers-wise?
if t_num == ct_num:
# Choose a random team.
team_index = choice((2, 3))

# Or do the terrorists outnumber the counter-terrorists?
elif t_num > ct_num:
team_index = 3

# Or is it the other way around?
else:
team_index = 2

# Change the player's team.
player.team = team_index
# Since we're setting the player's team directly, block the command.
return CommandReturn.BLOCK


@ClientCommand('spectate')
def spectate_command(command, index):
"""Command the player uses to switch to the spectator team."""
if DISABLE_MANUAL_SPECTATE:
# Don't allow players to spectate.
return CommandReturn.BLOCK
In addition to what you've requested, I've also disabled the ability to join the spectator team. If you still want the players to be able to become spectators, let me know and I'll adjust the plugin. :smile:
Last edited by VinciT on Sat May 22, 2021 9:53 pm, edited 3 times in total.
ImageImageImageImageImage
User avatar
L'In20Cible
Project Leader
Posts: 1433
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Re: Team Join Control

Postby L'In20Cible » Tue May 18, 2021 9:04 pm

If my memory serves me right, I believe you could also add something like that to force them on a random team as soon as they accept the MOTD instead of sending them the team menu:

Syntax: Select all

@ClientCommand('joingame')
def joingame_command(command, index):
Player(index).client_command('jointeam')
return CommandReturn.BLOCK
User avatar
VinciT
Senior Member
Posts: 315
Joined: Thu Dec 18, 2014 2:41 am

Re: Team Join Control

Postby VinciT » Tue May 18, 2021 9:37 pm

Oh that's awesome. I've updated the plugin to include that snippet. Thanks L'In20Cible!
ImageImageImageImageImage
[+35]Jumpman
Junior Member
Posts: 14
Joined: Tue Jul 24, 2018 9:26 am

Re: Team Join Control

Postby [+35]Jumpman » Wed May 19, 2021 9:34 am

VinciT wrote:Hi Jumpman, give this a shot:

In addition to what you've requested, I've also disabled the ability to join the spectator team. If you still want the players to be able to become spectators, let me know and I'll adjust the plugin. :smile:


Hi VinciT, It look very cool and big thanks for your time, yes all players must be able to join spector when they connect to the server, map changes or when they are dead, when they want to join the game dosent matter if they chose CT or T they will get forced to the team with lowest players, but i can see i forgot to write i was going to use it for GunGame, maybe that's why the code does not work for me or i do something wrong ?

Code: Select all

2021-05-19 10:22:56 - sp   -   MESSAGE   [SP] Loading plugin 'gungame'...
2021-05-19 10:22:56 - sp   -   WARNING   
[SP] Encountered a Warning:
  File '../addons/source-python/plugins/gungame/core/plugins/valid.py', line 105: UserWarning
    Custom plugin "force_team" is missing info.ini file.

2021-05-19 10:22:56 - sp   -   MESSAGE   [SP] Successfully loaded plugin 'gungame'.
cssbestrpg
Senior Member
Posts: 133
Joined: Sun May 17, 2020 7:56 am
Location: Finland
Contact:

Re: Team Join Control

Postby cssbestrpg » Wed May 19, 2021 3:56 pm

Try load it as separate plugin instead of inside of gungame. Just created folder for the plugin and .py file for it.
User avatar
VinciT
Senior Member
Posts: 315
Joined: Thu Dec 18, 2014 2:41 am

Re: Team Join Control

Postby VinciT » Wed May 19, 2021 9:39 pm

cssbestrpg wrote:Hi VinciT, It look very cool and big thanks for your time, yes all players must be able to join spector when they connect to the server, map changes or when they are dead..
No problem! I've modified the plugin to include two settings with which you can change the plugin's behavior.

As for the loading issue, cssbestrpg is correct - this is a standalone plugin and not part of GunGame.
Here's a quick rundown on how to install and load the plugin:
  1. Navigate to your server's ../addons/source-python/plugins/ folder
  2. Create a new folder and name it force_team
  3. Create a new file and name it force_team.py
  4. Copy the code from this post and paste it into the newly created force_team.py file
  5. Last but not least, add this to your server's autoexec.cfg file: sp plugin load force_team
ImageImageImageImageImage
[+35]Jumpman
Junior Member
Posts: 14
Joined: Tue Jul 24, 2018 9:26 am

Re: Team Join Control

Postby [+35]Jumpman » Thu May 20, 2021 12:26 pm

Thanks it working fine now, you have done a great work here big thanks for that :-)

I have just test your plugin out and found out that i cant change teams when I am alive ?
User avatar
VinciT
Senior Member
Posts: 315
Joined: Thu Dec 18, 2014 2:41 am

Re: Team Join Control

Postby VinciT » Sat May 22, 2021 9:55 pm

That is correct. I made the assumption that you only wanted players to choose their team once.
I've updated the plugin once more to include another setting to enable/disable that behavior.
ImageImageImageImageImage
[+35]Jumpman
Junior Member
Posts: 14
Joined: Tue Jul 24, 2018 9:26 am

Re: Team Join Control

Postby [+35]Jumpman » Sat May 29, 2021 7:20 am

Once again thank you, i will test it asap i not have so much time this week be course of work, but will return right after I have tested it thank you :-)

Return to “Plugin Requests”

Who is online

Users browsing this forum: No registered users and 2 guests