Compare commits

...

2 Commits

Author SHA1 Message Date
Sergiotarxz 206a934d2d Adding to the Info packet known_words. 2023-07-12 18:54:21 +02:00
Sergiotarxz f43070ffee Adding sending words. 2023-07-12 18:41:59 +02:00
6 changed files with 109 additions and 34 deletions

View File

@ -80,6 +80,7 @@ sub handle ( $self, $ws, $session, $data ) {
clear => $JSON::true,
$self->_available_actions($pj),
$self->_npcs($pj),
$self->_available_words($pj),
);
$info_packet_to_send->send($ws);
my $redis = LasTres::Redis->new;
@ -174,10 +175,16 @@ sub _on_redis_event ( $self, $ws, $session, $message, $topic, $topics ) {
if ( $data->{command} eq 'update-actions' ) {
LasTres::Controller::Websocket::OutputPacket::Info->new(
$self->_available_actions($pj),
$self->_npcs($pj) )->send($ws);
$self->_npcs($pj),
$self->_available_words($pj))->send($ws);
}
}
sub _available_words($self, $pj) {
my $known_words = $pj->known_words_hash_serialized;
return ( known_words => $known_words );
}
sub _available_actions ( $self, $pj ) {
return ( available_actions =>
{ map { $_->identifier => $_->hash($pj) } $pj->actions->@* }, );

View File

@ -27,7 +27,8 @@ has is_battling => ( is => 'rw' );
has remaining_frames => ( is => 'rw' );
has available_actions => ( is => 'rw' );
has npcs => ( is => 'rw' );
has npcs => ( is => 'rw' );
has known_words => ( is => 'rw' );
sub identifier {
return 'info';
@ -43,12 +44,17 @@ sub data ($self) {
my $remaining_frames = $self->remaining_frames;
my $is_battling = $self->is_battling;
my $available_actions = $self->available_actions;
my $npcs = $self->npcs;
my $npcs = $self->npcs;
my $known_words = $self->known_words;
if ( defined $is_battling ) {
$is_battling = $is_battling ? $JSON::true : $JSON::false;
}
return {
(
( defined $known_words ) ? ( known_words => $known_words )
: ()
),
(
( defined $clear ) ? ( clear => $clear )
: ()
@ -85,8 +91,7 @@ sub data ($self) {
: ()
),
(
(defined $npcs)
? ( npcs => $npcs)
( defined $npcs ) ? ( npcs => $npcs )
: ()
)
};

View File

@ -18,6 +18,8 @@ use JSON qw/to_json/;
use Moo;
use LasTres::Words;
__PACKAGE__->table('player_pjs');
__PACKAGE__->add_columns(
@ -147,6 +149,23 @@ sub knows_word ( $self, $word ) {
return 1;
}
sub known_words_hash ($self) {
$self = $self->get_from_storage;
my @words = $self->known_words;
my $words_factory = LasTres::Words->new;
my %result_words = map { ( $_->identifier => $_ ) }
map { $words_factory->get( $_->identifier ) } @words;
return \%result_words;
}
sub known_words_hash_serialized ($self) {
my %words = %{ $self->known_words_hash };
my @identifiers = keys %words;
%words =
map { ( $words{$_}->identifier => $words{$_}->serialize ) } @identifiers;
return \%words;
}
sub teach_word ( $self, $word ) {
require LasTres::Schema;
my $schema = LasTres::Schema->Schema;
@ -155,29 +174,31 @@ sub teach_word ( $self, $word ) {
if ( !$word->does('LasTres::Word') ) {
die 'The received word does not implement LasTres::Word.';
}
if ($self->knows_word($word)) {
if ( $self->knows_word($word) ) {
return;
}
my $known_word = $result_set_words->new(
{ identifier => $word->identifier, owner => $self->uuid } );
$known_word->insert_or_update;
my $team = $self->team;
$team->append_log_line([
{
color => 'green',
text => $self->nick,
},
{
text => ' aprendió la palabra '
},
{
color => 'purple',
text => $word->name,
},
{
text => '.'
}
]);
$team->append_log_line(
[
{
color => 'green',
text => $self->nick,
},
{
text => ' aprendió la palabra '
},
{
color => 'purple',
text => $word->name,
},
{
text => '.'
}
]
);
}
@ -207,7 +228,7 @@ sub set_known_location ( $self, $location ) {
my $array = $location->to_array;
my ( $planet, $super_area, $area, $location_id ) = @$array[ 0 .. 3 ];
my $schema = LasTres::Schema->Schema;
if (!$self->knows_location($location)) {
if ( !$self->knows_location($location) ) {
$schema->resultset('PJKnownPlaces')->new(
{
owner => $self->uuid,
@ -563,6 +584,7 @@ sub talk_npcs ($self) {
return {};
}
if ( $team->is_moving ) {
# There will be random encounters for
# some movement frames in certain areas.
return $self->_npc_list_to_hash( \@npcs );

View File

@ -29,4 +29,12 @@ requires qw/name identifier/;
return $hash{$class};
}
}
## DO NOT EXTEND NOT SUPPORTED.
sub serialize ($self) {
return {
name => $self->name,
identifier => $self->identifier,
};
}
1;

View File

@ -15,18 +15,20 @@ use Module::Pluggable search_path => ['LasTres::Word'],
die $error;
};
has hash => (
is => 'rw',
lazy => 1,
builder => \&_build_hash,
);
sub _build_hash($self) {
my @words = $self->plugins();
{
my %hash;
for my $word (@words) {
$hash{$word->identifier} = $word;
sub hash($self) {
if (!scalar %hash) {
my @words = $self->plugins();
for my $word (@words) {
$hash{$word->identifier} = $word;
}
}
return {%hash};
}
return \%hash;
}
sub get($self, $identifier) {
return $self->hash->{$identifier};
}
1;

31
t/02-words.t Normal file
View File

@ -0,0 +1,31 @@
#!/usr/bin/env perl
use v5.36.0;
use strict;
use warnings;
use feature 'signatures';
use Scalar::Util qw/blessed/;
use Test::Most qw/bail no_plan/;
{
use_ok 'LasTres::Words';
}
{
my $words_factory = LasTres::Words->new;
my $words = $words_factory->hash;
for my $identifier (keys %$words) {
my $word = $words->{$identifier};
test_word($word);
}
}
sub test_word($word) {
ok $word->does('LasTres::Word'), (blessed $word)
. ' implements LasTres::Word.';
ok defined $word->name, (blessed $word) . ' has name.';
ok defined $word->identifier, (blessed $word) . ' has identifier.';
}