diff --git a/docs/Empire.pod b/docs/Empire.pod index 126f00aa..ae2ed388 100644 --- a/docs/Empire.pod +++ b/docs/Empire.pod @@ -228,7 +228,8 @@ B You should probably B call this method directly, as it is a wast }, "tech_level" : 20, # Highests level university has gotten to. "self_destruct_active" : 0, - "self_destruct_date" : "" + "self_destruct_date" : "", + "primary_embassy_id" : 234567 } } diff --git a/lib/Lacuna/DB.pm b/lib/Lacuna/DB.pm index b819ce89..da734907 100644 --- a/lib/Lacuna/DB.pm +++ b/lib/Lacuna/DB.pm @@ -20,27 +20,24 @@ sub sqlt_deploy_hook { # 1: Lacuna Expanse Corp sub empire { my ($self, $id) = @_; - my $empires = $self->resultset('Lacuna::DB::Result::Empire'); + + my $empires = $self->resultset('Empire'); my %where; # set the where hash - if (ref $id && ref $id eq 'HASH') - { + if (ref $id && ref $id eq 'HASH') { %where = %$id; } # or a C for name - elsif ($id =~ /[%_]/) - { + elsif ($id =~ /[%_]/) { $where{name} = { like => $id }; } # or just the name (hopefully no names are purely numeric) - elsif ($id =~ /\D/) - { + elsif ($id =~ /\D/) { $where{name} = $id; } # or the numeric ID (probably most useful for testing) - else - { + else { $where{id} = $id; } @@ -50,7 +47,7 @@ sub empire { # similarly, a lot of typing can be saved with Lacuna->db->building($id) sub building { my ($self, $building_id) = @_; - my $building = $self->resultset('Lacuna::DB::Result::Building')->find($building_id); + my $building = $self->resultset('Building')->find($building_id); } no Moose; diff --git a/lib/Lacuna/DB/Result/Building/Embassy.pm b/lib/Lacuna/DB/Result/Building/Embassy.pm index 82d1e81c..4f842b45 100644 --- a/lib/Lacuna/DB/Result/Building/Embassy.pm +++ b/lib/Lacuna/DB/Result/Building/Embassy.pm @@ -283,13 +283,12 @@ before 'can_downgrade' => sub { if (defined $alliance && $self->body->empire_id == $alliance->leader_id) { my $alliance_members = $alliance->members->count; my $best_other_embassy = $self->body->empire - ->highest_embassy($self->body_id); + ->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) - { + 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.'] } } @@ -303,10 +302,9 @@ before 'can_demolish' => sub { # embassy able to take over. my $alliance_members = $alliance->members->count; my $best_other_embassy = $self->body->empire - ->highest_embassy($self->body_id); + ->highest_embassy($self->body_id); my $allowed_members = defined $best_other_embassy ? $best_other_embassy->max_members : 0; - if ($alliance_members > $allowed_members) - { + 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 dd4bd3e7..5c19c017 100644 --- a/lib/Lacuna/DB/Result/Empire.pm +++ b/lib/Lacuna/DB/Result/Empire.pm @@ -455,8 +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 $embassy = $self->highest_embassy; + my $embassy_id = defined $embassy ? $embassy->id : undef; my $status = { rpc_count => $self->rpc_count, @@ -472,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 => $embassy, + primary_embassy_id => $embassy_id, }; return $status; } @@ -1088,16 +1088,22 @@ sub pay_taxes { sub highest_embassy { my ($self, $excluding_body_id) = @_; - my %where = ( - 'body.empire_id' => $self->id, - 'me.class' => 'Lacuna::DB::Result::Building::Embassy' - ); - $where{body_id} = { '!=' => $excluding_body_id } if defined $excluding_body_id; - my $search = Lacuna->db->resultset('Lacuna::DB::Result::Building') - ->search(\%where, { join => 'body', order_by => { -desc => 'level' } }) - ->single(); - - return $search; + + my $search_rs = Lacuna->db->resultset('Building')->search({ + 'body.empire_id' => $self->id, + 'me.class' => 'Lacuna::DB::Result::Building::Embassy', + },{ + join => 'body', + order_by => { -desc => 'level' }, + }); + if (defined $excluding_body_id) { + $search_rs = $search_rs->search({ + body_id => { "!=" => $excluding_body_id }, + }); + } + my $embassy = $search_rs->search->single(); + + return $embassy; } no Moose;