Adding gain experience.

This commit is contained in:
Sergiotarxz 2023-06-27 02:07:37 +02:00
parent e6833001c3
commit 9765d3b0dc
9 changed files with 191 additions and 54 deletions

View File

@ -15,6 +15,7 @@ requires(
'uuid', 'race_string', 'nick', 'born_stats', 'uuid', 'race_string', 'nick', 'born_stats',
'health', 'mana', 'training_stats', 'experience', 'health', 'mana', 'training_stats', 'experience',
'combat_action', 'combat_target', 'team', 'update', 'combat_action', 'combat_target', 'team', 'update',
'gain_experience', 'update_team_sprites'
); );
sub append_log_line { sub append_log_line {
@ -75,14 +76,6 @@ sub attack ( $self, $enemy_entity ) {
} }
] ]
); );
if ( $enemy_entity->health == 0 ) {
$member->append_log_line(
[
{ color => 'red', text => "@{[$enemy_entity->nick]}" },
{ text => " se ha debilitado." },
]
);
}
} }
for my $member ( $team2->combat_members->@* ) { for my $member ( $team2->combat_members->@* ) {
$member->append_log_line( $member->append_log_line(
@ -101,15 +94,32 @@ sub attack ( $self, $enemy_entity ) {
} }
] ]
); );
if ( $enemy_entity->health == 0 ) {
$member->append_log_line(
[
{ color => 'green', text => "@{[$enemy_entity->nick]}" },
{ text => " se ha debilitado." },
]
);
}
} }
if ( $enemy_entity->health == 0 ) {
$enemy_entity->on_faint($self);
}
}
sub on_faint($self, $attacker) {
my $team2 = $self->team;
my $team1 = $attacker->team;
for my $member ( $team1->combat_members->@* ) {
$member->append_log_line(
[
{ color => 'red', text => "@{[$self->nick]}" },
{ text => " se ha debilitado." },
]
);
}
for my $member ( $team2->combat_members->@* ) {
$member->append_log_line(
[
{ color => 'green', text => "@{[$self->nick]}" },
{ text => " se ha debilitado." },
]
);
}
$attacker->gain_experience((($self->race->experience_drop_base * $self->level)/7)*1.5);
} }
sub level ($self) { sub level ($self) {

View File

@ -9,7 +9,7 @@ use feature 'signatures';
use Moo::Role; use Moo::Role;
requires qw/uuid combat_members current_battle to_serializable from_serializable update is_defeated on_win_combat on_lose_combat update/; requires qw/uuid combat_members current_battle to_serializable from_serializable update is_defeated on_win_combat on_lose_combat update on_end_combat/;
sub get_from_storage($self) { sub get_from_storage($self) {
return $self; return $self;

View File

@ -64,7 +64,7 @@ has zmana => (
); );
has combat_target => ( has combat_target => (
is => 'rw', is => 'rw',
accessor => "_combat_target", accessor => "_combat_target",
); );
@ -74,6 +74,9 @@ after team => sub ( $self, @extra ) {
} }
}; };
sub update_team_sprites ($self) {
}
sub _build_born_stats ($self) { sub _build_born_stats ($self) {
return LasTres::Stats->new( return LasTres::Stats->new(
health => int( rand(32) ), health => int( rand(32) ),
@ -108,30 +111,34 @@ sub combat_action ($self) {
return; return;
} }
sub combat_target ($self, @extra) { sub combat_target ( $self, @extra ) {
my $set; my $set;
if (@extra) { if (@extra) {
$set = shift @extra; $set = shift @extra;
} }
if (!defined $self->_combat_target) { if ( !defined $self->_combat_target ) {
$self->team( $self->team->get_from_storage ); $self->team( $self->team->get_from_storage );
my $battle_uuid = $self->team->current_battle; my $battle_uuid = $self->team->current_battle;
my $battle = LasTres::Battle->get_redis($battle_uuid); my $battle = LasTres::Battle->get_redis($battle_uuid);
my $target_team = $battle->get_enemy_team($self->team); my $target_team = $battle->get_enemy_team( $self->team );
my $target_team_members = $target_team->combat_members; my $target_team_members = $target_team->combat_members;
@$target_team_members = grep { $_->health > 0 } @$target_team_members; @$target_team_members = grep { $_->health > 0 } @$target_team_members;
if (!@$target_team_members) { if ( !@$target_team_members ) {
return; return;
} }
my $target_index = LasTres::Util::rand_range_int(0, $#$target_team_members); my $target_index =
$self->_combat_target($target_team_members->[$target_index]->uuid); LasTres::Util::rand_range_int( 0, $#$target_team_members );
$self->_combat_target( $target_team_members->[$target_index]->uuid );
} }
if (defined $set) { if ( defined $set ) {
$self->_combat_target($set); $self->_combat_target($set);
} }
return $self->_combat_target; return $self->_combat_target;
} }
sub gain_experience {
}
sub update ($self) { sub update ($self) {
return $self; return $self;
} }

View File

@ -56,6 +56,9 @@ sub to_serializable ($self) {
}; };
} }
sub on_end_combat($self) {
}
sub from_serializable ( $class, $hash ) { sub from_serializable ( $class, $hash ) {
if ( $hash->{is_db} ) { if ( $hash->{is_db} ) {
die "This is not an Enemy Team but a DB Team."; die "This is not an Enemy Team but a DB Team.";

View File

@ -9,7 +9,7 @@ use feature 'signatures';
use Moo::Role; use Moo::Role;
requires qw/spawn identifier name name_selection description is_playable base_stats/; requires qw/spawn identifier name name_selection description is_playable base_stats experience_drop_base/;
sub hash($self) { sub hash($self) {
return { return {

View File

@ -92,4 +92,8 @@ sub _build_description {
sub _build_is_playable { sub _build_is_playable {
return 1; return 1;
} }
sub experience_drop_base {
return 263;
}
1; 1;

View File

@ -36,6 +36,10 @@ sub is_playable {
return 0; return 0;
} }
sub experience_drop_base {
return 51;
}
sub base_stats { sub base_stats {
return LasTres::Stats->new( return LasTres::Stats->new(
health => 40, health => 40,

View File

@ -6,8 +6,8 @@ use strict;
use warnings; use warnings;
use feature 'signatures'; use feature 'signatures';
use parent 'DBIx::Class::Core'; use parent 'DBIx::Class::Core';
use utf8;
use UUID::URandom qw/create_uuid_string/; use UUID::URandom qw/create_uuid_string/;
use List::AllUtils; use List::AllUtils;
@ -21,8 +21,8 @@ __PACKAGE__->table('player_pjs');
__PACKAGE__->add_columns( __PACKAGE__->add_columns(
uuid => { uuid => {
data_type => 'uuid', data_type => 'uuid',
is_nullable => 0, is_nullable => 0,
}, },
owner => { owner => {
data_type => 'uuid', data_type => 'uuid',
@ -105,12 +105,12 @@ __PACKAGE__->add_columns(
is_nullable => 0, is_nullable => 0,
}, },
combat_target => { combat_target => {
data_type => 'uuid', data_type => 'uuid',
is_nullable => 1, is_nullable => 1,
}, },
combat_action => { combat_action => {
data_type => 'text', data_type => 'text',
is_nullable => 1, is_nullable => 1,
}, },
); );
@ -134,7 +134,7 @@ __PACKAGE__->belongs_to( 'owner', 'LasTres::Schema::Result::Player' );
sub knows_location ( $self, $location ) { sub knows_location ( $self, $location ) {
require LasTres::Schema; require LasTres::Schema;
my $schema = LasTres::Schema->Schema; my $schema = LasTres::Schema->Schema;
my $array = $location->to_array; my $array = $location->to_array;
my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ]; my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ];
$location = $array->[3]; $location = $array->[3];
my @places = $schema->resultset('PJKnownPlaces')->search( my @places = $schema->resultset('PJKnownPlaces')->search(
@ -158,15 +158,15 @@ sub set_known_location ( $self, $location ) {
my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ]; my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ];
$location = $array->[3]; $location = $array->[3];
my $schema = LasTres::Schema->Schema; my $schema = LasTres::Schema->Schema;
$schema->resultset('PJKnownPlaces') $schema->resultset('PJKnownPlaces')->new(
->new( { {
owner => $self->uuid, owner => $self->uuid,
planet => $planet, planet => $planet,
super_area => $super_area, super_area => $super_area,
area => $area, area => $area,
location => $location location => $location
} }
)->insert; )->insert;
} }
sub hash ($self) { sub hash ($self) {
@ -176,18 +176,20 @@ sub hash ($self) {
$image = $race->image; $image = $race->image;
} }
return { return {
uuid => $self->uuid, uuid => $self->uuid,
full_name => $self->full_name, full_name => $self->full_name,
short_name => $self->short_name, short_name => $self->short_name,
nick => $self->nick, nick => $self->nick,
race => $self->race, race => $self->race,
health => $self->health, health => $self->health,
max_health => $self->max_health, max_health => $self->max_health,
mana => $self->mana, mana => $self->mana,
level => $self->level, level => $self->level,
experience_to_next_level_complete => $self->experience_to_next_level_complete, experience_to_next_level_complete =>
experience_to_next_level_current => $self->experience_to_next_level_current, $self->experience_to_next_level_complete,
max_mana => $self->max_mana, experience_to_next_level_current =>
$self->experience_to_next_level_current,
max_mana => $self->max_mana,
( (
( defined $image ) ( defined $image )
? ( image => $image ) ? ( image => $image )
@ -196,11 +198,11 @@ sub hash ($self) {
}; };
} }
sub experience_to_next_level_complete($self) { sub experience_to_next_level_complete ($self) {
return ($self->level+1)**3 - $self->level**3; return ( $self->level + 1 )**3 - $self->level**3;
} }
sub experience_to_next_level_current($self) { sub experience_to_next_level_current ($self) {
return $self->experience - $self->level**3; return $self->experience - $self->level**3;
} }
@ -220,12 +222,117 @@ for my $column_name ( keys %$columns ) {
); );
} }
sub last_50_log ($self) { sub last_50_log ($self) {
return $self->logs->search( {}, return $self->logs->search( {},
{ limit => 50, order_by => { -desc => 'date' } } ); { limit => 50, order_by => { -desc => 'date' } } );
} }
sub update_team_sprites ($self) {
require LasTres::Redis;
my $redis = LasTres::Redis->new;
$redis->publish( $redis->pj_subscription($self),
to_json( { command => 'update-team-sprites' } ) );
}
sub on_level_up ( $self, $old_level ) {
my $old_experience = $self->experience;
my $team = $self->team;
my $enemy_team = $self->_enemy_team;
$self->health($self->max_health);
$self->update;
for my $member ( $team->combat_members->@* ) {
$member->update_team_sprites;
$member->append_log_line(
[
{
text => $self->nick,
color => 'green',
},
{
text => ' subió al nivel ' . $self->level
}
]
);
}
if ( defined $enemy_team ) {
for my $member ( $enemy_team->combat_members->@* ) {
$member->update_team_sprites;
$member->append_log_line(
[
{
text => $self->nick,
color => 'red',
},
{
text => ' subió al nivel ' . $self->level
}
]
);
}
}
}
sub _enemy_team ($self) {
my $team = $self->team;
my $enemy_team;
my $current_battle = $team->current_battle;
{
if ( !defined $current_battle ) {
next;
}
my $battle = LasTres::Battle->get_redis($current_battle);
if ( !defined $battle ) {
next;
}
$enemy_team = $battle->get_enemy_team($team);
}
return $enemy_team;
}
sub gain_experience ( $self, $experience_to_gain ) {
$experience_to_gain = int($experience_to_gain);
my $redis = LasTres::Redis->new;
my $old_experience = $self->experience;
my $old_level = $self->level;
my $team = $self->team;
my $current_battle = $team->current_battle;
$self->experience( $old_experience + $experience_to_gain );
$self->update;
for my $member ( $team->combat_members->@* ) {
$member->append_log_line(
[
{
text => $self->nick,
color => 'green',
},
{
text => " ganó $experience_to_gain puntos de experiencia."
}
]
);
}
my $enemy_team = $self->_enemy_team;
if ( defined $enemy_team ) {
for my $member ( $enemy_team->combat_members->@* ) {
$member->append_log_line(
[
{
text => $self->nick,
color => 'red',
},
{
text =>
" ganó $experience_to_gain puntos de experiencia."
}
]
);
}
}
if ( $old_level != $self->level ) {
$self->on_level_up($old_level);
}
}
sub append_log_line ( $self, $content ) { sub append_log_line ( $self, $content ) {
require LasTres::Schema; require LasTres::Schema;
require LasTres::Redis; require LasTres::Redis;
@ -275,7 +382,6 @@ sub location ($self) {
return $self->team->location; return $self->team->location;
} }
sub set_flag ( $self, $name ) { sub set_flag ( $self, $name ) {
require LasTres::Schema; require LasTres::Schema;
my $schema = LasTres::Schema->Schema; my $schema = LasTres::Schema->Schema;

View File

@ -188,6 +188,9 @@ sub combat_members ($self) {
return [@members]; return [@members];
} }
sub on_end_combat($self) {
}
sub to_serializable ($self) { sub to_serializable ($self) {
return { return {
is_db => $JSON::true, is_db => $JSON::true,