Guest User

Untitled

a guest
May 16th, 2018
147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.61 KB | None | 0 0
  1. #!/usr/bin/perl
  2.  
  3. use strict;
  4. use warnings;
  5. use Getopt::Long qw(GetOptions);
  6. use List::Util qw( first );
  7. use FindBin;
  8. use lib "$FindBin::Bin/../lib";
  9. use Games::Lacuna::Client ();
  10.  
  11. my $planet_name;
  12. my $assignment;
  13.  
  14. GetOptions(
  15. 'planet=s' => \$planet_name,
  16. 'training=s' => \$assignment,
  17. );
  18.  
  19. usage() if !$planet_name || !$assignment;
  20.  
  21. my %trainhash = (
  22. theft => 'TheftTraining',
  23. intel => 'IntelTraining',
  24. politics => 'PoliticsTraining',
  25. mayhem => 'MayhemTraining',
  26. );
  27.  
  28. unless ( first { $_ eq $assignment } keys %trainhash ) {
  29. die("Must specify one of the following training types:\n\n", join("\n", keys %trainhash), "\n");
  30. }
  31.  
  32. my $cfg_file = shift(@ARGV) || 'lacuna.yml';
  33. unless ( $cfg_file and -e $cfg_file ) {
  34. $cfg_file = eval{
  35. require File::HomeDir;
  36. require File::Spec;
  37. my $dist = File::HomeDir->my_dist_config('Games-Lacuna-Client');
  38. File::Spec->catfile(
  39. $dist,
  40. 'login.yml'
  41. ) if $dist;
  42. };
  43. unless ( $cfg_file and -e $cfg_file ) {
  44. die "Did not provide a config file";
  45. }
  46. }
  47.  
  48. my $client = Games::Lacuna::Client->new(
  49. cfg_file => $cfg_file,
  50. prompt_captcha => 1,
  51. # debug => 1,
  52. );
  53.  
  54. # Load the planets
  55. my $empire = $client->empire->get_status->{empire};
  56.  
  57. # reverse hash, to key by name instead of id
  58. my %planets = reverse %{ $empire->{planets} };
  59.  
  60. my $body = $client->body( id => $planets{$planet_name} );
  61. my $buildings = $body->get_buildings->{buildings};
  62.  
  63. my $intel_id = first {
  64. $buildings->{$_}->{url} eq '/intelligence'
  65. } keys %$buildings;
  66.  
  67. my $intel = $client->building( id => $intel_id, type => 'Intelligence' );
  68. my @spies;
  69.  
  70. my $building_id = first {
  71. $buildings->{$_}->{url} eq "/${assignment}training"
  72. } keys %$buildings;
  73.  
  74. unless ( $building_id ) {
  75. die("No $assignment training facility found on $planet_name.")
  76. }
  77.  
  78. my $building = $client->building( id => $building_id, type => $trainhash{ $assignment } );
  79.  
  80. SPY:
  81. for my $spy ( @{ $intel->view_spies->{spies} } ) {
  82.  
  83. next SPY unless $spy->{assignment} eq 'Idle';
  84.  
  85. my $return;
  86. eval {
  87. $return = $building->train_spy( $spy->{ id } );
  88. };
  89.  
  90. if ($@) {
  91. warn "Error: $@\n";
  92. next;
  93. }
  94.  
  95. print( $return->{trained} ? "Spy trained\n" : "Spy not trained\n" );
  96. }
  97.  
  98. exit;
  99.  
  100.  
  101. sub usage {
  102. die <<"END_USAGE";
  103. Usage: $0 CONFIG_FILE
  104. --planet PLANET
  105. --training TYPE
  106.  
  107. CONFIG_FILE defaults to 'lacuna.yml'
  108.  
  109. --planet is the planet that your spy is from.
  110.  
  111. --training must match one of the following: intel, mayhem, theft, politics
  112.  
  113. END_USAGE
  114.  
  115. }
Add Comment
Please, Sign In to add comment