… | |
… | |
46 | 1 => ['scroll', 'mailscroll'], |
46 | 1 => ['scroll', 'mailscroll'], |
47 | 2 => ['note', 'newspaper'], |
47 | 2 => ['note', 'newspaper'], |
48 | 3 => ['diploma', 'mailwarning'], |
48 | 3 => ['diploma', 'mailwarning'], |
49 | ); |
49 | ); |
50 | |
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 | |
51 | sub create_object { |
68 | sub create_object { |
52 | my ($name, $map, $x, $y, $cb, @a) = @_; |
69 | my ($name, $map, $x, $y, $cb, @a) = @_; |
53 | my $o = cf::object::new $name; |
70 | my $o = cf::object::new $name; |
54 | my $r = $cb->($o, @a); |
71 | my $r = $cb->($o, @a); |
55 | $map->insert_object ($o, $x, $y); |
72 | $map->insert_object ($o, $x, $y); |
56 | $r |
73 | $r |
57 | } |
74 | } |
58 | |
75 | |
59 | # this handler handles to notice the player that he has got mail |
76 | # this handler handles to notice the player that he has got mail |
60 | sub on_login { |
77 | cf::attach_to_players |
|
|
78 | on_login => sub { |
61 | my ($pl, $host) = @_; |
79 | my ($pl) = @_; |
62 | |
80 | |
63 | my $mails = CFMail::get_mail ($pl->ob->name); |
81 | my $mails = CFMail::get_mail ($pl->ob->name); |
64 | |
82 | |
65 | my $cnt = @{$mails || []}; |
83 | my $cnt = @{$mails || []}; |
66 | |
84 | |
67 | if ($cnt == 1) { |
85 | if ($cnt == 1) { |
68 | $pl->ob->message ("You got one mail."); |
86 | $pl->ob->message ("You got one mail."); |
69 | } elsif ($cnt > 1) { |
87 | } elsif ($cnt > 1) { |
70 | $pl->ob->message ("You got $cnt mails."); |
88 | $pl->ob->message ("You got $cnt mails."); |
71 | } else { |
89 | } else { |
72 | $pl->ob->message ("You haven't got any mail."); |
90 | $pl->ob->message ("You haven't got any mail."); |
|
|
91 | } |
73 | } |
92 | }, |
74 | |
93 | ; |
75 | 0 |
|
|
76 | } |
|
|
77 | |
94 | |
78 | # this event handler handles receiving of mails |
95 | # this event handler handles receiving of mails |
79 | sub on_apply { |
96 | cf::register_attachment ipo_mailbox => |
|
|
97 | on_apply => sub { |
80 | my ($ev, $box, $pl) = @_; |
98 | my ($box, $pl) = @_; |
81 | |
99 | |
82 | my $cnt; |
100 | my $cnt; |
83 | my $mails = CFMail::get_mail ($pl->name) || []; |
101 | my $mails = CFMail::get_mail ($pl->name) || []; |
84 | |
102 | |
85 | # count the mails that are in the container |
103 | # count the mails that are in the container |
86 | # FIXME: the problem with on_apply is that it is called even when |
104 | # 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 |
105 | # 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 |
106 | # twice. - This bug existed also with the old python plugin |
89 | |
107 | |
90 | my $plname = $pl->name; |
108 | my $plname = $pl->name; |
91 | for ($box->inv) { |
109 | for ($box->inv) { |
92 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
110 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
93 | and $cnt++; |
111 | and $cnt++; |
94 | } |
112 | } |
95 | |
113 | |
96 | for (@$mails) { |
114 | for (@$mails) { |
97 | my ($type, $from, $msg) = @$_; |
115 | my ($type, $from, $msg) = @$_; |
98 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
116 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
99 | my $mail = cf::object::new $type->[0]; |
117 | my $mail = cf::object::new $type->[0]; |
100 | $mail->set_name ("$type->[1] F: $from T: " .$pl->name); |
118 | $mail->set_name ("$type->[1] F: $from T: " .$pl->name); |
101 | $mail->set_name_plural ("$type->[1]s F: $from T: " .$pl->name); |
119 | $mail->set_name_plural ("$type->[1]s F: $from T: " .$pl->name); |
102 | $mail->set_message ($msg); |
120 | $mail->set_message ($msg); |
103 | $mail->set_value (0); |
121 | $mail->set_value (0); |
104 | $mail->insert_in_ob ($box); |
122 | $mail->insert_in_ob ($box); |
105 | } |
123 | } |
106 | |
124 | |
107 | $cnt += @$mails; |
125 | $cnt += @$mails; |
108 | |
126 | |
109 | if ($cnt == 1) { |
127 | if ($cnt == 1) { |
110 | $pl->message ("You got one mail."); |
128 | $pl->message ("You got one mail."); |
111 | } elsif ($cnt > 1) { |
129 | } elsif ($cnt > 1) { |
112 | $pl->message ("You got $cnt mails."); |
130 | $pl->message ("You got $cnt mails."); |
113 | } else { |
131 | } else { |
114 | $pl->message ("You haven't got any mail."); |
132 | $pl->message ("You haven't got any mail."); |
115 | } |
133 | } |
116 | |
134 | |
117 | CFMail::clear_mail ($pl->name); |
135 | CFMail::clear_mail ($pl->name); |
118 | |
136 | }, |
119 | 0; |
|
|
120 | } |
|
|
121 | |
|
|
122 | # this event handler handles the sending of mails |
137 | # this event handler handles the sending of mails |
123 | sub on_close { |
138 | on_close => sub { |
124 | my ($ev, $box, $pl) = @_; |
139 | my ($box, $pl) = @_; |
125 | |
140 | |
126 | my @mails; |
141 | my @mails; |
127 | |
142 | |
128 | my %sent_targets; |
143 | my %sent_targets; |
129 | |
144 | |
130 | for ($box->inv) { |
145 | for ($box->inv) { |
131 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
146 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
132 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message); |
147 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message); |
133 | $pl->message ("Sent mail$1 to $2 (from $3)."); |
148 | $pl->message ("Sent mail$1 to $2 (from $3)."); |
134 | $sent_targets{$2}++; |
149 | $sent_targets{$2}++; |
135 | push @mails, $_; |
150 | push @mails, $_; |
136 | |
151 | |
137 | } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
152 | } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
138 | # this is for mails that remain in the queue for the player |
153 | # this is for mails that remain in the queue for the player |
139 | CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message); |
154 | CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message); |
140 | push @mails, $_; |
155 | push @mails, $_; |
141 | } |
156 | } |
142 | } |
157 | } |
143 | |
158 | |
144 | $_->remove for @mails; |
159 | $_->remove for @mails; |
145 | |
160 | |
146 | # lets message player ingame: this is a NEW feature from the perl IPO :-) |
161 | notify_players (%sent_targets); |
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 | } |
162 | }, |
158 | |
163 | ; |
159 | 0; |
|
|
160 | } |
|
|
161 | |
164 | |
162 | # this is the main command interface for the IPO NPC |
165 | # this is the main command interface for the IPO NPC |
163 | cf::register_script_function "ipo::command" => sub { |
166 | cf::register_script_function "ipo::command" => sub { |
164 | my ($who, $msg, $npc) = @_; |
167 | my ($who, $msg, $npc) = @_; |
165 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
168 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
… | |
… | |
180 | create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments); |
183 | create_object ($pr->[1], $who->map, $x, $y, $pr->[2], $who->name, $arguments); |
181 | $who->reply ($npc, "Here is your $cmd"); |
184 | $who->reply ($npc, "Here is your $cmd"); |
182 | } |
185 | } |
183 | |
186 | |
184 | } elsif ($cmd eq 'receive') { |
187 | } elsif ($cmd eq 'receive') { |
185 | my $storage = cf::map::get_map ("/planes/IPO_storage"); |
188 | my $storage = cf::map::find ("/planes/IPO_storage"); |
186 | unless ($storage) { |
189 | unless ($storage) { |
187 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
190 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
188 | return 1; |
191 | return 1; |
189 | } |
192 | } |
190 | |
193 | |
… | |
… | |
207 | unless ($arguments =~ /^\S+$/) { |
210 | unless ($arguments =~ /^\S+$/) { |
208 | $who->reply ($npc, "Send to who?"); |
211 | $who->reply ($npc, "Send to who?"); |
209 | return 1; |
212 | return 1; |
210 | } |
213 | } |
211 | |
214 | |
212 | my $storage = cf::map::get_map ("/planes/IPO_storage"); |
215 | my $storage = cf::map::find ("/planes/IPO_storage"); |
213 | unless ($storage) { |
216 | unless ($storage) { |
214 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
217 | $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later."); |
215 | return 1; |
218 | return 1; |
216 | } |
219 | } |
217 | |
220 | |
… | |
… | |
224 | } |
227 | } |
225 | } |
228 | } |
226 | |
229 | |
227 | if ($cnt) { |
230 | if ($cnt) { |
228 | $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); |
229 | } else { |
234 | } else { |
230 | $who->reply ($npc, "Sorry, found no package to send to $arguments."); |
235 | $who->reply ($npc, "Sorry, found no package to send to $arguments."); |
231 | } |
236 | } |
232 | |
237 | |
233 | } else { |
238 | } else { |
234 | $who->reply ($npc, |
239 | $who->reply ($npc, |
235 | sprintf "How can I help you?\n" |
240 | sprintf "How can I help you?\n" |
236 | ."Here is a quick list of commands I understand:\n\n" |
241 | . "Here is a quick list of commands I understand:\n\n" |
237 | ."- pen (%s platinum)\n" |
242 | . "- pen (%s platinum)\n" |
238 | ."- literacy (%s platinum)\n" |
243 | . "- literacy (%s platinum)\n" |
239 | ."- mailscroll <friend> (%s platinum)\n" |
244 | . "- mailscroll <friend> (%s platinum)\n" |
240 | ."- bag <friend> (%s platinum)\n" |
245 | . "- bag <friend> (%s platinum)\n" |
241 | ."- package <friend> (%s platinum)\n" |
246 | . "- package <friend> (%s platinum)\n" |
242 | ."- carton <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" |
243 | .($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
250 | . ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
244 | 40, 1000, 1, 1, 5, 10 |
251 | 40, 1000, 1, 1, 5, 10 |
245 | ); |
252 | ); |
246 | } |
253 | } |
247 | 1 |
254 | 1 |
248 | }; |
255 | }; |
249 | |
256 | |
250 | package CFMail; |
257 | package CFMail; |
|
|
258 | |
251 | use POSIX qw/strftime/; |
259 | use POSIX qw/strftime/; |
252 | use CFDB; |
260 | use CFDB; |
253 | |
261 | |
254 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl"); |
262 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl"); |
255 | |
263 | |