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.5 by root, Sun May 7 08:10:23 2006 UTC vs.
Revision 1.20 by root, Wed Jul 19 08:52:57 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;
7
8sub valid_user($) {
9 cf::player::find $_[0]
10 or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2;
11}
12
13sub clean_timeouts($) {
14 my ($player) = @_;
15 my $NOW = time;
16
17 for my $hash (@$player{qw(ext_ignore_shout ext_ignore_tell)}) {
18 while (my ($k, $v) = each %$hash) {
19 if ($v < $NOW) {
20 $player->message ("Your ignore on $k has expired.", cf::NDI_GREEN | cf::NDI_UNIQUE);
21 delete $hash->{$k};
22 } elsif (!valid_user $k) {
23 $player->message ("Your ignore on $k is no longer valid (no such user).", cf::NDI_GREEN | cf::NDI_UNIQUE);
24 delete $hash->{$k};
25 }
26 }
27 }
28}
29
30sub on_logout {
31 my ($pl, $host) = @_;
32
33 clean_timeouts $pl->ob;
34}
35
36cf::register_command listen => 0, sub {
37 my ($who, $msg) = @_;
38 my $player = cf::player::find $who->name;
39
40 if ($msg ne "") {
41 my $prev_listen = $player->listening;
42 $player->listening ($msg);
43 if ($prev_listen == $player->listening) {
44 $who->message ("Your verbose level stayed $prev_listen.", cf::NDI_UNIQUE);
45 } else {
46 $who->message ("Your verbose level is now " . $player->listening . ". (previously: $prev_listen)", cf::NDI_UNIQUE);
47 }
48 } else {
49 $who->message ("Your verbose level is " . $player->listening . ".", cf::NDI_UNIQUE);
50 }
51};
52
6cf::register_command chat => 0, sub 53cf::register_command xsay => 0, sub {
7{
8 my ($who, $msg) = @_; 54 my ($who, $msg) = @_;
9 55
10 if ($msg) { 56 if ($msg) {
11 my $name = $who->name; 57 my $name = $who->name;
12 58
13 $_->ob->message ("$name chats: $msg", cf::NDI_BLUE) 59 my @messages = (["$name says: $msg", cf::NDI_GREY | cf::NDI_UNIQUE]);
14 for grep !$player->ob->{ext_ignore_shout}, cf::player::list;
15 60
61 # npcs, magic_ears etc.
62 # first find all objects and their inventories within a 5x5 square
63 # that have something resembling dialogue
64 my ($map, $x, $y) = ($who->map, $who->x - 2, $who->y - 2);
65
66 for my $npc (
67 grep NPC_Dialogue::has_dialogue $_,
68 map +($_, $_->inv),
69 grep $_,
70 map $map->at ($x + $_ % 5, $y + (int $_ / 5)),
71 0..24
72 ) {
73 # if some listener teleported us somewhere else, stop right here
74 last unless $map->path == $who->map->path;
75
76 my $dialog = new NPC_Dialogue ob => $who, npc => $npc;
77 my $reply = $dialog->tell ($msg);
78
79 if (defined $reply) {
80 if ($npc->type == cf::MAGIC_EAR) {
81 push @messages, [$reply, cf::NDI_BROWN | cf::NDI_UNIQUE] if length $reply;
82 $npc->use_trigger;
83 } else {
84 push @messages, [$npc->name . " says: $reply", cf::NDI_BROWN | cf::NDI_UNIQUE] if length $reply;
85 }
86 }
87 }
88
89 # send replies to all players on this map
90 for (@messages) {
91 my ($msg, $flags) = @$_;
92
93 $_->ob->message ($msg, $flags)
94 for grep $who->on_same_map_as ($_->ob), cf::player::list;
95 }
96
16 } else { 97 } else {
17 $who->message ("Chat what?", cf::NDI_UNIQUE); 98 $who->message ("What do you want to say?", cf::NDI_UNIQUE);
18 } 99 }
19}; 100};
20 101
21cf::register_command shout => 0, sub 102cf::register_command chat => 0, sub {
22{
23 my ($who, $msg) = @_; 103 my ($who, $msg) = @_;
24 104
25 if ($msg) { 105 if ($msg) {
26 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;
27 127
28 $_->ob->message ("$name shouts: $msg", cf::NDI_RED) 128 $_->ob->message ("$name shouts: $msg", cf::NDI_RED)
29 for grep !$player->ob->{ext_ignore_shout}, cf::player::list; 129 for grep { $_->ob->{ext_ignore_shout}{$name} < $NOW && $_->listening >= 2 } cf::player::list;
30 130
31 } else { 131 } else {
32 $who->message ("Shout what?", cf::NDI_UNIQUE); 132 $who->message ("Shout what?", cf::NDI_UNIQUE);
33 } 133 }
34 134
40 140
41 my $name = $who->name; 141 my $name = $who->name;
42 142
43 if (my $other = cf::player::find $target) { 143 if (my $other = cf::player::find $target) {
44 144
45 $other->ob->{ext_ignore_tell}{$name} >= time 145 if ($msg) {
46 or delete $other->ob->{ext_ignore_tell}{$name};
47
48 if ($target ne $name and !$other->ob->{ext_ignore_tell}{$name}) {
49 $who->message ("You tell $target: $msg");
50 $other->ob->message ("$name tells you: $msg");
51 $other->ob->{ext_last_tell} = $name;
52 } elsif ($target eq $name) { 146 if ($target eq $name) {
53 $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);
54 } else { 150 } else {
55 $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;
56 } 156 }
57
58 } else { 157 } else {
158 $who->message ("What do you want to tell $target?", cf::NDI_UNIQUE);
159 }
160
161 } else {
59 $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);
60 } 163 }
61}; 164};
62 165
63cf::register_command reply => 0, sub { 166cf::register_command reply => 0, sub {
64 my ($who, $args) = @_; 167 my ($who, $args) = @_;
65 my $name = $who->name; 168 my $name = $who->name;
66 169
67 if (my $other = cf::player::find $who->{ext_last_tell}) { 170 if (my $other = cf::player::find $who->{ext_last_tell}) {
171 if ($args) {
68 172
69 $other->ob->{ext_ignore_tell}{$name} >= time 173 $other->ob->{ext_ignore_tell}{$name} >= time
70 or delete $other->ob->{ext_ignore_tell}{$name}; 174 or delete $other->ob->{ext_ignore_tell}{$name};
71 175
72 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
73 $who->message ("You tell " . $other->ob->name . ": $args"); 179 $who->message ("You tell " . $other->ob->name . ": $args");
74 $other->ob->message ("$name tells you: $args"); 180 $other->ob->message ("$name tells you: $args");
75 $who->{ext_last_tell} = $other->ob->name; 181 $who->{ext_last_tell} = $other->ob->name;
76 } else { 182 } else {
77 $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);
78 } 187 }
79 188
80 } else { 189 } else {
81 $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);
82 } 191 }
85cf::register_command ignore => 0, sub { 194cf::register_command ignore => 0, sub {
86 my ($who, $args) = @_; 195 my ($who, $args) = @_;
87 my ($target, $type, $timeout) = split /\s+/, $args; 196 my ($target, $type, $timeout) = split /\s+/, $args;
88 197
89 if ($args eq "list") { 198 if ($args eq "list") {
199 clean_timeouts $who;
200
90 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}}) 201 if ((my @ignored_tell = sort keys %{$who->{ext_ignore_tell}})
91 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) { 202 + (my @ignored_shout = sort keys %{$who->{ext_ignore_shout}})) {
92 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 203 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
93 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE); 204 $who->message ((join ", ", @ignored_tell), cf::NDI_UNIQUE);
94 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 205 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
99 } 210 }
100 211
101 } elsif ($target && $type) { 212 } elsif ($target && $type) {
102 213
103 $timeout ne "" or $timeout = 24; 214 $timeout ne "" or $timeout = 24;
104
105 my $absolute_timeout = time + $timeout * 3600; 215 my $absolute_timeout = time + $timeout * 3600;
106 216
107 if (my $other = cf::player::find $target) { 217 if (valid_user $target) {
108 if ($type eq "tell") { 218 if ($type eq "tell") {
109 $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);
110 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 220 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
111 } elsif ($type eq "shout") { 221 } elsif ($type eq "shout") {
112 $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);
113 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 223 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
114 } elsif ($type eq "all") { 224 } elsif ($type eq "all") {
115 $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);
116 $who->{ext_ignore_tell}{$other->ob->name} = $absolute_timeout; 226 $who->{ext_ignore_tell}{$target} = $absolute_timeout;
117 $who->{ext_ignore_shout}{$other->ob->name} = $absolute_timeout; 227 $who->{ext_ignore_shout}{$target} = $absolute_timeout;
118 } else { 228 } else {
119 $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);
120 } 230 }
121 } else { 231 } else {
122 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 232 $who->message ("No such player: $target", cf::NDI_UNIQUE);
123 } 233 }
124 234
125 } else { 235 } else {
126 $who->message ("Usage:", cf::NDI_UNIQUE);
127 $who->message ("ignore <player> <tell | shout | all> <timeout>", cf::NDI_UNIQUE); 236 $who->message ("Usage: ignore <player> <tell | shout | all> <timeout>\n"
128 $who->message ("will ignore a player for <timeout> hours.", cf::NDI_UNIQUE); 237 . "will ignore a player for <timeout> hours.\n"
129 $who->message ("ignore list", cf::NDI_UNIQUE); 238 . "Usage: ignore list\n"
130 $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);
131 } 240 }
132}; 241};
133 242
134cf::register_command unignore => 0, sub { 243cf::register_command unignore => 0, sub {
135 my ($who, $args) = @_; 244 my ($who, $args) = @_;
136 my ($target, $type) = split /\s+/, $args; 245 my ($target, $type) = split /\s+/, $args;
137 246
138 if ($args eq "") { 247 if ($args eq "") {
139 if ($who->{ ext_ignore_tell }) { 248 if ($who->{ext_ignore_tell}) {
140 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE); 249 $who->message ("Currently ignoring private messages from: ", cf::NDI_UNIQUE);
141 $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);
142 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE); 251 $who->message ("Currently ignoring shouts from: ", cf::NDI_UNIQUE);
143 $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);
144 } else { 253 } else {
145 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE); 254 $who->message ("Not ignoring anyone", cf::NDI_UNIQUE);
146 } 255 }
147 } else { 256 } else {
148 257
149 if (my $other = cf::player::find $target) { 258 if (valid_user $target) {
150 if ($type eq "tell") { 259 if ($type eq "tell") {
151 $who->message ("Not ignoring private messages from " . $other->ob->name . " anymore.", cf::NDI_UNIQUE); 260 $who->message ("Not ignoring private messages from $target anymore.", cf::NDI_UNIQUE);
152 delete $who->{ext_ignore_tell}{$other->ob->name}; 261 delete $who->{ext_ignore_tell} {$target};
153 } elsif ($type eq "shout") { 262 } elsif ($type eq "shout") {
154 $who->message ("Not ignoring shouts from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 263 $who->message ("Not ignoring shouts from $target anymore.", cf::NDI_UNIQUE);
155 delete $who->{ext_ignore_shout}{$other->ob->name}; 264 delete $who->{ext_ignore_shout}{$target};
156 } elsif ($type eq "all") { 265 } elsif ($type eq "all") {
157 $who->message ("Not ignoring anything from " . $other->ob->name." anymore . ", cf::NDI_UNIQUE); 266 $who->message ("Not ignoring anything from $target anymore.", cf::NDI_UNIQUE);
158 delete $who->{ext_ignore_tell} {$other->ob->name}; 267 delete $who->{ext_ignore_tell} {$target};
159 delete $who->{ext_ignore_shout}{$other->ob->name}; 268 delete $who->{ext_ignore_shout}{$target};
160 } else { 269 } else {
161 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE); 270 $who->message ("You need to specify tell, shout or all.", cf::NDI_UNIQUE);
162 } 271 }
163 } else { 272 } else {
164 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE); 273 $who->message ("No such player or ambiguous name: $target", cf::NDI_UNIQUE);

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines