ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/ipo.ext
Revision: 1.16
Committed: Sun Oct 11 21:39:08 2009 UTC (14 years, 7 months ago) by sf-marcmagus
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_90, rel-2_92
Changes since 1.15: +20 -17 lines
Log Message:
Post Office now reports prices correctly [and consistently].
Prices tweaked.

File Contents

# User Rev Content
1 root 1.1 #! perl
2    
3     sub set_package {
4     my ($pkg, $from, $to, $bagname, $weight) = @_;
5    
6     $pkg->name ("$bagname T: $to F: $from");
7     $pkg->weight_limit ($weight);
8     $pkg->stats->Str (0);
9     }
10    
11 sf-marcmagus 1.16 # prices [in silver]
12 root 1.1 my %prices = (
13     pen => [
14 sf-marcmagus 1.16 10000, 'stylus',
15 elmex 1.12 sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (0); }
16 root 1.1 ],
17     literacy => [
18 sf-marcmagus 1.16 10000, 'scroll_literacy',
19 elmex 1.12 sub { $_[0]->value (0) }
20 root 1.1 ],
21     mailscroll => [
22 sf-marcmagus 1.16 50, 'mailscroll_empty',
23 root 1.1 sub {
24     $_[0]->name ("mailscroll T: $_[2] F: $_[1]");
25     $_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]");
26 elmex 1.12 $_[0]->value (0);
27 root 1.1 },
28     'plarg'
29     ],
30 sf-marcmagus 1.16 bag => [ 100, 'r_sack', sub { set_package (@_, bag => 5000) }, 'plarg' ],
31     package => [ 1000, 'r_sack', sub { set_package (@_, package => 50000) }, 'plarg' ],
32     carton => [ 2000, 'r_sack', sub { set_package (@_, carton => 100000) }, 'plarg' ],
33 root 1.1 mailwarning => [
34     0, 'diploma',
35     sub {
36     $_[0]->name ("mailwarning T: $_[2] F: $_[1]");
37     $_[0]->name_pl ("mailwarnings T: $_[2] F: $_[1]");
38     $_[0]->value (0);
39     },
40     'plarg'
41     ],
42     );
43    
44     my %mailtypes = (
45     1 => ['scroll', 'mailscroll'],
46     2 => ['note', 'newspaper'],
47     3 => ['diploma', 'mailwarning'],
48     );
49    
50     sub notify_players {
51     my (%sent_targets) = @_;
52    
53     # lets message player ingame: this is a NEW feature from the perl IPO :-)
54     for (keys %sent_targets) {
55 root 1.4 if (my $player = cf::player::find_active $_) {
56 root 1.1 my $cnt = $sent_targets{$_};
57    
58     if ($cnt == 1) {
59     $player->ob->message ("You've got new mail.");
60     } else {
61     $player->ob->message ("You've got $cnt new mails.");
62     }
63     }
64     }
65     }
66    
67     sub create_object {
68     my ($name, $map, $x, $y, $cb, @a) = @_;
69     my $o = cf::object::new $name;
70     my $r = $cb->($o, @a);
71 root 1.11 $map->insert ($o, $x, $y);
72 root 1.1 $r
73     }
74    
75 root 1.2 # this handler notifies the player of new mail
76     cf::player->attach (
77 root 1.1 on_login => sub {
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 root 1.2 );
93 root 1.1
94     # this event handler handles receiving of mails
95 root 1.2 cf::object::attachment ipo_mailbox =>
96 root 1.1 on_apply => sub {
97     my ($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->name ("$type->[1] F: $from T: " .$pl->name);
118     $mail->name_pl ("$type->[1]s F: $from T: " .$pl->name);
119     $mail->msg ($msg);
120     $mail->value (0);
121 elmex 1.5 $box->insert ($mail);
122 root 1.1 }
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     # this event handler handles the sending of mails
137     on_close => sub {
138     my ($box, $pl) = @_;
139    
140     my @mails;
141    
142     my %sent_targets;
143    
144     for ($box->inv) {
145     if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) {
146     CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->msg);
147     $pl->message ("Sent mail$1 to $2 (from $3).");
148     $sent_targets{$2}++;
149     push @mails, $_;
150    
151     } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) {
152     # this is for mails that remain in the queue for the player
153     CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->msg);
154     push @mails, $_;
155     }
156     }
157    
158     $_->remove for @mails;
159    
160     notify_players (%sent_targets);
161     },
162     ;
163    
164     # this is the main command interface for the IPO NPC
165     cf::register_script_function "ipo::command" => sub {
166     my ($who, $msg, $npc) = @_;
167     my ($cmd, $arguments) = split /\s+/, $msg, 2;
168     $cmd = lc $cmd;
169    
170 root 1.4 my $pl = cf::player::find_active $who->name;
171 root 1.1 my ($x, $y) = ($pl->ob->x, $pl->ob->y);
172    
173     if (my $pr = $prices{$cmd}) {
174     if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) {
175     return 1;
176     }
177    
178 sf-marcmagus 1.16 if ($who->pay_amount ($pr->[0])) {
179 elmex 1.14 cf::async {
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 elmex 1.12 }
187 root 1.1 } else {
188 elmex 1.12 $who->reply ($npc, "Sorry, you don't have enough money.");
189 root 1.1 }
190    
191     } elsif ($cmd eq 'receive') {
192 elmex 1.5 cf::async {
193 root 1.9 $Coro::current->{desc} = "ipo receive";
194    
195 elmex 1.5 my $storage = cf::map::find ("/planes/IPO_storage");
196     unless ($storage) {
197     $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
198     return 1;
199     }
200 root 1.10 $storage->load;
201 root 1.1
202 elmex 1.5 my $plname = $who->name;
203     my $cnt;
204     for ($storage->at (2, 2)) {
205     if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) {
206     $who->insert ($_);
207     $cnt++;
208     }
209 root 1.1 }
210    
211 elmex 1.5 if ($cnt) {
212 root 1.7 $who->reply ($npc, $cnt == 1 ? "Here is your package." : "Here are your packages.");
213 elmex 1.5 } else {
214 root 1.7 $who->reply ($npc, "Sorry, no deliveries for you sir.");
215 elmex 1.5 }
216 root 1.1 }
217    
218     } elsif ($cmd eq 'send') {
219     unless ($arguments =~ /^\S+$/) {
220     $who->reply ($npc, "Send to who?");
221     return 1;
222     }
223    
224 elmex 1.5 cf::async {
225 root 1.9 $Coro::current->{desc} = "ipo send";
226    
227 elmex 1.5 my $storage = cf::map::find ("/planes/IPO_storage");
228     unless ($storage) {
229     $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
230     return 1;
231     }
232 root 1.10 $storage->load;
233 root 1.1
234 elmex 1.5 my $cnt;
235     for ($who->inv) {
236     if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) {
237     $_->name ("$1 F: $2 T: $arguments");
238 root 1.11 $storage->insert ($_, 2, 2);
239 elmex 1.5 $cnt++;
240     }
241 root 1.1 }
242    
243 elmex 1.5 if ($cnt) {
244     $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n");
245     CFMail::send_mail (1, $arguments, $who->name, "You got $cnt packages from " . $who->name);
246     notify_players ($arguments => 1);
247     } else {
248     $who->reply ($npc, "Sorry, found no package to send to $arguments.");
249     }
250 root 1.1 }
251    
252     } else {
253     $who->reply ($npc,
254     sprintf "How can I help you?\n"
255     . "Here is a quick list of commands I understand:\n\n"
256 sf-marcmagus 1.16 . " - pen (%s)\n"
257     . " - literacy (%s)\n"
258     . " - mailscroll <friend> (%s)\n"
259     . " - bag <friend> (%s)\n"
260     . " - package <friend> (%s)\n"
261     . " - carton <friend> (%s)\n"
262 root 1.15 . " - send <friend> (send bags/packages/cartons)\n"
263     . " - receive (to receive packages for you)\n"
264     . ($who->flag (cf::FLAG_WIZ) ? " - mailwarning <player>\n" : ""),
265 sf-marcmagus 1.16 cf::cost_string_from_value($prices{'pen'}[0]),
266     cf::cost_string_from_value($prices{'literacy'}[0]),
267     cf::cost_string_from_value($prices{'mailscroll'}[0]),
268     cf::cost_string_from_value($prices{'bag'}[0]),
269     cf::cost_string_from_value($prices{'package'}[0]),
270     cf::cost_string_from_value($prices{'carton'}[0])
271 root 1.1 );
272     }
273     1
274     };
275    
276     package CFMail;
277    
278     use POSIX qw/strftime/;
279     use CFDB;
280    
281 root 1.6 my $MAILDB = CFDB->new (db_file => "$LOCALDIR/crossfiremail");
282 root 1.1
283     sub get_mail {
284     my ($toname) = @_;
285     $MAILDB->get ($toname);
286     }
287    
288     sub clear_mail {
289     my ($toname) = @_;
290     $MAILDB->clear ($toname);
291     }
292    
293     sub store_mail {
294     my ($type, $toname, $fromname, $message) = @_;
295     my $mails = $MAILDB->get ($toname);
296     push @$mails, [$type, $fromname, $message];
297     $MAILDB->set ($toname, $mails);
298     }
299    
300     sub send_mail {
301     my ($type, $toname, $fromname, $message) = @_;
302     my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time));
303 root 1.15 my $msg = "From: $fromname\rTo: $toname\rDate: $time\n\n$message\n";
304 root 1.1 store_mail ($type, $toname, $fromname, $msg);
305     }
306    
307     1;
308 root 1.15