// File: playernamecomplete.cfg // Author: FerreusRemedium // Created: 2013-02-20 // Last Change: 2013-03-06 // thanks to RaZgRiZ for the command flags & loopchar tutorial // allowed chars that are not in replacechars _pnc_allowedchars = "1234567890<>:.,_-+~#^^" // see /_pnc_cleanstring // Special chars could be added here so you can type their simpler version... // chars that shall be replaced _pnc_searchchars = "ETAOINSRHDLUCMFYWGPBVKXQJZ" // replacement chars _pnc_replacechars = "etaoinsrhdlucmfywgpbvkxqjz" // ensure replacechars are allowed or evil things will happen _pnc_allowedchars = (concatword $_pnc_replacechars $_pnc_allowedchars) _pnc_tag_delim = "~" _pnc_loopchar = [ loop $arg1 (strlen $arg2) [ $arg1 = (substr $arg2 $$arg1 1) arg3 ] ] // /_pnc_cleanstring // Return cleaned using _pnc_allowedchars, // _pnc_searchchars and _pnc_replacechars. _pnc_cleanstring = [ local c pos i pos = 0 while [< $pos (strlen $arg1)] [ c = (substr $arg1 $pos 1) i = (strstr $_pnc_searchchars $c) if (>= $i 0) [ arg1 = (strreplace $arg1 $c (substr $_pnc_replacechars $i 1)) pos = (+ $pos 1) ] [ if (< (strstr $_pnc_allowedchars $c) 0) [ arg1 = (strreplace $arg1 $c) // do not increase p, current char is deleted ] [ pos = (+ $pos 1) ] ] ] result $arg1 ] _pnc_getprivilege = [ || [? (isadmin $arg1) 2 0] [? (ismaster $arg1) 1 0] ] _pnc_isenemy = [ || [! (m_teammode (getmode))] [!=s (getclientteam $arg1) (getclientteam (getclientnum))] ] // /_pnc_checkplayer // Returns whether the client meets all conditions in . // // Flags: // s/S - spectator/not spectator // b/B - bot/not bot // i/I - ignored/not ignored // e/E - enemy/not enemy // p/P - current player/not current player // f/F - followed player/not followed player // m/M - master or admin/not master and not admin // a/A - admin/not admin // l/L - lower privilege/higher or equal privilege than player // h/H - higher privilege/lower or equal privilege than player _pnc_checkplayer = [ local c include cn cn = $arg2 include = 1 _pnc_loopchar c $arg1 [ if $include [ cases $c "s" [ include = (isspectator $cn) ] "S" [ include = (! (isspectator $cn)) ] "b" [ include = (isai $cn) ] "B" [ include = (! (isai $cn)) ] "i" [ include = (isignored $cn) ] "I" [ include = (! (isignored $cn)) ] "e" [ include = (_pnc_isenemy $cn) ] "E" [ include = (! (_pnc_isenemy $cn)) ] "p" [ include = (= $cn (getclientnum)) ] "P" [ include = (!= $cn (getclientnum)) ] () [ cases $c "f" [ include = (= $cn (getfollow)) ] "F" [ include = (!= $cn (getfollow)) ] "m" [ include = (|| [ismaster $cn] [isadmin $cn]) ] "M" [ include = (! (|| [ismaster $cn] [isadmin $cn])) ] "a" [ include = (isadmin $cn) ] "A" [ include = (! (isadmin $cn)) ] "l" [ include = (< (_pnc_getprivilege $cn) (_pnc_getprivilege (getclientnum))) ] "L" [ include = (>= (_pnc_getprivilege $cn) (_pnc_getprivilege (getclientnum))) ] "h" [ include = (> (_pnc_getprivilege $cn) (_pnc_getprivilege (getclientnum))) ] "H" [ include = (<= (_pnc_getprivilege $cn) (_pnc_getprivilege (getclientnum))) ] () [ error "Unknown flag:" $c ] ] ] ] result $include ] // /_pnc_getplayers // Returns a list of all players, filtered using . _pnc_getplayers = [ local list cns playername cns = (listclients 1) list = "" looplist cn $cns [ if (_pnc_checkplayer $arg1 $cn) [ playername = (getclientname $cn) playername = (_pnc_cleanstring $playername) playername = (concatword $playername $_pnc_tag_delim $cn) list = (concat $list $playername) ] ] result $list ] // /_pnc_gettag [~] // Returns . _pnc_gettag = [ local pos pos = (strstr $arg1 $_pnc_tag_delim) while [> $pos -1] [ arg1 = (substr $arg1 (+ $pos 1)) pos = (strstr $arg1 $_pnc_tag_delim) ] result $arg1 ] // /playernamecomplete [] [] // Create which calls supporting tab-completion of // connected players. // // The usage of is: // / [~] {ARGNUM-1 times} [...] // // is passed as the first argument to . // The renaming arguments are passed directly to . // // is passed as the 'th argument to command. // has to be called with at least arguments. // Arguments higher than are passed after . // If is missing, it counts as 1. // When is called with less than arguments, they are // replaced by empty strings. // // Tab-completion: // The first argument of can be tab-completed using the names of // the players that were connected when this function was called. // // are flags for the command _pnc_checkplayer. They specify // the conditions a player has to meet to appear in the completion. // // Every player name in the completion is cleaned from any character // that is not in _pnc_allowedchars. // After that, every character it the name that appears in _pnc_searchchars // is replaced with the character at the same position in _pnc_replacechars. // // Note: // The ~ delimiter can be changed by changing _pnc_tag_delim. This does not // work in runtime, and the script needs to be re-executed. // The completion also works when the name contains the delimiter, as the // last occurrence is used, but it does NOT work when the client number // contains the delimiter. // If there is no delimiter, nothing is cut from the argument. That means // you can also just write the cn instead of using tab-completion. // // Example: // /playernamecomplete d dosomething "PSE" 2 // // Creates command /d with the usage: // /d [~] [...] // // The first argument can be tab-completed with the names of all players that // are NOT the player himself, are NOT spectators and are in the player's team. // // /d calls /dosomething [...] // Where ARGS are the remaining arguments after ARG1. // Example of d: // /d foo~42 0 evil 5 // would call // /dosomething 0 42 evil 5 playernamecomplete = [ local cnarg cnarg = (? (>= $numargs 4) $arg4 1) if (< $cnarg 1) [ error "ARGNUM needs to be 1 or greater." ] [ $arg1 = [ local cmd cnarg; cmd = @(concatword "^"" $arg2 "^""); cnarg = @cnarg; do [@_pnc_body] ] listcomplete $arg1 (_pnc_getplayers $arg3) ] ] // Not actually a function, but used in wrapper functions. _pnc_body = [ local cn args argi cn = (_pnc_gettag $arg1) // create call like ["dosomething" $arg2 $cn $arg3 $arg4] do [@cmd @( if (> $numargs 0) [ argi = 1 result (loopconcat i (max $numargs $cnarg) [ if (= $i (- $cnarg 1)) [ result (concat $args [$cn]) ] [ argi = (+ $argi 1) result (concat $args (concatword [$arg] $argi)) ] ]) ] [ result "" ] )] ] // update-method example: // //_pnc_update = [ // // /f follow player, list non-spectators and not the followed one // playernamecomplete f follow "SF" // // /ig ignore player, list non-ignored players and not the player himself // playernamecomplete ig ignore "PI" // // /unig unignore player, list ignored players // playernamecomplete unig unignore "i" // // /ki kick player, list players with lesser privileges and not the player himself (rather redundant) // playernamecomplete ki kick "lP" //] // vim:set sw=4 ts=4 sts=0 noet sta sr ft=cube fdm=syntax: