burguillos.info/lib/BurguillosInfo/Schema/Result/ConquerNode.pm

104 lines
2.6 KiB
Perl

package BurguillosInfo::Schema::Result::ConquerNode;
use v5.36.0;
use strict;
use warnings;
use parent 'DBIx::Class::Core';
use feature 'signatures';
use JSON;
use GIS::Distance;
__PACKAGE__->table('conquer_node');
__PACKAGE__->load_components("TimeStamp");
__PACKAGE__->add_columns(
uuid => {
data_type => 'uuid',
is_nullable => 0,
},
name => {
data_type => 'text',
is_nullable => 0,
default_value => \'0',
},
type => {
data_type => 'text',
is_nullable => 0,
},
description => {
data_type => 'text',
is_nullable => 0,
},
geometry => {
data_type => 'geometry',
is_nullable => 0,
},
);
sub coordinate_2($self) {
require BurguillosInfo::Schema;
my $resultset = BurguillosInfo::Schema->Schema->resultset('ConquerNode');
my ($new_self) = $resultset->search({uuid => $self->uuid}, {
'+select' => {
ST_Y => { ST_Centroid => 'geometry' },
-as => 'coordinate_2',
}
});
return $new_self->get_column('coordinate_2');
}
sub coordinate_1($self) {
require BurguillosInfo::Schema;
my $resultset = BurguillosInfo::Schema->Schema->resultset('ConquerNode');
my ($new_self) = $resultset->search({uuid => $self->uuid}, {
'+select' => {
ST_X => { ST_Centroid => 'geometry' },
-as => 'coordinate_1',
}
});
return $new_self->get_column('coordinate_1');
}
sub serialize ( $self, $player = undef ) {
$self = $self->get_from_storage();
my $return = {
kind => 'ConquerNode',
uuid => $self->uuid,
name => $self->name,
description => $self->description,
type => $self->type,
coordinate_1 => $self->coordinate_1,
coordinate_2 => $self->coordinate_2,
is_near => $self->is_near($player),
};
return $return;
}
sub is_near ( $self, $player ) {
if ( !defined $player ) {
return $JSON::false;
}
# Meters
if ($self->get_distance_to_player($player) < 100) {
return $JSON::true;
}
return $JSON::false;
}
sub get_distance_to_player ($self, $player) {
my $longitude_player = $player->last_coordinate_1;
my $latitude_player = $player->last_coordinate_2;
my $longitude_node = $self->coordinate_1;
my $latitude_node = $self->coordinate_2;
my $gis = GIS::Distance->new;
# Setting distance to meters.
my $distance = $gis->distance_metal( $latitude_node, $longitude_node, $latitude_player, $longitude_player) * 1000;
}
__PACKAGE__->set_primary_key('uuid');
1;