ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/commands.ext
Revision: 1.19
Committed: Thu Mar 1 13:27:52 2007 UTC (17 years, 2 months ago) by pippijn
Branch: MAIN
Changes since 1.18: +7 -0 lines
Log Message:
afk in perl

File Contents

# Content
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 afk => sub {
93 my ($ob, $arg) = @_;
94
95 $ob->contr->ns->afk ($ob->contr->ns->afk ? 0 : 1);
96 $ob->reply (undef, $ob->contr->ns->afk ? "You are now AFK" : "You are no longer AFK");
97 };
98
99 cf::register_command rename => sub {
100 my ($ob, $arg) = @_;
101
102 $ob->speed_left ($ob->speed_left - 0.25);
103
104 if ($arg =~ /^\s* (?: <([^>]+)> \s+)? to \s+ <([^>]*)> \s*$/x) {
105 # compatibility syntax
106 rename_to $ob, $1, $2;
107 } elsif ($arg =~ /
108 ^\s*
109 (?:
110 (?: "((?:[^"]+|\\.)*)" | (\S+) )
111 \s+)?
112 to \s+
113 (?: "((?:[^"]+|\\.)*)" | (\S+) )
114 \s*$
115 /x) {
116 # does not unquote $1 or $3
117 rename_to $ob, $2||$1, $4||$3;
118 } else {
119 $ob->reply (undef, 'Syntax error. Rename usage: rename ["oldname"] to "newname"');
120 }
121
122 1
123 };
124
125 cf::register_command uptime => sub {
126 my ($ob, $arg) = @_;
127
128 my $startup = POSIX::strftime "%Y-%m-%d %H:%M:%S %Z", localtime $cf::UPTIME;
129 my $runtime = sprintf "%.1f", (time - $cf::UPTIME) / 86400;
130 $ob->reply (undef, "server started $startup, uptime ${runtime}\d");
131
132 1
133 };
134
135 my %IN_MEMORY = (
136 cf::MAP_IN_MEMORY => "I",
137 cf::MAP_SWAPPED => "S",
138 cf::MAP_LOADING => "L",
139 );
140
141 cf::register_command maps => sub {
142 my ($ob, $arg) = @_;
143
144 no re 'eval'; $arg = qr<$arg>;
145
146 my $format = "%2s %1s %3s %5s %.60s\n";
147
148 $ob->reply (undef, (sprintf $format, "Pl", "I", "Svd", "Reset", "Name"), cf::NDI_BLACK | cf::NDI_UNIQUE);
149
150 for (sort keys %cf::MAP) {
151 my $map = $cf::MAP{$_}
152 or next;
153
154 next unless $map->path =~ $arg;
155 next if $map->{deny_list};
156
157 my $svd = int $cf::RUNTIME - $map->{last_save};
158 $svd = "++" if $svd > 99;
159
160 $ob->reply (undef,
161 (sprintf $format,
162 (scalar $map->players),
163 $IN_MEMORY{$map->in_memory} || "?",
164 $svd,
165 (int $map->reset_at - $cf::RUNTIME),
166 $map->visible_name),
167 cf::NDI_BLACK | cf::NDI_UNIQUE);
168 }
169
170 1
171 };
172