diff Ikariam.pm @ 54:2d3c394b7940

rules for build new building and buy transporters
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 20 Oct 2008 23:24:15 +0800
parents d2ac1e198ce4
children 6e0d5e781949
line wrap: on
line diff
--- a/Ikariam.pm	Mon Oct 20 20:54:15 2008 +0800
+++ b/Ikariam.pm	Mon Oct 20 23:24:15 2008 +0800
@@ -37,8 +37,31 @@
         server => $server,
         user => $user,
         pass => $pass,
+        buildingIDs => {
+            townHall =>  0,
+            townhall =>  0,
+            port =>  3,
+            academy =>  4,
+            shipyard =>  5,
+            barracks =>  6,
+            warehouse =>  7,
+            wall =>  8,
+            tavern =>  9,
+            museum =>  10,
+            palace =>  11,
+            embassy =>  12,
+            branchOffice =>  13,
+            workshop =>  15,
+            'workshop-army' =>  15,
+            'workshop-fleet' =>  15,
+            safehouse =>  16,
+            palaceColony =>  17,
+            resource =>  1,
+            tradegood =>  2
+        }
     };
 
+
     $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
     $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
  
@@ -241,24 +264,47 @@
     return @cities;
 }
 
+sub increaseTransporter {
+    my $self = shift;
+    my $param = shift;
+    my $cityId = shift;
+
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        if($locations[$_] eq 'port') {
+            my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=increaseTransporter&id=%s&position=%s', 
+                    $self->{server}, $cityId, $_));
+        }
+    }
+}
+
 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);
+        $position = $_ if($locations[$_] eq $type);
     }
 
     if($position == -1)
     {
-        warn("we don't have the building yet.");
+        foreach (0..$#locations) {
+            next if($_ <= 2 && ($self->{buildingIDs}->{$type} ne "workshop-fleet" &&
+                    $self->{buildingIDs}->{$type} ne "shipyard"));
+            if($locations[$_] eq undef) {
+                my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=build&id=%s&position=%s&building=%d', 
+                        $self->{server}, $cityId, $_, $self->{buildingIDs}->{$type} ));
+                last;
+            }
+        }
     } else {
+        $self->{mech}->add_header( Referer =>  
+            sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
         my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
             action => 'CityScreen',
             'function' => 'upgradeBuilding',
@@ -267,6 +313,10 @@
             level => $self->{'cities'}->{$cityId}->{buildings}->{$type},
             oldView => $type,
             ]);
+#        my $content;
+#        gunzip \$res->content => \$content 
+#            or die "gunzip failed: $GunzipError\n";
+#        print ($content);
     }
 }
 
@@ -317,21 +367,6 @@
     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;
@@ -385,6 +420,14 @@
             }
         }
 
+        # transporters
+        my ($elem) = $html->getElementsByAttribute("class", 'transAvail');
+        $self->{'cities'}->{$cityId}->{transporters}->{avail} = $elem->innerText();
+        my ($elem) = $html->getElementsByAttribute("class", 'transSum');
+        $self->{'cities'}->{$cityId}->{transporters}->{sum} = $elem->innerText();
+        $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/\(//;
+        $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/\)//;
+
         $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}, $cityId));
         gunzip \$res->content => \$content 
             or die "gunzip failed: $GunzipError\n";