Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Trimming ex-code and hopefully speeding up ticking spies.

  • Loading branch information...
commit b492adc056d2bb28d72e6341aacc5433913897e6 1 parent fb5676c
@lemming552 lemming552 authored
View
109 lib/Lacuna/DB/Result/Building/IntelTraining.pm
@@ -88,114 +88,5 @@ has training_multiplier => (
}
);
-sub training_costs {
- my $self = shift;
- my $spy_id = shift;
- my $multiplier = $self->training_multiplier;
- my $costs = {
- water => 1100 * $multiplier,
- waste => 40 * $multiplier,
- energy => 100 * $multiplier,
- food => 1000 * $multiplier,
- ore => 10 * $multiplier,
- time => [],
- };
- if ($spy_id) {
- my $spy = $self->get_spy($spy_id);
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100)+1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level *
- ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- $costs->{time} = $train_time;
- }
- else {
- my $spies = $self->get_spies->search({ task => { in => ['Counter Espionage','Idle'] } });
- while (my $spy = $spies->next) {
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100)+1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level *
- ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- push @{$costs->{time}}, {
- spy_id => $spy->id,
- name => $spy->name,
- time => $train_time,
- level => $spy->level,
- offense_rating => $spy->offense,
- defense_rating => $spy->offense,
- intel => $spy->intel_xp,
- mayhem => $spy->mayhem_xp,
- politics => $spy->politics_xp,
- theft => $spy->theft_xp,
- task => $spy->task, # Should only be Idle or Counter Espionage
- based_from => {
- body_id => $spy->from_body_id,
- name => $spy->from_body->name,
- x => $spy->from_body->x,
- y => $spy->from_body->y,
- },
- };
- }
- }
- return $costs;
-}
-
-sub can_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- unless ($body->type_stored($resource) >= $costs->{$resource}) {
- confess [1011, 'Not enough '.$resource.' to train a spy.'];
- }
- }
- return 1;
-}
-
-sub spend_resources_to_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- my $spend = 'spend_'.$resource;
- $body->$spend($costs->{$resource});
- }
- $body->add_waste($costs->{waste});
-}
-
-sub train_spy {
- my ($self, $spy_id, $time_to_train) = @_;
- my $empire = $self->body->empire;
- my $spy = $self->get_spy($spy_id);
- unless ($spy->intel_xp < 2600) {
- confess [1013, $spy->name." has already learned all there is to know about Intelligence."];
- }
- unless (defined $time_to_train) {
- $time_to_train = $self->training_costs($spy_id)->{time};
- }
- $spy->is_available;
- unless ($spy->task ~~ ['Counter Espionage','Idle']) {
- confess [1011, 'Spy must be idle to train.'];
- }
- my $available_on = DateTime->now;
- $available_on->add(seconds => $time_to_train );
- my $total = $spy->intel_xp + $self->level;
- $total = 2600 if $total > 2600;
- $spy->intel_xp($total);
- $spy->update_level;
- $spy->task('Training');
- $spy->available_on($available_on);
- $spy->update;
- return $self;
-}
-
-
no Moose;
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
View
107 lib/Lacuna/DB/Result/Building/MayhemTraining.pm
@@ -91,112 +91,5 @@ has training_multiplier => (
}
);
-sub training_costs {
- my $self = shift;
- my $spy_id = shift;
- my $multiplier = $self->training_multiplier;
- my $costs = {
- water => 1100 * $multiplier,
- waste => 40 * $multiplier,
- energy => 100 * $multiplier,
- food => 1000 * $multiplier,
- ore => 10 * $multiplier,
- time => [],
- };
- if ($spy_id) {
- my $spy = $self->get_spy($spy_id);
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- $costs->{time} = $train_time;
- }
- else {
- my $spies = $self->get_spies->search({ task => { in => ['Counter Espionage','Idle'] } });
- while (my $spy = $spies->next) {
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- push @{$costs->{time}}, {
- spy_id => $spy->id,
- name => $spy->name,
- time => $train_time,
- level => $spy->level,
- offense_rating => $spy->offense,
- defense_rating => $spy->offense,
- intel => $spy->intel_xp,
- mayhem => $spy->mayhem_xp,
- politics => $spy->politics_xp,
- theft => $spy->theft_xp,
- task => $spy->task, # Should only be Idle or Counter Espionage
- based_from => {
- body_id => $spy->from_body_id,
- name => $spy->from_body->name,
- x => $spy->from_body->x,
- y => $spy->from_body->y,
- },
- };
- }
- }
- return $costs;
-}
-
-sub can_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- unless ($body->type_stored($resource) >= $costs->{$resource}) {
- confess [1011, 'Not enough '.$resource.' to train a spy.'];
- }
- }
- return 1;
-}
-
-sub spend_resources_to_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- my $spend = 'spend_'.$resource;
- $body->$spend($costs->{$resource});
- }
- $body->add_waste($costs->{waste});
-}
-
-sub train_spy {
- my ($self, $spy_id, $time_to_train) = @_;
- my $empire = $self->body->empire;
- my $spy = $self->get_spy($spy_id);
- unless ($spy->mayhem_xp < 2600) {
- confess [1013, $spy->name." has already learned all there is to know about Mayhem."];
- }
- unless (defined $time_to_train) {
- $time_to_train = $self->training_costs($spy_id)->{time};
- }
- $spy->is_available;
- unless ($spy->task ~~ ['Counter Espionage','Idle']) {
- confess [1011, 'Spy must be idle to train.'];
- }
- my $available_on = DateTime->now;
- $available_on->add(seconds => $time_to_train );
- my $total = $spy->mayhem_xp + $self->level;
- $total = 2600 if $total > 2600;
- $spy->mayhem_xp($total);
- $spy->update_level;
- $spy->task('Training');
- $spy->available_on($available_on);
- $spy->update;
- return $self;
-}
-
-
no Moose;
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
View
107 lib/Lacuna/DB/Result/Building/PoliticsTraining.pm
@@ -88,112 +88,5 @@ has training_multiplier => (
}
);
-sub training_costs {
- my $self = shift;
- my $spy_id = shift;
- my $multiplier = $self->training_multiplier;
- my $costs = {
- water => 1100 * $multiplier,
- waste => 40 * $multiplier,
- energy => 100 * $multiplier,
- food => 1000 * $multiplier,
- ore => 10 * $multiplier,
- time => [],
- };
- if ($spy_id) {
- my $spy = $self->get_spy($spy_id);
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- $costs->{time} = $train_time;
- }
- else {
- my $spies = $self->get_spies->search({ task => { in => ['Counter Espionage','Idle'] } });
- while (my $spy = $spies->next) {
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- push @{$costs->{time}}, {
- spy_id => $spy->id,
- name => $spy->name,
- time => $train_time,
- level => $spy->level,
- offense_rating => $spy->offense,
- defense_rating => $spy->offense,
- intel => $spy->intel_xp,
- mayhem => $spy->mayhem_xp,
- politics => $spy->politics_xp,
- theft => $spy->theft_xp,
- task => $spy->task, # Should only be Idle or Counter Espionage
- based_from => {
- body_id => $spy->from_body_id,
- name => $spy->from_body->name,
- x => $spy->from_body->x,
- y => $spy->from_body->y,
- },
- };
- }
- }
- return $costs;
-}
-
-sub can_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- unless ($body->type_stored($resource) >= $costs->{$resource}) {
- confess [1011, 'Not enough '.$resource.' to train a spy.'];
- }
- }
- return 1;
-}
-
-sub spend_resources_to_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- my $spend = 'spend_'.$resource;
- $body->$spend($costs->{$resource});
- }
- $body->add_waste($costs->{waste});
-}
-
-sub train_spy {
- my ($self, $spy_id, $time_to_train) = @_;
- my $empire = $self->body->empire;
- my $spy = $self->get_spy($spy_id);
- unless ($spy->politics_xp < 2600) {
- confess [1013, $spy->name." has already learned all there is to know about Politics."];
- }
- unless (defined $time_to_train) {
- $time_to_train = $self->training_costs($spy_id)->{time};
- }
- $spy->is_available;
- unless ($spy->task ~~ ['Counter Espionage','Idle']) {
- confess [1011, 'Spy must be idle to train.'];
- }
- my $available_on = DateTime->now;
- $available_on->add(seconds => $time_to_train );
- my $total = $spy->politics_xp + $self->level;
- $total = 2600 if $total > 2600;
- $spy->politics_xp($total);
- $spy->update_level;
- $spy->task('Training');
- $spy->available_on($available_on);
- $spy->update;
- return $self;
-}
-
-
no Moose;
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
View
107 lib/Lacuna/DB/Result/Building/TheftTraining.pm
@@ -88,112 +88,5 @@ has training_multiplier => (
}
);
-sub training_costs {
- my $self = shift;
- my $spy_id = shift;
- my $multiplier = $self->training_multiplier;
- my $costs = {
- water => 1100 * $multiplier,
- waste => 40 * $multiplier,
- energy => 100 * $multiplier,
- food => 1000 * $multiplier,
- ore => 10 * $multiplier,
- time => [],
- };
- if ($spy_id) {
- my $spy = $self->get_spy($spy_id);
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- $costs->{time} = $train_time;
- }
- else {
- my $spies = $self->get_spies->search({ task => { in => ['Counter Espionage','Idle'] } });
- while (my $spy = $spies->next) {
- my $xp_level = int(($spy->intel_xp + $spy->mayhem_xp + $spy->politics_xp + $spy->theft_xp)/100) + 1;
- my $train_time = sprintf('%.0f', 3600 * $xp_level * ((100 - (5 * $self->body->empire->management_affinity)) / 100));
- if ($self->body->happiness < 0) {
- my $unhappy_workers = abs($self->body->happiness)/100_000;
- $train_time = int($train_time * $unhappy_workers);
- }
- $train_time = 5184000 if ($train_time > 5184000); # Max time per spy is 60 days
- $train_time = 21600 if ($train_time < 21600); # Min time is 6 hour
- push @{$costs->{time}}, {
- spy_id => $spy->id,
- name => $spy->name,
- time => $train_time,
- level => $spy->level,
- offense_rating => $spy->offense,
- defense_rating => $spy->offense,
- intel => $spy->intel_xp,
- mayhem => $spy->mayhem_xp,
- politics => $spy->politics_xp,
- theft => $spy->theft_xp,
- task => $spy->task, # Should only be Idle or Counter Espionage
- based_from => {
- body_id => $spy->from_body_id,
- name => $spy->from_body->name,
- x => $spy->from_body->x,
- y => $spy->from_body->y,
- },
- };
- }
- }
- return $costs;
-}
-
-sub can_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- unless ($body->type_stored($resource) >= $costs->{$resource}) {
- confess [1011, 'Not enough '.$resource.' to train a spy.'];
- }
- }
- return 1;
-}
-
-sub spend_resources_to_train_spy {
- my ($self, $costs) = @_;
- my $body = $self->body;
- foreach my $resource (qw(water ore food energy)) {
- my $spend = 'spend_'.$resource;
- $body->$spend($costs->{$resource});
- }
- $body->add_waste($costs->{waste});
-}
-
-sub train_spy {
- my ($self, $spy_id, $time_to_train) = @_;
- my $empire = $self->body->empire;
- my $spy = $self->get_spy($spy_id);
- unless ($spy->theft_xp < 2600) {
- confess [1013, $spy->name." has already learned all there is to know about Theft."];
- }
- unless (defined $time_to_train) {
- $time_to_train = $self->training_costs($spy_id)->{time};
- }
- $spy->is_available;
- unless ($spy->task ~~ ['Counter Espionage','Idle']) {
- confess [1011, 'Spy must be idle to train.'];
- }
- my $available_on = DateTime->now;
- $available_on->add(seconds => $time_to_train );
- my $total = $spy->theft_xp + $self->level;
- $total = 2600 if $total > 2600;
- $spy->theft_xp($total);
- $spy->update_level;
- $spy->task('Training');
- $spy->available_on($available_on);
- $spy->update;
- return $self;
-}
-
-
no Moose;
__PACKAGE__->meta->make_immutable(inline_constructor => 0);
View
13 lib/Lacuna/DB/Result/Spies.pm
@@ -336,9 +336,16 @@ sub tick_all_spies {
my ($class,$verbose) = @_;
my $db = Lacuna->db;
- my $spies = $db->resultset('Spies')->search({
- -and => [{task => {'!=' => 'Idle'}},{task => {'!=' => 'Counter Espionage'}},{task => {'!=' => 'Mercenary Transport'}}],
- });
+ my $dtf = $db->storage->datetime_parser;
+ my $spies = Lacuna->db
+ ->resultset('Spies')
+ ->search( { available_on => { '<' => $dtf->format_datetime(DateTime->now) },
+ task => { 'not in' => ['Idle',
+ 'Counter Espionage',
+ 'Mercenary Transport',
+ 'Prisoner Transport'] },
+ });
+
# TODO further efficiencies could be made by ignoring spies not yet 'available'
while (my $spy = $spies->next) {
if ($verbose) {
View
35 lib/Lacuna/RPC/Building/IntelTraining.pm
@@ -14,41 +14,6 @@ sub model_class {
return 'Lacuna::DB::Result::Building::IntelTraining';
}
-sub train_spy {
- my ($self, $session_id, $building_id, $spy_id) = @_;
- my $empire = $self->get_empire_by_session($session_id);
- my $building = $self->get_building($empire, $building_id);
- my $body = $building->body;
- unless ($building->efficiency == 100) {
- confess [1013, "You can't train spies until your Intel Training Facility is repaired."];
- }
- if ($building->level < 1) {
- confess [1013, "You can't train spies until your Intel Training Facility is completed."];
- }
- my $spy = $building->get_spy($spy_id);
- my $trained = 0;
- my $costs = $building->training_costs($spy_id);
- my $reason;
- if (eval{$building->can_train_spy($costs)}) {
- $building->spend_resources_to_train_spy($costs);
- $building->train_spy($spy_id, $costs->{time});
- $trained++;
- }
- else {
- $reason = $@;
- }
- if ($trained) {
- $body->update;
- }
- my $quantity = 1;
- return {
- status => $self->format_status($empire, $body),
- trained => $trained,
- not_trained => $quantity - $trained,
- reason_not_trained => $reason,
- };
-}
-
around 'view' => sub {
my ($orig, $self, $session_id, $building_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
View
35 lib/Lacuna/RPC/Building/MayhemTraining.pm
@@ -14,41 +14,6 @@ sub model_class {
return 'Lacuna::DB::Result::Building::MayhemTraining';
}
-sub train_spy {
- my ($self, $session_id, $building_id, $spy_id) = @_;
- my $empire = $self->get_empire_by_session($session_id);
- my $building = $self->get_building($empire, $building_id);
- my $body = $building->body;
- unless ($building->efficiency == 100) {
- confess [1013, "You can't train spies until your Mayhem Training Facility is repaired."];
- }
- if ($building->level < 1) {
- confess [1013, "You can't train spies until your Mayhem Training Facility is completed."];
- }
- my $spy = $building->get_spy($spy_id);
- my $trained = 0;
- my $costs = $building->training_costs($spy_id);
- my $reason;
- if (eval{$building->can_train_spy($costs)}) {
- $building->spend_resources_to_train_spy($costs);
- $building->train_spy($spy_id, $costs->{time});
- $trained++;
- }
- else {
- $reason = $@;
- }
- if ($trained) {
- $body->update;
- }
- my $quantity = 1;
- return {
- status => $self->format_status($empire, $body),
- trained => $trained,
- not_trained => $quantity - $trained,
- reason_not_trained => $reason,
- };
-}
-
around 'view' => sub {
my ($orig, $self, $session_id, $building_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
View
35 lib/Lacuna/RPC/Building/PoliticsTraining.pm
@@ -14,41 +14,6 @@ sub model_class {
return 'Lacuna::DB::Result::Building::PoliticsTraining';
}
-sub train_spy {
- my ($self, $session_id, $building_id, $spy_id) = @_;
- my $empire = $self->get_empire_by_session($session_id);
- my $building = $self->get_building($empire, $building_id);
- my $body = $building->body;
- unless ($building->efficiency == 100) {
- confess [1013, "You can't train spies until your Politics Training Facility is repaired."];
- }
- if ($building->level < 1) {
- confess [1013, "You can't train spies until your Politics Training Facility is completed."];
- }
- my $spy = $building->get_spy($spy_id);
- my $trained = 0;
- my $costs = $building->training_costs($spy_id);
- my $reason;
- if (eval{$building->can_train_spy($costs)}) {
- $building->spend_resources_to_train_spy($costs);
- $building->train_spy($spy_id, $costs->{time});
- $trained++;
- }
- else {
- $reason = $@;
- }
- if ($trained) {
- $body->update;
- }
- my $quantity = 1;
- return {
- status => $self->format_status($empire, $body),
- trained => $trained,
- not_trained => $quantity - $trained,
- reason_not_trained => $reason,
- };
-}
-
around 'view' => sub {
my ($orig, $self, $session_id, $building_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
View
35 lib/Lacuna/RPC/Building/TheftTraining.pm
@@ -14,41 +14,6 @@ sub model_class {
return 'Lacuna::DB::Result::Building::TheftTraining';
}
-sub train_spy {
- my ($self, $session_id, $building_id, $spy_id) = @_;
- my $empire = $self->get_empire_by_session($session_id);
- my $building = $self->get_building($empire, $building_id);
- my $body = $building->body;
- unless ($building->efficiency == 100) {
- confess [1013, "You can't train spies until your Theft Training Facility is repaired."];
- }
- if ($building->level < 1) {
- confess [1013, "You can't train spies until your Theft Training Facility is completed."];
- }
- my $spy = $building->get_spy($spy_id);
- my $trained = 0;
- my $costs = $building->training_costs($spy_id);
- my $reason;
- if (eval{$building->can_train_spy($costs)}) {
- $building->spend_resources_to_train_spy($costs);
- $building->train_spy($spy_id, $costs->{time});
- $trained++;
- }
- else {
- $reason = $@;
- }
- if ($trained) {
- $body->update;
- }
- my $quantity = 1;
- return {
- status => $self->format_status($empire, $body),
- trained => $trained,
- not_trained => $quantity - $trained,
- reason_not_trained => $reason,
- };
-}
-
around 'view' => sub {
my ($orig, $self, $session_id, $building_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
Please sign in to comment.
Something went wrong with that request. Please try again.