diff Ikariam.pm @ 52:d2ac1e198ce4

implement a new agent based on Decision Tree (Decision::ParseTree)
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 20 Oct 2008 19:07:53 +0800
parents 96ee35378696
children 2d3c394b7940
line wrap: on
line diff
--- a/Ikariam.pm	Sat Oct 18 22:24:03 2008 +0800
+++ b/Ikariam.pm	Mon Oct 20 19:07:53 2008 +0800
@@ -241,6 +241,97 @@
     return @cities;
 }
 
+sub build {
+    my $self = shift;
+    my $type = shift;
+    my $cityId = shift;
+
+    die ("only wall are implmeneted") unless ($type eq "wall");
+    die ("we don't know about this city") unless(defined($self->{'cities'}->{$cityId}));
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (0..$#locations) {
+        $position = $_ if($locations[$_] == $type);
+    }
+
+    if($position == -1)
+    {
+        warn("we don't have the building yet.");
+    } else {
+        my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
+            action => 'CityScreen',
+            'function' => 'upgradeBuilding',
+            id => $cityId,
+            position => $position,
+            level => $self->{'cities'}->{$cityId}->{buildings}->{$type},
+            oldView => $type,
+            ]);
+    }
+}
+
+sub run {
+    my $self = shift;
+    # defense.
+    die("Not implemented");
+}
+
+sub research
+{
+    my $self = shift;
+    my $type = shift;
+    my $cityId = shift;
+
+    # check if we are researching the same stuff
+    my $res =  $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=changeResearch&id=%s&researchType=%s', $self->{server}, $cityId, $type));
+
+#    my $content;
+#    gunzip \$res->content => \$content 
+#        or die "gunzip failed: $GunzipError\n";
+#
+#    print ($content);
+}
+
+sub checkResearch {
+    my $self = shift;
+    my $cityId = shift;
+
+    my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=researchOverview&id=%s', $self->{server}, $cityId));
+
+    my $content;
+    gunzip \$res->content => \$content 
+        or die "gunzip failed: $GunzipError\n";
+    my $html = HTML::TagParser->new($content);
+
+    my @elems = $html->getElementsByAttribute('class', 'explored');
+
+    my $out = {};
+    foreach my $elem (@elems) {
+        my @items = getElementsByTagName($elem, "a");
+        foreach my $item (@items) {
+            if($item->getAttribute('href') =~ /view=researchDetail&id=\d+&position=\d+&researchId=(\d+)$/) {
+                @$out{$1} = $item->innerText();
+            }
+        }
+    }
+    return $out;
+}
+
+sub checkCity
+{
+    my $self = shift;
+    my $cityId = shift;
+
+    my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
+        action => 'header',
+        cityId => $cityId,
+        function => 'changeCurrentCity',
+        id => $cityId,
+        oldView => 'city',
+        ]);
+    # XXX
+}
+
 sub check
 {
     my $self = shift;
@@ -286,7 +377,7 @@
         foreach my $i (0..14) {
             my ($elem) = $html->getElementsByAttribute("id", "position" . $i);
             my $building = $elem->getAttribute('class');
-            if ($building ne "buildingGround land") {
+            if (!($building =~ /buildingGround/)) {
                 $self->{'cities'}->{$cityId}->{locations}[$i] = $building;
                 my $span = getElementsByAttribute($elem, "class", "textLabel");
                 my (undef, undef, $level) = split(/ /, $span->innerText());
@@ -303,7 +394,7 @@
         # if($content =~ /更新戰鬥報告: (\d+)/);
         # if($content =~ /新的戰鬥報告: (\d+)/);
 
-        # check townHall
+        # sub checkTownHall {
         $res = $self->{mech}->get(sprintf('http://%s/index.php?view=townHall&id=%d', $self->{server}, $cityId));
         gunzip \$res->content => \$content 
             or die "gunzip failed: $GunzipError\n";
@@ -351,7 +442,7 @@
             $self->{'cities'}->{$cityId}->{corruption} =~ s/%//g;
         }
 
-        # count
+        # countCiizens
         my @citizens_type = qw/citizens woodworkers specialworkers scientists/;
         @elems = $html->getElementsByAttribute('class', 'count');
         $self->{'cities'}->{$cityId}->{'citizens'} = {};
@@ -363,14 +454,11 @@
             $self->{'cities'}->{$cityId}->{'citizens'}->{total} += $elems[$i]->innerText();;
         }
 
-        # production
-        # skin/resources/icon_gold.gif
-        # skin/resources/icon_wood.gif
-        # skin/resources/icon_sulfur.gif (?)
-        # skin/resources/icon_research.gif
+        # } 
+        
+        $self->{'cities'}->{$cityId}->{'research'} = $self->checkResearch($cityId);
 
-
-        # check armies
+        # sub checkArmies {
         my %force_types;
         $force_types{'army'} = [ qw/undef undef Slinger Swordsman Phalanx Ram Archer Catapult Gunsman Mortar SteamGiant Gyrocopter Bombardier Doctor Cook/ ];
         $force_types{'fleet'} = [ qw/undef undef Ram-Ship BallistaShip Flamethrower CatapultShip MortarShip PaddleWheelRam DivingBoat/ ];
@@ -432,14 +520,16 @@
 }
 
 sub getElementsByTagName {
-    my $element    = shift;
+    my $element = shift;
     my $tagname = lc(shift);
     my ( $flat, $cur ) = @$element;
 
     my $out = [];
     for( ; $cur <= $#$flat ; $cur++ ) {
-        next if ( $flat->[$cur]->[001] ne $tagname );
-        next if $flat->[$cur]->[000];                 # close
+        last if ($flat->[ $cur + 1 ]->[001] eq $element->tagName() );
+        next if ($flat->[$cur]->[001] ne $tagname );
+        next if $flat->[$cur]->[000]; # close
+
         my $elem = HTML::TagParser::Element->new( $flat, $cur );
         return $elem unless wantarray;
         push( @$out, $elem );