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