Page 1 of 1

how to forcing player to specific team

Posted: Sun Sep 28, 2014 4:13 pm
by 8guawong
i've found

http://www.sourcepython.com/showthread.php?478-Help-with-Client-Command-Filter-to-force-players-to-join-specified-team&highlight=Client+Command+Filter

but all the wiki posted are not available anymore

here is what i have right now

Syntax: Select all

from commands.client import client_command_manager

def load():
client_command_manager.register_filter(client_command_filter)

def unload():
client_command_manager.unregister_filter(client_command_filter)

def client_command_filter(player, CCommand):
if CCommand == 'jointeam':
print('hello')


hello does not show up ~

tested game: CSGO

Posted: Sun Sep 28, 2014 4:33 pm
by Ayuto
You are trying to compare the Command object with a string. This will always result in False.
http://wiki.sourcepython.com/index.php/commands#Command
http://wiki.sourcepython.com/index.php/commands.client

Syntax: Select all

def client_command_filter(player, command):
if command[0] == 'jointeam':
print('hello')

Posted: Sun Sep 28, 2014 4:38 pm
by L'In20Cible
You should use the ClientCommandFilter decorator.

Posted: Sun Sep 28, 2014 5:47 pm
by 8guawong
Ayuto wrote:You are trying to compare the Command object with a string. This will always result in False.
http://wiki.sourcepython.com/index.php/commands#Command
http://wiki.sourcepython.com/index.php/commands.client

Syntax: Select all

def client_command_filter(player, command):
if command[0] == 'jointeam':
print('hello')


thanks got it!

Posted: Sun Sep 28, 2014 5:56 pm
by 8guawong
L'In20Cible wrote:You should use the ClientCommandFilter decorator.


whats the difference if i may ask

i tried both way and both works :p

Posted: Sun Sep 28, 2014 6:22 pm
by Ayuto
Yes, the result is the same, but if you use the decorator the code is shorter (50%) and less error prone as you don't need to unregister the filter when you unload the plugin. This will be done automatically.

Just compare these two scripts and you will understand.

Syntax: Select all

from commands.client import client_command_manager

def load():
client_command_manager.register_filter(client_command_filter)

def unload():
client_command_manager.unregister_filter(client_command_filter)

def client_command_filter(player, command):
pass

Syntax: Select all

from commands.client import ClientCommandFilter

@ClientCommandFilter
def client_command_filter(player, command):
pass
As you can see the second script is much easier to use.

Posted: Sun Sep 28, 2014 8:33 pm
by BackRaw
I use something like that:

Syntax: Select all

from commands import CommandReturn
from commands.client import ClientCommandFilter

# 'team_manager' is a dictionary containing teamid values for "t" and "ct", identical to:
team_manager = {
"t": 2,
"ct": 3
}


@ClientCommandFilter
def jointeam_filter(playerinfo, command):
"""
Gets called whenever a client command is executed.
"""

# was 'jointeam' the command?
if command[0] != "jointeam":

# if not, allow the action
return CommandReturn.CONTINUE

# get the player count of the T team
count_t = len(tuple(playeriter_t))

# get the selected TeamID
teamid = int(command[1])

# grab the player's index
index = index_from_playerinfo(playerinfo)

# are there players on T?
if count_t < 1 and teamid != team_manager["t"]:

# if not, tell the player that they must join T
tell(index, "There are no players on the Terrorist side, please join there.")

# and don't allow the previous action
return CommandReturn.BLOCK

# if yes ...
if count_t > 0:

# ... did we chose the T team?
if teamid == team_manager["t"]:

# if yes, tell a message
tell(index, "There are players on the Terrorist side, but none on the Counter-Terrorist side.")
tell(index, "Please join there.")

# and don't allow the previous action
return CommandReturn.BLOCK

# if not, is this player currently on the T team and wants to switch to the CT team?
if playerinfo.get_team_index() == team_manager["t"]:

tell(index, "Players are auto-switched by the script.")

return CommandReturn.BLOCK

# if not, allow the action
return CommandReturn.CONTINUE


# 'tell' is a wrapper function to make telling players a message easy
# like es.tell() if you're familiar with EventScripts.

Posted: Mon Sep 29, 2014 12:08 am
by 8guawong
Ayuto wrote:Yes, the result is the same, but if you use the decorator the code is shorter (50%) and less error prone as you don't need to unregister the filter when you unload the plugin. This will be done automatically.

Just compare these two scripts and you will understand.

Syntax: Select all

from commands.client import client_command_manager

def load():
client_command_manager.register_filter(client_command_filter)

def unload():
client_command_manager.unregister_filter(client_command_filter)

def client_command_filter(player, command):
pass

Syntax: Select all

from commands.client import ClientCommandFilter

@ClientCommandFilter
def client_command_filter(player, command):
pass
As you can see the second script is much easier to use.


Yep code is shorter i see :)

Posted: Mon Sep 29, 2014 12:10 am
by 8guawong
BackRaw wrote:I use something like that:

Syntax: Select all

from commands import CommandReturn
from commands.client import ClientCommandFilter

# 'team_manager' is a dictionary containing teamid values for "t" and "ct", identical to:
team_manager = {
"t": 2,
"ct": 3
}


@ClientCommandFilter
def jointeam_filter(playerinfo, command):
"""
Gets called whenever a client command is executed.
"""

# was 'jointeam' the command?
if command[0] != "jointeam":

# if not, allow the action
return CommandReturn.CONTINUE

# get the player count of the T team
count_t = len(tuple(playeriter_t))

# get the selected TeamID
teamid = int(command[1])

# grab the player's index
index = index_from_playerinfo(playerinfo)

# are there players on T?
if count_t < 1 and teamid != team_manager["t"]:

# if not, tell the player that they must join T
tell(index, "There are no players on the Terrorist side, please join there.")

# and don't allow the previous action
return CommandReturn.BLOCK

# if yes ...
if count_t > 0:

# ... did we chose the T team?
if teamid == team_manager["t"]:

# if yes, tell a message
tell(index, "There are players on the Terrorist side, but none on the Counter-Terrorist side.")
tell(index, "Please join there.")

# and don't allow the previous action
return CommandReturn.BLOCK

# if not, is this player currently on the T team and wants to switch to the CT team?
if playerinfo.get_team_index() == team_manager["t"]:

tell(index, "Players are auto-switched by the script.")

return CommandReturn.BLOCK

# if not, allow the action
return CommandReturn.CONTINUE


# 'tell' is a wrapper function to make telling players a message easy
# like es.tell() if you're familiar with EventScripts.


Hi why return CommandReturn.CONTINUE and CommandReturn.BLOCK?? I just use return True and return False
any difference?

Posted: Mon Sep 29, 2014 12:42 am
by 8guawong
btw is there an equivalent of

Code: Select all

es.cexec(userid, 'jointeam 3')

for SP?

-------------------------- edit ----------------------

nvm got it

Syntax: Select all

engine_server.client_command(edict, 'jointeam 3')

Posted: Mon Sep 29, 2014 11:08 pm
by satoon101
Technically, you should be using CommandReturn to block or allow the command. However, True/False work just the same. You can also return None (which also works by not returning any value) and it will work as True/CommandReturn.CONTINUE. If I remember correctly, only returning a False value or CommandReturn.BLOCK will block the command.