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.
Team Join Control
-
- Junior Member
- Posts: 14
- Joined: Tue Jul 24, 2018 9:26 am
Re: Team Join Control
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.
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
Last edited by VinciT on Sat May 22, 2021 9:53 pm, edited 3 times in total.
- L'In20Cible
- Project Leader
- Posts: 1534
- Joined: Sat Jul 14, 2012 9:29 pm
- Location: Québec
Re: Team Join Control
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
Re: Team Join Control
Oh that's awesome. I've updated the plugin to include that snippet. Thanks L'In20Cible!
-
- Junior Member
- Posts: 14
- Joined: Tue Jul 24, 2018 9:26 am
Re: Team Join Control
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.
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'.
-
- Senior Member
- Posts: 309
- Joined: Sun May 17, 2020 7:56 am
- Location: Finland
- Contact:
Re: Team Join Control
Try load it as separate plugin instead of inside of gungame. Just created folder for the plugin and .py file for it.
Re: Team Join Control
No problem! I've modified the plugin to include two settings with which you can change the plugin's behavior.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..
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:
- Navigate to your server's ../addons/source-python/plugins/ folder
- Create a new folder and name it force_team
- Create a new file and name it force_team.py
- Copy the code from this post and paste it into the newly created force_team.py file
- Last but not least, add this to your server's autoexec.cfg file: sp plugin load force_team
-
- Junior Member
- Posts: 14
- Joined: Tue Jul 24, 2018 9:26 am
Re: Team Join Control
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 ?
I have just test your plugin out and found out that i cant change teams when I am alive ?
Re: Team Join Control
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.
I've updated the plugin once more to include another setting to enable/disable that behavior.
-
- Junior Member
- Posts: 14
- Joined: Tue Jul 24, 2018 9:26 am
Re: Team Join Control
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 :-)
Who is online
Users browsing this forum: No registered users and 9 guests