From d019eeffa40300771c019fe8235062278bbf5f5c Mon Sep 17 00:00:00 2001 From: Ian Docherty Date: Sun, 23 Dec 2012 14:23:14 -0500 Subject: [PATCH] Fixed a few issues with building work_ends time --- lib/Lacuna/DB/Result/Building.pm | 29 ++++++++++- .../DB/Result/Building/SubspaceSupplyDepot.pm | 48 ++++++++++++------- lib/Lacuna/DB/Result/Building/ThemePark.pm | 4 +- lib/Lacuna/DB/Result/Schedule.pm | 2 - var/www/public/changes.txt | 3 +- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/lib/Lacuna/DB/Result/Building.pm b/lib/Lacuna/DB/Result/Building.pm index 62f80ce9..fcc3895f 100755 --- a/lib/Lacuna/DB/Result/Building.pm +++ b/lib/Lacuna/DB/Result/Building.pm @@ -698,7 +698,7 @@ sub is_not_max_level { if ($self->level >= $max_level && 'Resources' ~~ [ $self->build_tags] && (!('Storage' ~~ [$self->build_tags]) || $self->isa('Lacuna::DB::Result::Building::Waste::Exchanger'))) { -# resource buildings except storage buildings (treat a Waste Exchanger as if it were not a storage building) + # resource buildings except storage buildings (treat a Waste Exchanger as if it were not a storage building) my $stockpile = $self->body->get_building_of_class('Lacuna::DB::Result::Building::Stockpile'); if (defined $stockpile) { if ($max_level + $stockpile->extra_resource_levels > $self->level) { @@ -894,6 +894,33 @@ sub work_seconds_remaining { return ($seconds > 0) ? $seconds : 0; } + +# Reschedule work to end at a different time +# +sub reschedule_work { + my ($self, $new_work_ends) = @_; + + $self->is_working(1); + $self->work_ends($new_work_ends); + $self->update; + + my $schedule = Lacuna->db->resultset('Schedule')->search({ + parent_table => 'Building', + parent_id => $self->id, + task => 'finish_work', + }); + $schedule->delete if defined $schedule; + + Lacuna->db->resultset('Schedule')->create({ + delivery => $new_work_ends, + parent_table => 'Building', + parent_id => $self->id, + task => 'finish_work', + }); + return $self; +} + + sub start_work { my ($self, $work, $duration) = @_; my $now = DateTime->now; diff --git a/lib/Lacuna/DB/Result/Building/SubspaceSupplyDepot.pm b/lib/Lacuna/DB/Result/Building/SubspaceSupplyDepot.pm index bf551752..9e628969 100644 --- a/lib/Lacuna/DB/Result/Building/SubspaceSupplyDepot.pm +++ b/lib/Lacuna/DB/Result/Building/SubspaceSupplyDepot.pm @@ -33,7 +33,8 @@ use constant name => 'Subspace Supply Depot'; use constant time_to_build => 0; after finish_upgrade => sub { - my $self = shift; + my ($self) = @_; + $self->start_work({}, 60 * 60 * 24 * 5)->update; $self->body->empire->send_predefined_message( tags => ['Alert'], @@ -44,62 +45,73 @@ after finish_upgrade => sub { }; sub transmit_food { - my $self = shift; - unless ($self->work_ends->epoch - time > 3600 ) { + my ($self) = @_; + + if ($self->work_ends->epoch - time <= 3600 ) { confess [1011, 'Not enough energy remaining to transmit resources.'] } - $self->work_ends($self->work_ends->subtract(seconds => 3600)); - $self->update; + my $new_work_ends = $self->work_ends->subtract(seconds => 3600); + $self->reschedule_work($new_work_ends); + my @types = (FOOD_TYPES); $self->body->add_type($types[ rand @types ], 3600)->update; } sub transmit_ore { - my $self = shift; + my ($self) = @_; + unless ($self->work_ends->epoch - time > 3600 ) { confess [1011, 'Not enough energy remaining to transmit resources.'] } - $self->work_ends($self->work_ends->subtract(seconds => 3600)); - $self->update; + my $new_work_ends = $self->work_ends->subtract(seconds => 3600); + $self->reschedule_work($new_work_ends); + my @types = (ORE_TYPES); $self->body->add_type($types[ rand @types ], 3600)->update; } sub transmit_water { - my $self = shift; + my ($self) = @_; + unless ($self->work_ends->epoch - time > 3600 ) { confess [1011, 'Not enough energy remaining to transmit resources.'] } - $self->work_ends($self->work_ends->subtract(seconds => 3600)); - $self->update; + my $new_work_ends = $self->work_ends->subtract(seconds => 3600); + $self->reschedule_work($new_work_ends); + $self->body->add_type('water', 3600)->update; } sub transmit_energy { - my $self = shift; + my ($self) = @_; + unless ($self->work_ends->epoch - time > 3600 ) { confess [1011, 'Not enough energy remaining to transmit resources.'] } - $self->work_ends($self->work_ends->subtract(seconds => 3600)); - $self->update; + my $new_work_ends = $self->work_ends->subtract(seconds => 3600); + $self->reschedule_work($new_work_ends); + $self->body->add_type('energy', 3600)->update; } sub complete_build_queue { - my $self = shift; + my ($self) = @_; + my $time_to_complete = $self->body->get_existing_build_queue_time->epoch - time; if ($time_to_complete > $self->work_ends->epoch - time) { confess [1011, 'Not enough time remaining to complete the build queue.']; } - $self->work_ends($self->work_ends->subtract(seconds => $time_to_complete)); - $self->update; + my $new_work_ends = $self->work_ends->subtract(seconds => $time_to_complete); + $self->reschedule_work($new_work_ends); + foreach my $build (@{$self->body->builds}) { $build->finish_upgrade; } } after finish_work => sub { - my $self = shift; + my ($self) = @_; + my $body = $self->body; $body->needs_surface_refresh(1); $body->needs_recalc(1); diff --git a/lib/Lacuna/DB/Result/Building/ThemePark.pm b/lib/Lacuna/DB/Result/Building/ThemePark.pm index 2bb209e4..e16ac43e 100644 --- a/lib/Lacuna/DB/Result/Building/ThemePark.pm +++ b/lib/Lacuna/DB/Result/Building/ThemePark.pm @@ -106,9 +106,9 @@ sub operate { $body->update; if ($self->is_working) { - $self->work_ends($self->work_ends->add(seconds=>60 * 60)); + my $new_work_ends = $self->work_ends->add(seconds => 3600); $self->work({ food_type_count => $types }); - $self->update; + $self->reschedule_work($new_work_ends); } else { $self->start_work({ food_type_count => $types }, 60*60)->update; diff --git a/lib/Lacuna/DB/Result/Schedule.pm b/lib/Lacuna/DB/Result/Schedule.pm index 98ff9fdc..355eb7b2 100644 --- a/lib/Lacuna/DB/Result/Schedule.pm +++ b/lib/Lacuna/DB/Result/Schedule.pm @@ -63,8 +63,6 @@ sub queue_for_delivery { $delay = 0 if $delay < 0; my $now = DateTime->now; - print STDERR "DELIVERY: now: $now delivery: $delivery delay: $delay table: ".$self->parent_table." task: ".$self->task."\n"; - my $queue = Lacuna->queue || 'default'; my $priority = $self->priority || 1000; my $job = $queue->publish($self->queue, diff --git a/var/www/public/changes.txt b/var/www/public/changes.txt index e583636d..15414ab1 100644 --- a/var/www/public/changes.txt +++ b/var/www/public/changes.txt @@ -1,12 +1,13 @@ 3.0876: - Add: Seasonal gift - - Fix: Deployed bleeders - Fix: Add glyph medals for excav finds. - Fix: Compare vs max spies, not level when downgrading IntMin. - Fix: Downgrading your capitol should be possible to level 10 if you have a stockpile. - Fix: Recalc chains properly on Move System. - Fix: BHG Jump Zone checks alliance id's of seized stars on target. - Fix: Target planet SAWs will not fire on incoming allied ships. + - Fix: Resolved problem with deployed bleeders + - Fix: Resolved issue with Sub Space Depot not finishing work when used 3.0875: - Fix: Spy count check on insurrection. -- 2.51.2