ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/ipo.ext
Revision: 1.17
Committed: Sat Jan 30 23:30:26 2010 UTC (14 years, 3 months ago) by root
Branch: MAIN
CVS Tags: rel-2_93
Changes since 1.16: +43 -50 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.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 sf-marcmagus 1.16 # prices [in silver]
12 root 1.1 my %prices = (
13     pen => [
14 sf-marcmagus 1.16 10000, 'stylus',
15 elmex 1.12 sub { $_[0]->name ('IPO Writing Pen'); $_[0]->value (0); }
16 root 1.1 ],
17     literacy => [
18 sf-marcmagus 1.16 10000, 'scroll_literacy',
19 elmex 1.12 sub { $_[0]->value (0) }
20 root 1.1 ],
21     mailscroll => [
22 sf-marcmagus 1.16 50, 'mailscroll_empty',
23 root 1.1 sub {
24     $_[0]->name ("mailscroll T: $_[2] F: $_[1]");
25     $_[0]->name_pl ("mailscrolls T: $_[2] F: $_[1]");
26 elmex 1.12 $_[0]->value (0);
27 root 1.1 },
28     'plarg'
29     ],
30 sf-marcmagus 1.16 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 root 1.1 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 root 1.4 if (my $player = cf::player::find_active $_) {
56 root 1.1 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 root 1.11 $map->insert ($o, $x, $y);
72 root 1.1 $r
73     }
74    
75 root 1.2 # this handler notifies the player of new mail
76     cf::player->attach (
77 root 1.1 on_login => sub {
78     my ($pl) = @_;
79    
80 root 1.17 my $cnt = @{ $pl->{ip_mails} };
81 root 1.1
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 root 1.2 );
91 root 1.1
92     # this event handler handles receiving of mails
93 root 1.2 cf::object::attachment ipo_mailbox =>
94 root 1.1 on_apply => sub {
95 root 1.17 my ($box, $ob) = @_;
96 root 1.1
97     my $cnt;
98 root 1.17 my $mails = $ob->contr->{ipo_mails} || [];
99 root 1.1
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 root 1.17 my $plname = $ob->name;
106    
107 root 1.1 for ($box->inv) {
108 root 1.17 ++$cnt if $_->name =~ /^mail(?:scroll|warning) F: \S+ T: \Q$plname\E/;
109 root 1.1 }
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 root 1.17 $mail->name ("$type->[1] F: $from T: $plname");
116     $mail->name_pl ("$type->[1]s F: $from T: $plname");
117 root 1.1 $mail->msg ($msg);
118     $mail->value (0);
119 elmex 1.5 $box->insert ($mail);
120 root 1.1 }
121    
122     $cnt += @$mails;
123    
124     if ($cnt == 1) {
125 root 1.17 $ob->message ("You got one mail.");
126 root 1.1 } elsif ($cnt > 1) {
127 root 1.17 $ob->message ("You got $cnt mails.");
128 root 1.1 } else {
129 root 1.17 $ob->message ("You haven't got any mail.");
130 root 1.1 }
131    
132 root 1.17 delete $ob->contr->{ipo_mails};
133 root 1.1 },
134     # this event handler handles the sending of mails
135     on_close => sub {
136 root 1.17 my ($box, $ob) = @_;
137 root 1.1
138 root 1.17 my @mails = grep $_->name =~ /^mail(?:scroll|warning) [TF]: /, $box->inv;
139     $_->remove for @mails;
140 root 1.1
141 root 1.17 # we can lose mails here, when the player is unloadable and the server crashes. shit happens.
142     cf::async {
143     my %sent_targets;
144 root 1.1
145 root 1.17 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 root 1.1 }
158    
159 root 1.17 notify_players (%sent_targets);
160     };
161 root 1.1 },
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 root 1.4 my $pl = cf::player::find_active $who->name;
171 root 1.1 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 sf-marcmagus 1.16 if ($who->pay_amount ($pr->[0])) {
179 elmex 1.14 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 elmex 1.12 }
187 root 1.1 } else {
188 elmex 1.12 $who->reply ($npc, "Sorry, you don't have enough money.");
189 root 1.1 }
190    
191     } elsif ($cmd eq 'receive') {
192 elmex 1.5 cf::async {
193 root 1.9 $Coro::current->{desc} = "ipo receive";
194    
195 elmex 1.5 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 root 1.10 $storage->load;
201 root 1.1
202 elmex 1.5 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 root 1.1 }
210    
211 elmex 1.5 if ($cnt) {
212 root 1.7 $who->reply ($npc, $cnt == 1 ? "Here is your package." : "Here are your packages.");
213 elmex 1.5 } else {
214 root 1.7 $who->reply ($npc, "Sorry, no deliveries for you sir.");
215 elmex 1.5 }
216 root 1.1 }
217    
218     } elsif ($cmd eq 'send') {
219     unless ($arguments =~ /^\S+$/) {
220     $who->reply ($npc, "Send to who?");
221     return 1;
222     }
223    
224 elmex 1.5 cf::async {
225 root 1.9 $Coro::current->{desc} = "ipo send";
226    
227 elmex 1.5 my $storage = cf::map::find ("/planes/IPO_storage");
228     unless ($storage) {
229     $who->reply ($npc, "Sorry, our package delivery service is currently in strike. Please come back later.");
230     return 1;
231     }
232 root 1.10 $storage->load;
233 root 1.1
234 elmex 1.5 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 root 1.11 $storage->insert ($_, 2, 2);
239 elmex 1.5 $cnt++;
240     }
241 root 1.1 }
242    
243 elmex 1.5 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 root 1.1 }
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 sf-marcmagus 1.16 . " - 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 root 1.15 . " - 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 sf-marcmagus 1.16 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 root 1.1 );
272     }
273     1
274     };
275    
276     package CFMail;
277    
278     use POSIX qw/strftime/;
279    
280 root 1.17 rename "$cf::LOCALDIR/crossfiremail", "$cf::LOCALDIR/crossfiremail.is-now-on-player";
281 root 1.1
282 root 1.17 sub store_mail {
283     my ($type, $toname, $fromname, $message) = @_;
284 root 1.1
285 root 1.17 my $pl = cf::player::find $toname
286     or return;
287 root 1.1
288 root 1.17 push @{ $pl->{ipo_mails} }, [$type, $fromname, $message];
289 root 1.1 }
290    
291     sub send_mail {
292     my ($type, $toname, $fromname, $message) = @_;
293 root 1.17
294     my $time = strftime ("%a, %d %b %Y %H:%M:%S UTC", gmtime EV::now);
295 root 1.15 my $msg = "From: $fromname\rTo: $toname\rDate: $time\n\n$message\n";
296 root 1.17
297     store_mail $type, $toname, $fromname, $msg;
298 root 1.1 }
299    
300 root 1.17 1
301 root 1.15