… | |
… | |
248 | |
248 | |
249 | Converts a perl data structure into its JSON representation. |
249 | Converts a perl data structure into its JSON representation. |
250 | |
250 | |
251 | =cut |
251 | =cut |
252 | |
252 | |
253 | our $json_coder = JSON::XS->new->convert_blessed->utf8->max_size (1e6); # accept ~1mb max |
253 | our $json_coder = JSON::XS->new->utf8->max_size (1e6); # accept ~1mb max |
254 | |
254 | |
255 | sub to_json ($) { $json_coder->encode ($_[0]) } |
255 | sub to_json ($) { $json_coder->encode ($_[0]) } |
256 | sub from_json ($) { $json_coder->decode ($_[0]) } |
256 | sub 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 | 1 while s/\s*\n\s*\n\s*/\n/; |
|
|
1418 | |
1410 | $_ |
1419 | $_ |
|
|
1420 | } |
|
|
1421 | |
|
|
1422 | sub hintmode { |
|
|
1423 | $_[0]{hintmode} = $_[1] if @_ > 1; |
|
|
1424 | $_[0]{hintmode} |
1411 | } |
1425 | } |
1412 | |
1426 | |
1413 | =item $player->ext_reply ($msgid, %msg) |
1427 | =item $player->ext_reply ($msgid, %msg) |
1414 | |
1428 | |
1415 | Sends an ext reply to the player. |
1429 | Sends an ext reply to the player. |
… | |
… | |
1418 | |
1432 | |
1419 | sub ext_reply($$%) { |
1433 | sub ext_reply($$%) { |
1420 | my ($self, $id, %msg) = @_; |
1434 | my ($self, $id, %msg) = @_; |
1421 | |
1435 | |
1422 | $msg{msgid} = $id; |
1436 | $msg{msgid} = $id; |
1423 | $self->send ("ext " . cf::to_json \%msg); |
1437 | $self->send ("ext " . $self->ns->{json_coder}->encode (\%msg)); |
1424 | } |
1438 | } |
1425 | |
1439 | |
1426 | =item $player->ext_event ($type, %msg) |
1440 | =item $player->ext_event ($type, %msg) |
1427 | |
1441 | |
1428 | Sends an ext event to the client. |
1442 | Sends an ext event to the client. |
… | |
… | |
2391 | my ($self, $color, $type, $msg, @extra) = @_; |
2405 | my ($self, $color, $type, $msg, @extra) = @_; |
2392 | |
2406 | |
2393 | $msg = $self->pl->expand_cfpod ($msg); |
2407 | $msg = $self->pl->expand_cfpod ($msg); |
2394 | |
2408 | |
2395 | if ($self->can_msg) { |
2409 | if ($self->can_msg) { |
2396 | $self->send_packet ("msg " . cf::to_json [$color, $type, $msg, @extra]); |
2410 | $self->send_packet ("msg " . $self->{json_coder}->encode ([$color, $type, $msg, @extra])); |
2397 | } else { |
2411 | } else { |
2398 | # replace some tags by gcfclient-compatible ones |
2412 | # replace some tags by gcfclient-compatible ones |
2399 | for ($msg) { |
2413 | for ($msg) { |
2400 | 1 while |
2414 | 1 while |
2401 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
2415 | s/<b>([^<]*)<\/b>/[b]${1}[\/b]/ |
… | |
… | |
2424 | |
2438 | |
2425 | sub cf::client::ext_event($$%) { |
2439 | sub cf::client::ext_event($$%) { |
2426 | my ($self, $type, %msg) = @_; |
2440 | my ($self, $type, %msg) = @_; |
2427 | |
2441 | |
2428 | $msg{msgtype} = "event_$type"; |
2442 | $msg{msgtype} = "event_$type"; |
2429 | $self->send_packet ("ext " . cf::to_json \%msg); |
2443 | $self->send_packet ("ext " . $self->{json_coder}->encode (\%msg)); |
2430 | } |
2444 | } |
2431 | |
2445 | |
2432 | =item $success = $client->query ($flags, "text", \&cb) |
2446 | =item $success = $client->query ($flags, "text", \&cb) |
2433 | |
2447 | |
2434 | Queues a query to the client, calling the given callback with |
2448 | Queues a query to the client, calling the given callback with |
… | |
… | |
2457 | |
2471 | |
2458 | 1 |
2472 | 1 |
2459 | } |
2473 | } |
2460 | |
2474 | |
2461 | cf::client->attach ( |
2475 | cf::client->attach ( |
|
|
2476 | on_connect => sub { |
|
|
2477 | my ($ns) = @_; |
|
|
2478 | |
|
|
2479 | $ns->{json_coder} = JSON::XS->new->utf8->max_size (1e6)->convert_blessed; |
|
|
2480 | }, |
2462 | on_reply => sub { |
2481 | on_reply => sub { |
2463 | my ($ns, $msg) = @_; |
2482 | my ($ns, $msg) = @_; |
2464 | |
2483 | |
2465 | # this weird shuffling is so that direct followup queries |
2484 | # this weird shuffling is so that direct followup queries |
2466 | # get handled first |
2485 | # get handled first |
… | |
… | |
2481 | } |
2500 | } |
2482 | }, |
2501 | }, |
2483 | on_exticmd => sub { |
2502 | on_exticmd => sub { |
2484 | my ($ns, $buf) = @_; |
2503 | my ($ns, $buf) = @_; |
2485 | |
2504 | |
2486 | my $msg = eval { from_json $buf }; |
2505 | my $msg = eval { $ns->{json_coder}->decode ($buf) }; |
2487 | |
2506 | |
2488 | if (ref $msg) { |
2507 | if (ref $msg) { |
2489 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
2508 | if (my $cb = $EXTICMD{$msg->{msgtype}}) { |
2490 | if (my %reply = $cb->($ns, $msg)) { |
2509 | if (my %reply = $cb->($ns, $msg)) { |
2491 | $reply{msgid} = $msg->{msgid}; |
2510 | $reply{msgid} = $msg->{msgid}; |
2492 | $ns->send ("ext " . cf::to_json \%reply); |
2511 | $ns->send ("ext " . $ns->{json_coder}->encode (\%reply)); |
2493 | } |
2512 | } |
2494 | } |
2513 | } |
2495 | } else { |
2514 | } else { |
2496 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2515 | warn "client " . ($ns->pl ? $ns->pl->ob->name : $ns->host) . " sent unparseable exti message: <$buf>\n"; |
2497 | } |
2516 | } |