Games-Lacuna-Client

 view release on metacpan or  search on metacpan

lib/Games/Lacuna/Client/Governor/Archaeology.pm  view on Meta::CPAN

#
#===============================================================================
#
#  DESCRIPTION:  Archaeology plugin for Governor. Automates the
#                searching of ore for glyphs.
#
#===============================================================================

package Games::Lacuna::Client::Governor::Archaeology;
{
  $Games::Lacuna::Client::Governor::Archaeology::VERSION = '0.003';
}
use strict;
use warnings qw(FATAL all);
use Carp;
use English qw(-no_match_vars);
use Data::Dumper;

{
    use List::MoreUtils qw(any);
    use Games::Lacuna::Client::PrettyPrint qw(trace message warning action ptime phours);

    sub run {
        my $class   = shift;
        my $gov     = shift;
        my ($pid, $status, $cfg) = @{$gov->{current}}{qw(planet_id status config)};

        my ($arch) = $gov->find_buildings('Archaeology');

        if (not defined $arch) {
            warning("There is no Archaeology Ministry on ".$gov->{planet_names}->{$pid});
            return;
        }

        if ( my $time = $gov->building_details($pid,$arch->{building_id})->{work}{seconds_remaining} ){
            $gov->set_next_action_if_sooner( $time );
            warning("The Archaeology Ministry on ".$gov->{planet_names}->{$pid}." is busy.");
            return;
        }
        my %ore_avail = %{$arch->get_ores_available_for_processing->{ore}};
        my @ores = keys %ore_avail;

        if (defined $cfg->{archaeology}->{search_only}) {
            @ores = grep { my $o = $_; any { $o eq $_ } @{$cfg->{archaeology}->{search_only}} } @ores;
        }

        if (defined $cfg->{archaeology}->{do_not_search}) {
            @ores = grep { my $o = $_; not any { $o eq $_ } @{$cfg->{archaeology}->{do_not_search}} } @ores;
        }

        my $selection = $cfg->{archaeology}->{'select'} || 'most';

        my $ore;
        if ($selection eq 'most') {
            ($ore) = sort { $ore_avail{$b} <=> $ore_avail{$a} } @ores;
        }
        elsif ($selection eq 'least') {
            ($ore) = sort { $ore_avail{$a} <=> $ore_avail{$b} } @ores;
        }
        elsif ($selection eq 'random') {
            ($ore) = splice(@ores, rand(@ores), 1)
        }
        else {
            warning("Unknown archaeology selection command: $selection");
        }

        if( not $ore ){
            warning('Unable to find a suitable ore for archaeology');
            return;
        }

        eval {
            $arch->search_for_glyph($ore);
        };
        if ($@) {
            warning("Unable to search for $ore at archaeology ministry: $@");
        } else {
            action("Searching for $ore glyph at archaeology ministry");
        }
    }

}

1;
__END__
=pod

=head1 NAME

Games::Lacuna::Client::Governor::Archaeology - A plugin for Governor that will automate searching of ore for glyphs.

=head1 SYNOPSIS

    Add 'archaeology' to the Governor configuration priorities list.

=head1 DESCRIPTION



( run in 2.024 seconds using v1.01-cache-2.11-cpan-5b529ec07f3 )