Scripts, Tips and Tricks

De piernov
Aller à : navigation, rechercher

Executing commands from "server console"

Hopmod newbies may be wondering how it's possible to run unprompted commands outside of event handlers, the answer is through a script pipe. The out-of-box solution is to use a named pipe where the server reads and executes code from this pipe.

Look in your server.conf file to make sure you have the command that sets this up, it should look something like this: script_pipe mkfifochan "serverexec 777" []

Here's an example of sending script code to the server from the shell:

echo "console graham [hear me roarrr]" > serverexec

...the code is run immediately. If code doesn't run as expected then look in the server's stderr file for error messages.

Banning w00t clan players

Copy and paste this code snippet into your onconnect event handler. This script bans any players wearing the w00t clan tag at the end of their names.

if (match "^.*|w00t$" (player_name $cn)) [
    spec $cn
    console "script" (concat "Everybody say bye bye to" (player_name $cn))
    sleep 7000 [kick @cn]
]

symbol? function

This is a predicate function that takes an id argument and returns true if the id exists in the symbol table, and returns false otherwise. The symbol? function is useful in situations where a symbol (function, variable, alias etc) may not exist because a module hasn't been loaded, for instance, and you need to determine at runtime if a dependent script is runnable. See the player country message code for a concrete usage example of the symbol? function.

Guidelines for extending event handlers in server.conf

  • If possible append your additional code to the event handler, if your code contains any errors the existing code which has been tested and works fine won't be run because the handler function had to exit because it encountered your buggy code first.
  • Always test your changes however minor, and think about how other event handlers may be affected.

One-shot handler idiom

Some useful commands in common.csl

common.csl is a script file that contains general support functions (written as aliases) useful for scripts and server admins. The script is executed from server.conf at server startup.

Name Parameters Description
specall Move all players to spectators. The unspecall function does the opposite to this function.
announce text Broadcast server announcement to all connected players. To ensure all players can actually read the message: all players are temporarily spec'd for 7 seconds, no game activity means no flooding the player's message console. I imagine this command will become very annoying to players if overused, it should only be used to announce imminent actions that will interrupt the current game, server shutdown or forced map change as examples.
console operator text Broadcast message to all connected players coming from the server console operator. Use this function instead of crafting your own messages with the low-level msg function.
msgteam team text Send a private message to each team member of the team name given in the first argument.
msgspecs text Send a private message to all the spectators.