… | |
… | |
285 | $who->message ("Intruder Alert!"); |
285 | $who->message ("Intruder Alert!"); |
286 | } |
286 | } |
287 | }; |
287 | }; |
288 | |
288 | |
289 | ############################################################################# |
289 | ############################################################################# |
|
|
290 | # utility functions |
|
|
291 | |
|
|
292 | use JSON::Syck (); # TODO# replace by JSON::PC once working |
|
|
293 | |
|
|
294 | $JSON::Syck::ImplicitUnicode = 1; |
|
|
295 | |
|
|
296 | sub from_json($) { |
|
|
297 | JSON::Syck::Load $_[0] |
|
|
298 | } |
|
|
299 | |
|
|
300 | sub to_json($) { |
|
|
301 | JSON::Syck::Dump $_[0] |
|
|
302 | } |
|
|
303 | |
|
|
304 | ############################################################################# |
290 | # extcmd framework, basically convert ext <id> <pkg> arg1 args |
305 | # extcmd framework, basically convert ext <msg> |
291 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
306 | # into pkg::->on_extcmd_arg1 (...) while shortcutting a few |
292 | |
307 | |
293 | sub on_extcmd { |
308 | sub on_extcmd { |
294 | my ($pl, $buf) = @_; |
309 | my ($pl, $buf) = @_; |
295 | |
310 | |
296 | my ($type) = $buf =~ s/^(\S+) // ? $1 : ""; |
311 | my $msg = eval { from_json $buf }; |
297 | |
312 | |
298 | $extcmd{$type}[0]->($pl, $buf) |
313 | if (ref $msg) { |
299 | if $extcmd{$type}; |
314 | if (my $cb = $extcmd{$msg->{msgtype}}) { |
|
|
315 | if (my %reply = $cb->[0]->($pl, $msg)) { |
|
|
316 | $pl->ext_reply ($msg->{msgid}, %reply); |
|
|
317 | } |
|
|
318 | } |
|
|
319 | } else { |
|
|
320 | warn "player " . ($pl->ob->name) . " sent unparseable ext message: <$buf>\n"; |
|
|
321 | } |
|
|
322 | |
|
|
323 | 1 |
300 | } |
324 | } |
301 | |
325 | |
302 | ############################################################################# |
326 | ############################################################################# |
303 | # load/save/clean perl data associated with a map |
327 | # load/save/clean perl data associated with a map |
304 | |
328 | |
… | |
… | |
390 | sub cf::player::exists($) { |
414 | sub cf::player::exists($) { |
391 | cf::player::find $_[0] |
415 | cf::player::find $_[0] |
392 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
416 | or -f sprintf "%s/%s/%s/%s.pl", cf::localdir, cf::playerdir, ($_[0]) x 2; |
393 | } |
417 | } |
394 | |
418 | |
|
|
419 | =item $player->reply ($npc, $msg[, $flags]) |
|
|
420 | |
|
|
421 | Sends a message to the player, as if the npc C<$npc> replied. C<$npc> |
|
|
422 | can be C<undef>. Does the right thing when the player is currently in a |
|
|
423 | dialogue with the given NPC character. |
|
|
424 | |
|
|
425 | =cut |
|
|
426 | |
395 | # rough implementation of a future "reply" method that works |
427 | # rough implementation of a future "reply" method that works |
396 | # with dialog boxes. |
428 | # with dialog boxes. |
397 | sub cf::object::player::reply($$$;$) { |
429 | sub cf::object::player::reply($$$;$) { |
398 | my ($self, $npc, $msg, $flags) = @_; |
430 | my ($self, $npc, $msg, $flags) = @_; |
399 | |
431 | |
… | |
… | |
403 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
435 | push @{ $self->{record_replies} }, [$npc, $msg, $flags]; |
404 | } else { |
436 | } else { |
405 | $msg = $npc->name . " says: $msg" if $npc; |
437 | $msg = $npc->name . " says: $msg" if $npc; |
406 | $self->message ($msg, $flags); |
438 | $self->message ($msg, $flags); |
407 | } |
439 | } |
|
|
440 | } |
|
|
441 | |
|
|
442 | =item $player->ext_reply ($msgid, $msgtype, %msg) |
|
|
443 | |
|
|
444 | Sends an ext reply to the player. |
|
|
445 | |
|
|
446 | =cut |
|
|
447 | |
|
|
448 | sub cf::player::ext_reply($$$%) { |
|
|
449 | my ($self, $id, %msg) = @_; |
|
|
450 | |
|
|
451 | $msg{msgid} = $id; |
|
|
452 | |
|
|
453 | $self->send ("ext " . to_json \%msg); |
408 | } |
454 | } |
409 | |
455 | |
410 | ############################################################################# |
456 | ############################################################################# |
411 | # map scripting support |
457 | # map scripting support |
412 | |
458 | |