diff Ikariam.pm @ 284:10f14db31151

added new dump spy info function
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 04 Dec 2008 03:22:11 +0800
parents 6bce423892f4
children 2ddd2bd21ef2
line wrap: on
line diff
--- a/Ikariam.pm	Tue Dec 02 17:21:21 2008 +0800
+++ b/Ikariam.pm	Thu Dec 04 03:22:11 2008 +0800
@@ -727,42 +727,10 @@
     my $self = shift;
     my $cityId = shift;
 
-    $self->changeCity($cityId);
 
     my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=city&id=%d", $self->{server}, $cityId));
     my $extractor = new Ikariam::Extractor(content => $res->content);
 
-    $self->{'cities'}->{$cityId}->{resources}->{gold} = $extractor->find('//span[@id="value_gold"]/text()');
-    $self->{'cities'}->{$cityId}->{resources}->{gold} =~ s/,//g;
-
-    $self->{'cities'}->{$cityId}->{name} = $extractor->find('//span[@class="city"]/text()');
-
-    my $island = $extractor->find('//div[@id="breadcrumbs"]/a[@class="island"]');
-    if($island =~ /(\w+)\[(\d+):(\d+)\]/) {
-        $self->{'cities'}->{$cityId}->{island}->{name} = $1;
-        $self->{'cities'}->{$cityId}->{island}->{x} = $2;
-        $self->{'cities'}->{$cityId}->{island}->{y} = $3;
-        $self->{'cities'}->{$cityId}->{island}->{id} = my $island = $extractor->find('//div[@id="breadcrumbs"]/a[@class="island"]/@href');
-        $self->{'cities'}->{$cityId}->{island}->{id} =~ s/\?view=island&id=(\d+)/$1/;
-    }
-    
-    $self->{'cities'}->{$cityId}->{construction} = defined($extractor->find('//*[@class="constructionSite"]')) ? 1 : 0;
-
-    # maxCapacity
-    my $page = $extractor->{doc}->toString(1);
-    if($page =~ /maxCapacity : {\s+wood: (\d+),\s+wine: (\d+),\s+marble: (\d+),\s+crystal: (\d+),\s+sulfur: (\d+)\s+}/s) {
-        $self->{'cities'}->{$cityId}->{maxCapacity}->{wood} = $1;
-        $self->{'cities'}->{$cityId}->{maxCapacity}->{wine} = $2;
-        $self->{'cities'}->{$cityId}->{maxCapacity}->{marble} = $3;
-        $self->{'cities'}->{$cityId}->{maxCapacity}->{crystal} = $4;
-        $self->{'cities'}->{$cityId}->{maxCapacity}->{sulfur} = $5;
-    }
-
-    foreach my $good (qw/wood wine marble crystal sulfur/) {
-        $self->{'cities'}->{$cityId}->{resources}->{$good} = $extractor->find(sprintf('//span[@id="value_%s"]', $good));
-        $self->{'cities'}->{$cityId}->{resources}->{$good} =~ s/,//g;
-    }
-
     foreach my $i (0..14) {
         my @buildings = $extractor->find(sprintf('//li[@id="position%s"]/@class', $i));
         foreach my $building (@buildings) {
@@ -782,6 +750,39 @@
         }
     }
 
+    $self->{'cities'}->{$cityId}->{name} = $extractor->find('//span[@class="city"]/text()');
+
+    my $island = $extractor->find('//div[@id="breadcrumbs"]/a[@class="island"]');
+    if($island =~ /(\w+)\[(\d+):(\d+)\]/) {
+        $self->{'cities'}->{$cityId}->{island}->{name} = $1;
+        $self->{'cities'}->{$cityId}->{island}->{x} = $2;
+        $self->{'cities'}->{$cityId}->{island}->{y} = $3;
+        $self->{'cities'}->{$cityId}->{island}->{id} = my $island = $extractor->find('//div[@id="breadcrumbs"]/a[@class="island"]/@href');
+        $self->{'cities'}->{$cityId}->{island}->{id} =~ s/\?view=island&id=(\d+)/$1/;
+    }
+
+    return  $self->{'cities'}->{$cityId} if(wantarray());
+
+    $self->{'cities'}->{$cityId}->{resources}->{gold} = $extractor->find('//span[@id="value_gold"]/text()');
+    $self->{'cities'}->{$cityId}->{resources}->{gold} =~ s/,//g;
+    
+    $self->{'cities'}->{$cityId}->{construction} = defined($extractor->find('//*[@class="constructionSite"]')) ? 1 : 0;
+
+    # maxCapacity
+    my $page = $extractor->{doc}->toString(1);
+    if($page =~ /maxCapacity : {\s+wood: (\d+),\s+wine: (\d+),\s+marble: (\d+),\s+crystal: (\d+),\s+sulfur: (\d+)\s+}/s) {
+        $self->{'cities'}->{$cityId}->{maxCapacity}->{wood} = $1;
+        $self->{'cities'}->{$cityId}->{maxCapacity}->{wine} = $2;
+        $self->{'cities'}->{$cityId}->{maxCapacity}->{marble} = $3;
+        $self->{'cities'}->{$cityId}->{maxCapacity}->{crystal} = $4;
+        $self->{'cities'}->{$cityId}->{maxCapacity}->{sulfur} = $5;
+    }
+
+    foreach my $good (qw/wood wine marble crystal sulfur/) {
+        $self->{'cities'}->{$cityId}->{resources}->{$good} = $extractor->find(sprintf('//span[@id="value_%s"]', $good));
+        $self->{'cities'}->{$cityId}->{resources}->{$good} =~ s/,//g;
+    }
+
     $self->{'cities'}->{$cityId}->{transporters}->{avail} = $extractor->find('//span[@id="value_transAvail"]/text()');
     $self->{'cities'}->{$cityId}->{transporters}->{sum} = $extractor->find('//span[@id="value_transSum"]/text()');
     $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/[\(|\)]//g;
@@ -831,6 +832,31 @@
     }
 }
 
+sub checkSafeHouse {
+    my $self = shift;
+    my $cityId = shift;
+    my $data;
+
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (0..$#locations) {
+        if($locations[$_] eq 'safehouse') {
+            my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=safehouse&id=%d&position=%d', $self->{server}, $cityId, $_ ));
+            my $extractor = Ikariam::Extractor->new(content => $res->content);
+            foreach (1..25) {
+                my @links = $extractor->find(sprintf('//div[@class="spyinfo" and position() = %d]//a/@href', $_));
+                last if($#links < 0);
+                if ($links[0] =~ /id=(\d+)/) {
+                    @{$data->{$1}->{city}} = $self->checkCity($1);
+                    @{$data->{$1}->{risks}} = Ikariam::Extractor->new(content => $self->{mech}->get($links[1])->content)->find('//div[@class="missionRisk"]');
+                }
+            }
+            last;
+        }
+    }
+    return $data;
+}
+
+
 sub checkMilitaryAdvisorMilitaryMovements  {
     my $self = shift;
     my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}));
@@ -1090,6 +1116,7 @@
 
     # looking for cities
     foreach my $cityId (keys(%{$self->{'cities'}})) {
+        $self->changeCity($cityId);
         $self->checkCity($cityId);
         $self->checkTownHall($cityId);
         $self->checkArmies($cityId);