ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/ipo.ext
Revision: 1.9
Committed: Sun Aug 27 15:23:30 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.8: +72 -75 lines
Log Message:
further conversion

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 notify_players {
52 my (%sent_targets) = @_;
53
54 # lets message player ingame: this is a NEW feature from the perl IPO :-)
55 for (keys %sent_targets) {
56 if (my $player = cf::player::find $_) {
57 my $cnt = $sent_targets{$_};
58
59 if ($cnt == 1) {
60 $player->ob->message ("You've got new mail.");
61 } else {
62 $player->ob->message ("You've got $cnt new mails.");
63 }
64 }
65 }
66 }
67
68 sub create_object {
69 my ($name, $map, $x, $y, $cb, @a) = @_;
70 my $o = cf::object::new $name;
71 my $r = $cb->($o, @a);
72 $map->insert_object ($o, $x, $y);
73 $r
74 }
75
76 # this handler handles to notice the player that he has got mail
77 cf::attach_to_players
78 on_login => sub {
79 my ($pl) = @_;
80
81 my $mails = CFMail::get_mail ($pl->ob->name);
82
83 my $cnt = @{$mails || []};
84
85 if ($cnt == 1) {
86 $pl->ob->message ("You got one mail.");
87 } elsif ($cnt > 1) {
88 $pl->ob->message ("You got $cnt mails.");
89 } else {
90 $pl->ob->message ("You haven't got any mail.");
91 }
92 },
93 ;
94
95 # this event handler handles receiving of mails
96 cf::register_attachment ipo_mailbox =>
97 on_apply => sub {
98 my ($box, $pl) = @_;
99
100 my $cnt;
101 my $mails = CFMail::get_mail ($pl->name) || [];
102
103 # count the mails that are in the container
104 # FIXME: the problem with on_apply is that it is called even when
105 # the player closes the container. so we get a 'You have X mails.' message
106 # twice. - This bug existed also with the old python plugin
107
108 my $plname = $pl->name;
109 for ($box->inv) {
110 $_->name =~ /\S+ F: \S+ T: \Q$plname\E/
111 and $cnt++;
112 }
113
114 for (@$mails) {
115 my ($type, $from, $msg) = @$_;
116 $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll'];
117 my $mail = cf::object::new $type->[0];
118 $mail->set_name ("$type->[1] F: $from T: " .$pl->name);
119 $mail->set_name_plural ("$type->[1]s F: $from T: " .$pl->name);
120 $mail->set_message ($msg);
121 $mail->set_value (0);
122 $mail->insert_in_ob ($box);
123 }
124
125 $cnt += @$mails;
126
127 if ($cnt == 1) {
128 $pl->message ("You got one mail.");
129 } elsif ($cnt > 1) {
130 $pl->message ("You got $cnt mails.");
131 } else {
132 $pl->message ("You haven't got any mail.");
133 }
134
135 CFMail::clear_mail ($pl->name);
136 },
137 # this event handler handles the sending of mails
138 on_close => sub {
139 my ($box, $pl) = @_;
140
141 my @mails;
142
143 my %sent_targets;
144
145 for ($box->inv) {
146 if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) {
147 CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message);
148 $pl->message ("Sent mail$1 to $2 (from $3).");
149 $sent_targets{$2}++;
150 push @mails, $_;
151
152 } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) {
153 # this is for mails that remain in the queue for the player
154 CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message);
155 push @mails, $_;
156 }
157 }
158
159 $_->remove for @mails;
160
161 notify_players (%sent_targets);
162 },
163 ;
164
165 # this is the main command interface for the IPO NPC
166 cf::register_script_function "ipo::command" => sub {
167 my ($who, $msg, $npc) = @_;
168 my ($cmd, $arguments) = split /\s+/, $msg, 2;
169 $cmd = lc $cmd;
170
171 my $pl = cf::player::find $who->name;
172 my ($x, $y) = ($pl->ob->x, $pl->ob->y);
173
174 if (my $pr = $prices{$cmd}) {
175 if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) {
176 return 1;
177 }
178
179 $who->pay_amount ($pr->[0] * $price_fact);
180 if ($pr->[3] && not cf::player::exists $arguments) {
181 $who->reply ($npc, "Sorry, there is no '$arguments'");
182 } else {
183 create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments);
184 $who->reply ($npc, "Here is your $cmd");
185 }
186
187 } elsif ($cmd eq 'receive') {
188 my $storage = cf::map::get_map ("/planes/IPO_storage");
189 unless ($storage) {
190 $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
191 return 1;
192 }
193
194 my $plname = $who->name;
195 my $cnt;
196 for ($storage->at (2, 2)) {
197 if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) {
198 $_->insert_in_ob ($who);
199 $cnt++;
200 }
201 }
202
203 if ($cnt) {
204 $who->reply ($npc, $cnt == 1 ? "Here is your pakage." : "Here are your packages.");
205 } else {
206 $who->reply ($npc, "Sorry, no deliverys for you sir.");
207 }
208
209 } elsif ($cmd eq 'send') {
210 unless ($arguments =~ /^\S+$/) {
211 $who->reply ($npc, "Send to who?");
212 return 1;
213 }
214
215 my $storage = cf::map::get_map ("/planes/IPO_storage");
216 unless ($storage) {
217 $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
218 return 1;
219 }
220
221 my $cnt;
222 for ($who->inv) {
223 if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) {
224 $_->set_name ("$1 F: $2 T: $arguments");
225 $_->teleport ($storage, 2, 2);
226 $cnt++;
227 }
228 }
229
230 if ($cnt) {
231 $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n");
232 CFMail::send_mail (1, $arguments, $who->name, "You got $cnt packages from " . $who->name);
233 notify_players ($arguments => 1);
234 } else {
235 $who->reply ($npc, "Sorry, found no package to send to $arguments.");
236 }
237
238 } else {
239 $who->reply ($npc,
240 sprintf "How can I help you?\n"
241 . "Here is a quick list of commands I understand:\n\n"
242 . "- pen (%s platinum)\n"
243 . "- literacy (%s platinum)\n"
244 . "- mailscroll <friend> (%s platinum)\n"
245 . "- bag <friend> (%s platinum)\n"
246 . "- package <friend> (%s platinum)\n"
247 . "- carton <friend> (%s platinum)\n"
248 . "- send <friend> (send bags/packages/cartons)\n"
249 . "- receive (to receive packages for you)\n"
250 . ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""),
251 40, 1000, 1, 1, 5, 10
252 );
253 }
254 1
255 };
256
257 package CFMail;
258
259 use POSIX qw/strftime/;
260 use CFDB;
261
262 my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl");
263
264 sub get_mail {
265 my ($toname) = @_;
266 $MAILDB->get ($toname);
267 }
268
269 sub clear_mail {
270 my ($toname) = @_;
271 $MAILDB->clear ($toname);
272 }
273
274 sub store_mail {
275 my ($type, $toname, $fromname, $message) = @_;
276 my $mails = $MAILDB->get ($toname);
277 push @$mails, [$type, $fromname, $message];
278 $MAILDB->set ($toname, $mails);
279 }
280
281 sub send_mail {
282 my ($type, $toname, $fromname, $message) = @_;
283 my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time));
284 my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n";
285 store_mail ($type, $toname, $fromname, $msg);
286 }
287
288 1;