ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/chat.ext
(Generate patch)

Comparing deliantra/maps/perl/chat.ext (file contents):
Revision 1.7 by root, Sun May 7 08:26:47 2006 UTC vs.
Revision 1.26 by root, Fri Jul 21 08:33:52 2006 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines