ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/ipo.ext
Revision: 1.8
Committed: Fri Aug 25 17:08:20 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.7: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 elmex 1.1 #! perl
2 root 1.8 #CONVERSION: PARTIAL, MISSING ON_APPLY
3 elmex 1.1
4     my $price_fact = 50;
5    
6     sub set_package {
7 elmex 1.2 my ($pkg, $from, $to, $bagname, $weight) = @_;
8 elmex 1.1 $pkg->set_name ("$bagname T: $to F: $from");
9     $pkg->set_weight_limit ($weight);
10     $pkg->set_str (0);
11     }
12    
13     # prices in plat.
14     my %prices = (
15     pen => [
16     40, 'stylus',
17 elmex 1.2 sub { $_[0]->set_name ('IPO Writing Pen'); $_[0]->set_value (40 * $price_fact); }
18 elmex 1.1 ],
19     literacy => [
20     1000, 'scroll_literacy',
21     sub { $_[0]->set_value (1000 * $price_fact) }
22     ],
23     mailscroll => [
24     1, 'scroll',
25     sub {
26     $_[0]->set_name ("mailscroll T: $_[2] F: $_[1]");
27     $_[0]->set_name_plural ("mailscrolls T: $_[2] F: $_[1]");
28     $_[0]->set_value (1 * $price_fact);
29     },
30     'plarg'
31     ],
32     bag => [ 1, 'r_sack', sub { set_package (@_, bag => 5000) }, 'plarg' ],
33     package => [ 5, 'r_sack', sub { set_package (@_, package => 50000) }, 'plarg' ],
34     carton => [10, 'r_sack', sub { set_package (@_, carton => 100000) }, 'plarg' ],
35     mailwarning => [
36     0, 'diploma',
37     sub {
38     $_[0]->set_name ("mailwarning T: $_[2] F: $_[1]");
39     $_[0]->set_name_plural ("mailwarnings T: $_[2] F: $_[1]");
40     $_[0]->set_value (0);
41     },
42     'plarg'
43     ],
44     );
45    
46 elmex 1.2 my %mailtypes = (
47     1 => ['scroll', 'mailscroll'],
48     2 => ['note', 'newspaper'],
49     3 => ['diploma', 'mailwarning'],
50     );
51    
52 elmex 1.5 sub notify_players {
53     my (%sent_targets) = @_;
54    
55     # lets message player ingame: this is a NEW feature from the perl IPO :-)
56     for (keys %sent_targets) {
57     if (my $player = cf::player::find $_) {
58     my $cnt = $sent_targets{$_};
59    
60     if ($cnt == 1) {
61     $player->ob->message ("You've got new mail.");
62     } else {
63     $player->ob->message ("You've got $cnt new mails.");
64     }
65     }
66     }
67     }
68    
69 elmex 1.1 sub create_object {
70     my ($name, $map, $x, $y, $cb, @a) = @_;
71     my $o = cf::object::new $name;
72     my $r = $cb->($o, @a);
73     $map->insert_object ($o, $x, $y);
74     $r
75     }
76    
77 elmex 1.2 # this handler handles to notice the player that he has got mail
78 root 1.7 cf::attach_to_players
79     on_login => sub {
80     my ($pl) = @_;
81    
82     my $mails = CFMail::get_mail ($pl->ob->name);
83    
84     my $cnt = @{$mails || []};
85    
86     if ($cnt == 1) {
87     $pl->ob->message ("You got one mail.");
88     } elsif ($cnt > 1) {
89     $pl->ob->message ("You got $cnt mails.");
90     } else {
91     $pl->ob->message ("You haven't got any mail.");
92     }
93     },
94     ;
95 elmex 1.2
96     # this event handler handles receiving of mails
97     sub on_apply {
98     my ($ev, $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     0;
138     }
139    
140     # this event handler handles the sending of mails
141     sub on_close {
142     my ($ev, $box, $pl) = @_;
143    
144     my @mails;
145    
146     my %sent_targets;
147    
148     for ($box->inv) {
149     if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) {
150     CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message);
151     $pl->message ("Sent mail$1 to $2 (from $3).");
152     $sent_targets{$2}++;
153     push @mails, $_;
154    
155     } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) {
156     # this is for mails that remain in the queue for the player
157     CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message);
158     push @mails, $_;
159     }
160     }
161    
162     $_->remove for @mails;
163    
164 elmex 1.5 notify_players (%sent_targets);
165 elmex 1.2
166     0;
167     }
168    
169     # this is the main command interface for the IPO NPC
170 elmex 1.1 cf::register_script_function "ipo::command" => sub {
171     my ($who, $msg, $npc) = @_;
172     my ($cmd, $arguments) = split /\s+/, $msg, 2;
173     $cmd = lc $cmd;
174    
175     my $pl = cf::player::find $who->name;
176     my ($x, $y) = ($pl->ob->x, $pl->ob->y);
177    
178     if (my $pr = $prices{$cmd}) {
179     if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) {
180     return 1;
181     }
182    
183     $who->pay_amount ($pr->[0] * $price_fact);
184     if ($pr->[3] && not cf::player::exists $arguments) {
185     $who->reply ($npc, "Sorry, there is no '$arguments'");
186     } else {
187     create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments);
188     $who->reply ($npc, "Here is your $cmd");
189     }
190    
191     } elsif ($cmd eq 'receive') {
192     my $storage = cf::map::get_map ("/planes/IPO_storage");
193     unless ($storage) {
194     $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
195     return 1;
196     }
197    
198     my $plname = $who->name;
199     my $cnt;
200     for ($storage->at (2, 2)) {
201     if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) {
202     $_->insert_in_ob ($who);
203     $cnt++;
204     }
205     }
206    
207     if ($cnt) {
208     $who->reply ($npc, $cnt == 1 ? "Here is your pakage." : "Here are your packages.");
209     } else {
210     $who->reply ($npc, "Sorry, no deliverys for you sir.");
211     }
212    
213     } elsif ($cmd eq 'send') {
214     unless ($arguments =~ /^\S+$/) {
215     $who->reply ($npc, "Send to who?");
216     return 1;
217     }
218    
219     my $storage = cf::map::get_map ("/planes/IPO_storage");
220     unless ($storage) {
221     $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
222     return 1;
223     }
224    
225     my $cnt;
226     for ($who->inv) {
227 elmex 1.2 if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) {
228 elmex 1.1 $_->set_name ("$1 F: $2 T: $arguments");
229     $_->teleport ($storage, 2, 2);
230     $cnt++;
231     }
232     }
233    
234     if ($cnt) {
235     $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n");
236 elmex 1.5 CFMail::send_mail (1, $arguments, $who->name, "You got $cnt packages from " . $who->name);
237     notify_players ($arguments => 1);
238 elmex 1.1 } else {
239     $who->reply ($npc, "Sorry, found no package to send to $arguments.");
240     }
241    
242     } else {
243     $who->reply ($npc,
244 elmex 1.2 sprintf "How can I help you?\n"
245 elmex 1.1 ."Here is a quick list of commands I understand:\n\n"
246 elmex 1.2 ."- pen (%s platinum)\n"
247     ."- literacy (%s platinum)\n"
248     ."- mailscroll <friend> (%s platinum)\n"
249     ."- bag <friend> (%s platinum)\n"
250     ."- package <friend> (%s platinum)\n"
251     ."- carton <friend> (%s platinum)\n"
252 elmex 1.5 ."- send <friend> (send bags/packages/cartons)\n"
253     ."- receive (to receive packages for you)\n"
254 elmex 1.2 .($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""),
255     40, 1000, 1, 1, 5, 10
256 elmex 1.1 );
257     }
258     1
259 elmex 1.2 };
260    
261     package CFMail;
262     use POSIX qw/strftime/;
263 elmex 1.4 use CFDB;
264 elmex 1.2
265 elmex 1.4 my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl");
266 elmex 1.2
267     sub get_mail {
268     my ($toname) = @_;
269 elmex 1.3 $MAILDB->get ($toname);
270 elmex 1.2 }
271    
272     sub clear_mail {
273     my ($toname) = @_;
274 elmex 1.3 $MAILDB->clear ($toname);
275 elmex 1.2 }
276    
277     sub store_mail {
278     my ($type, $toname, $fromname, $message) = @_;
279 elmex 1.3 my $mails = $MAILDB->get ($toname);
280     push @$mails, [$type, $fromname, $message];
281     $MAILDB->set ($toname, $mails);
282 elmex 1.2 }
283    
284     sub send_mail {
285     my ($type, $toname, $fromname, $message) = @_;
286     my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time));
287     my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n";
288 elmex 1.3 store_mail ($type, $toname, $fromname, $msg);
289 elmex 1.2 }
290    
291     1;