ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/lib/cf.pm
(Generate patch)

Comparing deliantra/server/lib/cf.pm (file contents):
Revision 1.287 by root, Mon Jun 25 05:43:45 2007 UTC vs.
Revision 1.297 by root, Thu Jul 5 08:10:30 2007 UTC

20use Coro::Signal; 20use Coro::Signal;
21use Coro::Semaphore; 21use Coro::Semaphore;
22use Coro::AIO; 22use Coro::AIO;
23use Coro::Storable; 23use Coro::Storable;
24 24
25use JSON::XS 1.4 ();
25use BDB (); 26use BDB ();
26use Data::Dumper; 27use Data::Dumper;
27use Digest::MD5; 28use Digest::MD5;
28use Fcntl; 29use Fcntl;
29use YAML::Syck (); 30use YAML::Syck ();
237 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge; 238 $d =~ s/([\x00-\x07\x09\x0b\x0c\x0e-\x1f])/sprintf "\\x%02x", ord($1)/ge;
238 $d 239 $d
239 } || "[unable to dump $_[0]: '$@']"; 240 } || "[unable to dump $_[0]: '$@']";
240} 241}
241 242
242use JSON::XS ();
243
244=item $ref = cf::from_json $json 243=item $ref = cf::from_json $json
245 244
246Converts a JSON string into the corresponding perl data structure. 245Converts a JSON string into the corresponding perl data structure.
247 246
248=item $json = cf::to_json $ref 247=item $json = cf::to_json $ref
249 248
250Converts a perl data structure into its JSON representation. 249Converts a perl data structure into its JSON representation.
251 250
252=cut 251=cut
253 252
254our $json_coder = JSON::XS->new->convert_blessed->utf8; 253our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
255 254
256sub to_json ($) { $json_coder->encode ($_[0]) } 255sub to_json ($) { $json_coder->encode ($_[0]) }
257sub from_json ($) { $json_coder->decode ($_[0]) } 256sub from_json ($) { $json_coder->decode ($_[0]) }
258 257
259=item cf::lock_wait $string 258=item cf::lock_wait $string
1087 cf::override; 1086 cf::override;
1088 }, 1087 },
1089 on_extcmd => sub { 1088 on_extcmd => sub {
1090 my ($pl, $buf) = @_; 1089 my ($pl, $buf) = @_;
1091 1090
1092 my $msg = eval { from_json $buf }; 1091 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1093 1092
1094 if (ref $msg) { 1093 if (ref $msg) {
1095 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
1096 if (my %reply = $cb->($pl, $msg)) { 1095 if (my %reply = $cb->($pl, $msg)) {
1097 $pl->ext_reply ($msg->{msgid}, %reply); 1096 $pl->ext_reply ($msg->{msgid}, %reply);
1401 # replace G<male|female> tags 1400 # replace G<male|female> tags
1402 || s{G<([^>|]*)\|([^>]*)>}{ 1401 || s{G<([^>|]*)\|([^>]*)>}{
1403 $self->gender ? $2 : $1 1402 $self->gender ? $2 : $1
1404 }ge 1403 }ge
1405 # replace H<hint text> 1404 # replace H<hint text>
1406 || s/H<([^\>]*)>/<fg name="lightblue">[$1]<\/fg>/g; 1405 || s{H<([^\>]*)>}
1406 {
1407 ("<fg name=\"lightblue\">[$1 (Use hintmode to suppress hints)]</fg>",
1408 "<fg name=\"lightblue\">[Hint suppressed, see hintmode]</fg>",
1409 "")
1410 [$self->{hintmode}]
1411 }ge;
1407 1412
1408 # create single paragraphs (very hackish) 1413 # create single paragraphs (very hackish)
1409 s/(?<=\S)\n(?=\w)/ /g; 1414 s/(?<=\S)\n(?=\w)/ /g;
1410 1415
1416 # compress some whitespace
1417 s/\s+\n/\n/g; # ws line-ends
1418 s/\n\n+/\n/g; # double lines
1419 s/^\n+//; # beginning lines
1420 s/\n+$//; # ending lines
1421
1411 $_ 1422 $_
1423}
1424
1425sub hintmode {
1426 $_[0]{hintmode} = $_[1] if @_ > 1;
1427 $_[0]{hintmode}
1412} 1428}
1413 1429
1414=item $player->ext_reply ($msgid, %msg) 1430=item $player->ext_reply ($msgid, %msg)
1415 1431
1416Sends an ext reply to the player. 1432Sends an ext reply to the player.
1419 1435
1420sub ext_reply($$%) { 1436sub ext_reply($$%) {
1421 my ($self, $id, %msg) = @_; 1437 my ($self, $id, %msg) = @_;
1422 1438
1423 $msg{msgid} = $id; 1439 $msg{msgid} = $id;
1424 $self->send ("ext " . cf::to_json \%msg); 1440 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1425} 1441}
1426 1442
1427=item $player->ext_event ($type, %msg) 1443=item $player->ext_event ($type, %msg)
1428 1444
1429Sends an ext event to the client. 1445Sends an ext event to the client.
2341 my $hp = $exit->stats->hp; 2357 my $hp = $exit->stats->hp;
2342 my $sp = $exit->stats->sp; 2358 my $sp = $exit->stats->sp;
2343 2359
2344 $self->enter_link; 2360 $self->enter_link;
2345 2361
2362 # if exit is damned, update players death & WoR home-position
2363 $self->contr->savebed ($slaying, $hp, $sp)
2364 if $exit->flag (FLAG_DAMNED);
2365
2346 (async { 2366 (async {
2347 $self->deactivate_recursive; # just to be sure 2367 $self->deactivate_recursive; # just to be sure
2348 unless (eval { 2368 unless (eval {
2349 $self->goto ($slaying, $hp, $sp); 2369 $self->goto ($slaying, $hp, $sp);
2350 2370
2391sub cf::client::send_msg { 2411sub cf::client::send_msg {
2392 my ($self, $color, $type, $msg, @extra) = @_; 2412 my ($self, $color, $type, $msg, @extra) = @_;
2393 2413
2394 $msg = $self->pl->expand_cfpod ($msg); 2414 $msg = $self->pl->expand_cfpod ($msg);
2395 2415
2416 return unless @extra || length $msg;
2417
2396 if ($self->can_msg) { 2418 if ($self->can_msg) {
2397 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2419 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2398 } else { 2420 } else {
2399 # replace some tags by gcfclient-compatible ones 2421 # replace some tags by gcfclient-compatible ones
2400 for ($msg) { 2422 for ($msg) {
2401 1 while 2423 1 while
2402 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2424 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2425 2447
2426sub cf::client::ext_event($$%) { 2448sub cf::client::ext_event($$%) {
2427 my ($self, $type, %msg) = @_; 2449 my ($self, $type, %msg) = @_;
2428 2450
2429 $msg{msgtype} = "event_$type"; 2451 $msg{msgtype} = "event_$type";
2430 $self->send_packet ("ext " . cf::to_json \%msg); 2452 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2431} 2453}
2432 2454
2433=item $success = $client->query ($flags, "text", \&cb) 2455=item $success = $client->query ($flags, "text", \&cb)
2434 2456
2435Queues a query to the client, calling the given callback with 2457Queues a query to the client, calling the given callback with
2458 2480
2459 1 2481 1
2460} 2482}
2461 2483
2462cf::client->attach ( 2484cf::client->attach (
2485 on_connect => sub {
2486 my ($ns) = @_;
2487
2488 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2489 },
2463 on_reply => sub { 2490 on_reply => sub {
2464 my ($ns, $msg) = @_; 2491 my ($ns, $msg) = @_;
2465 2492
2466 # this weird shuffling is so that direct followup queries 2493 # this weird shuffling is so that direct followup queries
2467 # get handled first 2494 # get handled first
2482 } 2509 }
2483 }, 2510 },
2484 on_exticmd => sub { 2511 on_exticmd => sub {
2485 my ($ns, $buf) = @_; 2512 my ($ns, $buf) = @_;
2486 2513
2487 my $msg = eval { from_json $buf }; 2514 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2488 2515
2489 if (ref $msg) { 2516 if (ref $msg) {
2490 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2517 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2491 if (my %reply = $cb->($ns, $msg)) { 2518 if (my %reply = $cb->($ns, $msg)) {
2492 $reply{msgid} = $msg->{msgid}; 2519 $reply{msgid} = $msg->{msgid};
2493 $ns->send ("ext " . cf::to_json \%reply); 2520 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2494 } 2521 }
2495 } 2522 }
2496 } else { 2523 } else {
2497 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2524 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2498 } 2525 }
2555 2582
2556=pod 2583=pod
2557 2584
2558The following functions and methods are available within a safe environment: 2585The following functions and methods are available within a safe environment:
2559 2586
2560 cf::object contr pay_amount pay_player map 2587 cf::object
2588 contr pay_amount pay_player map x y force_find force_add
2589 insert remove
2590
2561 cf::object::player player 2591 cf::object::player
2562 cf::player peaceful 2592 player
2563 cf::map trigger 2593
2594 cf::player
2595 peaceful
2596
2597 cf::map
2598 trigger
2564 2599
2565=cut 2600=cut
2566 2601
2567for ( 2602for (
2568 ["cf::object" => qw(contr pay_amount pay_player map)], 2603 ["cf::object" => qw(contr pay_amount pay_player map force_find force_add x y
2604 insert remove)],
2569 ["cf::object::player" => qw(player)], 2605 ["cf::object::player" => qw(player)],
2570 ["cf::player" => qw(peaceful)], 2606 ["cf::player" => qw(peaceful)],
2571 ["cf::map" => qw(trigger)], 2607 ["cf::map" => qw(trigger)],
2572) { 2608) {
2573 no strict 'refs'; 2609 no strict 'refs';
2709 load_facedata "$DATADIR/facedata" 2745 load_facedata "$DATADIR/facedata"
2710 or die "unable to load facedata\n"; 2746 or die "unable to load facedata\n";
2711} 2747}
2712 2748
2713sub reload_archetypes { 2749sub reload_archetypes {
2750 load_resource_file "$DATADIR/archetypes"
2751 or die "unable to load archetypes\n";
2752 #d# NEED to laod twice to resolve forward references
2753 # this really needs to be done in an extra post-pass
2754 # (which needs to be synchronous, so solve it differently)
2714 load_resource_file "$DATADIR/archetypes" 2755 load_resource_file "$DATADIR/archetypes"
2715 or die "unable to load archetypes\n"; 2756 or die "unable to load archetypes\n";
2716} 2757}
2717 2758
2718sub reload_treasures { 2759sub reload_treasures {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines