changeset 286:2ddd2bd21ef2

list spydata when check enemies
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Thu, 04 Dec 2008 21:54:00 +0800
parents c864134ebe3e
children d194ff3edb2f
files Ikariam.pm enemy.pl
diffstat 2 files changed, 44 insertions(+), 19 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Thu Dec 04 03:22:24 2008 +0800
+++ b/Ikariam.pm	Thu Dec 04 21:54:00 2008 +0800
@@ -723,10 +723,10 @@
     );
 }
 
-sub checkCity {
+sub readCity {
     my $self = shift;
     my $cityId = shift;
-
+    my $data;
 
     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);
@@ -735,7 +735,7 @@
         my @buildings = $extractor->find(sprintf('//li[@id="position%s"]/@class', $i));
         foreach my $building (@buildings) {
             if (!($building =~ /buildingGround/) && !($building =~ /townhall/)) {
-                $self->{'cities'}->{$cityId}->{locations}[$i] = $building;
+                $data->{locations}[$i] = $building;
 
                 my $span;
                 my @spans = $extractor->find(sprintf('//li[@id="position%s"]//span[@class="textLabel"]/text()', $i));
@@ -745,23 +745,33 @@
                     $span = $spans[0];
                 }
                 my (undef, undef, $level) = split(/ /, $span);
-                $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level;
+                $data->{buildings}->{$building} = $level;
             }
         }
     }
 
-    $self->{'cities'}->{$cityId}->{name} = $extractor->find('//span[@class="city"]/text()');
+    $data->{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/;
+        $data->{island}->{name} = $1;
+        $data->{island}->{x} = $2;
+        $data->{island}->{y} = $3;
+        $data->{island}->{id} = my $island = $extractor->find('//div[@id="breadcrumbs"]/a[@class="island"]/@href');
+        $data->{island}->{id} =~ s/\?view=island&id=(\d+)/$1/;
     }
 
-    return  $self->{'cities'}->{$cityId} if(wantarray());
+    return $data;
+}
+
+sub checkCity {
+    my $self = shift;
+    my $cityId = shift;
+
+    $self->{'cities'}->{$cityId} = $self->readCity($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;
@@ -846,7 +856,7 @@
                 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}->{city} = $self->readCity($1);
                     @{$data->{$1}->{risks}} = Ikariam::Extractor->new(content => $self->{mech}->get($links[1])->content)->find('//div[@class="missionRisk"]');
                 }
             }
--- a/enemy.pl	Thu Dec 04 03:22:24 2008 +0800
+++ b/enemy.pl	Thu Dec 04 21:54:00 2008 +0800
@@ -2,7 +2,7 @@
 use strict;
 use Ikariam;
 use Data::Dumper;
-use YAML qw/LoadFile/;
+use YAML qw/Dump LoadFile/;
 use POSIX qw(strftime);
 
 package main;
@@ -106,17 +106,32 @@
         my $island = Ikariam::Island->retrieve($city->island);
         my $capture = $city->citylevel * ($city->citylevel - 1) * $u->trader_score_secondary / 10000;
 
-        printf("Name: %s\n", $city->cityname);
+        printf("Name: %s [%s:%s]\n", $city->cityname,  $island->x, $island->y);
         if(defined($city->status)) {
             printf("Status: <<<%s>>>\n", $city->status);
         }
-        printf("Level: %s\n", $city->citylevel);
-        printf("Island: %s\n", $island->name);
         printf("Location: [%s:%s] http://%s/index.php?view=island&id=%s&selectCity=%d\n", 
-            $island->x, $island->y, $::server, $island->id, $city->cityId);
-        printf("Tradegood: %s\n", $tradegoodText[$island->tradegood]);
-        printf("Wonder: %s\n", $wonderText[$island->wonder]);
+                $island->x, $island->y, $::server, $island->id, $city->cityId);
+        if(-f sprintf("spy-%d-dump.yaml", $city->cityId)) {
+            my $spydata = LoadFile(sprintf("spy-%d-dump.yaml", $city->cityId));
+            if($spydata->{city}->{buildings}->{townHall} ne $city->citylevel) {
+                printf("*OUTDATED* ");
+            }
+            printf("Hall: %s, Wall: %s, Port: %s, Safehouse: %s, Risk: %s\n", 
+                    $spydata->{city}->{buildings}->{townHall}, 
+                    $spydata->{city}->{buildings}->{wall}, 
+                    $spydata->{city}->{buildings}->{port}, 
+                    $spydata->{city}->{buildings}->{safehouse}, 
+                    $spydata->{risks}[1]);
+        } else {
+            printf("Level: %s\n", $city->citylevel);
+        }
+
         printf("Capture: %d\n", $capture);
+        printf("Tradegood: %s, Wonder: %s\n", 
+                $tradegoodText[$island->tradegood], $wonderText[$island->wonder]);
+        # printf("Island: %s\n", $island->name);
+
         myTownToVicim($island->id);
         printf("\n");
     }