1 |
#! perl |
2 |
|
3 |
use POSIX (); |
4 |
|
5 |
# miscellaneous commands |
6 |
|
7 |
sub rename_to($$$) { |
8 |
my ($ob, $from, $to) = @_; |
9 |
|
10 |
$to =~ /^[a-zA-Z0-9.,=#\/%$!^ ]*$/ |
11 |
or return $ob->message ("rename: name must consist only of letters, digits, spaces and a few other things."); |
12 |
|
13 |
127 >= length $to |
14 |
or return $ob->message ("rename: new name must be <= 127 characters."); |
15 |
|
16 |
my $item; |
17 |
|
18 |
if (length $from) { |
19 |
$item = $ob->find_best_object_match ($from) |
20 |
or return $ob->message ("rename: could not find a matching item to rename."); |
21 |
} else { |
22 |
$item = $ob->find_marked_object () |
23 |
or return $ob->message ("rename: no from name and no marked item found to rename."); |
24 |
} |
25 |
|
26 |
$item->custom_name (length $to ? $to : undef); |
27 |
|
28 |
if (length $to) { |
29 |
$item->custom_name ($to); |
30 |
$ob->message ("Your " . $item->base_name . " will now be called $to."); |
31 |
} else { |
32 |
$item->custom_name (undef); |
33 |
$ob->message ("You stop calling your " . $item->base_name . " with weird names."); |
34 |
} |
35 |
|
36 |
$ob->esrv_update_item (cf::UPD_NAME, $item); |
37 |
|
38 |
1 |
39 |
} |
40 |
|
41 |
sub ext::schmorp_irc::users; # HACK: TODO: replace by signal |
42 |
|
43 |
sub who_listing(;$) { |
44 |
my ($privileged) = @_; |
45 |
|
46 |
my ($numwiz, $numafk) = (0, 0); |
47 |
my @pl; |
48 |
|
49 |
foreach my $pl (cf::player::list) { |
50 |
my $ns = $pl->ns or next; |
51 |
my $ob = $pl->ob; |
52 |
|
53 |
next unless $ob->map |
54 |
&& ($privileged || !$pl->hidden); |
55 |
|
56 |
$numwiz++ if $ob->flag (cf::FLAG_WIZ); |
57 |
$numafk++ if $ns->afk; |
58 |
|
59 |
push @pl, $pl; |
60 |
} |
61 |
|
62 |
( |
63 |
"Total Players in The World. (" . (scalar @pl) . ") -- WIZ($numwiz) AFK($numafk) BOT(0)", |
64 |
( |
65 |
map { |
66 |
my ($pl, $ob, $ns) = ($_, $_->ob, $_->ns); |
67 |
|
68 |
"* " . $ob->name . "/" . $ob->level . " " . (length $pl->own_title ? $pl->own_title : "the " . $pl->title) |
69 |
. ($pl->peaceful ? " [peaceful]" : " [HOSTILE]") |
70 |
. ($ns->afk ? " [AFK]" : "") |
71 |
. ($ob->flag (cf::FLAG_WIZ) ? " [WIZ]" : "") |
72 |
. " [" . $pl->ns->version . "]" |
73 |
. " [" . ($pl->peaceful || $privileged ? $ob->map->visible_name : $ob->region->name) . "]" |
74 |
. (sprintf " [rtt %.3fs]", $pl->ns->rtt * 1e-6) |
75 |
. ($privileged ? " " . $pl->ns->host : "") |
76 |
} sort { (lc $a->ob->name) cmp (lc $b->ob->name) } @pl |
77 |
), |
78 |
eval { "* IRC: " . join ", ", ext::schmorp_irc::users }, |
79 |
) |
80 |
} |
81 |
|
82 |
cf::register_command who => sub { |
83 |
my ($ob, $arg) = @_; |
84 |
|
85 |
$ob->speed_left ($ob->speed_left - 0.25); |
86 |
|
87 |
$ob->reply (undef, (join "\n", who_listing $ob->may ("extended_who")), cf::NDI_UNIQUE | cf::NDI_DK_ORANGE); |
88 |
|
89 |
1 |
90 |
}; |
91 |
|
92 |
cf::register_command usekeys => sub { |
93 |
my ($ob, $arg) = @_; |
94 |
my @types = ("inventory", "keyrings", "containers"); |
95 |
my $mapping = { |
96 |
inventory => 1, |
97 |
keyrings => 2, |
98 |
containers => 3, |
99 |
}; |
100 |
|
101 |
my $oldtype = $ob->contr->usekeys; |
102 |
my $oldtype_name = $types[$oldtype]; |
103 |
|
104 |
return $ob->reply (undef, "usekeys is set to $oldtype_name") |
105 |
unless $arg; |
106 |
|
107 |
return $ob->reply (undef, "usekeys: Unknown options '$arg', valid options are inventory, keyrings, containers") |
108 |
unless $mapping->{$arg}; |
109 |
|
110 |
$ob->contr->usekeys ($mapping->{$arg} - 1); # HACK: because of the $mapping->{$arg} check before, where $arg should not be 0 |
111 |
# but $arg would be 0 if a user enters an incorrect value |
112 |
$ob->reply (undef, "usekeys" . ($oldtype == $ob->contr->usekeys ? "" : " now") . " set to " . $types[$ob->contr->usekeys]); |
113 |
|
114 |
1 |
115 |
}; |
116 |
|
117 |
cf::register_command afk => sub { |
118 |
my ($ob, $arg) = @_; |
119 |
|
120 |
$ob->contr->ns->afk ($ob->contr->ns->afk ? 0 : 1); |
121 |
$ob->reply (undef, $ob->contr->ns->afk ? "You are now AFK" : "You are no longer AFK"); |
122 |
|
123 |
1 |
124 |
}; |
125 |
|
126 |
cf::register_command sound => sub { |
127 |
my ($ob, $arg) = @_; |
128 |
|
129 |
$ob->contr->ns->sound ($ob->contr->ns->sound ? 0 : 1); |
130 |
$ob->reply (undef, $ob->contr->ns->sound ? "The sounds are enabled." : "Silence is golden..."); |
131 |
|
132 |
1 |
133 |
}; |
134 |
|
135 |
cf::register_command brace => sub { |
136 |
my ($ob, $arg) = @_; |
137 |
|
138 |
$ob->contr->braced ($ob->contr->braced ? 0 : 1); |
139 |
$ob->reply (undef, $ob->contr->braced ? "You are braced." : "Not braced."); |
140 |
|
141 |
1 |
142 |
}; |
143 |
|
144 |
# XXX: This has a bug. After one sets his wimpy level to 0 and resets it to |
145 |
# some other level (which may also be 0), this does not get echoed, |
146 |
# but it does get set. |
147 |
cf::register_command wimpy => sub { |
148 |
my ($ob, $arg) = @_; |
149 |
|
150 |
my $wimpy = $ob->run_away; |
151 |
return $ob->reply (undef, "Your current wimpy level is $wimpy.") |
152 |
if $arg eq ""; |
153 |
|
154 |
return $ob->run_away ($arg) && $ob->reply (undef, "Your new wimpy level is $arg.") |
155 |
if $arg =~ /^\d+$/ and $arg <= 100; |
156 |
|
157 |
$ob->reply (undef, "Incorrect parameters for wimpy: $arg"); |
158 |
|
159 |
1 |
160 |
}; |
161 |
|
162 |
cf::register_command peaceful => sub { |
163 |
my ($ob, $arg) = @_; |
164 |
|
165 |
$ob->reply (undef, "You cannot change your peaceful setting with this command." |
166 |
." Please speak to the priest in the temple of Gorokh" |
167 |
." if you want to become hostile or in temple of Valriel" |
168 |
." if you want to become peaceful again."); |
169 |
|
170 |
#$ob->contr->peaceful ($ob->contr->peaceful ? 0 : 1); |
171 |
#$ob->reply (undef, $ob->contr->peaceful ? "You will attack other players." : "You will not attack other players."); |
172 |
|
173 |
1 |
174 |
}; |
175 |
|
176 |
|
177 |
cf::register_command rename => sub { |
178 |
my ($ob, $arg) = @_; |
179 |
|
180 |
$ob->speed_left ($ob->speed_left - 0.25); |
181 |
|
182 |
if ($arg =~ /^\s* (?: <([^>]+)> \s+)? to \s+ <([^>]*)> \s*$/x) { |
183 |
# compatibility syntax |
184 |
rename_to $ob, $1, $2; |
185 |
} elsif ($arg =~ / |
186 |
^\s* |
187 |
(?: |
188 |
(?: "((?:[^"]+|\\.)*)" | (\S+) ) |
189 |
\s+)? |
190 |
to \s+ |
191 |
(?: "((?:[^"]+|\\.)*)" | (\S+) ) |
192 |
\s*$ |
193 |
/x) { |
194 |
# does not unquote $1 or $3 |
195 |
rename_to $ob, $2||$1, $4||$3; |
196 |
} else { |
197 |
$ob->reply (undef, 'Syntax error. Rename usage: rename ["oldname"] to "newname"'); |
198 |
} |
199 |
|
200 |
1 |
201 |
}; |
202 |
|
203 |
cf::register_command uptime => sub { |
204 |
my ($ob, $arg) = @_; |
205 |
|
206 |
my $startup = POSIX::strftime "%Y-%m-%d %H:%M:%S %Z", localtime $cf::UPTIME; |
207 |
my $runtime = sprintf "%.1f", (time - $cf::UPTIME) / 86400; |
208 |
$ob->reply (undef, "server started $startup, uptime ${runtime}\d"); |
209 |
|
210 |
1 |
211 |
}; |
212 |
|
213 |
my %IN_MEMORY = ( |
214 |
cf::MAP_IN_MEMORY => "I", |
215 |
cf::MAP_SWAPPED => "S", |
216 |
cf::MAP_LOADING => "L", |
217 |
); |
218 |
|
219 |
cf::register_command maps => sub { |
220 |
my ($ob, $arg) = @_; |
221 |
|
222 |
no re 'eval'; $arg = qr<$arg>; |
223 |
|
224 |
my $format = "%2s %1s %3s %5s %.60s\n"; |
225 |
|
226 |
$ob->reply (undef, (sprintf $format, "Pl", "I", "Svd", "Reset", "Name"), cf::NDI_BLACK | cf::NDI_UNIQUE); |
227 |
|
228 |
for (sort keys %cf::MAP) { |
229 |
my $map = $cf::MAP{$_} |
230 |
or next; |
231 |
|
232 |
next unless $map->path =~ $arg; |
233 |
next if $map->{deny_list}; |
234 |
|
235 |
my $svd = int $cf::RUNTIME - $map->{last_save}; |
236 |
$svd = "++" if $svd > 99; |
237 |
|
238 |
$ob->reply (undef, |
239 |
(sprintf $format, |
240 |
(scalar $map->players), |
241 |
$IN_MEMORY{$map->in_memory} || "?", |
242 |
$svd, |
243 |
(int $map->reset_at - $cf::RUNTIME), |
244 |
$map->visible_name), |
245 |
cf::NDI_BLACK | cf::NDI_UNIQUE); |
246 |
} |
247 |
|
248 |
1 |
249 |
}; |
250 |
|