… | |
… | |
72 | $map->insert_object ($o, $x, $y); |
72 | $map->insert_object ($o, $x, $y); |
73 | $r |
73 | $r |
74 | } |
74 | } |
75 | |
75 | |
76 | # 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 |
77 | sub on_login { |
77 | cf::attach_to_players |
|
|
78 | on_login => sub { |
78 | my ($pl) = @_; |
79 | my ($pl) = @_; |
79 | |
80 | |
80 | my $mails = CFMail::get_mail ($pl->ob->name); |
81 | my $mails = CFMail::get_mail ($pl->ob->name); |
81 | |
82 | |
82 | my $cnt = @{$mails || []}; |
83 | my $cnt = @{$mails || []}; |
83 | |
84 | |
84 | if ($cnt == 1) { |
85 | if ($cnt == 1) { |
85 | $pl->ob->message ("You got one mail."); |
86 | $pl->ob->message ("You got one mail."); |
86 | } elsif ($cnt > 1) { |
87 | } elsif ($cnt > 1) { |
87 | $pl->ob->message ("You got $cnt mails."); |
88 | $pl->ob->message ("You got $cnt mails."); |
88 | } else { |
89 | } else { |
89 | $pl->ob->message ("You haven't got any mail."); |
90 | $pl->ob->message ("You haven't got any mail."); |
|
|
91 | } |
90 | } |
92 | }, |
91 | |
93 | ; |
92 | 0 |
|
|
93 | } |
|
|
94 | |
94 | |
95 | # this event handler handles receiving of mails |
95 | # this event handler handles receiving of mails |
96 | sub on_apply { |
96 | cf::register_attachment ipo_mailbox => |
|
|
97 | on_apply => sub { |
97 | my ($ev, $box, $pl) = @_; |
98 | my ($box, $pl) = @_; |
98 | |
99 | |
99 | my $cnt; |
100 | my $cnt; |
100 | my $mails = CFMail::get_mail ($pl->name) || []; |
101 | my $mails = CFMail::get_mail ($pl->name) || []; |
101 | |
102 | |
102 | # count the mails that are in the container |
103 | # count the mails that are in the container |
103 | # 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 |
104 | # 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 |
105 | # twice. - This bug existed also with the old python plugin |
106 | # twice. - This bug existed also with the old python plugin |
106 | |
107 | |
107 | my $plname = $pl->name; |
108 | my $plname = $pl->name; |
108 | for ($box->inv) { |
109 | for ($box->inv) { |
109 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
110 | $_->name =~ /\S+ F: \S+ T: \Q$plname\E/ |
110 | and $cnt++; |
111 | and $cnt++; |
111 | } |
112 | } |
112 | |
113 | |
113 | for (@$mails) { |
114 | for (@$mails) { |
114 | my ($type, $from, $msg) = @$_; |
115 | my ($type, $from, $msg) = @$_; |
115 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
116 | $type = $mailtypes{$type || 1} || ['scroll', 'mailscroll']; |
116 | my $mail = cf::object::new $type->[0]; |
117 | my $mail = cf::object::new $type->[0]; |
117 | $mail->set_name ("$type->[1] F: $from T: " .$pl->name); |
118 | $mail->set_name ("$type->[1] F: $from T: " .$pl->name); |
118 | $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); |
119 | $mail->set_message ($msg); |
120 | $mail->set_message ($msg); |
120 | $mail->set_value (0); |
121 | $mail->set_value (0); |
121 | $mail->insert_in_ob ($box); |
122 | $mail->insert_in_ob ($box); |
122 | } |
123 | } |
123 | |
124 | |
124 | $cnt += @$mails; |
125 | $cnt += @$mails; |
125 | |
126 | |
126 | if ($cnt == 1) { |
127 | if ($cnt == 1) { |
127 | $pl->message ("You got one mail."); |
128 | $pl->message ("You got one mail."); |
128 | } elsif ($cnt > 1) { |
129 | } elsif ($cnt > 1) { |
129 | $pl->message ("You got $cnt mails."); |
130 | $pl->message ("You got $cnt mails."); |
130 | } else { |
131 | } else { |
131 | $pl->message ("You haven't got any mail."); |
132 | $pl->message ("You haven't got any mail."); |
132 | } |
133 | } |
133 | |
134 | |
134 | CFMail::clear_mail ($pl->name); |
135 | CFMail::clear_mail ($pl->name); |
135 | |
136 | }, |
136 | 0; |
|
|
137 | } |
|
|
138 | |
|
|
139 | # this event handler handles the sending of mails |
137 | # this event handler handles the sending of mails |
140 | sub on_close { |
138 | on_close => sub { |
141 | my ($ev, $box, $pl) = @_; |
139 | my ($box, $pl) = @_; |
142 | |
140 | |
143 | my @mails; |
141 | my @mails; |
144 | |
142 | |
145 | my %sent_targets; |
143 | my %sent_targets; |
146 | |
144 | |
147 | for ($box->inv) { |
145 | for ($box->inv) { |
148 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
146 | if ($_->name =~ m/^mail(scroll|warning) T: (\S+) F: (\S+)/) { |
149 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message); |
147 | CFMail::send_mail ($1 eq 'scroll' ? 1 : 3, $2, $3, $_->message); |
150 | $pl->message ("Sent mail$1 to $2 (from $3)."); |
148 | $pl->message ("Sent mail$1 to $2 (from $3)."); |
151 | $sent_targets{$2}++; |
149 | $sent_targets{$2}++; |
152 | push @mails, $_; |
150 | push @mails, $_; |
153 | |
151 | |
154 | } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
152 | } elsif ($_->name =~ m/^mail(scroll|warning) F: (\S+) T: (\S+)/) { |
155 | # 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 |
156 | CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message); |
154 | CFMail::store_mail ($1 eq 'scroll' ? 1 : 3, $3, $2, $_->message); |
157 | push @mails, $_; |
155 | push @mails, $_; |
158 | } |
156 | } |
159 | } |
157 | } |
160 | |
158 | |
161 | $_->remove for @mails; |
159 | $_->remove for @mails; |
162 | |
160 | |
163 | notify_players (%sent_targets); |
161 | notify_players (%sent_targets); |
164 | |
162 | }, |
165 | 0; |
163 | ; |
166 | } |
|
|
167 | |
164 | |
168 | # this is the main command interface for the IPO NPC |
165 | # this is the main command interface for the IPO NPC |
169 | cf::register_script_function "ipo::command" => sub { |
166 | cf::register_script_function "ipo::command" => sub { |
170 | my ($who, $msg, $npc) = @_; |
167 | my ($who, $msg, $npc) = @_; |
171 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
168 | my ($cmd, $arguments) = split /\s+/, $msg, 2; |
… | |
… | |
186 | 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); |
187 | $who->reply ($npc, "Here is your $cmd"); |
184 | $who->reply ($npc, "Here is your $cmd"); |
188 | } |
185 | } |
189 | |
186 | |
190 | } elsif ($cmd eq 'receive') { |
187 | } elsif ($cmd eq 'receive') { |
191 | my $storage = cf::map::get_map ("/planes/IPO_storage"); |
188 | my $storage = cf::map::find ("/planes/IPO_storage"); |
192 | unless ($storage) { |
189 | unless ($storage) { |
193 | $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."); |
194 | return 1; |
191 | return 1; |
195 | } |
192 | } |
196 | |
193 | |
… | |
… | |
213 | unless ($arguments =~ /^\S+$/) { |
210 | unless ($arguments =~ /^\S+$/) { |
214 | $who->reply ($npc, "Send to who?"); |
211 | $who->reply ($npc, "Send to who?"); |
215 | return 1; |
212 | return 1; |
216 | } |
213 | } |
217 | |
214 | |
218 | my $storage = cf::map::get_map ("/planes/IPO_storage"); |
215 | my $storage = cf::map::find ("/planes/IPO_storage"); |
219 | unless ($storage) { |
216 | unless ($storage) { |
220 | $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."); |
221 | return 1; |
218 | return 1; |
222 | } |
219 | } |
223 | |
220 | |
… | |
… | |
239 | } |
236 | } |
240 | |
237 | |
241 | } else { |
238 | } else { |
242 | $who->reply ($npc, |
239 | $who->reply ($npc, |
243 | sprintf "How can I help you?\n" |
240 | sprintf "How can I help you?\n" |
244 | ."Here is a quick list of commands I understand:\n\n" |
241 | . "Here is a quick list of commands I understand:\n\n" |
245 | ."- pen (%s platinum)\n" |
242 | . "- pen (%s platinum)\n" |
246 | ."- literacy (%s platinum)\n" |
243 | . "- literacy (%s platinum)\n" |
247 | ."- mailscroll <friend> (%s platinum)\n" |
244 | . "- mailscroll <friend> (%s platinum)\n" |
248 | ."- bag <friend> (%s platinum)\n" |
245 | . "- bag <friend> (%s platinum)\n" |
249 | ."- package <friend> (%s platinum)\n" |
246 | . "- package <friend> (%s platinum)\n" |
250 | ."- carton <friend> (%s platinum)\n" |
247 | . "- carton <friend> (%s platinum)\n" |
251 | ."- send <friend> (send bags/packages/cartons)\n" |
248 | . "- send <friend> (send bags/packages/cartons)\n" |
252 | ."- receive (to receive packages for you)\n" |
249 | . "- receive (to receive packages for you)\n" |
253 | .($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
250 | . ($who->flag (cf::FLAG_WIZ) ? "- mailwarning <player>" : ""), |
254 | 40, 1000, 1, 1, 5, 10 |
251 | 40, 1000, 1, 1, 5, 10 |
255 | ); |
252 | ); |
256 | } |
253 | } |
257 | 1 |
254 | 1 |
258 | }; |
255 | }; |
259 | |
256 | |
260 | package CFMail; |
257 | package CFMail; |
|
|
258 | |
261 | use POSIX qw/strftime/; |
259 | use POSIX qw/strftime/; |
262 | use CFDB; |
260 | use CFDB; |
263 | |
261 | |
264 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl"); |
262 | my $MAILDB = CFDB->new (db_file => cf::localdir . "/crossfiremail.perl"); |
265 | |
263 | |