102 lines
2.9 KiB
Perl
102 lines
2.9 KiB
Perl
package LasTres::Controller::Websocket::InputPacket::Init;
|
|
|
|
use v5.36.0;
|
|
|
|
use strict;
|
|
use warnings;
|
|
|
|
use feature 'signatures';
|
|
|
|
use Data::Dumper;
|
|
|
|
use Moo;
|
|
|
|
use JSON;
|
|
|
|
use LasTres::Flags;
|
|
use LasTres::Redis;
|
|
use LasTres::DAO::PJs;
|
|
|
|
with 'LasTres::Controller::Websocket::InputPacket';
|
|
|
|
my $redis = LasTres::Redis->new;
|
|
my $result_set_pjs = LasTres::DAO::PJs->ResultSet;
|
|
|
|
sub identifier {
|
|
return 'init';
|
|
}
|
|
|
|
sub handle ( $self, $ws, $session, $data ) {
|
|
if ( ref $data ne 'HASH' ) {
|
|
return $ws->send(
|
|
encode_json( { error => "Data should be a hashref." } ) );
|
|
}
|
|
my $pj_uuid = $data->{pj_uuid};
|
|
if ( !defined $pj_uuid ) {
|
|
return $ws->send( encode_json( { error => "No pj sent." } ) );
|
|
}
|
|
my @pjs = $result_set_pjs->search( { uuid => $pj_uuid } );
|
|
if ( !scalar @pjs ) {
|
|
return $ws->send(
|
|
encode_json( { error => 'This pj does not exists' } ) );
|
|
}
|
|
my $pj = $pjs[0];
|
|
my $user = $session->{user};
|
|
if ( $pj->owner->uuid ne $user->uuid ) {
|
|
return $ws->send(
|
|
encode_json( { error => 'You are not the owner of this pj.' } ) );
|
|
}
|
|
$session->{pj} = $pj;
|
|
my $team = $pj->team;
|
|
my @team_members = $team->members;
|
|
my @friends = grep { $pj->uuid ne $_->uuid } @team_members;
|
|
my $team_pjs = [ map { $_->hash } ( $pj, @friends ) ];
|
|
my $location = $team->location;
|
|
my $connected_places = $self->_get_connected_places($pj);
|
|
|
|
$pj->append_log_line(
|
|
[
|
|
{ text => 'Nueva conexion a este pj.', color => 'red' },
|
|
]
|
|
);
|
|
|
|
if ( !$pj->get_flag( LasTres::Flags::INTRO_MESSAGE_SENT_FLAG() ) ) {
|
|
$pj->set_flag(LasTres::Flags::INTRO_MESSAGE_SENT_FLAG);
|
|
$pj->append_log_line(
|
|
[
|
|
{ text => 'Bienvenido a ' },
|
|
{ text => 'LasTres', color => 'green' },
|
|
{ text => '. Esperamos que disfrutes del juego.' }
|
|
]
|
|
);
|
|
}
|
|
my $info_packet_to_send =
|
|
LasTres::Controller::Websocket::OutputPacket::Info->new(
|
|
set_log => [$pj->last_50_log],
|
|
team_pjs => $team_pjs,
|
|
location_data => {
|
|
current => $location->hash,
|
|
connected_places => $connected_places,
|
|
},
|
|
clear => $JSON::true,
|
|
);
|
|
$info_packet_to_send->send($ws);
|
|
}
|
|
|
|
sub _get_connected_places ( $self, $pj ) {
|
|
my $team = $pj->team;
|
|
my $location = $team->location;
|
|
|
|
my $connected_places = [];
|
|
if ( $location->can('connected_places') ) {
|
|
@$connected_places = ( @{ $team->location->connected_places } );
|
|
}
|
|
@$connected_places =
|
|
( @$connected_places, @{ $location->parent->children } );
|
|
@$connected_places =
|
|
grep { $_->identifier ne $location->identifier } @$connected_places;
|
|
@$connected_places = map { $_->hash } @$connected_places;
|
|
return $connected_places;
|
|
}
|
|
1;
|