how to forcing player to specific team

Please post any questions about developing your plugin here. Please use the search function before posting!
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

how to forcing player to specific team

Postby 8guawong » Sun Sep 28, 2014 4:13 pm

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
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Sep 28, 2014 4:33 pm

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')
User avatar
L'In20Cible
Project Leader
Posts: 1536
Joined: Sat Jul 14, 2012 9:29 pm
Location: Québec

Postby L'In20Cible » Sun Sep 28, 2014 4:38 pm

You should use the ClientCommandFilter decorator.
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Sun Sep 28, 2014 5:47 pm

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!
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Sun Sep 28, 2014 5:56 pm

L'In20Cible wrote:You should use the ClientCommandFilter decorator.


whats the difference if i may ask

i tried both way and both works :p
User avatar
Ayuto
Project Leader
Posts: 2209
Joined: Sat Jul 07, 2012 8:17 am
Location: Germany

Postby Ayuto » Sun Sep 28, 2014 6:22 pm

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.
User avatar
BackRaw
Senior Member
Posts: 537
Joined: Sun Jul 15, 2012 1:46 am
Location: Germany
Contact:

Postby BackRaw » Sun Sep 28, 2014 8:33 pm

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.
My Github repositories:

Source.Python: https://github.com/backraw
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Sep 29, 2014 12:08 am

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 :)
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Sep 29, 2014 12:10 am

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?
8guawong
Senior Member
Posts: 148
Joined: Sat Sep 20, 2014 3:06 am

Postby 8guawong » Mon Sep 29, 2014 12:42 am

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')
User avatar
satoon101
Project Leader
Posts: 2703
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Mon Sep 29, 2014 11:08 pm

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.
Image

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 173 guests