--- deliantra/server/ext/commands.ext 2007/01/01 16:00:09 1.7 +++ deliantra/server/ext/commands.ext 2007/08/10 05:25:16 1.58 @@ -1,13 +1,316 @@ -#! perl +#! perl # mandatory depends=irc use POSIX (); # miscellaneous commands +sub who_listing(;$$) { + my ($privileged, $select) = @_; + + my ($numwiz, $numafk) = (0, 0); + my @pl; + + foreach my $pl (cf::player::list) { + my $ns = $pl->ns or next; + my $ob = $pl->ob; + + next unless $ob->map + && ($privileged || !$pl->hidden); + + $numwiz++ if $ob->flag (cf::FLAG_WIZ); + $numafk++ if $ns->afk; + + push @pl, $pl; + } + + ( + "Total Players in The World. (" . (scalar @pl) . ") -- WIZ($numwiz) AFK($numafk) BOT(0)", + (grep /$select/, + map { + my ($pl, $ob, $ns) = ($_, $_->ob, $_->ns); + + "* " . $ob->name . "/" . $ob->level . " " . (length $pl->own_title ? $pl->own_title : "the " . $pl->title) + . ($pl->gender ? " [f]" : " [m]") + . ($pl->peaceful ? " [peaceful]" : " [HOSTILE]") + . ($ns->afk ? " [AFK]" : "") + . ($ob->flag (cf::FLAG_WIZ) ? " [WIZ]" : "") + . " [" . $pl->ns->version . "]" + . " [" . ($pl->peaceful || $privileged ? $ob->map->visible_name : $ob->region->name) . "]" + . (sprintf " [rtt %.3fs]", $pl->ns->tcpi_rtt * 1e-6) + . ($privileged ? " " . $pl->ns->host : "") + } sort { (lc $a->ob->name) cmp (lc $b->ob->name) } @pl + ), + eval { "* IRC: " . join ", ", ext::irc::users }, + ) +} + +cf::register_command who => sub { + my ($ob, $arg) = @_; + + $ob->speed_left ($ob->speed_left - 4); + + $ob->reply (undef, (join "\n", who_listing $ob->may ("extended_who"), $arg), cf::NDI_UNIQUE | cf::NDI_DK_ORANGE); +}; + +cf::register_command seen => sub { + my ($pl, $args) = @_; + + if (my ($login) = $args =~ /(\S+)/) { + if ($login eq $pl->name) { + $pl->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE); + } elsif (cf::player::find_active $login) { + $pl->message ("$login is right here on this server!", cf::NDI_UNIQUE); + } elsif (cf::player::exists $login + and stat cf::player::path $login) { + my $time = (stat _)[9]; + + $pl->message ("$login was last seen here " + . (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time) + . " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_UNIQUE); + } else { + $pl->message ("No player named $login is known to me.", cf::NDI_UNIQUE); + } + } else { + $pl->message ("Usage: seen ", cf::NDI_UNIQUE); + } +}; + +cf::register_command body => sub { + my ($ob) = @_; + + # Too hard to try and make a header that lines everything up, so just + # give a description. (comment from C++) + my $reply = + "The first column is the name of the body location.\n\n" + . "The second column is how many of those locations your body has.\n\n" + . "The third column is how many slots in that location are available.\n\n"; + + $reply .= sprintf " %-20s %3s %5s\n", "Location", "You", "Avail"; + for (0 .. cf::NUM_BODY_LOCATIONS - 1) { + my $msg = cf::object::slot_nonuse_name $_; + $msg =~ s/^.*? a //; + $reply .= sprintf " %-20s %3d %5d\n", $msg, $ob->slot_info ($_), $ob->slot_used ($_) + if $ob->slot_info ($_) or $ob->slot_used ($_); + } + + $reply .= "You are not allowed to wear armor\n\n" + unless $ob->flag (cf::FLAG_USE_ARMOUR); + $reply .= "You are not allowed to use weapons\n\n" + unless $ob->flag (cf::FLAG_USE_WEAPON); + + $ob->reply (undef, $reply); +}; + +cf::register_command mark => sub { + my ($pl, $arg) = @_; + + if (length $arg) { + my $ob = $pl->find_best_object_match ($arg); + + return $pl->reply (undef, "Could not find an object that matches $arg") + unless $ob; + + $pl->contr->mark ($ob); + $pl->reply (undef, (sprintf "Marked item %s", $ob->name, $ob->title)); + } else { + my $ob = $pl->find_marked_object; + + $pl->reply (undef, $ob + ? (sprintf "%s %s * is marked.", $ob->name, $ob->title) + : "You have no marked object."); + } +}; + +for my $cmd ("run", "fire") { + my $oncmd = "${cmd}_on"; + cf::register_command $cmd => sub { + my ($ob, $arg) = @_; + + $ob->reply (undef, "Can't $cmd into a non adjacent square.") + if $arg < 0 or $arg >= 9; + + $ob->contr->$oncmd (1); + $ob->move_player ($arg); + }; + + cf::register_command "${cmd}_stop" => sub { + my ($ob) = @_; + + $ob->contr->$oncmd (0); + }; +} + +cf::register_command mapinfo => sub { + my ($ob) = @_; + + my $observe = $ob->contr->observe; + + my $map = $observe->map + or return; + $ob->reply (undef, (sprintf "%s (%s) %s", $map->name, $map->path, $observe->region->longname)); + $ob->reply (undef, (sprintf "players: %d difficulty: %d size: %d start: %dx%d timeout: %d", + (scalar $map->players), $map->difficulty, $map->width, $map->height, $map->enter_x, $map->enter_y, $map->timeout)) + if $ob->flag (cf::FLAG_WIZ); + $ob->reply (undef, $map->msg); +}; + +cf::register_command whereami => sub { + my ($ob) = @_; + + my $reg = $ob->contr->observe->region; + $ob->reply (undef, (sprintf "You are %s.\n%s", $reg->longname, $reg->msg)); +}; + +cf::register_command whereabouts => sub { + my ($ob, $arg) = @_; + + my %count; + + for my $pl (cf::player::list) { + ++$count{$pl->ob->region->longname}; + } + + my $msg = "In the world currently there are:\n\n" + . join "", map "$count{$_} player(s) in $_\n\n", sort keys %count; + + $ob->contr->ns->send_msg (log => $msg, cf::NDI_REPLY); +}; + +cf::register_command hiscore => sub { + my ($ob, $arg) = @_; + + my $url = $cf::CFG->{hiscore_url}; + $ob->contr->ns->send_msg (log => "See $url", cf::NDI_REPLY); +}; + +sub _set_mode($$$@) { + my ($name, $ob, $arg, $slot, @choices) = @_; + + my $oldmode = $ob->contr->$slot; + + return $ob->reply (undef, "$name is set to $choices[$oldmode]") + unless $arg; + + my ($idx) = grep $choices[$_] eq $arg, 0 .. $#choices + or return $ob->reply (undef, "$name: Unknown options '$arg', valid options are @choices"), 1; + + $ob->contr->$slot ($idx); + $ob->reply (undef, "$name" . ($oldmode == $ob->contr->unapply ? "" : " now") . " set to " . $choices[$ob->contr->$slot]); +} + +cf::register_command applymode => sub { + my ($ob, $arg) = @_; + + _set_mode "applymode", $ob, $arg, unapply => qw(nochoice never always); +}; + +cf::register_command petmode => sub { + my ($ob, $arg) = @_; + + _set_mode "petmode", $ob, $arg, petmode => qw(normal sad defend arena); +}; + +cf::register_command usekeys => sub { + my ($ob, $arg) = @_; + + _set_mode "usekeys", $ob, $arg, usekeys => qw(inventory keyrings containers); +}; + +cf::register_command hintmode => sub { + my ($ob, $arg) = @_; + + _set_mode "hintmode", $ob, $arg, hintmode => qw(show mark hide); +}; + +cf::register_command afk => sub { + my ($ob, $arg) = @_; + + $ob->contr->ns->afk ($ob->contr->ns->afk ? 0 : 1); + $ob->reply (undef, $ob->contr->ns->afk ? "You are now AFK" : "You are no longer AFK"); +}; + +cf::register_command sound => sub { + my ($ob, $arg) = @_; + + $ob->contr->ns->sound ($ob->contr->ns->sound ? 0 : 1); + $ob->reply (undef, $ob->contr->ns->sound ? "The sounds are enabled." : "Silence is golden..."); +}; + +cf::register_command brace => sub { + my ($ob, $arg) = @_; + + $ob->contr->braced ($ob->contr->braced ? 0 : 1); + $ob->reply (undef, $ob->contr->braced ? "You are braced." : "Not braced."); +}; + +cf::register_command 'output-rate' => sub { + my ($ob, $arg) = @_; + + return $ob->reply (undef, sprintf "Output rate is presently %dbps.", $ob->contr->ns->max_rate / $cf::TICK) + unless $arg > 0; + + # minimum is 5k/s + # maximum is 100k/s, this should be configurable + $ob->contr->ns->max_rate ((List::Util::max 5000, List::Util::min 100000, $arg) * $cf::TICK); + $ob->reply (undef, sprintf "Output rate now set to %dbps.", $ob->contr->ns->max_rate / $cf::TICK); +}; + +cf::register_command 'output-count' => sub { + my ($ob, $arg) = @_; + + return $ob->reply (undef, "Output count is presently " . $ob->contr->outputs_count) + unless $arg > 0; + + $arg = 4 if $arg < 4; + + $ob->contr->outputs_count ($arg); + $ob->reply (undef, "Output count now set to " . $ob->contr->outputs_count); +}; + +cf::register_command 'output-sync' => sub { + my ($ob, $arg) = @_; + + return $ob->reply (undef, sprintf "Output sync time is presently %.1fs", $ob->contr->outputs_sync * $cf::TICK) + unless length $arg; + + $arg = 0.5 if $arg < 0.5; + + $ob->contr->outputs_sync ($arg / $cf::TICK); + $ob->reply (undef, sprintf "Output sync time now set to %.1fs", $ob->contr->outputs_sync * $cf::TICK); +}; + +# XXX: This has a bug. After one sets his wimpy level to 0 and resets it to +# some other level (which may also be 0), this does not get echoed, +# but it does get set. +cf::register_command wimpy => sub { + my ($ob, $arg) = @_; + + my $wimpy = $ob->run_away; + return $ob->reply (undef, "Your current wimpy level is $wimpy.") + if $arg eq ""; + + return $ob->run_away ($arg) && $ob->reply (undef, "Your new wimpy level is $arg.") + if $arg =~ /^\d+$/ and $arg <= 100; + + $ob->reply (undef, "Incorrect parameters for wimpy: $arg"); +}; + +cf::register_command peaceful => sub { + my ($ob, $arg) = @_; + + $ob->reply (undef, "You cannot change your peaceful setting with this command." + ." Please speak to the priest in the temple of Gorokh" + ." if you want to become hostile or in temple of Valriel" + ." if you want to become peaceful again."); + + #$ob->contr->peaceful ($ob->contr->peaceful ? 0 : 1); + #$ob->reply (undef, $ob->contr->peaceful ? "You will attack other players." : "You will not attack other players."); +}; + sub rename_to($$$) { my ($ob, $from, $to) = @_; - $to =~ /^[a-zA-Z0-9.,=#\/%$!^]*$/ + $to =~ /^[a-zA-Z0-9.,=#\/%$!^ ]*$/ or return $ob->message ("rename: name must consist only of letters, digits, spaces and a few other things."); 127 >= length $to @@ -19,7 +322,7 @@ $item = $ob->find_best_object_match ($from) or return $ob->message ("rename: could not find a matching item to rename."); } else { - $item = $ob->find_marked_object () + $item = $ob->find_marked_object or return $ob->message ("rename: no from name and no marked item found to rename."); } @@ -38,57 +341,6 @@ 1 } -sub ext::schmorp_irc::users; # HACK: TODO: replace by signal - -sub who_listing(;$) { - my ($privileged) = @_; - - my ($numwiz, $numafk) = (0, 0); - my @pl; - - foreach my $pl (cf::player::list) { - my $ns = $pl->ns or next; - my $ob = $pl->ob; - - next unless $ob->map - && ($privileged || !$pl->hidden); - - $numwiz++ if $ob->flag (cf::FLAG_WIZ); - $numafk++ if $ns->afk; - - push @pl, $pl; - } - - ( - "Total Players in The World. (" . (scalar @pl) . ") -- WIZ($numwiz) AFK($numafk) BOT(0)", - ( - map { - my ($pl, $ob, $ns) = ($_, $_->ob, $_->ns); - - "* " . $ob->name . "/" . $ob->level . " " . (length $pl->own_title ? $pl->own_title : "the " . $pl->title) - . ($pl->peaceful ? " [peaceful]" : " [HOSTILE]") - . ($ns->afk ? " [AFK]" : "") - . ($ob->flag (cf::FLAG_WIZ) ? " [WIZ]" : "") - . " [" . $pl->ns->version . "]" - . " [" . ($pl->peaceful || $privileged ? $ob->map->{path}->visible_name : $ob->map->region ? $ob->map->region->name : "the unknown") . "]" - . (sprintf " [rtt %.3fs]", $pl->ns->rtt * 1e-6) - . ($privileged ? " " . $pl->ns->host : "") - } sort { (lc $a->ob->name) cmp (lc $b->ob->name) } @pl - ), - eval { "* IRC: " . join ", ", ext::schmorp_irc::users }, - ) -} - -cf::register_command who => sub { - my ($ob, $arg) = @_; - - $ob->speed_left ($ob->speed_left - 0.25); - - $ob->reply (undef, (join "\n", who_listing $ob->may ("extended_who")), cf::NDI_UNIQUE | cf::NDI_DK_ORANGE); - - 1 -}; - cf::register_command rename => sub { my ($ob, $arg) = @_; @@ -111,8 +363,6 @@ } else { $ob->reply (undef, 'Syntax error. Rename usage: rename ["oldname"] to "newname"'); } - - 1 }; cf::register_command uptime => sub { @@ -121,34 +371,41 @@ my $startup = POSIX::strftime "%Y-%m-%d %H:%M:%S %Z", localtime $cf::UPTIME; my $runtime = sprintf "%.1f", (time - $cf::UPTIME) / 86400; $ob->reply (undef, "server started $startup, uptime ${runtime}\d"); - - 1 }; +my %IN_MEMORY = ( + cf::MAP_IN_MEMORY => "I", + cf::MAP_SWAPPED => "S", + cf::MAP_LOADING => "L", +); + cf::register_command maps => sub { my ($ob, $arg) = @_; no re 'eval'; $arg = qr<$arg>; - my $format = "%2s %2s %5s %-30.30s\n"; + my $format = "%2s %1s %3s %5s %.60s\n"; - my $msg = sprintf $format, "Pl", "Fl", "Reset", "Name"; + $ob->reply (undef, (sprintf $format, "Pl", "I", "Svd", "Reset", "Name"), cf::NDI_BLACK | cf::NDI_UNIQUE); for (sort keys %cf::MAP) { my $map = $cf::MAP{$_} or next; next unless $map->path =~ $arg; + next if $map->{deny_list}; - $msg .= sprintf $format, - (scalar $map->players), - 0, - (int $map->reset_at - $cf::RUNTIME), - $map->{path}->visible_name; - } + my $svd = int $cf::RUNTIME - $map->{last_save}; + $svd = "++" if $svd > 99; - $ob->reply (undef, $msg); - - 1 + $ob->reply (undef, + (sprintf $format, + (scalar $map->players), + $IN_MEMORY{$map->in_memory} || "?", + $svd, + (int $map->reset_at - $cf::RUNTIME), + $map->visible_name), + cf::NDI_BLACK | cf::NDI_UNIQUE); + } };