diff --git a/lib/LasTres/CombatCapableEntity.pm b/lib/LasTres/CombatCapableEntity.pm index 9ee2ae1..52a1c10 100644 --- a/lib/LasTres/CombatCapableEntity.pm +++ b/lib/LasTres/CombatCapableEntity.pm @@ -15,6 +15,7 @@ requires( 'uuid', 'race_string', 'nick', 'born_stats', 'health', 'mana', 'training_stats', 'experience', 'combat_action', 'combat_target', 'team', 'update', + 'gain_experience', 'update_team_sprites' ); 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->@* ) { $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) { diff --git a/lib/LasTres/CombatCapableTeam.pm b/lib/LasTres/CombatCapableTeam.pm index 4b0dcfe..36dc8ba 100644 --- a/lib/LasTres/CombatCapableTeam.pm +++ b/lib/LasTres/CombatCapableTeam.pm @@ -9,7 +9,7 @@ use feature 'signatures'; 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) { return $self; diff --git a/lib/LasTres/Enemy.pm b/lib/LasTres/Enemy.pm index 7fd51ed..34c7ced 100644 --- a/lib/LasTres/Enemy.pm +++ b/lib/LasTres/Enemy.pm @@ -64,7 +64,7 @@ has zmana => ( ); has combat_target => ( - is => 'rw', + is => 'rw', accessor => "_combat_target", ); @@ -74,6 +74,9 @@ after team => sub ( $self, @extra ) { } }; +sub update_team_sprites ($self) { +} + sub _build_born_stats ($self) { return LasTres::Stats->new( health => int( rand(32) ), @@ -108,30 +111,34 @@ sub combat_action ($self) { return; } -sub combat_target ($self, @extra) { +sub combat_target ( $self, @extra ) { my $set; if (@extra) { $set = shift @extra; } - if (!defined $self->_combat_target) { + if ( !defined $self->_combat_target ) { $self->team( $self->team->get_from_storage ); my $battle_uuid = $self->team->current_battle; 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; @$target_team_members = grep { $_->health > 0 } @$target_team_members; - if (!@$target_team_members) { + if ( !@$target_team_members ) { return; } - my $target_index = LasTres::Util::rand_range_int(0, $#$target_team_members); - $self->_combat_target($target_team_members->[$target_index]->uuid); + my $target_index = + 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); } return $self->_combat_target; } +sub gain_experience { +} + sub update ($self) { return $self; } diff --git a/lib/LasTres/EnemyTeam.pm b/lib/LasTres/EnemyTeam.pm index 26d0c69..036929f 100644 --- a/lib/LasTres/EnemyTeam.pm +++ b/lib/LasTres/EnemyTeam.pm @@ -56,6 +56,9 @@ sub to_serializable ($self) { }; } +sub on_end_combat($self) { +} + sub from_serializable ( $class, $hash ) { if ( $hash->{is_db} ) { die "This is not an Enemy Team but a DB Team."; diff --git a/lib/LasTres/Race.pm b/lib/LasTres/Race.pm index 851ffb7..c09cfc1 100644 --- a/lib/LasTres/Race.pm +++ b/lib/LasTres/Race.pm @@ -9,7 +9,7 @@ use feature 'signatures'; 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) { return { diff --git a/lib/LasTres/Race/Aldimor.pm b/lib/LasTres/Race/Aldimor.pm index 60cf36c..fc427a6 100644 --- a/lib/LasTres/Race/Aldimor.pm +++ b/lib/LasTres/Race/Aldimor.pm @@ -92,4 +92,8 @@ sub _build_description { sub _build_is_playable { return 1; } + +sub experience_drop_base { + return 263; +} 1; diff --git a/lib/LasTres/Race/Conejo.pm b/lib/LasTres/Race/Conejo.pm index d9df0ad..e592448 100644 --- a/lib/LasTres/Race/Conejo.pm +++ b/lib/LasTres/Race/Conejo.pm @@ -36,6 +36,10 @@ sub is_playable { return 0; } +sub experience_drop_base { + return 51; +} + sub base_stats { return LasTres::Stats->new( health => 40, diff --git a/lib/LasTres/Schema/Result/PJ.pm b/lib/LasTres/Schema/Result/PJ.pm index 1ba7394..7268d25 100644 --- a/lib/LasTres/Schema/Result/PJ.pm +++ b/lib/LasTres/Schema/Result/PJ.pm @@ -6,8 +6,8 @@ use strict; use warnings; use feature 'signatures'; - use parent 'DBIx::Class::Core'; +use utf8; use UUID::URandom qw/create_uuid_string/; use List::AllUtils; @@ -21,8 +21,8 @@ __PACKAGE__->table('player_pjs'); __PACKAGE__->add_columns( uuid => { - data_type => 'uuid', - is_nullable => 0, + data_type => 'uuid', + is_nullable => 0, }, owner => { data_type => 'uuid', @@ -105,12 +105,12 @@ __PACKAGE__->add_columns( is_nullable => 0, }, combat_target => { - data_type => 'uuid', - is_nullable => 1, + data_type => 'uuid', + is_nullable => 1, }, combat_action => { - data_type => 'text', - is_nullable => 1, + data_type => 'text', + is_nullable => 1, }, ); @@ -134,7 +134,7 @@ __PACKAGE__->belongs_to( 'owner', 'LasTres::Schema::Result::Player' ); sub knows_location ( $self, $location ) { require LasTres::Schema; my $schema = LasTres::Schema->Schema; - my $array = $location->to_array; + my $array = $location->to_array; my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ]; $location = $array->[3]; my @places = $schema->resultset('PJKnownPlaces')->search( @@ -158,15 +158,15 @@ sub set_known_location ( $self, $location ) { my ( $planet, $super_area, $area ) = @$array[ 0 .. 2 ]; $location = $array->[3]; my $schema = LasTres::Schema->Schema; - $schema->resultset('PJKnownPlaces') - ->new( { + $schema->resultset('PJKnownPlaces')->new( + { owner => $self->uuid, planet => $planet, super_area => $super_area, area => $area, location => $location } -)->insert; + )->insert; } sub hash ($self) { @@ -176,18 +176,20 @@ sub hash ($self) { $image = $race->image; } return { - uuid => $self->uuid, - full_name => $self->full_name, - short_name => $self->short_name, - nick => $self->nick, - race => $self->race, - health => $self->health, - max_health => $self->max_health, - mana => $self->mana, - level => $self->level, - experience_to_next_level_complete => $self->experience_to_next_level_complete, - experience_to_next_level_current => $self->experience_to_next_level_current, - max_mana => $self->max_mana, + uuid => $self->uuid, + full_name => $self->full_name, + short_name => $self->short_name, + nick => $self->nick, + race => $self->race, + health => $self->health, + max_health => $self->max_health, + mana => $self->mana, + level => $self->level, + experience_to_next_level_complete => + $self->experience_to_next_level_complete, + experience_to_next_level_current => + $self->experience_to_next_level_current, + max_mana => $self->max_mana, ( ( defined $image ) ? ( image => $image ) @@ -196,11 +198,11 @@ sub hash ($self) { }; } -sub experience_to_next_level_complete($self) { - return ($self->level+1)**3 - $self->level**3; +sub experience_to_next_level_complete ($self) { + 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; } @@ -220,12 +222,117 @@ for my $column_name ( keys %$columns ) { ); } - sub last_50_log ($self) { return $self->logs->search( {}, { 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 ) { require LasTres::Schema; require LasTres::Redis; @@ -275,7 +382,6 @@ sub location ($self) { return $self->team->location; } - sub set_flag ( $self, $name ) { require LasTres::Schema; my $schema = LasTres::Schema->Schema; diff --git a/lib/LasTres/Schema/Result/Team.pm b/lib/LasTres/Schema/Result/Team.pm index 3754368..db8f27d 100644 --- a/lib/LasTres/Schema/Result/Team.pm +++ b/lib/LasTres/Schema/Result/Team.pm @@ -188,6 +188,9 @@ sub combat_members ($self) { return [@members]; } +sub on_end_combat($self) { +} + sub to_serializable ($self) { return { is_db => $JSON::true,