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.289 by root, Sat Jun 30 03:00:54 2007 UTC vs.
Revision 1.295 by root, Wed Jul 4 05:08:15 2007 UTC

248 248
249Converts a perl data structure into its JSON representation. 249Converts a perl data structure into its JSON representation.
250 250
251=cut 251=cut
252 252
253our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max 253our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max
254 254
255sub to_json ($) { $json_coder->encode ($_[0]) } 255sub to_json ($) { $json_coder->encode ($_[0]) }
256sub from_json ($) { $json_coder->decode ($_[0]) } 256sub from_json ($) { $json_coder->decode ($_[0]) }
257 257
258=item cf::lock_wait $string 258=item cf::lock_wait $string
1086 cf::override; 1086 cf::override;
1087 }, 1087 },
1088 on_extcmd => sub { 1088 on_extcmd => sub {
1089 my ($pl, $buf) = @_; 1089 my ($pl, $buf) = @_;
1090 1090
1091 my $msg = eval { from_json $buf }; 1091 my $msg = eval { $pl->ns->{json_coder}->decode ($buf) };
1092 1092
1093 if (ref $msg) { 1093 if (ref $msg) {
1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) { 1094 if (my $cb = $EXTCMD{$msg->{msgtype}}) {
1095 if (my %reply = $cb->($pl, $msg)) { 1095 if (my %reply = $cb->($pl, $msg)) {
1096 $pl->ext_reply ($msg->{msgid}, %reply); 1096 $pl->ext_reply ($msg->{msgid}, %reply);
1400 # replace G<male|female> tags 1400 # replace G<male|female> tags
1401 || s{G<([^>|]*)\|([^>]*)>}{ 1401 || s{G<([^>|]*)\|([^>]*)>}{
1402 $self->gender ? $2 : $1 1402 $self->gender ? $2 : $1
1403 }ge 1403 }ge
1404 # replace H<hint text> 1404 # replace H<hint text>
1405 || 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;
1406 1412
1407 # create single paragraphs (very hackish) 1413 # create single paragraphs (very hackish)
1408 s/(?<=\S)\n(?=\w)/ /g; 1414 s/(?<=\S)\n(?=\w)/ /g;
1409 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
1410 $_ 1422 $_
1423}
1424
1425sub hintmode {
1426 $_[0]{hintmode} = $_[1] if @_ > 1;
1427 $_[0]{hintmode}
1411} 1428}
1412 1429
1413=item $player->ext_reply ($msgid, %msg) 1430=item $player->ext_reply ($msgid, %msg)
1414 1431
1415Sends an ext reply to the player. 1432Sends an ext reply to the player.
1418 1435
1419sub ext_reply($$%) { 1436sub ext_reply($$%) {
1420 my ($self, $id, %msg) = @_; 1437 my ($self, $id, %msg) = @_;
1421 1438
1422 $msg{msgid} = $id; 1439 $msg{msgid} = $id;
1423 $self->send ("ext " . cf::to_json \%msg); 1440 $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg));
1424} 1441}
1425 1442
1426=item $player->ext_event ($type, %msg) 1443=item $player->ext_event ($type, %msg)
1427 1444
1428Sends an ext event to the client. 1445Sends an ext event to the client.
2390sub cf::client::send_msg { 2407sub cf::client::send_msg {
2391 my ($self, $color, $type, $msg, @extra) = @_; 2408 my ($self, $color, $type, $msg, @extra) = @_;
2392 2409
2393 $msg = $self->pl->expand_cfpod ($msg); 2410 $msg = $self->pl->expand_cfpod ($msg);
2394 2411
2412 return unless @extra || length $msg;
2413
2395 if ($self->can_msg) { 2414 if ($self->can_msg) {
2396 $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); 2415 $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra]));
2397 } else { 2416 } else {
2398 # replace some tags by gcfclient-compatible ones 2417 # replace some tags by gcfclient-compatible ones
2399 for ($msg) { 2418 for ($msg) {
2400 1 while 2419 1 while
2401 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ 2420 s/<b>([^<]*)<\/b>/[b]${1}[\/b]/
2424 2443
2425sub cf::client::ext_event($$%) { 2444sub cf::client::ext_event($$%) {
2426 my ($self, $type, %msg) = @_; 2445 my ($self, $type, %msg) = @_;
2427 2446
2428 $msg{msgtype} = "event_$type"; 2447 $msg{msgtype} = "event_$type";
2429 $self->send_packet ("ext " . cf::to_json \%msg); 2448 $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg));
2430} 2449}
2431 2450
2432=item $success = $client->query ($flags, "text", \&cb) 2451=item $success = $client->query ($flags, "text", \&cb)
2433 2452
2434Queues a query to the client, calling the given callback with 2453Queues a query to the client, calling the given callback with
2457 2476
2458 1 2477 1
2459} 2478}
2460 2479
2461cf::client->attach ( 2480cf::client->attach (
2481 on_connect => sub {
2482 my ($ns) = @_;
2483
2484 $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed;
2485 },
2462 on_reply => sub { 2486 on_reply => sub {
2463 my ($ns, $msg) = @_; 2487 my ($ns, $msg) = @_;
2464 2488
2465 # this weird shuffling is so that direct followup queries 2489 # this weird shuffling is so that direct followup queries
2466 # get handled first 2490 # get handled first
2481 } 2505 }
2482 }, 2506 },
2483 on_exticmd => sub { 2507 on_exticmd => sub {
2484 my ($ns, $buf) = @_; 2508 my ($ns, $buf) = @_;
2485 2509
2486 my $msg = eval { from_json $buf }; 2510 my $msg = eval { $ns->{json_coder}->decode ($buf) };
2487 2511
2488 if (ref $msg) { 2512 if (ref $msg) {
2489 if (my $cb = $EXTICMD{$msg->{msgtype}}) { 2513 if (my $cb = $EXTICMD{$msg->{msgtype}}) {
2490 if (my %reply = $cb->($ns, $msg)) { 2514 if (my %reply = $cb->($ns, $msg)) {
2491 $reply{msgid} = $msg->{msgid}; 2515 $reply{msgid} = $msg->{msgid};
2492 $ns->send ("ext " . cf::to_json \%reply); 2516 $ns->send ("ext " . $ns->{json_coder}->encode (\%reply));
2493 } 2517 }
2494 } 2518 }
2495 } else { 2519 } else {
2496 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; 2520 warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n";
2497 } 2521 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines