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

154 lines
3.8 KiB
Perl

package BurguillosInfo::Schema::Result::ConquerUser;
use v5.36.0;
use strict;
use warnings;
use parent 'DBIx::Class::Core';
use feature 'signatures';
use JSON;
__PACKAGE__->table('conquer_user');
__PACKAGE__->load_components("TimeStamp");
__PACKAGE__->add_columns(
uuid => {
data_type => 'uuid',
is_nullable => 0,
},
team => {
data_type => 'uuid',
is_nullable => 1,
},
username => {
data_type => 'text',
is_nullable => 0,
},
encrypted_password => {
data_type => 'text',
is_nullable => 0,
},
last_activity => {
data_type => 'timestamp',
is_nullable => 0,
default_value => \'NOW()',
},
registration_date => {
data_type => 'timestamp',
is_nullable => 0,
default_value => \'NOW()',
},
is_admin => {
data_type => 'boolean',
is_nullable => 0,
default_value => \'0',
},
last_coordinate_1 => {
data_type => 'real',
is_nullable => 0,
default_value => \'0',
},
last_coordinate_2 => {
data_type => 'real',
is_nullable => 0,
default_value => \'0',
},
experience => {
data_type => 'integer',
is_nullable => 0,
default_value => \'125',
},
current_hp => {
data_type => 'integer',
is_nullable => 0,
default_value => \'999',
}
);
sub max_health($self) {
$self = $self->get_from_storage();
my $base = 50;
my $born_value = 31;
return int(
(($base * 2 + $born_value) * $self->level)
/ 100 + $self->level + 10
);
}
sub level($self) {
$self = $self->get_from_storage();
return int($self->experience ** (1/3) + 0.0000000000001);
}
sub attack($self) {
$self = $self->get_from_storage();
my $base = 50;
my $born_value = 31;
return int(
(($base * 2 + $self->level)*$self->level)
/100
);
}
sub defense($self) {
$self = $self->get_from_storage();
my $base = 50;
my $born_value = 31;
return int(
(($base * 2 + $self->level)*$self->level)
/100
);
}
sub health($self, $health = undef) {
$self = $self->get_from_storage();
my $hp = $self->current_hp;
if ($hp > $self->max_health) {
$self->current_hp($self->max_health);
$self->update;
$self = $self->get_from_storage();
}
if (defined $health) {
if ($health > $self->max_health) {
$health = $self->max_health;
}
$self->current_hp($health);
$self->update;
$self = $self->get_from_storage();
}
return $self->current_hp;
}
sub coordinates ( $self, $coordinates = undef ) {
if ( defined $coordinates ) {
if ( ref $coordinates ne 'ARRAY' || scalar $coordinates->@* != 2 ) {
die 'The second parameter of this subroutine '
. 'must be an ARRAYREF of exactly two elements.';
}
$self->last_coordinate_1( $coordinates->[0] );
$self->last_coordinate_2( $coordinates->[1] );
}
return [ $self->last_coordinate_1, $self->last_coordinate_2 ];
}
sub serialize_to_owner ($self) {
$self = $self->get_from_storage();
return {
kind => 'ConquerUser',
uuid => $self->uuid,
team => $self->team,
username => $self->username,
is_admin => $self->is_admin ? $JSON::true : $JSON::false,
last_activity => $self->last_activity,
registration_date => $self->registration_date,
};
}
__PACKAGE__->set_primary_key('uuid');
__PACKAGE__->belongs_to('team_object', 'BurguillosInfo::Schema::Result::ConquerTeam', 'team');
__PACKAGE__->add_unique_constraint( "unique_constraint_username",
['username'] );
1;