changeset 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 336164c9a983
children ff00d108e955
files Ikariam.pm agent.pl building.yaml
diffstat 3 files changed, 77 insertions(+), 20 deletions(-) [+]
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";
--- a/agent.pl	Mon Oct 20 20:54:15 2008 +0800
+++ b/agent.pl	Mon Oct 20 23:24:15 2008 +0800
@@ -70,6 +70,17 @@
     return 1;
 }
 
+sub is_gold_enoughforcargo {
+    my ($self, $city) = @_;
+    my @cargoCost = qw/160 244 396 812 1240 1272 1832 1888 3848 3972 5204 5384 6868 7120 8864 9200 11268 11712 14108 14680 23320 24288 28664 29880 34956 36468 42348 44212 51024 53308 61236 64024 73096 76468 87020 91088 103224 116524 122072 137432 180060 202132 211964 237444 249108 278276 292076 306623 321963 338138 355198 373191 392171 412195 433320 455612 479135 503962 530166 557828 587031 617863 650420 684802 721113 759466 799981 842783 888005 935790 986286 1039654 1096062 1155689 1218724 1285369 1355837 1430353 1509159 1592508 1680670 1773932 1872597 1976989 2087448 2204338 2328045 2458976 2597567 2744276 2899594 3064040 3238163 3422550 3617820 3824635 4043693 4275738 4521561 4782000 5057946 5350345 5660202 5988585 6336630 6705540 7096598 7511164 7950683 8416694 8910828 9434823 9990523 10579889 11205006 11868090 12571498 13317734 14109462 14949514/;
+
+    if($city->{resources}->{gold} >= ($cargoCost[$city->{transporters}->{sum}]*5)) {
+        return 1;
+    } else {
+        return 0;
+    }
+}
+
 sub is_drydock_researched {
     my ($self, $city) = @_;
     return (defined($city->{research}->{4010}) ?  1 : 0);
@@ -135,11 +146,12 @@
 
 $i->logout;
 
+
 sub triggerAction {
     my ($action, $cityId) = @_;
 
     my ($func, $param) = split(/_/,$action);
-    # printf('$i->%s("%s", %s);\n\n', $func, $param, $cityId);
+    printf('$i->%s("%s", %s);'. "\n\n", $func, $param, $cityId);
     eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
     warn $@ if $@;
 }
--- a/building.yaml	Mon Oct 20 20:54:15 2008 +0800
+++ b/building.yaml	Mon Oct 20 23:24:15 2008 +0800
@@ -11,10 +11,12 @@
          0:
             # 確認是否為主城
             # 確認是否有學院
+            - is_gold_enoughforcargo:
+               1: increaseTransporter
             - is_wall_enough:
                0: build_wall
             - is_space_enough:
-               0: build_cityhall
+               0: build_townHall
             - is_corruption:
                1: build_governorsresidence
             # 倉庫庫存量