view agent.pl @ 376:6ca0677a361e

refiend output format and fixed timing issue.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 14 Apr 2009 17:02:17 +0800
parents 0522468991c1
children
line wrap: on
line source

#!/usr/bin/perl

use strict;
use Ikariam;
use Ikariam::CitiesRules;
use Data::Dumper;
use Decision::ParseTree q{ParseTree};
use YAML qw/LoadFile Dump DumpFile/;

package main;
my $verbose = 1;

our $i = new Ikariam($::server, $::user, $::pass);
$i->login;
my $cities = $i->check;

# Genereic rules for both overall and city level arranagement.
my $rules = Ikariam::CitiesRules->new($i);
# blanace resources, arrange defance 
my $tree  = LoadFile('overall.yaml');
my $decision; $decision->{parse_path} = []; $decision->{parse_answer} = undef;
my $action = ParseTree($tree, $rules, $decision);
triggerAction($action, (keys(%$cities))[0]) if(defined($action));
# print STDERR Dump($decision) if(defined($verbose));

# show cities.
foreach my $cityId (keys(%$cities)) {
    printf("%s http://%s/index.php?view=city&id=%d\n", 
        $cities->{$cityId}->{name}, $::server, $cityId);
    printf("construction: %s\n", $cities->{$cityId}->{construction});

    foreach my $thing (qw/resources space buildings citizens army fleet/) {
        printf("<%s>\n", uc($thing));
        foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) {
            printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i});
        }
        print("\n");
    }
    print("\n");

    # maybe this should be moved to Rules.
    $i->blanceHurmanResource($cityId);
 
    # build spy
    $i->buildSpy('spy', $cityId);
    # enlarge the scientist number.
    $i->set("academy", $cityId);

    # Only when you are not happy.
# $i->set("tavern", $cityId);

    # build military!
#    $tree  = LoadFile('military.yaml');
#    $cities->{$cityId}->{parse_path} = [];
#    $cities->{$cityId}->{parse_answer} = undef;
#    if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
#        triggerAction($action, $cityId);
#    }
    DumpFile("military-$cityId-dump.yaml", $cities->{$cityId});

    # build and upgrade for cities
#    $tree  = LoadFile('city.yaml');
#    $cities->{$cityId}->{parse_path} = [];
#    $cities->{$cityId}->{parse_answer} = undef;
#    if (my $action = ParseTree($tree, $rules, $cities->{$cityId})) {
#        # printf("%s is suppressed\n", $action);
#        triggerAction($action, $cityId);
#    }

    
    DumpFile("city-$cityId-dump.yaml", $cities->{$cityId});
}
DumpFile("research-dump.yaml", $i->{'research'});
DumpFile("wars-dump.yaml", $i->{'military'});

$i->logout;

sub triggerAction {
    my ($action, $cityId) = @_;

    my ($func, $param) = split(/_/,$action);
    printf('$i->%s("%s", %s);'. "\n", $func, $param, $cityId);
    eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
    warn $@ if $@;
}