LasTres/lib/LasTres/Controller/Websocket/InputPacket/Talk.pm

78 lines
2.0 KiB
Perl

package LasTres::Controller::Websocket::InputPacket::Talk;
use v5.36.0;
use strict;
use warnings;
use utf8;
use Moo;
use List::AllUtils;
use JSON qw/to_json/;
use LasTres::Words;
sub identifier {
return 'talk';
}
sub handle ( $self, $ws, $session, $data ) {
if ( ref $data ne 'HASH' ) {
return $ws->send( to_json( { error => "Data should be a hashref." } ) );
}
if ( !defined $session->{pj} ) {
return $ws->send(
to_json( { error => 'The pj for this session does not exist.' } ) );
}
$session->{pj} = $session->{pj}->get_from_storage;
my $pj = $session->{pj};
my $npc_identifier = $data->{npc};
if ( !defined $npc_identifier ) {
return $ws->send(
to_json( { error => 'You did not send a npc to talk with.' } ) );
}
my $available_npcs = $pj->talk_npcs;
my $npc = $available_npcs->{$npc_identifier};
if ( !defined $npc ) {
return $ws->send(
to_json(
{ error => 'The npc you sent is not available to talk with.' }
)
);
}
my $possible_word_identifier = $data->{word};
if ( !defined $possible_word_identifier ) {
$self->handle_wordlessly_talk( $pj, $npc );
return;
}
my $word_identifier = $possible_word_identifier;
my $maybe_word = $pj->get_word($word_identifier);
if ( !defined $maybe_word ) {
return $ws->send(
to_json( { error => 'You do not know this word.' } ) );
}
my $word = $maybe_word;
$self->handle_say_word($pj, $npc, $word);
}
sub handle_say_word($self, $pj, $npc, $word) {
$npc->talk( $pj, $word );
$self->end_conversation($pj)
}
sub handle_wordlessly_talk ( $self, $pj, $npc ) {
$npc->talk( $pj, undef );
$self->end_conversation($pj);
}
sub end_conversation ( $self, $pj ) {
my $team = $pj->team;
for my $member ( $team->combat_members->@* ) {
$pj->update_location;
$pj->update_actions;
$pj->update_team_sprites;
}
}
1;