Skip to content
This repository
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 68 lines (50 sloc) 1.634 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#!/usr/bin/perl

use strict;

use Client;
use Getopt::Long;
use JSON::PP;

my $config_name = "config.json";
my $body_name;

GetOptions(
  "config=s" => \$config_name,
  "body=s" => \$body_name,
) or die "$0 --config=foo.json --body=Bar\n";

my $client = Client->new(config => $config_name);
my $body_id;
if ($body_name) {
  my $planets = $client->empire_status->{planets};
  for my $id (keys(%$planets)) {
    $body_id = $id if $planets->{$id} =~ /$body_name/;
  }
  die "No matching planet for name $body_name\n" unless $body_id;
} else {
  $body_id = $client->empire_status->{home_planet_id};
}

my $result = $client->body_buildings($body_id);
my $time = time();

warn "Time delta: ".($time - $result->{status}{_time})."\n";

my @buildings;

for my $id (keys %{$result->{buildings}}) {
  my $name = $result->{buildings}{$id}{name};
  my $level = $result->{buildings}{$id}{level};
  push(@buildings, { name => $name, level => $level });
}

my %stored;

for my $resource (qw(food ore water energy waste)) {
  $stored{$resource} = resource_info($result->{status}, $resource, $time);
}

print encode_json({ buildings => [ @buildings ], stored => { %stored }, time => $time })."\n";

exit(0);

sub resource_info {
  my $status = shift;
  my $resource = shift;
  my $time = shift;

  my $body = $status->{body};

  my $stored = $body->{"${resource}_stored"};
  my $capacity = $body->{"${resource}_capacity"};
  my $production = $body->{"${resource}_hour"};

  my $current = $stored + $production * ($time - $status->{_time}) / 3600;
  $current = 0 if $current < 0;
  $current = $capacity if $current > $capacity;

  return $current;
}
Something went wrong with that request. Please try again.