ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/ipo.ext
Revision: 1.2
Committed: Mon Jul 24 18:10:12 2006 UTC (17 years, 9 months ago) by elmex
Branch: MAIN
Changes since 1.1: +192 -10 lines
Log Message:
implemented mail transmission

File Contents

# Content
1 #! perl
2
3 my $price_fact = 50;
4
5 sub set_package {
6 my ($pkg, $from, $to, $bagname, $weight) = @_;
7 $pkg->set_name ("$bagname T: $to F: $from");
8 $pkg->set_weight_limit ($weight);
9 $pkg->set_str (0);
10 }
11
12 # prices in plat.
13 my %prices = (
14 pen => [
15 40, 'stylus',
16 sub { $_[0]->set_name ('IPO Writing Pen'); $_[0]->set_value (40 * $price_fact); }
17 ],
18 literacy => [
19 1000, 'scroll_literacy',
20 sub { $_[0]->set_value (1000 * $price_fact) }
21 ],
22 mailscroll => [
23 1, 'scroll',
24 sub {
25 $_[0]->set_name ("mailscroll T: $_[2] F: $_[1]");
26 $_[0]->set_name_plural ("mailscrolls T: $_[2] F: $_[1]");
27 $_[0]->set_value (1 * $price_fact);
28 },
29 'plarg'
30 ],
31 bag => [ 1, 'r_sack', sub { set_package (@_, bag => 5000) }, 'plarg' ],
32 package => [ 5, 'r_sack', sub { set_package (@_, package => 50000) }, 'plarg' ],
33 carton => [10, 'r_sack', sub { set_package (@_, carton => 100000) }, 'plarg' ],
34 mailwarning => [
35 0, 'diploma',
36 sub {
37 $_[0]->set_name ("mailwarning T: $_[2] F: $_[1]");
38 $_[0]->set_name_plural ("mailwarnings T: $_[2] F: $_[1]");
39 $_[0]->set_value (0);
40 },
41 'plarg'
42 ],
43 );
44
45 my %mailtypes = (
46 1 => ['scroll', 'mailscroll'],
47 2 => ['note', 'newspaper'],
48 3 => ['diploma', 'mailwarning'],
49 );
50
51 sub create_object {
52 my ($name, $map, $x, $y, $cb, @a) = @_;
53 my $o = cf::object::new $name;
54 my $r = $cb->($o, @a);
55 $map->insert_object ($o, $x, $y);
56 $r
57 }
58
59 # this handler handles to notice the player that he has got mail
60 sub on_login {
61 my ($pl, $host) = @_;
62
63 my $mails = CFMail::get_mail ($pl->ob->name);
64
65 my $cnt = @{$mails || []};
66
67 if ($cnt == 1) {
68 $pl->ob->message ("You got one mail.");
69 } elsif ($cnt > 1) {
70 $pl->ob->message ("You got $cnt mails.");
71 } else {
72 $pl->ob->message ("You haven't got any mail.");
73 }
74
75 0
76 }
77
78 # this event handler handles receiving of mails
79 sub on_apply {
80 my ($ev, $box, $pl) = @_;
81
82 my $cnt;
83 my $mails = CFMail::get_mail ($pl->name) || [];
84
85 # count the mails that are in the container
86 # FIXME: the problem with on_apply is that it is called even when
87 # the player closes the container. so we get a 'You have X mails.' message
88 # twice. - This bug existed also with the old python plugin
89
90 my $plname = $pl->name;
91 for ($box->inv) {
92 $_->name =~ /\S+ F: \S+ T: \Q$plname\E/
93 and $cnt++;
94 }
95
96 for (@$mails) {
97 my ($type, $from, $msg) = @$_;
98 $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll'];
99 my $mail = cf::object::new $type->[0];
100 $mail->set_name ("$type->[1] F: $from T: " .$pl->name);
101 $mail->set_name_plural ("$type->[1]s F: $from T: " .$pl->name);
102 $mail->set_message ($msg);
103 $mail->set_value (0);
104 $mail->insert_in_ob ($box);
105 }
106
107 $cnt += @$mails;
108
109 if ($cnt == 1) {
110 $pl->message ("You got one mail.");
111 } elsif ($cnt > 1) {
112 $pl->message ("You got $cnt mails.");
113 } else {
114 $pl->message ("You haven't got any mail.");
115 }
116
117 CFMail::clear_mail ($pl->name);
118
119 0;
120 }
121
122 # this event handler handles the sending of mails
123 sub on_close {
124 my ($ev, $box, $pl) = @_;
125
126 my @mails;
127
128 my %sent_targets;
129
130 for ($box->inv) {
131 if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) {
132 CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message);
133 $pl->message ("Sent mail$1 to $2 (from $3).");
134 $sent_targets{$2}++;
135 push @mails, $_;
136
137 } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) {
138 # this is for mails that remain in the queue for the player
139 CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message);
140 push @mails, $_;
141 }
142 }
143
144 $_->remove for @mails;
145
146 # lets message player ingame: this is a NEW feature from the perl IPO :-)
147 for (keys %sent_targets) {
148 if (my $player = cf::player::find $_) {
149 my $cnt = $sent_targets{$_};
150
151 if ($cnt == 1) {
152 $player->ob->message ("You've got new mail.");
153 } else {
154 $player->ob->message ("You've got $cnt new mails.");
155 }
156 }
157 }
158
159 0;
160 }
161
162 # this is the main command interface for the IPO NPC
163 cf::register_script_function "ipo::command" => sub {
164 my ($who, $msg, $npc) = @_;
165 my ($cmd, $arguments) = split /\s+/, $msg, 2;
166 $cmd = lc $cmd;
167
168 my $pl = cf::player::find $who->name;
169 my ($x, $y) = ($pl->ob->x, $pl->ob->y);
170
171 if (my $pr = $prices{$cmd}) {
172 if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) {
173 return 1;
174 }
175
176 $who->pay_amount ($pr->[0] * $price_fact);
177 if ($pr->[3] && not cf::player::exists $arguments) {
178 $who->reply ($npc, "Sorry, there is no '$arguments'");
179 } else {
180 create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments);
181 $who->reply ($npc, "Here is your $cmd");
182 }
183
184 } elsif ($cmd eq 'receive') {
185 my $storage = cf::map::get_map ("/planes/IPO_storage");
186 unless ($storage) {
187 $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
188 return 1;
189 }
190
191 my $plname = $who->name;
192 my $cnt;
193 for ($storage->at (2, 2)) {
194 if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) {
195 $_->insert_in_ob ($who);
196 $cnt++;
197 }
198 }
199
200 if ($cnt) {
201 $who->reply ($npc, $cnt == 1 ? "Here is your pakage." : "Here are your packages.");
202 } else {
203 $who->reply ($npc, "Sorry, no deliverys for you sir.");
204 }
205
206 } elsif ($cmd eq 'send') {
207 unless ($arguments =~ /^\S+$/) {
208 $who->reply ($npc, "Send to who?");
209 return 1;
210 }
211
212 my $storage = cf::map::get_map ("/planes/IPO_storage");
213 unless ($storage) {
214 $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
215 return 1;
216 }
217
218 my $cnt;
219 for ($who->inv) {
220 if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) {
221 $_->set_name ("$1 F: $2 T: $arguments");
222 $_->teleport ($storage, 2, 2);
223 $cnt++;
224 }
225 }
226
227 if ($cnt) {
228 $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n");
229 } else {
230 $who->reply ($npc, "Sorry, found no package to send to $arguments.");
231 }
232
233 } else {
234 $who->reply ($npc,
235 sprintf "How can I help you?\n"
236 ."Here is a quick list of commands I understand:\n\n"
237 ."- pen (%s platinum)\n"
238 ."- literacy (%s platinum)\n"
239 ."- mailscroll <friend> (%s platinum)\n"
240 ."- bag <friend> (%s platinum)\n"
241 ."- package <friend> (%s platinum)\n"
242 ."- carton <friend> (%s platinum)\n"
243 .($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""),
244 40, 1000, 1, 1, 5, 10
245 );
246 }
247 1
248 };
249
250 package CFMail;
251 use YAML qw/LoadFile DumpFile Dump/;
252 use POSIX qw/strftime/;
253
254 my $maildb_file = cf::localdir . "/crossfiremail.perl";
255 my $last_check = (-M $maildb_file) + 1;
256 my $MAILDB;
257
258 sub check_maildb {
259 if ($last_check > (-M $maildb_file)) {
260 $last_check = -M $maildb_file;
261
262 my $maildb = eval { my $m = LoadFile ($maildb_file); return $m };
263 if ($@) {
264 warn "ERROR when reading mail database $maildb_file: $@\n";
265 $maildb_file = cf::localdir . "/crossfiremail.perl.after_load_failure";
266 } else {
267 $MAILDB = $maildb;
268 }
269 }
270 }
271
272 # This is not thread or multiprocess safe!
273 # XXX: A second process will (of course) terribly damage the maildatabase
274 # but i assume there wont be any second process.
275 sub sync_maildb {
276 eval {
277 DumpFile ("$maildb_file.tmp", $MAILDB);
278 };
279 if ($@) {
280 warn "ERROR when writing mail database $maildb_file.tmp: $@\n";
281 } else {
282 rename "$maildb_file.tmp", $maildb_file;
283 $last_check = -M $maildb_file;
284 }
285 }
286
287 sub get_mail {
288 my ($toname) = @_;
289 check_maildb;
290 $MAILDB->{$toname};
291 }
292
293 sub clear_mail {
294 my ($toname) = @_;
295 check_maildb;
296 $MAILDB->{$toname} = [];
297 sync_maildb;
298 }
299
300 sub store_mail {
301 my ($type, $toname, $fromname, $message) = @_;
302 check_maildb;
303 push @{$MAILDB->{$toname}}, [$type, $fromname, $message];
304 sync_maildb;
305 }
306
307 sub send_mail {
308 my ($type, $toname, $fromname, $message) = @_;
309 my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time));
310 my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n";
311 check_maildb;
312 push @{$MAILDB->{$toname}}, [$type, $fromname, $msg];
313 sync_maildb;
314 }
315
316 check_maildb ();
317
318 1;