diff --git a/js-src/output-packet/move-to.ts b/js-src/output-packet/move-to.ts new file mode 100644 index 0000000..a9d8cdf --- /dev/null +++ b/js-src/output-packet/move-to.ts @@ -0,0 +1,28 @@ +import OutputPacket from '@lastres/output-packet' +import type { Location } from '@lastres/location' +export interface MoveToPacketInterface { + location: string + area: string + super_area: string + planet: string +} +export default class MoveToPacket extends OutputPacket { + location: Location + constructor (location: Location) { + super() + this.location = location + } + + command (): string { + return 'move_to' + } + + data (): MoveToPacketInterface { + return { + location: this.location.location.identifier, + area: this.location.area.identifier, + super_area: this.location.super_area.identifier, + planet: this.location.planet.identifier + } + } +} diff --git a/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm b/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm new file mode 100644 index 0000000..4b4118e --- /dev/null +++ b/lib/LasTres/Controller/Websocket/InputPacket/MoveBetweenLocations.pm @@ -0,0 +1,73 @@ +package LasTres::Controller::Websocket::InputPacket::MoveBetweenLocations; + +use v5.36.0; + +use strict; +use warnings; + +use feature 'signatures'; + +use Data::Dumper; + +use Moo; + +use JSON; +use JSON qw/to_json/; + +use LasTres::Flags; +use LasTres::Redis; +use LasTres::DAO::PJs; +use LasTres::Location; + +with 'LasTres::Controller::Websocket::InputPacket'; + +my $redis = LasTres::Redis->new; +my $result_set_pjs = LasTres::DAO::PJs->ResultSet; + +sub identifier { + return 'move_to'; +} + +sub handle ( $self, $ws, $session, $data ) { + if ( ref $data ne 'HASH' ) { + return $ws->send( to_json( { error => "Data should be a hashref." } ) ); + } + my $planet = $data->{planet}; + my $super_area = $data->{super_area}; + my $area = $data->{area}; + my $location_id = $data->{location}; + if ( !defined $planet + || !defined $super_area + || !defined $area + || !defined $location_id ) + { + return $ws->send( to_json( { error => "The location is malformed" } ) ); + } + my $pj = $session->{pj}; + my $team = $pj->team; + my $leader = $team->leader; + if ( $leader->uuid ne $pj->uuid ) { + return $ws->send( + to_json( { error => 'You are not the team leader.' } ) ); + } + my $location = $team->location; + my $wanted_location = + LasTres::Location::get( $planet, $super_area, $area, $location_id ); + if ( !$location->is_connected_by_move( $wanted_location, $pj ) ) { + return $ws->send( to_json( { error => 'You cannot travel there.' } ) ); + } + + $wanted_location->place_team($team); + + my $connected_places = $location->get_available_locations_to_move_to($pj); + @$connected_places = map { $_->hash } @$connected_places; + my $info_packet_to_send = + LasTres::Controller::Websocket::OutputPacket::Info->new( + location_data => { + current => $location->hash, + connected_places => $connected_places, + }, + ); + $info_packet_to_send->send($ws); +} +1;