diff agent.pl @ 81:d57920014cb4

split buliding.yaml to city.yaml and overall.yaml
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 25 Oct 2008 00:42:59 +0800
parents 7ab5fc8c847c
children 82acbba33b97
line wrap: on
line diff
--- a/agent.pl	Fri Oct 24 21:56:03 2008 +0800
+++ b/agent.pl	Sat Oct 25 00:42:59 2008 +0800
@@ -11,17 +11,16 @@
 use Data::Dumper;
 
 sub new {
-    my ( $class ) = @_;
-    my $self = {};
+    my ( $class, $i ) = @_;
+    my $self = {
+        ikariam => $i,
+    };
     return bless $self, $class;
 }
 
 sub is_attacked {
     my ($self, $city) = @_;
-    # XXX
-    # $self->{'military'}->{wars}->{attacks}
-    # return ($city->{wars}->{attacks} > 0 ) ? 1 : 0;
-    return 0;
+    return ($self->{'ikariam'}->{'military'}->{attacks} > 0 ) ? 1 : 0;
 }
 
 sub is_constructing {
@@ -32,6 +31,7 @@
 sub is_wall_enough {
     my ($self, $city) = @_;
     # http://ikariam.wikia.com/wiki/Wall_Defense
+    # Basic rule - the wall level must equale or large then townHall level.
     return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ?  1 : 0);
 }
 
@@ -97,47 +97,48 @@
 
 sub is_expansion_researched {
     my ($self, $city) = @_;
-    return (defined($city->{research}->{1030}) ?  1 : 0);
+    # $self->{'ikariam'}->
+    return (defined($self->{'ikariam'}->{research}->{1030}) ?  1 : 0);
 }
 
 sub is_culturalexchange_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{1040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{1040}) ?  1 : 0);
 }
 
 sub is_wealth_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{2030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{2030}) ?  1 : 0);
 }
 
 sub is_winepress_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{2040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{2040}) ?  1 : 0);
 }
 
 sub is_paper_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3020}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3020}) ?  1 : 0);
 }
 
 sub is_espionage_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3030}) ?  1 : 0);
 }
 
 sub is_invention_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3040}) ?  1 : 0);
 }
 
 sub is_professionalarmy_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{4030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{4030}) ?  1 : 0);
 }
 
 sub is_drydock_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{4010}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{4010}) ?  1 : 0);
 }
 
 sub is_barracks_level_enough {
@@ -145,7 +146,7 @@
     return 0 if(!defined($city->{buildings}->{barracks}));
     # 方陣兵需要 level 4
     # optimum is 5
-    return ($city->{buildings}->{barracks} >= 3 ? 1 : 0);
+    return ($city->{buildings}->{barracks} >= 4 ? 1 : 0);
 }
 
 sub is_shipyard_level_enough {
@@ -159,24 +160,36 @@
 sub rule_engagement
 {
     my ($self, $city) = @_;
-    # XXX
+    # TODO
     # 計算距離, 可搶劫比例, 是否有聯盟, 軍事分數 (win rate)
 }
 
 sub rule_resource
 {
-    # XXX
+    # TODO 
 }
 1; 
 
 
 package main;
+my $verbose = undef;
+
 our $i = new Ikariam($::server, $::user, $::pass);
 $i->login;
 my $cities = $i->check;
-my $rules = Ikariam::Cities::Rules->new;
-my $tree  = LoadFile('building.yaml');
-# print Dumper($tree);
+
+# Genereic rules for both overall and city level arranagement.
+my $rules = Ikariam::Cities::Rules->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 Dumper($decision) if(defined($verbose));
+
+
 # show cities.
 foreach my $cityId (keys(%$cities)) {
     printf("%s http://%s/index.php?view=city&id=%d\n", 
@@ -192,25 +205,23 @@
         print("\n");
     }
     print("\n");
-    # print(Dumper($cities));
 
-    # make decisions
-
-    # for the Decision Tree
+    # build and upgrade for cities
+    $tree  = LoadFile('city.yaml');
     $cities->{$cityId}->{parse_path} = [];
     $cities->{$cityId}->{parse_answer} = undef;
     while (my $action = ParseTree($tree, $rules, $cities->{$cityId}))
     {
-        # TODO: remove the last rule, if the result is fallback
         triggerAction($action, $cityId);
         if($cities->{$cityId}->{parse_answer} ne "fallback") {
             last ;
         } else {
-
+            # TODO: remove the last rule, if the result is fallback
         }
     }
     # Debug
-    print(Dumper($cities->{$cityId}->{parse_path}));
+    print(Dumper($cities->{$cityId}->{parse_path})) if(defined($verbose));
+    print(Dumper($cities)) if(defined($verbose));
 }
 
 $i->logout;
@@ -220,7 +231,7 @@
     my ($action, $cityId) = @_;
 
     my ($func, $param) = split(/_/,$action);
-    printf('$i->%s("%s", %s);'. "\n\n", $func, $param, $cityId);
+    printf('$i->%s("%s", %s);'. "\n", $func, $param, $cityId);
     # eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
     # warn $@ if $@;
 }