1 | #! perl |
1 | #! perl |
2 | |
2 | |
3 | my $price_fact = 50; |
3 | my $price_fact = 50; |
4 | |
4 | |
5 | sub set_package { |
5 | sub set_package { |
6 | my ($pkg, $to, $from, $bagname, $weight) = @_; |
6 | my ($pkg, $from, $to, $bagname, $weight) = @_; |
7 | $pkg->set_name ("$bagname T: $to F: $from"); |
7 | $pkg->set_name ("$bagname T: $to F: $from"); |
8 | $pkg->set_weight_limit ($weight); |
8 | $pkg->set_weight_limit ($weight); |
9 | $pkg->set_str (0); |
9 | $pkg->set_str (0); |
10 | } |
10 | } |
11 | |
11 | |
12 | # prices in plat. |
12 | # prices in plat. |
13 | my %prices = ( |
13 | my %prices = ( |
14 | pen => [ |
14 | pen => [ |
15 | 40, 'stylus', |
15 | 40, 'stylus', |
16 | sub { $_[0]->set_name ('IPO Writing Pen'); $_[0]->set_value (40 * $price_fact); }, 'plarg' |
16 | sub { $_[0]->set_name ('IPO Writing Pen'); $_[0]->set_value (40 * $price_fact); } |
17 | ], |
17 | ], |
18 | literacy => [ |
18 | literacy => [ |
19 | 1000, 'scroll_literacy', |
19 | 1000, 'scroll_literacy', |
20 | sub { $_[0]->set_value (1000 * $price_fact) } |
20 | sub { $_[0]->set_value (1000 * $price_fact) } |
21 | ], |
21 | ], |
… | |
… | |
40 | }, |
40 | }, |
41 | 'plarg' |
41 | 'plarg' |
42 | ], |
42 | ], |
43 | ); |
43 | ); |
44 | |
44 | |
|
|
45 | my %mailtypes = ( |
|
|
46 | 1 => ['scroll', 'mailscroll'], |
|
|
47 | 2 => ['note', 'newspaper'], |
|
|
48 | 3 => ['diploma', 'mailwarning'], |
|
|
49 | ); |
|
|
50 | |
|
|
51 | sub notify_players { |
|
|
52 | my (%sent_targets) = @_; |
|
|
53 | |
|
|
54 | # lets message player ingame: this is a NEW feature from the perl IPO :-) |
|
|
55 | for (keys %sent_targets) { |
|
|
56 | if (my $player = cf::player::find $_) { |
|
|
57 | my $cnt = $sent_targets{$_}; |
|
|
58 | |
|
|
59 | if ($cnt == 1) { |
|
|
60 | $player->ob->message ("You've got new mail."); |
|
|
61 | } else { |
|
|
62 | $player->ob->message ("You've got $cnt new mails."); |
|
|
63 | } |
|
|
64 | } |
|
|
65 | } |
|
|
66 | } |
|
|
67 | |
45 | sub create_object { |
68 | sub create_object { |
46 | my ($name, $map, $x, $y, $cb, @a) = @_; |
69 | my ($name, $map, $x, $y, $cb, @a) = @_; |
47 | my $o = cf::object::new $name; |
70 | my $o = cf::object::new $name; |
48 | my $r = $cb->($o, @a); |
71 | my $r = $cb->($o, @a); |
49 | $map->insert_object ($o, $x, $y); |
72 | $map->insert_object ($o, $x, $y); |
50 | $r |
73 | $r |
51 | } |
74 | } |
52 | |
75 | |
|
|
76 | # this handler handles to notice the player that he has got mail |
|
|
77 | cf::attach_to_players |
|
|
78 | on_login => sub { |
|
|
79 | my ($pl) = @_; |
|
|
80 | |
|
|
81 | my $mails = CFMail::get_mail ($pl->ob->name); |
|
|
82 | |
|
|
83 | my $cnt = @{$mails || []}; |
|
|
84 | |
|
|
85 | if ($cnt == 1) { |
|
|
86 | $pl->ob->message ("You got one mail."); |
|
|
87 | } elsif ($cnt > 1) { |
|
|
88 | $pl->ob->message ("You got $cnt mails."); |
|
|
89 | } else { |
|
|
90 | $pl->ob->message ("You haven't got any mail."); |
|
|
91 | } |
|
|
92 | }, |
|
|
93 | ; |
|
|
94 | |
|
|
95 | # this event handler handles receiving of mails |
|
|
96 | cf::register_attachment ipo_mailbox => |
|
|
97 | on_apply => sub { |
|
|
98 | my ($box, $pl) = @_; |
|
|
99 | |
|
|
100 | my $cnt; |
|
|
101 | my $mails = CFMail::get_mail ($pl->name) || []; |
|
|
102 | |
|
|
103 | # count the mails that are in the container |
|
|
104 | # FIXME: the problem with on_apply is that it is called even when |
|
|
105 | # the player closes the container. so we get a 'You have X mails.' message |
|
|
106 | # twice. - This bug existed also with the old python plugin |
|
|
107 | |
|
|
108 | my $plname = $pl->name; |
|
|
109 | for ($box->inv) { |
|
|
110 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
|
|
111 | and $cnt++; |
|
|
112 | } |
|
|
113 | |
|
|
114 | for (@$mails) { |
|
|
115 | my ($type, $from, $msg) = @$_; |
|
|
116 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
|
|
117 | my $mail = cf::object::new $type->[0]; |
|
|
118 | $mail->set_name ("$type->[1] F: $from T: " .$pl->name); |
|
|
119 | $mail->set_name_plural ("$type->[1]s F: $from T: " .$pl->name); |
|
|
120 | $mail->set_message ($msg); |
|
|
121 | $mail->set_value (0); |
|
|
122 | $mail->insert_in_ob ($box); |
|
|
123 | } |
|
|
124 | |
|
|
125 | $cnt += @$mails; |
|
|
126 | |
|
|
127 | if ($cnt == 1) { |
|
|
128 | $pl->message ("You got one mail."); |
|
|
129 | } elsif ($cnt > 1) { |
|
|
130 | $pl->message ("You got $cnt mails."); |
|
|
131 | } else { |
|
|
132 | $pl->message ("You haven't got any mail."); |
|
|
133 | } |
|
|
134 | |
|
|
135 | CFMail::clear_mail ($pl->name); |
|
|
136 | }, |
|
|
137 | # this event handler handles the sending of mails |
|
|
138 | on_close => sub { |
|
|
139 | my ($box, $pl) = @_; |
|
|
140 | |
|
|
141 | my @mails; |
|
|
142 | |
|
|
143 | my %sent_targets; |
|
|
144 | |
|
|
145 | for ($box->inv) { |
|
|
146 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
|
|
147 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message); |
|
|
148 | $pl->message ("Sent mail$1 to $2 (from $3)."); |
|
|
149 | $sent_targets{$2}++; |
|
|
150 | push @mails, $_; |
|
|
151 | |
|
|
152 | } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
|
|
153 | # this is for mails that remain in the queue for the player |
|
|
154 | CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message); |
|
|
155 | push @mails, $_; |
|
|
156 | } |
|
|
157 | } |
|
|
158 | |
|
|
159 | $_->remove for @mails; |
|
|
160 | |
|
|
161 | notify_players (%sent_targets); |
|
|
162 | }, |
|
|
163 | ; |
|
|
164 | |
|
|
165 | # this is the main command interface for the IPO NPC |
53 | cf::register_script_function "ipo::command" => sub { |
166 | cf::register_script_function "ipo::command" => sub { |
54 | my ($who, $msg, $npc) = @_; |
167 | my ($who, $msg, $npc) = @_; |
55 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
168 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
56 | $cmd = lc $cmd; |
169 | $cmd = lc $cmd; |
57 | |
170 | |
… | |
… | |
105 | return 1; |
218 | return 1; |
106 | } |
219 | } |
107 | |
220 | |
108 | my $cnt; |
221 | my $cnt; |
109 | for ($who->inv) { |
222 | for ($who->inv) { |
110 | if ($_->name () =~ /^\S+ T: \Q$arguments\E F: (\S+)$/) { |
223 | if ($_->name () =~ /^(bag|package|carton) T: \Q$arguments\E F: (\S+)$/) { |
111 | $_->set_name ("$1 F: $2 T: $arguments"); |
224 | $_->set_name ("$1 F: $2 T: $arguments"); |
112 | $_->teleport ($storage, 2, 2); |
225 | $_->teleport ($storage, 2, 2); |
113 | $cnt++; |
226 | $cnt++; |
114 | } |
227 | } |
115 | } |
228 | } |
116 | |
229 | |
117 | if ($cnt) { |
230 | if ($cnt) { |
118 | $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n"); |
231 | $who->reply ($npc, $cnt == 1 ? "Package sent to $arguments." : "Sent $cnt packages to $arguments\n"); |
|
|
232 | CFMail::send_mail (1, $arguments, $who->name, "You got $cnt packages from " . $who->name); |
|
|
233 | notify_players ($arguments => 1); |
119 | } else { |
234 | } else { |
120 | $who->reply ($npc, "Sorry, found no package to send to $arguments."); |
235 | $who->reply ($npc, "Sorry, found no package to send to $arguments."); |
121 | } |
236 | } |
122 | |
237 | |
123 | } else { |
238 | } else { |
124 | $who->reply ($npc, |
239 | $who->reply ($npc, |
125 | "How can I help you?\n" |
240 | sprintf "How can I help you?\n" |
126 | ."Here is a quick list of commands I understand:\n\n" |
241 | . "Here is a quick list of commands I understand:\n\n" |
127 | ."- pen (%s platinum)\n" |
242 | . "- pen (%s platinum)\n" |
128 | ."- literacy (%s platinum)\n" |
243 | . "- literacy (%s platinum)\n" |
129 | ."- mailscroll <friend> (%s platinum)\n" |
244 | . "- mailscroll <friend> (%s platinum)\n" |
130 | ."- bag <friend> (%s platinum)\n" |
245 | . "- bag <friend> (%s platinum)\n" |
131 | ."- package <friend> (%s platinum)\n" |
246 | . "- package <friend> (%s platinum)\n" |
|
|
247 | . "- carton <friend> (%s platinum)\n" |
|
|
248 | . "- send <friend> (send bags/packages/cartons)\n" |
|
|
249 | . "- receive (to receive packages for you)\n" |
132 | .($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : "") |
250 | . ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
|
|
251 | 40, 1000, 1, 1, 5, 10 |
133 | ); |
252 | ); |
134 | } |
253 | } |
135 | 1 |
254 | 1 |
|
|
255 | }; |
|
|
256 | |
|
|
257 | package CFMail; |
|
|
258 | |
|
|
259 | use POSIX qw/strftime/; |
|
|
260 | use CFDB; |
|
|
261 | |
|
|
262 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl"); |
|
|
263 | |
|
|
264 | sub get_mail { |
|
|
265 | my ($toname) = @_; |
|
|
266 | $MAILDB->get ($toname); |
136 | } |
267 | } |
|
|
268 | |
|
|
269 | sub clear_mail { |
|
|
270 | my ($toname) = @_; |
|
|
271 | $MAILDB->clear ($toname); |
|
|
272 | } |
|
|
273 | |
|
|
274 | sub store_mail { |
|
|
275 | my ($type, $toname, $fromname, $message) = @_; |
|
|
276 | my $mails = $MAILDB->get ($toname); |
|
|
277 | push @$mails, [$type, $fromname, $message]; |
|
|
278 | $MAILDB->set ($toname, $mails); |
|
|
279 | } |
|
|
280 | |
|
|
281 | sub send_mail { |
|
|
282 | my ($type, $toname, $fromname, $message) = @_; |
|
|
283 | my $time = strftime ("%a, %d %b %Y %H:%M:%S CEST", localtime (time)); |
|
|
284 | my $msg = "From: $fromname\nTo: $toname\nDate: $time\n\n$message\n"; |
|
|
285 | store_mail ($type, $toname, $fromname, $msg); |
|
|
286 | } |
|
|
287 | |
|
|
288 | 1; |