1 | #! perl |
1 | #! perl |
2 | |
|
|
3 | my $price_fact = 50; |
|
|
4 | |
2 | |
5 | sub set_package { |
3 | sub set_package { |
6 | my ($pkg, $from, $to, $bagname, $weight) = @_; |
4 | my ($pkg, $from, $to, $bagname, $weight) = @_; |
7 | |
5 | |
8 | $pkg->name ("$bagname T: $to F: $from"); |
6 | $pkg->name ("$bagname T: $to F: $from"); |
9 | $pkg->weight_limit ($weight); |
7 | $pkg->weight_limit ($weight); |
10 | $pkg->stats->Str (0); |
8 | $pkg->stats->Str (0); |
11 | } |
9 | } |
12 | |
10 | |
13 | # prices in plat. |
11 | # prices [in silver] |
14 | my %prices = ( |
12 | my %prices = ( |
15 | pen => [ |
13 | pen => [ |
16 | 40, 'stylus', |
14 | 10000, 'stylus', |
17 | sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (40 * $price_fact); } |
15 | sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (0); } |
18 | ], |
16 | ], |
19 | literacy => [ |
17 | literacy => [ |
20 | 1000, 'scroll_literacy', |
18 | 10000, 'scroll_literacy', |
21 | sub { $_[0]->value (1000 * $price_fact) } |
19 | sub { $_[0]->value (0) } |
22 | ], |
20 | ], |
23 | mailscroll => [ |
21 | mailscroll => [ |
24 | 1, 'scroll', |
22 | 50, 'mailscroll_empty', |
25 | sub { |
23 | sub { |
26 | $_[0]->name ("mailscroll T: $_[2] F: $_[1]"); |
24 | $_[0]->name ("mailscroll T: $_[2] F: $_[1]"); |
27 | $_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]"); |
25 | $_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]"); |
28 | $_[0]->value (1 * $price_fact); |
26 | $_[0]->value (0); |
29 | }, |
27 | }, |
30 | 'plarg' |
28 | 'plarg' |
31 | ], |
29 | ], |
32 | bag => [ 1, 'r_sack', sub { set_package (@_, bag => 5000) }, 'plarg' ], |
30 | bag => [ 100, 'r_sack', sub { set_package (@_, bag => 5000) }, 'plarg' ], |
33 | package => [ 5, 'r_sack', sub { set_package (@_, package => 50000) }, 'plarg' ], |
31 | package => [ 1000, 'r_sack', sub { set_package (@_, package => 50000) }, 'plarg' ], |
34 | carton => [10, 'r_sack', sub { set_package (@_, carton => 100000) }, 'plarg' ], |
32 | carton => [ 2000, 'r_sack', sub { set_package (@_, carton => 100000) }, 'plarg' ], |
35 | mailwarning => [ |
33 | mailwarning => [ |
36 | 0, 'diploma', |
34 | 0, 'diploma', |
37 | sub { |
35 | sub { |
38 | $_[0]->name ("mailwarning T: $_[2] F: $_[1]"); |
36 | $_[0]->name ("mailwarning T: $_[2] F: $_[1]"); |
39 | $_[0]->name_pl ("mailwarnings T: $_[2] F: $_[1]"); |
37 | $_[0]->name_pl ("mailwarnings T: $_[2] F: $_[1]"); |
… | |
… | |
68 | |
66 | |
69 | sub create_object { |
67 | sub create_object { |
70 | my ($name, $map, $x, $y, $cb, @a) = @_; |
68 | my ($name, $map, $x, $y, $cb, @a) = @_; |
71 | my $o = cf::object::new $name; |
69 | my $o = cf::object::new $name; |
72 | my $r = $cb->($o, @a); |
70 | my $r = $cb->($o, @a); |
73 | $map->insert_object ($o, $x, $y); |
71 | $map->insert ($o, $x, $y); |
74 | $r |
72 | $r |
75 | } |
73 | } |
76 | |
74 | |
77 | # this handler notifies the player of new mail |
75 | # this handler notifies the player of new mail |
78 | cf::player->attach ( |
76 | cf::player->attach ( |
79 | on_login => sub { |
77 | on_login => sub { |
80 | my ($pl) = @_; |
78 | my ($pl) = @_; |
81 | |
79 | |
82 | my $mails = CFMail::get_mail ($pl->ob->name); |
80 | my $cnt = @{ $pl->{ipo_mails} }; |
83 | |
|
|
84 | my $cnt = @{$mails || []}; |
|
|
85 | |
81 | |
86 | if ($cnt == 1) { |
82 | if ($cnt == 1) { |
87 | $pl->ob->message ("You got one mail."); |
83 | $pl->ob->message ("You got one mail."); |
88 | } elsif ($cnt > 1) { |
84 | } elsif ($cnt > 1) { |
89 | $pl->ob->message ("You got $cnt mails."); |
85 | $pl->ob->message ("You got $cnt mails."); |
… | |
… | |
94 | ); |
90 | ); |
95 | |
91 | |
96 | # this event handler handles receiving of mails |
92 | # this event handler handles receiving of mails |
97 | cf::object::attachment ipo_mailbox => |
93 | cf::object::attachment ipo_mailbox => |
98 | on_apply => sub { |
94 | on_apply => sub { |
99 | my ($box, $pl) = @_; |
95 | my ($box, $ob) = @_; |
100 | |
96 | |
101 | my $cnt; |
97 | my $cnt; |
102 | my $mails = CFMail::get_mail ($pl->name) || []; |
98 | my $mails = $ob->contr->{ipo_mails} || []; |
103 | |
99 | |
104 | # count the mails that are in the container |
100 | # count the mails that are in the container |
105 | # FIXME: the problem with on_apply is that it is called even when |
101 | # 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 |
102 | # 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 |
103 | # twice. - This bug existed also with the old python plugin |
108 | |
104 | |
109 | my $plname = $pl->name; |
105 | my $plname = $ob->name; |
|
|
106 | |
110 | for ($box->inv) { |
107 | for ($box->inv) { |
111 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
108 | ++$cnt if $_->name =~ /^mail(?:scroll|warning) F: \S+ T: \Q$plname\E/; |
112 | and $cnt++; |
|
|
113 | } |
109 | } |
114 | |
110 | |
115 | for (@$mails) { |
111 | for (@$mails) { |
116 | my ($type, $from, $msg) = @$_; |
112 | my ($type, $from, $msg) = @$_; |
117 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
113 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
118 | my $mail = cf::object::new $type->[0]; |
114 | my $mail = cf::object::new $type->[0]; |
119 | $mail->name ("$type->[1] F: $from T: " .$pl->name); |
115 | $mail->name ("$type->[1] F: $from T: $plname"); |
120 | $mail->name_pl ("$type->[1]s F: $from T: " .$pl->name); |
116 | $mail->name_pl ("$type->[1]s F: $from T: $plname"); |
121 | $mail->msg ($msg); |
117 | $mail->msg ($msg); |
122 | $mail->value (0); |
118 | $mail->value (0); |
123 | $box->insert ($mail); |
119 | $box->insert ($mail); |
124 | } |
120 | } |
125 | |
121 | |
126 | $cnt += @$mails; |
122 | $cnt += @$mails; |
127 | |
123 | |
128 | if ($cnt == 1) { |
124 | if ($cnt == 1) { |
129 | $pl->message ("You got one mail."); |
125 | $ob->message ("You got one mail."); |
130 | } elsif ($cnt > 1) { |
126 | } elsif ($cnt > 1) { |
131 | $pl->message ("You got $cnt mails."); |
127 | $ob->message ("You got $cnt mails."); |
132 | } else { |
128 | } else { |
133 | $pl->message ("You haven't got any mail."); |
129 | $ob->message ("You haven't got any mail."); |
134 | } |
130 | } |
135 | |
131 | |
136 | CFMail::clear_mail ($pl->name); |
132 | delete $ob->contr->{ipo_mails}; |
137 | }, |
133 | }, |
138 | # this event handler handles the sending of mails |
134 | # this event handler handles the sending of mails |
139 | on_close => sub { |
135 | on_close => sub { |
140 | my ($box, $pl) = @_; |
136 | my ($box, $ob) = @_; |
141 | |
137 | |
142 | my @mails; |
138 | my @mails = grep $_->name =~ /^mail(?:scroll|warning) [TF]: /, $box->inv; |
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; |
139 | $_->remove for @mails; |
161 | |
140 | |
|
|
141 | # we can lose mails here, when the player is unloadable and the server crashes. shit happens. |
|
|
142 | cf::async { |
|
|
143 | my %sent_targets; |
|
|
144 | |
|
|
145 | for (@mails) { |
|
|
146 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
|
|
147 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->msg); |
|
|
148 | $ob->message ("Sent mail$1 to $2 (from $3)."); |
|
|
149 | ++$sent_targets{$2}; |
|
|
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 | } |
|
|
155 | |
|
|
156 | $_->destroy; |
|
|
157 | } |
|
|
158 | |
162 | notify_players (%sent_targets); |
159 | notify_players (%sent_targets); |
|
|
160 | }; |
163 | }, |
161 | }, |
164 | ; |
162 | ; |
165 | |
163 | |
166 | # this is the main command interface for the IPO NPC |
164 | # this is the main command interface for the IPO NPC |
167 | cf::register_script_function "ipo::command" => sub { |
165 | cf::register_script_function "ipo::command" => sub { |
… | |
… | |
175 | if (my $pr = $prices{$cmd}) { |
173 | if (my $pr = $prices{$cmd}) { |
176 | if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) { |
174 | if ($cmd eq 'mailwarning' and !$who->flag (cf::FLAG_WIZ)) { |
177 | return 1; |
175 | return 1; |
178 | } |
176 | } |
179 | |
177 | |
180 | $who->pay_amount ($pr->[0] * $price_fact); |
178 | if ($who->pay_amount ($pr->[0])) { |
|
|
179 | cf::async { |
181 | if ($pr->[3] && not cf::player::exists $arguments) { |
180 | if ($pr->[3] && not cf::player::exists $arguments) { |
182 | $who->reply ($npc, "Sorry, there is no '$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 | } |
183 | } else { |
187 | } else { |
184 | create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments); |
188 | $who->reply ($npc, "Sorry, you don't have enough money."); |
185 | $who->reply ($npc, "Here is your $cmd"); |
|
|
186 | } |
189 | } |
187 | |
190 | |
188 | } elsif ($cmd eq 'receive') { |
191 | } elsif ($cmd eq 'receive') { |
189 | cf::async { |
192 | cf::async { |
|
|
193 | $Coro::current->{desc} = "ipo receive"; |
|
|
194 | |
190 | my $storage = cf::map::find ("/planes/IPO_storage"); |
195 | my $storage = cf::map::find ("/planes/IPO_storage"); |
191 | unless ($storage) { |
196 | unless ($storage) { |
192 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
197 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
193 | return 1; |
198 | return 1; |
194 | } |
199 | } |
|
|
200 | $storage->load; |
195 | |
201 | |
196 | my $plname = $who->name; |
202 | my $plname = $who->name; |
197 | my $cnt; |
203 | my $cnt; |
198 | for ($storage->at (2, 2)) { |
204 | for ($storage->at (2, 2)) { |
199 | if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) { |
205 | if ($_->name () =~ /^\S+ F: \S+ T: \Q$plname\E$/) { |
… | |
… | |
201 | $cnt++; |
207 | $cnt++; |
202 | } |
208 | } |
203 | } |
209 | } |
204 | |
210 | |
205 | if ($cnt) { |
211 | if ($cnt) { |
206 | $who->reply ($npc, $cnt == 1 ? "Here is your pakage." : "Here are your packages."); |
212 | $who->reply ($npc, $cnt == 1 ? "Here is your package." : "Here are your packages."); |
207 | } else { |
213 | } else { |
208 | $who->reply ($npc, "Sorry, no deliverys for you sir."); |
214 | $who->reply ($npc, "Sorry, no deliveries for you sir."); |
209 | } |
215 | } |
210 | } |
216 | } |
211 | |
217 | |
212 | } elsif ($cmd eq 'send') { |
218 | } elsif ($cmd eq 'send') { |
213 | unless ($arguments =~ /^\S+$/) { |
219 | unless ($arguments =~ /^\S+$/) { |
214 | $who->reply ($npc, "Send to who?"); |
220 | $who->reply ($npc, "Send to who?"); |
215 | return 1; |
221 | return 1; |
216 | } |
222 | } |
217 | |
223 | |
218 | cf::async { |
224 | cf::async { |
|
|
225 | $Coro::current->{desc} = "ipo send"; |
|
|
226 | |
219 | my $storage = cf::map::find ("/planes/IPO_storage"); |
227 | my $storage = cf::map::find ("/planes/IPO_storage"); |
220 | unless ($storage) { |
228 | unless ($storage) { |
221 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
229 | $who->reply ($npc, "Sorry, our package delivery service is currently on strike. Please come back later."); |
222 | return 1; |
230 | return 1; |
223 | } |
231 | } |
|
|
232 | $storage->load; |
224 | |
233 | |
225 | my $cnt; |
234 | my $cnt; |
226 | for ($who->inv) { |
235 | for ($who->inv) { |
227 | if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) { |
236 | if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) { |
228 | $_->name ("$1 F: $2 T: $arguments"); |
237 | $_->name ("$1 F: $2 T: $arguments"); |
229 | $_->teleport ($storage, 2, 2); |
238 | $storage->insert ($_, 2, 2); |
230 | $cnt++; |
239 | $cnt++; |
231 | } |
240 | } |
232 | } |
241 | } |
233 | |
242 | |
234 | if ($cnt) { |
243 | if ($cnt) { |
… | |
… | |
242 | |
251 | |
243 | } else { |
252 | } else { |
244 | $who->reply ($npc, |
253 | $who->reply ($npc, |
245 | sprintf "How can I help you?\n" |
254 | sprintf "How can I help you?\n" |
246 | . "Here is a quick list of commands I understand:\n\n" |
255 | . "Here is a quick list of commands I understand:\n\n" |
247 | . "- pen (%s platinum)\n" |
256 | . " - pen (%s)\n" |
248 | . "- literacy (%s platinum)\n" |
257 | . " - literacy (%s)\n" |
249 | . "- mailscroll <friend> (%s platinum)\n" |
258 | . " - mailscroll <friend> (%s)\n" |
250 | . "- bag <friend> (%s platinum)\n" |
259 | . " - bag <friend> (%s)\n" |
251 | . "- package <friend> (%s platinum)\n" |
260 | . " - package <friend> (%s)\n" |
252 | . "- carton <friend> (%s platinum)\n" |
261 | . " - carton <friend> (%s)\n" |
253 | . "- send <friend> (send bags/packages/cartons)\n" |
262 | . " - send <friend> (send bags/packages/cartons)\n" |
254 | . "- receive (to receive packages for you)\n" |
263 | . " - receive (to receive packages for you)\n" |
255 | . ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
264 | . ($who->flag (cf::FLAG_WIZ) ? " - mailwarning <player>\n" : ""), |
256 | 40, 1000, 1, 1, 5, 10 |
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]) |
257 | ); |
271 | ); |
258 | } |
272 | } |
259 | 1 |
273 | 1 |
260 | }; |
274 | }; |
261 | |
275 | |
262 | package CFMail; |
276 | package CFMail; |
263 | |
277 | |
264 | use POSIX qw/strftime/; |
278 | use POSIX qw/strftime/; |
265 | use CFDB; |
|
|
266 | |
279 | |
267 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail"); |
280 | rename "$cf::LOCALDIR/crossfiremail", "$cf::LOCALDIR/crossfiremail.is-now-on-player"; |
268 | |
|
|
269 | sub get_mail { |
|
|
270 | my ($toname) = @_; |
|
|
271 | $MAILDB->get ($toname); |
|
|
272 | } |
|
|
273 | |
|
|
274 | sub clear_mail { |
|
|
275 | my ($toname) = @_; |
|
|
276 | $MAILDB->clear ($toname); |
|
|
277 | } |
|
|
278 | |
281 | |
279 | sub store_mail { |
282 | sub store_mail { |
280 | my ($type, $toname, $fromname, $message) = @_; |
283 | my ($type, $toname, $fromname, $message) = @_; |
281 | my $mails = $MAILDB->get ($toname); |
284 | |
|
|
285 | my $pl = cf::player::find $toname |
|
|
286 | or return; |
|
|
287 | |
282 | push @$mails, [$type, $fromname, $message]; |
288 | push @{ $pl->{ipo_mails} }, [$type, $fromname, $message]; |
283 | $MAILDB->set ($toname, $mails); |
|
|
284 | } |
289 | } |
285 | |
290 | |
286 | sub send_mail { |
291 | sub send_mail { |
287 | my ($type, $toname, $fromname, $message) = @_; |
292 | my ($type, $toname, $fromname, $message) = @_; |
|
|
293 | |
288 | my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time)); |
294 | my $time = strftime ("%a, %d %b %Y %H:%M:%S UTC", gmtime AE::now); |
289 | my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n"; |
295 | my $msg = "From: $fromname\rTo: $toname\rDate: $time\n\n$message\n"; |
|
|
296 | |
290 | store_mail ($type, $toname, $fromname, $msg); |
297 | store_mail $type, $toname, $fromname, $msg; |
291 | } |
298 | } |
292 | |
299 | |
293 | 1; |
|
|