diff Ikariam.pm @ 183:2362c8c8909e

implemented military builder.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 21:14:55 +0800
parents 0cfc7a19a4d2
children c6c6db6eea76
line wrap: on
line diff
--- a/Ikariam.pm	Sun Nov 02 18:01:33 2008 +0800
+++ b/Ikariam.pm	Sun Nov 02 21:14:55 2008 +0800
@@ -142,7 +142,7 @@
 
 
     # if debug
-    # LWP::Debug::level('+trace');
+    LWP::Debug::level('+trace');
 
     $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
     $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
@@ -312,23 +312,18 @@
                 delete($info{'ally'}) 
             } else {
                 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/@href', $i));
-                # http://s2.ikariam.tw/index.php?view=allyPage&allyId=264&oldView=island&id=569
                 if($href =~ /&allyId=(\d+)&/) {
                     $info{'allyId'} = $1;
                 }
             }
 
             # Ally Id
-
             my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
-            # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
             if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
                 $info{'user'} = $1;
                 $info{'cityId'} = $2;
-            # ?view=sendAllyMessage&allyId=1192&oldView=island&id=721
             } else {
-                # 聯盟
-                # this is me.
+                # 聯盟 this is me.
                 my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i));
                 if($id =~ /city_(\d+)/) {
                     $info{'user'} = undef; # FIXME
@@ -372,6 +367,7 @@
     }
 }
 
+
 # for tavern only
 sub set {
     my $self = shift;
@@ -391,13 +387,151 @@
          $self->{mech}->submit_form(
                  form_number => 1,
                  fields      => {
-                 amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
-                 }
+                         amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
+                     }
                  );
  
     }
 }
 
+sub is_shipyard_upgrading {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "shipyard";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    return 0;
+}
+
+sub is_navy_trainning {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "shipyard";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    # FIXME we can not found the shipyard
+    return 0;
+}
+
+sub buildShips {
+    my $self = shift;
+    my $shipType = shift;
+    my $cityId = shift;
+    my $type = 'shipyard';
+
+    warn("buildShips $shipType");
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (1..2) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        $self->{mech}->submit_form(
+                form_number => 1,
+                fields      => {
+                    $shipType => 1,
+                }
+                );
+    }
+}
+
+sub is_army_trainning {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = "barracks";
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    # FIXME we can not found the shipyard
+    return 0;
+}
+
+sub is_barracks_upgrading {
+    my $self = shift;
+    my $cityId = shift;
+    my $type = 'barracks';
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
+            return 1;
+        } else {
+            return 0;
+        }
+    }
+    return 0;
+}
+
+sub buildUnits {
+    my $self = shift;
+    my $unitType = shift;
+    my $cityId = shift;
+    my $type = 'barracks';
+
+    warn("buildShips $unitType");
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (2..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+    if($position != -1) {
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        $self->{mech}->submit_form(
+                form_number => 1,
+                fields      => {
+                    $unitType => 1,
+                }
+                );
+    }
+}
+
 sub build {
     my $self = shift;
     my $type = shift;
@@ -433,7 +567,7 @@
     my $self = shift;
     my $param = shift;
     # defense.
-    die("run $param not implemented yet.");
+    warn ("run $param not implemented yet.");
 }
 
 sub research
@@ -474,8 +608,8 @@
         $self->{mech}->submit_form(
                 form_number => 1,
                 fields      => {
-                cargo_army_302 => '2',
-                }
+                      cargo_army_302 => '2',
+                   }
                 );
     } else {
         warn ($treaty);
@@ -607,7 +741,6 @@
             }
         }
     }
-    # print(Dumper($self->{'military'}));
 }
 
 sub checkMilitaryAdvisorReportView {
@@ -1002,6 +1135,7 @@
     }
     foreach(@cities) {
         $self->{'cities'}->{$_} = {};
+        $self->{'cities'}->{$_}->{id} = $_;
     }
 }