Page 1 of 1

Server, Client, and Say Commands

Posted: Tue Jan 15, 2013 6:05 am
by satoon101
I just added a new update to the command structure. Previously, the Python-side API only allowed the used of decorators to register/unregister the commands. With this update, you can now use a registration class (which the decorators now tie into) to register your commands:

  • ServerCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [description]
      4. [flags]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.server import ServerCommandRegistry

      def server_command_callback(CCommand):
      print('Test command just called')

      ServerCommandRegistry.register_command('test_command', server_command_callback, 'Test command')

      def unload():
      ServerCommandRegistry.unregister_command('test_command', server_command)
  • ClientCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import ClientCommandRegistry

      def client_command_callback(edict, CCommand):
      print('Test client command just called')

      ClientCommandRegistry.register_command('client_test', client_command_callback)

      def unload():
      ClientCommandRegistry.unregister_command('client_test', client_command_callback)
  • SayCommandRegistry:
    • register_command:
      1. <names>
      2. <callback>
      3. [level]
      4. [permission]
      5. [flag]
      6. [fail_callback]
    • unregister_command:
      1. <names>
      2. <callback>
    • Use:

      Syntax: Select all

      from commands.client import SayCommandRegistry

      def say_command_callback(index, teamonly, CCommand):
      print('Test say command just called')

      SayCommandRegistry.register_command('say_test', say_command_callback)

      def unload():
      SayCommandRegistry.unregister_command('say_test', say_command_callback)

With ClientCommandRegistry and SayCommandRegistry, you can also register filters (though client command filters has not been added, as of yet):

Syntax: Select all

from commands.client import ClientCommandRegistry
from commands.say import SayCommandRegistry

def load():
ClientCommandRegistry.register_filter(client_command_filter)
SayCommandRegistry.register_filter(say_filter)

def client_command_filter(edict, CCommand):
print('Client Command Filter')

def say_filter(index, teamonly, CCommand):
print('Say Filter')

def unload():
ClientCommandRegistry.unregister_filter(client_command_filter)
SayCommandRegistry.unregister_filter(say_filter)


You can also, of course, use the decorators to register your commands/filters, and they will automatically be unregistered when you unload your script:

Syntax: Select all

from commands.client import ClientCommand
from commands.client import ClientCommandFilter
from commands.say import SayCommand
from commands.say import SayFilter
from commands.server import ServerCommand

@ClientCommand('client_test')
def client_command_test(edict, CCommand):
print('Client Command Test')

@ClientCommandFilter
def client_command_filter_test(edict, CCommand):
print('Client Command Filter Test')

@SayCommand('say_test')
def say_command_test(index, teamonly, CCommand):
print('Say Command Test')

@SayFilter
def say_filter_test(index, teamonly, CCommand):
print('Say Filter Test')

@ServerCommand('server_test')
def server_command_test(CCommand):
print('Server Command Test')
Again, client command filters are not implemented, so that portion of the code above will probably raise an error. All of the rest should work perfectly fine.

If you notice any bugs, have any questions/comments, please feel free to post them.

Satoon

Re: Server, Client, and Say Commands

Posted: Tue Nov 21, 2017 10:01 am
by battleweaver
Say, I have an endless warmup period, then i want to execute "mp_warmup_end" at server side. How can I execute this command directly from plugin?

Re: Server, Client, and Say Commands

Posted: Tue Nov 21, 2017 10:35 am
by BackRaw
battleweaver wrote:Say, I have an endless warmup period, then i want to execute "mp_warmup_end" at server side. How can I execute this command directly from plugin?

Syntax: Select all

from engines.server import engine_server

# where you need it:
engine_server.execute_server_command('mp_warmup_end')
Should do it.

Re: Server, Client, and Say Commands

Posted: Tue Nov 21, 2017 12:51 pm
by Ayuto
Not quite.

Syntax: Select all

from engines.server import execute_server_command

# where you need it:
execute_server_command('mp_warmup_end')

Re: Server, Client, and Say Commands

Posted: Tue Nov 21, 2017 4:05 pm
by battleweaver
Guys, thank you very much! I was so inattentive while reading wiki.

Re: Server, Client, and Say Commands

Posted: Tue Nov 21, 2017 4:06 pm
by BackRaw
Ayuto wrote:Not quite.

Syntax: Select all

from engines.server import execute_server_command

# where you need it:
execute_server_command('mp_warmup_end')

Whoops :D

Re: Server, Client, and Say Commands

Posted: Wed Nov 22, 2017 10:44 am
by battleweaver
A problem though.
It worked correctly, but server did not recognize the other command: execute_server_command('bot_kick all')

Re: Server, Client, and Say Commands

Posted: Wed Nov 22, 2017 3:58 pm
by Ayuto
IIRC, it should raise an exception, because "bot_kick all" is not a server command, but a server command plus an argument. Try this instead:

Syntax: Select all

execute_server_command('bot_kick', 'all')

Re: Server, Client, and Say Commands

Posted: Thu Nov 23, 2017 10:28 am
by battleweaver
Ayuto wrote:IIRC, it should raise an exception, because "bot_kick all" is not a server command, but a server command plus an argument. Try this instead:

Syntax: Select all

execute_server_command('bot_kick', 'all')

Test case:
"sp_version" = "607", deathmatch gamemode

Syntax: Select all

from commands.typed import TypedSayCommand
from messages import SayText2
from engines.server import execute_server_command

@TypedSayCommand('!test1')
def cmd_on_test1(command_info):
execute_server_command('bot_kick') #enough to kick all bots
SayText2(f"Bots kicked").send()

@TypedSayCommand('!test2')
def cmd_on_test2(command_info):
execute_server_command('exec', 'my.cfg') #config is located in ../csgo/cfg/
SayText2(f"Config executed").send()

@TypedSayCommand('!test3')
def cmd_on_test3(command_info):
execute_server_command('mp_warmup_end')
SayText2(f"Warmup ended").send()


Output:
All test cases returned SayText to user, but only test2 executed server command.

This is most confusing, because i used

Syntax: Select all

execute_server_command('mp_warmup_end')
in other places of my code, and it works fine.

Re: Server, Client, and Say Commands

Posted: Thu Nov 23, 2017 6:47 pm
by Ayuto
Hmm, the immediate execution of the server command doesn't seem to work in certain cases. In this case use queue_server_command.

Re: Server, Client, and Say Commands

Posted: Fri Nov 24, 2017 12:41 pm
by battleweaver
Ayuto wrote:Hmm, the immediate execution of the server command doesn't seem to work in certain cases. In this case use queue_server_command.

Works as you predicted. Thanks a lot!