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

# Content
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 # prices [in silver]
12 my %prices = (
13 pen => [
14 10000, 'stylus',
15 sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (0); }
16 ],
17 literacy => [
18 10000, 'scroll_literacy',
19 sub { $_[0]->value (0) }
20 ],
21 mailscroll => [
22 50, 'mailscroll_empty',
23 sub {
24 $_[0]->name ("mailscroll T: $_[2] F: $_[1]");
25 $_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]");
26 $_[0]->value (0);
27 },
28 'plarg'
29 ],
30 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 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 if (my $player = cf::player::find_active $_) {
56 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 $map->insert ($o, $x, $y);
72 $r
73 }
74
75 # this handler notifies the player of new mail
76 cf::player->attach (
77 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 );
93
94 # this event handler handles receiving of mails
95 cf::object::attachment ipo_mailbox =>
96 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 $box->insert ($mail);
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 # 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 my $pl = cf::player::find_active $who->name;
171 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 if ($who->pay_amount ($pr->[0])) {
179 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 }
187 } else {
188 $who->reply ($npc, "Sorry, you don't have enough money.");
189 }
190
191 } elsif ($cmd eq 'receive') {
192 cf::async {
193 $Coro::current->{desc} = "ipo receive";
194
195 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 $storage->load;
201
202 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 }
210
211 if ($cnt) {
212 $who->reply ($npc, $cnt == 1 ? "Here is your package." : "Here are your packages.");
213 } else {
214 $who->reply ($npc, "Sorry, no deliveries for you sir.");
215 }
216 }
217
218 } elsif ($cmd eq 'send') {
219 unless ($arguments =~ /^\S+$/) {
220 $who->reply ($npc, "Send to who?");
221 return 1;
222 }
223
224 cf::async {
225 $Coro::current->{desc} = "ipo send";
226
227 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 $storage->load;
233
234 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 $storage->insert ($_, 2, 2);
239 $cnt++;
240 }
241 }
242
243 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 }
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 . " - 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 . " - 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 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 );
272 }
273 1
274 };
275
276 package CFMail;
277
278 use POSIX qw/strftime/;
279 use CFDB;
280
281 my $MAILDB = CFDB->new (db_file => "$LOCALDIR/crossfiremail");
282
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 my $msg = "From: $fromname\rTo: $toname\rDate: $time\n\n$message\n";
304 store_mail ($type, $toname, $fromname, $msg);
305 }
306
307 1;
308