1 |
root |
1.1 |
#! perl |
2 |
|
|
|
3 |
|
|
my $price_fact = 50; |
4 |
|
|
|
5 |
|
|
sub set_package { |
6 |
|
|
my ($pkg, $from, $to, $bagname, $weight) = @_; |
7 |
|
|
|
8 |
|
|
$pkg->name ("$bagname T: $to F: $from"); |
9 |
|
|
$pkg->weight_limit ($weight); |
10 |
|
|
$pkg->stats->Str (0); |
11 |
|
|
} |
12 |
|
|
|
13 |
|
|
# prices in plat. |
14 |
|
|
my %prices = ( |
15 |
|
|
pen => [ |
16 |
|
|
40, 'stylus', |
17 |
|
|
sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (40 * $price_fact); } |
18 |
|
|
], |
19 |
|
|
literacy => [ |
20 |
|
|
1000, 'scroll_literacy', |
21 |
|
|
sub { $_[0]->value (1000 * $price_fact) } |
22 |
|
|
], |
23 |
|
|
mailscroll => [ |
24 |
root |
1.8 |
1, 'mailscroll_empty', |
25 |
root |
1.1 |
sub { |
26 |
|
|
$_[0]->name ("mailscroll T: $_[2] F: $_[1]"); |
27 |
|
|
$_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]"); |
28 |
|
|
$_[0]->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]->name ("mailwarning T: $_[2] F: $_[1]"); |
39 |
|
|
$_[0]->name_pl ("mailwarnings T: $_[2] F: $_[1]"); |
40 |
|
|
$_[0]->value (0); |
41 |
|
|
}, |
42 |
|
|
'plarg' |
43 |
|
|
], |
44 |
|
|
); |
45 |
|
|
|
46 |
|
|
my %mailtypes = ( |
47 |
|
|
1 => ['scroll', 'mailscroll'], |
48 |
|
|
2 => ['note', 'newspaper'], |
49 |
|
|
3 => ['diploma', 'mailwarning'], |
50 |
|
|
); |
51 |
|
|
|
52 |
|
|
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 |
root |
1.4 |
if (my $player = cf::player::find_active $_) { |
58 |
root |
1.1 |
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 |
|
|
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 |
root |
1.11 |
$map->insert ($o, $x, $y); |
74 |
root |
1.1 |
$r |
75 |
|
|
} |
76 |
|
|
|
77 |
root |
1.2 |
# this handler notifies the player of new mail |
78 |
|
|
cf::player->attach ( |
79 |
root |
1.1 |
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 |
root |
1.2 |
); |
95 |
root |
1.1 |
|
96 |
|
|
# this event handler handles receiving of mails |
97 |
root |
1.2 |
cf::object::attachment ipo_mailbox => |
98 |
root |
1.1 |
on_apply => sub { |
99 |
|
|
my ($box, $pl) = @_; |
100 |
|
|
|
101 |
|
|
my $cnt; |
102 |
|
|
my $mails = CFMail::get_mail ($pl->name) || []; |
103 |
|
|
|
104 |
|
|
# count the mails that are in the container |
105 |
|
|
# FIXME: the problem with on_apply is that it is called even when |
106 |
|
|
# the player closes the container. so we get a 'You have X mails.' message |
107 |
|
|
# twice. - This bug existed also with the old python plugin |
108 |
|
|
|
109 |
|
|
my $plname = $pl->name; |
110 |
|
|
for ($box->inv) { |
111 |
|
|
$_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
112 |
|
|
and $cnt++; |
113 |
|
|
} |
114 |
|
|
|
115 |
|
|
for (@$mails) { |
116 |
|
|
my ($type, $from, $msg) = @$_; |
117 |
|
|
$type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
118 |
|
|
my $mail = cf::object::new $type->[0]; |
119 |
|
|
$mail->name ("$type->[1] F: $from T: " .$pl->name); |
120 |
|
|
$mail->name_pl ("$type->[1]s F: $from T: " .$pl->name); |
121 |
|
|
$mail->msg ($msg); |
122 |
|
|
$mail->value (0); |
123 |
elmex |
1.5 |
$box->insert ($mail); |
124 |
root |
1.1 |
} |
125 |
|
|
|
126 |
|
|
$cnt += @$mails; |
127 |
|
|
|
128 |
|
|
if ($cnt == 1) { |
129 |
|
|
$pl->message ("You got one mail."); |
130 |
|
|
} elsif ($cnt > 1) { |
131 |
|
|
$pl->message ("You got $cnt mails."); |
132 |
|
|
} else { |
133 |
|
|
$pl->message ("You haven't got any mail."); |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
CFMail::clear_mail ($pl->name); |
137 |
|
|
}, |
138 |
|
|
# this event handler handles the sending of mails |
139 |
|
|
on_close => sub { |
140 |
|
|
my ($box, $pl) = @_; |
141 |
|
|
|
142 |
|
|
my @mails; |
143 |
|
|
|
144 |
|
|
my %sent_targets; |
145 |
|
|
|
146 |
|
|
for ($box->inv) { |
147 |
|
|
if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
148 |
|
|
CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->msg); |
149 |
|
|
$pl->message ("Sent mail$1 to $2 (from $3)."); |
150 |
|
|
$sent_targets{$2}++; |
151 |
|
|
push @mails, $_; |
152 |
|
|
|
153 |
|
|
} elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
154 |
|
|
# this is for mails that remain in the queue for the player |
155 |
|
|
CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->msg); |
156 |
|
|
push @mails, $_; |
157 |
|
|
} |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
$_->remove for @mails; |
161 |
|
|
|
162 |
|
|
notify_players (%sent_targets); |
163 |
|
|
}, |
164 |
|
|
; |
165 |
|
|
|
166 |
|
|
# this is the main command interface for the IPO NPC |
167 |
|
|
cf::register_script_function "ipo::command" => sub { |
168 |
|
|
my ($who, $msg, $npc) = @_; |
169 |
|
|
my ($cmd, $arguments) = split /\s+/, $msg, 2; |
170 |
|
|
$cmd = lc $cmd; |
171 |
|
|
|
172 |
root |
1.4 |
my $pl = cf::player::find_active $who->name; |
173 |
root |
1.1 |
my ($x, $y) = ($pl->ob->x, $pl->ob->y); |
174 |
|
|
|
175 |
|
|
if (my $pr = $prices{$cmd}) { |
176 |
|
|
if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) { |
177 |
|
|
return 1; |
178 |
|
|
} |
179 |
|
|
|
180 |
|
|
$who->pay_amount ($pr->[0] * $price_fact); |
181 |
|
|
if ($pr->[3] && not cf::player::exists $arguments) { |
182 |
|
|
$who->reply ($npc, "Sorry, there is no '$arguments'"); |
183 |
|
|
} else { |
184 |
|
|
create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments); |
185 |
|
|
$who->reply ($npc, "Here is your $cmd"); |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
} elsif ($cmd eq 'receive') { |
189 |
elmex |
1.5 |
cf::async { |
190 |
root |
1.9 |
$Coro::current->{desc} = "ipo receive"; |
191 |
|
|
|
192 |
elmex |
1.5 |
my $storage = cf::map::find ("/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 |
root |
1.10 |
$storage->load; |
198 |
root |
1.1 |
|
199 |
elmex |
1.5 |
my $plname = $who->name; |
200 |
|
|
my $cnt; |
201 |
|
|
for ($storage->at (2, 2)) { |
202 |
|
|
if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) { |
203 |
|
|
$who->insert ($_); |
204 |
|
|
$cnt++; |
205 |
|
|
} |
206 |
root |
1.1 |
} |
207 |
|
|
|
208 |
elmex |
1.5 |
if ($cnt) { |
209 |
root |
1.7 |
$who->reply ($npc, $cnt == 1 ? "Here is your package." : "Here are your packages."); |
210 |
elmex |
1.5 |
} else { |
211 |
root |
1.7 |
$who->reply ($npc, "Sorry, no deliveries for you sir."); |
212 |
elmex |
1.5 |
} |
213 |
root |
1.1 |
} |
214 |
|
|
|
215 |
|
|
} elsif ($cmd eq 'send') { |
216 |
|
|
unless ($arguments =~ /^\S+$/) { |
217 |
|
|
$who->reply ($npc, "Send to who?"); |
218 |
|
|
return 1; |
219 |
|
|
} |
220 |
|
|
|
221 |
elmex |
1.5 |
cf::async { |
222 |
root |
1.9 |
$Coro::current->{desc} = "ipo send"; |
223 |
|
|
|
224 |
elmex |
1.5 |
my $storage = cf::map::find ("/planes/IPO_storage"); |
225 |
|
|
unless ($storage) { |
226 |
|
|
$who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
227 |
|
|
return 1; |
228 |
|
|
} |
229 |
root |
1.10 |
$storage->load; |
230 |
root |
1.1 |
|
231 |
elmex |
1.5 |
my $cnt; |
232 |
|
|
for ($who->inv) { |
233 |
|
|
if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) { |
234 |
|
|
$_->name ("$1 F: $2 T: $arguments"); |
235 |
root |
1.11 |
$storage->insert ($_, 2, 2); |
236 |
|
|
$who->esrv_del_item ($_->count); |
237 |
elmex |
1.5 |
$cnt++; |
238 |
|
|
} |
239 |
root |
1.1 |
} |
240 |
|
|
|
241 |
elmex |
1.5 |
if ($cnt) { |
242 |
|
|
$who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n"); |
243 |
|
|
CFMail::send_mail (1, $arguments, $who->name, "You got $cnt packages from " . $who->name); |
244 |
|
|
notify_players ($arguments => 1); |
245 |
|
|
} else { |
246 |
|
|
$who->reply ($npc, "Sorry, found no package to send to $arguments."); |
247 |
|
|
} |
248 |
root |
1.1 |
} |
249 |
|
|
|
250 |
|
|
} else { |
251 |
|
|
$who->reply ($npc, |
252 |
|
|
sprintf "How can I help you?\n" |
253 |
|
|
. "Here is a quick list of commands I understand:\n\n" |
254 |
|
|
. "- pen (%s platinum)\n" |
255 |
|
|
. "- literacy (%s platinum)\n" |
256 |
|
|
. "- mailscroll <friend> (%s platinum)\n" |
257 |
|
|
. "- bag <friend> (%s platinum)\n" |
258 |
|
|
. "- package <friend> (%s platinum)\n" |
259 |
|
|
. "- carton <friend> (%s platinum)\n" |
260 |
|
|
. "- send <friend> (send bags/packages/cartons)\n" |
261 |
|
|
. "- receive (to receive packages for you)\n" |
262 |
|
|
. ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
263 |
|
|
40, 1000, 1, 1, 5, 10 |
264 |
|
|
); |
265 |
|
|
} |
266 |
|
|
1 |
267 |
|
|
}; |
268 |
|
|
|
269 |
|
|
package CFMail; |
270 |
|
|
|
271 |
|
|
use POSIX qw/strftime/; |
272 |
|
|
use CFDB; |
273 |
|
|
|
274 |
root |
1.6 |
my $MAILDB = CFDB->new (db_file => "$LOCALDIR/crossfiremail"); |
275 |
root |
1.1 |
|
276 |
|
|
sub get_mail { |
277 |
|
|
my ($toname) = @_; |
278 |
|
|
$MAILDB->get ($toname); |
279 |
|
|
} |
280 |
|
|
|
281 |
|
|
sub clear_mail { |
282 |
|
|
my ($toname) = @_; |
283 |
|
|
$MAILDB->clear ($toname); |
284 |
|
|
} |
285 |
|
|
|
286 |
|
|
sub store_mail { |
287 |
|
|
my ($type, $toname, $fromname, $message) = @_; |
288 |
|
|
my $mails = $MAILDB->get ($toname); |
289 |
|
|
push @$mails, [$type, $fromname, $message]; |
290 |
|
|
$MAILDB->set ($toname, $mails); |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
sub send_mail { |
294 |
|
|
my ($type, $toname, $fromname, $message) = @_; |
295 |
|
|
my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time)); |
296 |
|
|
my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n"; |
297 |
|
|
store_mail ($type, $toname, $fromname, $msg); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
1; |