Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added some documentation for where I'm going with this stuff

  • Loading branch information...
commit c598632f1e903b1bde9adcf0c5aa4faeb08b4c3d 1 parent 93b42e0
Xaositect authored
Showing with 86 additions and 5 deletions.
  1. +52 −0 Client/TaskRunner.pm
  2. +30 −2 Tie/Lacuna.pm
  3. +4 −3 t/test_tie_lacuna.t.pl
View
52 Client/TaskRunner.pm
@@ -3,6 +3,58 @@ package Client::TaskRunner;
use Moose;
use List::Util qw( min max first );
+=pod
+
+=head1 Description
+
+A demultiplexer for the Lacuna Client. Set up Client::Tasks with schedules.
+ - All work shares the same Client object and cache
+ - No possibility of multiple scripts stepping on your rpc rate limit
+ - Just-in-time scheduling of work
+
+=head1 Usage
+
+$client = Client->new(config => $ARGV[0]);
+$runner = Client::TaskRunner->new('client' => $client);
+
+# set up a simple periodic task to check for a specific email then stop
+$runner->add_task(Client::Task->new(
+ 'repeat_after' => 30, # seconds
+ 'callback' => sub {
+ my ($self, $runner) = @_;
+ my $client = $runner->client();
+ ... check email ...
+ if $email =~ /I accept your terms/ {
+ $self->stop_repeating();
+ } else {
+ ... build more snarks ...
+ }
+ }
+));
+
+# set up a cron-scheduled task that creates new one-off tasks that run at exactly the right time.
+# in this case, maybe we want to automatically ship spies to another planet to post them on the merc guild
+$auto_spy_auction = Client::Task->new(
+ 'cron_spec' => DateTime::Event::Cron->new('0 * * * *'),
+ 'callback' => sub {
+ my ($self, $runner) = @_;
+ ... for empty IntMin slots ... {
+ ... train spy, get spy id, calculate time until training finishes ...
+ $runner->add_task(Client::Task->new('next_run' => $training_time, 'callback' => sub {
+ my ($self, $runner) = @_;
+ ... ship spy to your merc guild planet, get time until arrival ...
+ $runner->add_task(Client::Task->new( 'next_run' => $arrival_time, 'callback' => sub { ... put spy up for auction ... } ));
+ }));
+ }
+ }
+);
+$runner->add_task($auto_spy_auction);
+
+# run all tasks on schedule until we don't have any more to run
+$runner->run();
+
+=cut
+
has 'client' => (
is => 'ro',
isa => 'Client', # Lacuna client, that is
View
32 Tie/Lacuna.pm
@@ -7,6 +7,34 @@ use Data::Dumper;
our @ISA = 'Tie::Hash';
+=pod
+
+=head1 Description
+
+A hash-like interface for Lacuna data access.
+Relies heavily on Client.pm's caching layer.
+
+=head1 Usage
+
+my $lacuna = Tie::Lacuna::get_tie();
+
+# get empire id
+my $empire_id = $lacuna->{'empire'}->{id};
+
+#get planet status by id
+my $planet_status = $lacuna->{'planets'}->{$planet_id}->{status};
+$ or name
+my $planet_status = $lacuna->{'planets'}->{'Planet name here'}->{status};
+
+#get hashref of buildings on a planet (keyed by id)
+my $buildings = $lacuna->{planets}->{'Planet name here'}->{buildings};
+
+# list of ships on a planet
+my @ships = $lacuna->{planets}->{'Planet name here'}->{ships};
+
+=cut
+
+
{
#$lacuna->{planets}->{'foo'}->{ships}
#$lacuna->{planets}->{'foo'}->{buildings}
@@ -16,7 +44,7 @@ our @ISA = 'Tie::Hash';
'planets' => '_get_planets',
},
'planet' => {
- (map { $_ => '_get_planet' } qw( status buildings body )),
+ (map { $_ => '_get_planet' } qw( status buildings body name )),
'ships' => '_get_ships',
},
};
@@ -79,7 +107,7 @@ sub _get_planets {
my $planets_by_id = $client->empire_status()->{'planets'};
my $planets_by_name = {
map {
- my $planet = get_tie($client, 'planet', { 'id' => $_ });
+ my $planet = get_tie($client, 'planet', { 'id' => $_, 'name' => $planets_by_id->{$_} });
(
$planets_by_id->{$_} => $planet,
$_ => $planet
View
7 t/test_tie_lacuna.t.pl
@@ -32,7 +32,8 @@ BEGIN
my $planet_name = [keys %{ $lacuna->{planets} }]->[0];
my $planet = $lacuna->{planets}->{ $planet_name };
diag("Using planet: $planet_name ($planet->{id})");
-is_deeply([sort keys %$planet], [ qw( body buildings id ships status ) ], 'planet has the right keys');
+is_deeply([sort keys %$planet], [ qw( body buildings id name ships status ) ], 'planet has the right keys');
+is($planet->{name}, $planet_name, 'planet name is populated');
my $buildings = $planet->{'buildings'};
isa_ok($buildings, 'HASH', 'autofetch $lacuna->{planets}->{"'.$planet_name.'"}->{buildings}');
@@ -42,7 +43,7 @@ BEGIN
my $ships = $planet->{ships};
isa_ok($ships, 'ARRAY', 'autofetch $lacuna->{empire}->{"The Beginning"}->{ships}');
+diag(Dumper($ships));
-
-done_testing(13);
+done_testing(14);
Please sign in to comment.
Something went wrong with that request. Please try again.