Adding move between locations.

This commit is contained in:
Sergiotarxz 2023-06-18 20:31:23 +02:00
parent 7fa92b9cc0
commit c0acc2ee55
2 changed files with 101 additions and 0 deletions

View File

@ -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
}
}
}

View File

@ -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;