ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/chat.ext
Revision: 1.24
Committed: Fri Jul 21 08:24:47 2006 UTC (17 years, 10 months ago) by root
Branch: MAIN
Changes since 1.23: +3 -1 lines
Log Message:
forgot an important case :)

File Contents

# Content
1 #! perl
2
3 # implement a replacement for the built-in say/chat/shout/tell/reply commands
4 # adds ignore/unignore functionality
5
6 use NPC_Dialogue;
7 use POSIX (); # for strftime only
8
9 sub clean_timeouts($) {
10 my ($player) = @_;
11 my $NOW = time;
12
13 for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) {
14 while (my ($k, $v) = each %$hash) {
15 if ($v < $NOW) {
16 $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE);
17 delete $hash->{$k};
18 } elsif (!cf::player::exists $k) {
19 $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE);
20 delete $hash->{$k};
21 }
22 }
23 }
24 }
25
26 sub on_logout {
27 my ($pl, $host) = @_;
28
29 clean_timeouts $pl->ob;
30 }
31
32 cf::register_command listen => 0, sub {
33 my ($who, $msg) = @_;
34 my $player = cf::player::find $who->name;
35
36 if ($msg ne "") {
37 my $prev_listen = $player->listening;
38 $player->listening ($msg);
39 if ($prev_listen == $player->listening) {
40 $who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE);
41 } else {
42 $who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE);
43 }
44 } else {
45 $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE);
46 }
47 };
48
49 cf::register_command say => 0, sub {
50 my ($who, $msg) = @_;
51
52 if ($msg) {
53 my $name = $who->name;
54
55 $_->ob->message ("$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE)
56 for grep $who->on_same_map_as ($_->ob), cf::player::list;
57
58 # npcs, magic_ears etc.
59 # first find all objects and their inventories within a 5x5 square
60 # that have something resembling dialogue
61 my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2);
62
63 for my $npc (
64 grep NPC_Dialogue::has_dialogue $_,
65 map +($_, $_->inv),
66 grep $_,
67 map $map->at ($x + $_ % 5, $y + (int $_ / 5)),
68 0..24
69 ) {
70 # if some listener teleported us somewhere else, stop right here
71 last unless $map->path == $who->map->path;
72
73 my $dialog = new NPC_Dialogue ob => $who, npc => $npc;
74 my $reply = $dialog->tell ($msg);
75
76 if (defined $reply) {
77 if ($npc->type == cf::MAGIC_EAR) {
78 if (length $reply) {
79 $_->ob->message ($reply, cf::NDI_BROWN | cf::NDI_UNIQUE)
80 for grep $who->on_same_map_as ($_->ob), cf::player::list;
81 }
82 $npc->use_trigger;
83 } else {
84 if (length $reply) {
85 $_->ob->message ($npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE)
86 for grep $who->on_same_map_as ($_->ob), cf::player::list;
87 }
88 }
89 }
90 }
91
92 } else {
93 $who->message ("What do you want to say?", cf::NDI_UNIQUE);
94 }
95 };
96
97 cf::register_command chat => 0, sub {
98 my ($who, $msg) = @_;
99
100 if ($msg) {
101 my $name = $who->name;
102 my $NOW = time;
103
104 cf::LOG cf::llevDebug, sprintf "QBERT [%s] %s\n", $name, $msg;
105
106 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE)
107 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 10 } cf::player::list;
108
109 } else {
110 $who->message ("Chat what?", cf::NDI_UNIQUE);
111 }
112 };
113
114 cf::register_command shout => 0, sub {
115 my ($who, $msg) = @_;
116
117 if ($msg) {
118 my $NOW = time;
119 my $name = $who->name;
120
121 cf::LOG cf::llevDebug, sprintf "QBERT {%s} %s\n", $name, $msg;
122
123 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
124 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
125
126 } else {
127 $who->message ("Shout what?", cf::NDI_UNIQUE);
128 }
129
130 };
131
132 cf::register_command tell => 0, sub {
133 my ($who, $args) = @_;
134 my ($target, $msg) = split /\s+/, $args, 2;
135
136 my $name = $who->name;
137
138 if (my $other = cf::player::find $target) {
139
140 if ($msg) {
141 if ($target eq $name) {
142 $who->message ("You are talking to yourself, you freak!", cf::NDI_UNIQUE);
143 } elsif ($other->ob->{ext_ignore_tell}{$name} >= time) {
144 $who->message ("$target ignores what you say. Give up on it.", cf::NDI_UNIQUE);
145 } else {
146 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $target, $msg;
147
148 $who->message ("You tell $target: $msg");
149 $other->ob->message ("$name tells you: $msg");
150 $other->ob->{ext_last_tell} = $name;
151 }
152 } else {
153 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
154 }
155
156 } else {
157 $who->message ("No such player. Your message: $msg", cf::NDI_UNIQUE);
158 }
159 };
160
161 cf::register_command reply => 0, sub {
162 my ($who, $args) = @_;
163 my $name = $who->name;
164
165 if (my $other = cf::player::find $who->{ext_last_tell}) {
166 if ($args) {
167
168 $other->ob->{ext_ignore_tell}{$name} >= time
169 or delete $other->ob->{ext_ignore_tell}{$name};
170
171 if ($other->ob->{ext_ignore_tell}{$name} < time) {
172 cf::LOG cf::llevDebug, sprintf "TELL [%s>%s] %s\n", $name, $other->ob->name, $args;
173
174 $who->message ("You tell " . $other->ob->name . ": $args");
175 $other->ob->message ("$name tells you: $args");
176 $who->{ext_last_tell} = $other->ob->name;
177 } else {
178 $who->message ($other->ob->name . " ignores what you say. Give up on it.", cf::NDI_UNIQUE);
179 }
180 } else {
181 $who->message ("What do you want to tell ".$other->ob->name."?", cf::NDI_UNIQUE);
182 }
183
184 } else {
185 $who->message ("Can't reply, player left. Your message: $args", cf::NDI_UNIQUE);
186 }
187 };
188
189 cf::register_command ignore => 0, sub {
190 my ($who, $args) = @_;
191 my ($target, $type, $timeout) = split /\s+/, $args;
192
193 if ($args eq "list") {
194 clean_timeouts $who;
195
196 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
197 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
198 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
199 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
200 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
201 $who->message ((join ", ", @ignored_shout), cf::NDI_UNIQUE);
202 $who->message ("To stop ignoring one, use unignore.", cf::NDI_UNIQUE);
203 } else {
204 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
205 }
206
207 } elsif ($target && $type) {
208
209 $timeout ne "" or $timeout = 24;
210 my $absolute_timeout = time + $timeout * 3600;
211
212 if (cf::player::exists $target) {
213 if ($type eq "tell") {
214 $who->message ("Now ignoring private messages from $target for $timeout hours.", cf::NDI_UNIQUE);
215 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
216 } elsif ($type eq "shout") {
217 $who->message ("Now ignoring shouts from $target for $timeout hours.", cf::NDI_UNIQUE);
218 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
219 } elsif ($type eq "all") {
220 $who->message ("Now ignoring everything from $target for $timeout hours.", cf::NDI_UNIQUE);
221 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
222 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
223 } else {
224 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
225 }
226 } else {
227 $who->message ("No such player: $target", cf::NDI_UNIQUE);
228 }
229
230 } else {
231 $who->message ("Usage: ignore <player> <tell|shout|all> <timeout>\n"
232 . "will ignore a player for <timeout> hours.\n"
233 . "Usage: ignore list\n"
234 . "will show you a list of players currently ignored.", cf::NDI_UNIQUE);
235 }
236 };
237
238 cf::register_command unignore => 0, sub {
239 my ($who, $args) = @_;
240 my ($target, $type) = split /\s+/, $args;
241
242 if ($args eq "") {
243 if ($who->{ext_ignore_tell}) {
244 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
245 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_tell} }), cf::NDI_UNIQUE);
246 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
247 $who->message ((join ", ", sort keys %{ $who->{ext_ignore_shout} }), cf::NDI_UNIQUE);
248 } else {
249 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
250 }
251 } else {
252 if (cf::player::exists $target) {
253 if ($type eq "tell") {
254 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
255 delete $who->{ext_ignore_tell} {$target};
256 } elsif ($type eq "shout") {
257 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
258 delete $who->{ext_ignore_shout}{$target};
259 } elsif ($type eq "all") {
260 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
261 delete $who->{ext_ignore_tell} {$target};
262 delete $who->{ext_ignore_shout}{$target};
263 } else {
264 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
265 }
266 } else {
267 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);
268 }
269 }
270 };
271
272 cf::register_command seen => 0, sub {
273 my ($who, $args) = @_;
274
275 if (my ($login) = $args =~ /(\S+)/) {
276 if ($login eq $who->name) {
277 $who->message ("Very funny, $login. Ha. Ha.", cf::NDI_UNIQUE);
278 } elsif (cf::player::find $login) {
279 $who->message ("$login is right here on this server!", cf::NDI_UNIQUE);
280 } elsif (cf::player::exists $login
281 and stat sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($login) x 2) {
282 my $time = (stat _)[9];
283
284 $who->message ("$login was last seen here on "
285 . (POSIX::strftime "%Y-%m-%d %H:%M:%S +0000", gmtime $time)
286 . " which was " . (int +(time - $time) / 3600) . " hours ago.", cf::NDI_UNIQUE);
287 } else {
288 $who->message ("No player named $login is known to me.", cf::NDI_UNIQUE);
289 }
290 } else {
291 $who->message ("Usage: seen <player>", cf::NDI_UNIQUE);
292 }
293 };
294