ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/ipo.ext
Revision: 1.6
Committed: Fri Aug 25 13:24:05 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.5: +1 -1 lines
Log Message:
moved plug-ins to new plug-in system, where applicable

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