Adding health update on attack and scroll improvements.

This commit is contained in:
Sergiotarxz 2023-06-26 21:26:31 +02:00
parent 664f002967
commit e6833001c3
6 changed files with 61 additions and 33 deletions

View File

@ -92,15 +92,10 @@ export default class InputPackets {
const [scrollHeight, scrollTop, offsetHeight] = scrollData
if (firstTime) {
firstTime = false
console.log(scrollHeight, logPresentation.scrollHeight)
logPresentation.scrollTo(0, logPresentation.scrollHeight)
return
}
if (scrollHeight === offsetHeight) {
logPresentation.scrollTo(0, logPresentation.scrollHeight)
return
}
if (scrollHeight <= scrollTop + offsetHeight) {
if (scrollHeight <= scrollTop + offsetHeight * (3 / 2)) {
logPresentation.scrollTo(0, logPresentation.scrollHeight)
}
}

View File

@ -9,6 +9,7 @@ use utf8;
use feature 'signatures';
use Moo::Role;
use JSON qw/to_json from_json/;
requires(
'uuid', 'race_string', 'nick', 'born_stats',
@ -48,6 +49,15 @@ sub attack ( $self, $enemy_entity ) {
$enemy_entity->health($final_health);
my $team1 = $self->team;
my $team2 = $enemy_entity->team;
require LasTres::Redis;
my $redis = LasTres::Redis->new;
for my $team ( $team1, $team2 ) {
next if !$team->isa('LasTres::Schema::Result::Team');
for my $pj ( $team->members ) {
$redis->publish( $redis->pj_subscription($pj),
to_json( { command => 'update-team-sprites' } ) );
}
}
for my $member ( $team1->combat_members->@* ) {
$member->append_log_line(
[

View File

@ -45,9 +45,6 @@ sub handle ( $self, $ws, $session, $data ) {
}
$session->{pj} = $pj;
my $team = $pj->team;
my @team_members = $team->members;
my @friends = grep { $pj->uuid ne $_->uuid } @team_members;
my $team_pjs = [ map { $_->hash } ( $pj, @friends ) ];
my $location = $team->location;
$pj->append_log_line(
@ -77,7 +74,7 @@ sub handle ( $self, $ws, $session, $data ) {
LasTres::Controller::Websocket::OutputPacket::Info->new(
set_log => [ $pj->last_50_log ],
$self->_location_data($pj),
team_pjs => $team_pjs,
team_pjs => $team->combat_members_serializable($pj),
clear => $JSON::true,
);
$info_packet_to_send->send($ws);
@ -139,6 +136,12 @@ sub _on_redis_event ( $self, $ws, $session, $message, $topic, $topics ) {
}
return;
}
if ($data->{command} eq 'update-team-sprites') {
my $team = $pj->team;
LasTres::Controller::Websocket::OutputPacket::Info->new(
team_pjs => $team->combat_members_serializable($pj),
)->send($ws);
}
}
sub _get_connected_places ( $self, $pj ) {

View File

@ -81,6 +81,7 @@ sub _expire_battle($self, $battle_uuid) {
$team->update;
}
}
sub _increment_frame_for_combat($self, $battle_uuid) {
my $battle = LasTres::Battle->get_redis($battle_uuid);
if (!defined $battle) {

View File

@ -76,44 +76,62 @@ __PACKAGE__->add_columns(
accessor => '_location',
},
current_battle => {
data_type => 'uuid',
data_type => 'uuid',
is_nullable => 1,
},
);
sub is_defeated($self) {
my @members = $self->members;
sub is_defeated ($self) {
my @members = $self->members;
my @alive_members = grep { $_->health > 0 } @members;
if (!scalar @alive_members) {
if ( !scalar @alive_members ) {
return 1;
}
return 0;
}
sub on_win_combat($self) {
sub combat_members_serializable ( $self, $pj = undef ) {
my @team_members = $self->members;
if ( defined $pj ) {
@team_members = grep { $pj->uuid ne $_->uuid } @team_members;
@team_members = ( $pj, @team_members );
}
my $team_pjs = [ map { $_->hash } (@team_members) ];
return $team_pjs;
}
sub on_lose_combat($self) {
sub on_win_combat ($self) {
}
sub on_lose_combat ($self) {
require LasTres::Redis;
my $redis = LasTres::Redis->new;
my $last_spawn = $self->get_spawn;
$last_spawn->place_team($self);
$self->is_moving(0);
$self->moving_to('null');
for my $member ($self->combat_members->@*) {
$member->health($member->max_health);
$member->mana($member->max_mana);
for my $member ( $self->combat_members->@* ) {
$member->health( $member->max_health );
$member->mana( $member->max_mana );
$member->update;
}
$self->update;
for my $pj ( $self->members ) {
$redis->publish( $redis->pj_subscription($pj),
to_json( { command => 'update-team-sprites' } ) );
}
}
sub update($self) {
sub update ($self) {
return $self->SUPER::update();
}
sub get_spawn($self) {
sub get_spawn ($self) {
my $location;
eval {
$location = LasTres::Location::get(@{from_json($self->last_spawn)});
$location =
LasTres::Location::get( @{ from_json( $self->last_spawn ) } );
};
if ($@) {
$location = $self->leader->race->spawn;
@ -121,10 +139,10 @@ sub get_spawn($self) {
return $location;
}
sub start_random_combat($self) {
sub start_random_combat ($self) {
$self = $self->get_from_storage;
my $location = $self->location;
my $area = $location->to_array->[2];
my $area = $location->to_array->[2];
}
# May throw error, it is needed to handle.
@ -162,9 +180,10 @@ sub send_frame_to_members ($self) {
}
}
sub combat_members($self) {
sub combat_members ($self) {
$self = $self->get_from_storage;
my @members = $self->members;
# Add friendly NPCs here when implemented.
return [@members];
}
@ -172,19 +191,19 @@ sub combat_members($self) {
sub to_serializable ($self) {
return {
is_db => $JSON::true,
uuid => $self->uuid,
}
uuid => $self->uuid,
};
}
sub from_serializable($class, $hash) {
sub from_serializable ( $class, $hash ) {
require LasTres::Schema;
my $schema = LasTres::Schema->Schema;
my $schema = LasTres::Schema->Schema;
my $resultset = $schema->resultset('Team');
if (!$hash->{is_db}) {
if ( !$hash->{is_db} ) {
die 'This is not a database Team.';
}
my @teams = $resultset->search({uuid => $hash->{uuid}});
if (!@teams) {
my @teams = $resultset->search( { uuid => $hash->{uuid} } );
if ( !@teams ) {
die 'This team does not exists.';
}
return $teams[0];

File diff suppressed because one or more lines are too long