ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/ipo.ext
Revision: 1.20
Committed: Tue May 10 11:01:45 2022 UTC (2 years ago) by root
Branch: MAIN
CVS Tags: HEAD
Changes since 1.19: +2 -2 lines
Log Message:
*** empty log message ***

File Contents

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