diff --git a/bin/clean_up_empires.pl b/bin/clean_up_empires.pl index 4cbd5e32..5fa0a0ad 100644 --- a/bin/clean_up_empires.pl +++ b/bin/clean_up_empires.pl @@ -5,7 +5,7 @@ use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; use List::Util qw(shuffle); -use Lacuna::Util qw(randint format_date to_seconds); +use Lacuna::Util qw(randint format_date); use Getopt::Long; $|=1; our $quiet; @@ -31,7 +31,7 @@ my $delete_tally = 0; my $active_duration = 0; while (my $empire = $to_be_deleted->next) { $delete_tally++; - $active_duration += to_seconds($start - $empire->date_created); + $active_duration += $start->epoch - $empire->date_created->epoch; $empire->delete; } @@ -40,7 +40,7 @@ my $old_half_created = $start->clone->subtract(hours => 2); $to_be_deleted = $empires->search({ stage => 'new', date_created => { '<' => $old_half_created }}); while (my $empire = $to_be_deleted->next) { $delete_tally++; - $active_duration += to_seconds($start - $empire->date_created); + $active_duration += $start->epoch - $empire->date_created->epoch; $empire->delete; } @@ -76,9 +76,9 @@ while (my $empire = $inactives->next) { $empire->enable_self_destruct; } -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start->epoch)/60)." minutes have elapsed"); ############### diff --git a/bin/determine_lottery_winner.pl b/bin/determine_lottery_winner.pl index 10898907..4c98a915 100644 --- a/bin/determine_lottery_winner.pl +++ b/bin/determine_lottery_winner.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(format_date to_seconds randint); +use Lacuna::Util qw(format_date randint); use Getopt::Long; $|=1; @@ -14,7 +14,7 @@ GetOptions( ); out('Started'); -my $start = DateTime->now; +my $start = time; out('Loading DB'); our $db = Lacuna->db; @@ -34,9 +34,9 @@ if ($empire_id) { } } -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start)/60)." minutes have elapsed"); ############### diff --git a/bin/summarize_economy.pl b/bin/summarize_economy.pl index acfb494c..18d8ad50 100644 --- a/bin/summarize_economy.pl +++ b/bin/summarize_economy.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(randint format_date to_seconds); +use Lacuna::Util qw(randint format_date); use Getopt::Long; use DateTime; use DateTime::Format::Strptime; @@ -33,8 +33,7 @@ if ($all) { $economy_log->delete; my $oldest = $essentia_log->search->get_column('date_stamp')->min; $oldest = DateTime::Format::Strptime->new(pattern=>'%F')->parse_datetime($oldest); - my $now = DateTime->now; - while ($oldest < $now) { + while ($oldest < $start) { summarize($oldest); $oldest->add(days=>1); } @@ -44,9 +43,9 @@ else { } -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start->epoch)/60)." minutes have elapsed"); ############### diff --git a/bin/summarize_server.pl b/bin/summarize_server.pl index 1e0c32fe..7e39ea65 100644 --- a/bin/summarize_server.pl +++ b/bin/summarize_server.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(format_date to_seconds); +use Lacuna::Util qw(format_date); use Getopt::Long; use JSON; use SOAP::Amazon::S3; @@ -19,7 +19,7 @@ GetOptions( ); out('Started'); -my $start = DateTime->now; +my $start = time; out('Loading DB'); our $db = Lacuna->db; @@ -35,9 +35,9 @@ rank_empires(); rank_alliances(); generate_overview(); -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start)/60)." minutes have elapsed"); ############### @@ -400,7 +400,7 @@ sub summarize_spies { defense_success_rate_delta => 0, success_rate => $success_rate, success_rate_delta => 0, - age => to_seconds(DateTime->now - $spy->date_created), + age => time - $spy->date_created->epoch, times_captured => $spy->times_captured, times_turned => $spy->times_turned, seeds_planted => $spy->seeds_planted, diff --git a/bin/tick_planets.pl b/bin/tick_planets.pl index 70303859..d7edafee 100644 --- a/bin/tick_planets.pl +++ b/bin/tick_planets.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(randint format_date to_seconds); +use Lacuna::Util qw(randint format_date); use Getopt::Long; $|=1; our $quiet; @@ -13,7 +13,7 @@ GetOptions( out('Started'); -my $start = DateTime->now; +my $start = time; out('Loading DB'); our $db = Lacuna->db; @@ -25,9 +25,9 @@ while (my $planet = $planets->next) { $planet->tick; } -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start)/60)." minutes have elapsed"); ############### diff --git a/bin/trickle_essentia_veins.pl b/bin/trickle_essentia_veins.pl index 4d1b7d11..af3cbba8 100644 --- a/bin/trickle_essentia_veins.pl +++ b/bin/trickle_essentia_veins.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(format_date to_seconds); +use Lacuna::Util qw(format_date); use Getopt::Long; $|=1; @@ -14,7 +14,7 @@ GetOptions( ); out('Started'); -my $start = DateTime->now; +my $start = time; out('Loading DB'); our $db = Lacuna->db; @@ -22,9 +22,9 @@ our $db = Lacuna->db; trickle(); -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start)/60)." minutes have elapsed"); ############### diff --git a/bin/weekly_medals.pl b/bin/weekly_medals.pl index 8fd2c65a..afa764a2 100644 --- a/bin/weekly_medals.pl +++ b/bin/weekly_medals.pl @@ -3,7 +3,7 @@ use strict; use lib '/data/Lacuna-Server/lib'; use Lacuna::DB; use Lacuna; -use Lacuna::Util qw(format_date to_seconds); +use Lacuna::Util qw(format_date); use Getopt::Long; $|=1; @@ -14,7 +14,7 @@ GetOptions( ); out('Started'); -my $start = DateTime->now; +my $start = time; out('Loading DB'); our $db = Lacuna->db; @@ -26,9 +26,9 @@ spies(); colonies(); empires(); -my $finish = DateTime->now; +my $finish = time; out('Finished'); -out((to_seconds($finish - $start)/60)." minutes have elapsed"); +out((($finish - $start)/60)." minutes have elapsed"); ############### diff --git a/lib/Lacuna/DB/Result/Building.pm b/lib/Lacuna/DB/Result/Building.pm index 648c1560..7973cf86 100644 --- a/lib/Lacuna/DB/Result/Building.pm +++ b/lib/Lacuna/DB/Result/Building.pm @@ -5,7 +5,7 @@ no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; use Lacuna::Constants ':all'; use List::Util qw(shuffle); -use Lacuna::Util qw(format_date to_seconds); +use Lacuna::Util qw(format_date); __PACKAGE__->load_components('DynamicSubclass'); __PACKAGE__->table('building'); @@ -185,7 +185,7 @@ sub consumption_hour { sub farming_production_bonus { my ($self) = @_; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->food_boost) ? 25 : 0; + my $boost = (time < $empire->food_boost->epoch) ? 25 : 0; return (100 + $boost + $empire->farming_affinity * 3) / 100; } @@ -372,7 +372,7 @@ sub food_hour { sub energy_production_bonus { my ($self) = @_; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->energy_boost) ? 25 : 0; + my $boost = (time < $empire->energy_boost->epoch) ? 25 : 0; return (100 + $boost + $empire->science_affinity * 3) / 100; } @@ -398,7 +398,7 @@ sub mining_production_bonus { my $refinery = $self->body->refinery; my $refinery_bonus = (defined $refinery) ? $refinery->level * 5 : 0; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->ore_boost) ? 25 : 0; + my $boost = (time < $empire->ore_boost->epoch) ? 25 : 0; return (100 + $boost + $refinery_bonus + $empire->mining_affinity * 3) / 100; } @@ -422,7 +422,7 @@ sub ore_hour { sub water_production_bonus { my ($self) = @_; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->water_boost) ? 25 : 0; + my $boost = (time < $empire->water_boost->epoch) ? 25 : 0; return (100 + $boost + $empire->environmental_affinity * 3) / 100; } @@ -468,7 +468,7 @@ sub waste_hour { sub happiness_production_bonus { my ($self) = @_; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->happiness_boost) ? 25 : 0; + my $boost = (time < $empire->happiness_boost->epoch) ? 25 : 0; return (100 + $boost + ($empire->political_affinity * 6)) / 100; } @@ -497,7 +497,7 @@ has storage_bonus => ( default => sub { my ($self) = @_; my $empire = $self->body->empire; - my $boost = (DateTime->now < $empire->storage_boost) ? 25 : 0; + my $boost = (time < $empire->storage_boost->epoch) ? 25 : 0; return (100 + $boost) / 100; }, ); @@ -622,13 +622,12 @@ sub downgrade { sub upgrade_status { my ($self) = @_; - my $now = DateTime->now; my $complete = $self->upgrade_ends; if ($self->is_upgrading) { return { - seconds_remaining => to_seconds($complete - $now), + seconds_remaining => $complete->epoch - time, start => format_date($self->upgrade_started), - end => format_date($self->upgrade_ends), + end => format_date($complete), }; } else { @@ -809,7 +808,7 @@ sub work_started_formatted { sub work_seconds_remaining { my ($self) = @_; return 0 unless $self->is_working; - my $seconds = to_seconds($self->work_ends - DateTime->now); + my $seconds = $self->work_ends - time; return ($seconds > 0) ? $seconds : 0; } diff --git a/lib/Lacuna/DB/Result/Building/Development.pm b/lib/Lacuna/DB/Result/Building/Development.pm index 2c1c9efe..bde44d90 100644 --- a/lib/Lacuna/DB/Result/Building/Development.pm +++ b/lib/Lacuna/DB/Result/Building/Development.pm @@ -3,7 +3,6 @@ package Lacuna::DB::Result::Building::Development; use Moose; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; -use Lacuna::Util qw(to_seconds); sub subsidize_build_queue { my ($self) = @_; @@ -30,13 +29,13 @@ sub format_build_queue { my ($self) = @_; my @queue; my $builds = $self->body->builds; - my $now = DateTime->now; + my $now = time; while (my $build = $builds->next) { push @queue, { building_id => $build->id, name => $build->name, to_level => ($build->level + 1), - seconds_remaining => to_seconds($build->upgrade_ends - $now), + seconds_remaining => $build->upgrade_ends->epoch - $now, x => $build->x, y => $build->y, }; diff --git a/lib/Lacuna/DB/Result/Building/Park.pm b/lib/Lacuna/DB/Result/Building/Park.pm index 10bce2c5..8fcd1870 100644 --- a/lib/Lacuna/DB/Result/Building/Park.pm +++ b/lib/Lacuna/DB/Result/Building/Park.pm @@ -5,7 +5,6 @@ no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; use DateTime; use Lacuna::Constants qw(FOOD_TYPES); -use Lacuna::Util qw(to_seconds); around 'build_tags' => sub { my ($orig, $class) = @_; diff --git a/lib/Lacuna/DB/Result/Building/Shipyard.pm b/lib/Lacuna/DB/Result/Building/Shipyard.pm index 66acbc60..85adbca2 100644 --- a/lib/Lacuna/DB/Result/Building/Shipyard.pm +++ b/lib/Lacuna/DB/Result/Building/Shipyard.pm @@ -3,7 +3,7 @@ package Lacuna::DB::Result::Building::Shipyard; use Moose; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building'; -use Lacuna::Util qw(to_seconds format_date); +use Lacuna::Util qw(format_date); use DateTime; around 'build_tags' => sub { diff --git a/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm b/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm index 1ea120c5..c8e5363e 100644 --- a/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm +++ b/lib/Lacuna/DB/Result/Building/Waste/Recycling.pm @@ -3,7 +3,6 @@ package Lacuna::DB::Result::Building::Waste::Recycling; use Moose; no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Building::Waste'; -use Lacuna::Util qw(to_seconds); has max_recycle => ( is => 'ro', diff --git a/lib/Lacuna/DB/Result/Map/Body/Planet.pm b/lib/Lacuna/DB/Result/Map/Body/Planet.pm index 36081c6a..d02a7a5d 100644 --- a/lib/Lacuna/DB/Result/Map/Body/Planet.pm +++ b/lib/Lacuna/DB/Result/Map/Body/Planet.pm @@ -5,7 +5,7 @@ no warnings qw(uninitialized); extends 'Lacuna::DB::Result::Map::Body'; use Lacuna::Constants qw(FOOD_TYPES ORE_TYPES BUILDABLE_CLASSES); use List::Util qw(shuffle); -use Lacuna::Util qw(to_seconds randint format_date); +use Lacuna::Util qw(randint format_date); use DateTime; no warnings 'uninitialized'; @@ -128,7 +128,7 @@ around get_status => sub { $self->tick; # in case what we just did is going to change our stats } unless ($empire->is_isolationist) { # don't need to warn about incoming ships if can't be attacked - my $now = DateTime->now; + my $now = time; my $incoming_ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search( { foreign_body_id => $self->id, @@ -137,7 +137,7 @@ around get_status => sub { } ); while (my $ship = $incoming_ships->next) { - if ($ship->date_available <= $now) { + if ($ship->date_available->epoch <= $now) { $ship->body->tick; } else { @@ -667,6 +667,7 @@ sub tick { } my $now = DateTime->now; + my $now_epoch = $now->epoch; my %todo; my $i; # in case 2 things finish at exactly the same time @@ -685,13 +686,13 @@ sub tick { ], }); while (my $building = $buildings->next) { - if ($building->is_upgrading && $building->upgrade_ends <= $now) { + if ($building->is_upgrading && $building->upgrade_ends->epoch <= $now_epoch) { $todo{format_date($building->upgrade_ends).$i} = { object => $building, type => 'building upgraded', }; } - if ($building->is_working && $building->work_ends <= $now) { + if ($building->is_working && $building->work_ends->epoch <= $now_epoch) { $todo{format_date($building->work_ends).$i} = { object => $building, type => 'building work complete', @@ -750,7 +751,7 @@ sub tick { my $still_enabled = 0; foreach my $resource (qw(energy water ore happiness food storage)) { my $boost = $resource.'_boost'; - if ($now > $empire->$boost) { + if ($now_epoch > $empire->$boost->epoch) { $self->needs_recalc(1); } else { @@ -782,8 +783,7 @@ sub tick { sub tick_to { my ($self, $now) = @_; - my $interval = $now - $self->last_tick; - my $seconds = to_seconds($interval); + my $seconds = $now->epoch - $self->last_tick->epoch; my $tick_rate = $seconds / 3600; $self->last_tick($now); if ($self->needs_recalc) { diff --git a/lib/Lacuna/DB/Result/Ships.pm b/lib/Lacuna/DB/Result/Ships.pm index c2694be7..6c2fd8f6 100644 --- a/lib/Lacuna/DB/Result/Ships.pm +++ b/lib/Lacuna/DB/Result/Ships.pm @@ -3,7 +3,7 @@ package Lacuna::DB::Result::Ships; use Moose; no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; -use Lacuna::Util qw(format_date to_seconds randint); +use Lacuna::Util qw(format_date randint); use DateTime; use feature "switch"; @@ -146,7 +146,7 @@ sub get_status { sub seconds_remaining { my $self = shift; - return to_seconds(DateTime->now - $self->date_available); + return time - $self->date_available->epoch; } sub turn_around { diff --git a/lib/Lacuna/DB/Result/Spies.pm b/lib/Lacuna/DB/Result/Spies.pm index a2f1da5f..0500e959 100644 --- a/lib/Lacuna/DB/Result/Spies.pm +++ b/lib/Lacuna/DB/Result/Spies.pm @@ -3,7 +3,7 @@ package Lacuna::DB::Result::Spies; use Moose; no warnings qw(uninitialized); extends 'Lacuna::DB::Result'; -use Lacuna::Util qw(format_date to_seconds randint random_element); +use Lacuna::Util qw(format_date randint random_element); use DateTime; use feature "switch"; use Lacuna::Constants qw(ORE_TYPES FOOD_TYPES); @@ -97,9 +97,9 @@ sub format_started_assignment { sub seconds_remaining_on_assignment { my $self = shift; - my $now = DateTime->now; - if ($self->available_on > $now) { - return to_seconds($self->available_on - $now); + my $now = time; + if ($self->available_on->epoch > $now) { + return $self->available_on->epoch - $now; } else { return 0; @@ -112,7 +112,7 @@ sub is_available { if ($task ~~ ['Idle','Counter Espionage']) { return 1; } - elsif (DateTime->now > $self->available_on) { + elsif (time > $self->available_on->epoch) { if ($task eq 'Debriefing') { $self->task('Counter Espionage'); $self->update; @@ -137,7 +137,7 @@ sub is_available { } elsif ($task eq 'Travelling') { my $infiltration_time = $self->available_on->clone->add(hours => 1); - if ($infiltration_time > DateTime->now && $self->empire_id ne $self->on_body->empire_id) { + if ($infiltration_time->epoch > time && $self->empire_id ne $self->on_body->empire_id) { $self->task('Infiltrating'); $self->started_assignment(DateTime->now); $self->available_on($infiltration_time); diff --git a/lib/Lacuna/RPC/Building/SpacePort.pm b/lib/Lacuna/RPC/Building/SpacePort.pm index 526a7acb..0958e9c5 100644 --- a/lib/Lacuna/RPC/Building/SpacePort.pm +++ b/lib/Lacuna/RPC/Building/SpacePort.pm @@ -435,10 +435,10 @@ sub view_foreign_ships { my $building = $self->get_building($empire, $building_id); $page_number ||= 1; my @fleet; - my $now = DateTime->now; + my $now = time; my $ships = $building->foreign_ships->search({}, {rows=>25, page=>$page_number, join => 'body' }); while (my $ship = $ships->next) { - if ($ship->date_available <= $now) { + if ($ship->date_available->epoch <= $now) { $ship->body->tick; } else { diff --git a/lib/Lacuna/Tutorial.pm b/lib/Lacuna/Tutorial.pm index 72765c6e..2aa2be85 100644 --- a/lib/Lacuna/Tutorial.pm +++ b/lib/Lacuna/Tutorial.pm @@ -443,8 +443,8 @@ sub essentia { my $empire = $self->empire; my $home = $empire->home_planet; if ($finish) { - my $now = DateTime->now; - if ($empire->food_boost >= $now && $empire->water_boost >= $now && $empire->ore_boost >= $now && $empire->energy_boost >= $now) { + my $now = time; + if ($empire->food_boost->epoch >= $now && $empire->water_boost->epoch >= $now && $empire->ore_boost->epoch >= $now && $empire->energy_boost->epoch >= $now) { $home->add_trona(700); $home->add_bread(700); $home->add_energy(700); diff --git a/lib/Lacuna/Util.pm b/lib/Lacuna/Util.pm index a6464f31..d75d6bf9 100644 --- a/lib/Lacuna/Util.pm +++ b/lib/Lacuna/Util.pm @@ -6,12 +6,8 @@ use DateTime::Format::Duration; use DateTime::Format::Strptime; require Exporter; @ISA = qw(Exporter); -@EXPORT_OK = qw(randint to_seconds format_date random_element); +@EXPORT_OK = qw(randint format_date random_element); -sub to_seconds { - my $duration = shift; - return DateTime::Format::Duration->new(pattern=>'%s')->format_duration($duration); -} sub format_date { my ($date, $format) = @_;