From 2c806e76e278f1a33198df797228cf2ef7be075c Mon Sep 17 00:00:00 2001 From: Darin McBride Date: Fri, 4 Jul 2014 17:21:39 -0600 Subject: [PATCH] Remaining downgrade/demolish leader's embassy code. --- lib/Lacuna/DB/Result/Building/Embassy.pm | 32 ++++++++++++++++++++---- lib/Lacuna/DB/Result/Empire.pm | 4 ++- 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/Lacuna/DB/Result/Building/Embassy.pm b/lib/Lacuna/DB/Result/Building/Embassy.pm index 7de8ecdd..82d1e81c 100644 --- a/lib/Lacuna/DB/Result/Building/Embassy.pm +++ b/lib/Lacuna/DB/Result/Building/Embassy.pm @@ -5,6 +5,7 @@ use utf8; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; use Lacuna::Util qw(format_date); +use List::Util qw(max); around 'build_tags' => sub { my ($orig, $class) = @_; @@ -43,10 +44,12 @@ use constant water_consumption => 6; use constant waste_production => 1; - +# by accepting an optional level, we can centralise the calculation. +# default level, of course, is the embassy's level. sub max_members { - my $self = shift; - return $self->level * 2; + my $self = shift; + my $level = @_ ? shift : $self->level; + return $level * 2; } sub alliance { @@ -278,7 +281,17 @@ before 'can_downgrade' => sub { my $self = shift; my $alliance = eval{$self->alliance}; if (defined $alliance && $self->body->empire_id == $alliance->leader_id) { - confess [1013, 'You cannot downgrade an Embassy while you are an alliance leader.'] + my $alliance_members = $alliance->members->count; + my $best_other_embassy = $self->body->empire + ->highest_embassy($self->body_id); + my $allowed_members = max( + defined $best_other_embassy ? $best_other_embassy->max_members : 0, + $self->max_members($self->level - 1) + ); + if ($alliance_members > $allowed_members) + { + confess [1013, 'You cannot downgrade this Embassy while you are an alliance leader with ' . $alliance_members . ' members.'] + } } }; @@ -286,7 +299,16 @@ before 'can_demolish' => sub { my $self = shift; my $alliance = eval{$self->alliance}; if (defined $alliance && $self->body->empire_id == $alliance->leader_id) { - confess [1013, 'You cannot demolish an Embassy while you are an alliance leader.'] + # as alliance leader, can only demolish if there's another + # embassy able to take over. + my $alliance_members = $alliance->members->count; + my $best_other_embassy = $self->body->empire + ->highest_embassy($self->body_id); + my $allowed_members = defined $best_other_embassy ? $best_other_embassy->max_members : 0; + if ($alliance_members > $allowed_members) + { + confess [1013, 'You cannot demolish this Embassy while you are an alliance leader. Reassign leadership or dissolve the alliance first.'] + } } }; diff --git a/lib/Lacuna/DB/Result/Empire.pm b/lib/Lacuna/DB/Result/Empire.pm index ba7c349d..dd4bd3e7 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -455,6 +455,8 @@ sub get_status { while (my $planet = $planet_rs->next) { $planets{$planet->id} = $planet->name; } + my $embassy = $self->highest_embassy; + $embassy = $embassy->id if $embassy; my $status = { rpc_count => $self->rpc_count, @@ -470,7 +472,7 @@ sub get_status { planets => \%planets, self_destruct_active=> $self->self_destruct_active, self_destruct_date => $self->self_destruct_date_formatted, - primary_embassy_id => $self->highest_embassy->id, + primary_embassy_id => $embassy, }; return $status; } -- 2.51.2