changeset 34:91e387b51aa0

added more rules checking
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 11 Oct 2008 05:19:43 +0800
parents d183277b4d93
children d9d4aedc9e03
files Ikariam.pm inference.pl
diffstat 2 files changed, 56 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Sat Oct 11 05:19:29 2008 +0800
+++ b/Ikariam.pm	Sat Oct 11 05:19:43 2008 +0800
@@ -259,8 +259,8 @@
         my @elems;
 
         my ($elem) = $html->getElementsByAttribute("id", "value_gold");
-        $self->{'cities'}->{$cityId}->{gold} = $elem->innerText();
-        $self->{'cities'}->{$cityId}->{gold} =~ s/,//g;
+        $self->{'cities'}->{$cityId}->{resources}->{gold} = $elem->innerText();
+        $self->{'cities'}->{$cityId}->{resources}->{gold} =~ s/,//g;
 
         my ($elem) = $html->getElementsByAttribute("class", "city");
         $self->{'cities'}->{$cityId}->{name} = $elem->innerText();
@@ -272,8 +272,8 @@
         # check goods
         foreach my $good (qw/wood wine marble crystal sulfur/) {
             my ($elem) = $html->getElementsByAttribute("id", "value_" . $good);
-            $self->{'cities'}->{$cityId}->{$good} = $elem->innerText();
-            $self->{'cities'}->{$cityId}->{$good} =~ s/,//g;
+            $self->{'cities'}->{$cityId}->{resources}->{$good} = $elem->innerText();
+            $self->{'cities'}->{$cityId}->{resources}->{$good} =~ s/,//g;
         }
 
         # search locations
@@ -288,11 +288,14 @@
             }
         }
 
-        # $res = $self->{mech}->get(sprintf('http://%s/index.php?view=townHall&id=%d', $self->{server}, $cityId));
-        # gunzip \$res->content => \$content 
-        # or die "gunzip failed: $GunzipError\n";
+        $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}, $cityId));
+        gunzip \$res->content => \$content 
+            or die "gunzip failed: $GunzipError\n";
 
-
+        $self->{'cities'}->{$cityId}->{force}->{attacks} = $1 if($content =~ /敵人攻擊: (\d+)/);
+        $self->{'cities'}->{$cityId}->{force}->{wars} = $1 if($content =~ /我方軍隊行程: (\d+)/);
+        # if($content =~ /更新戰鬥報告: (\d+)/);
+        # if($content =~ /新的戰鬥報告: (\d+)/);
 
         # check townHall
         $res = $self->{mech}->get(sprintf('http://%s/index.php?view=townHall&id=%d', $self->{server}, $cityId));
--- a/inference.pl	Sat Oct 11 05:19:29 2008 +0800
+++ b/inference.pl	Sat Oct 11 05:19:43 2008 +0800
@@ -10,7 +10,7 @@
 sub Warn
 {
     my ($city, $msg) = @_;
-    printf("WARNING: http://%s/index.php?view=city&id=%s (%s) %s \n", 
+    printf("http://%s/index.php?view=city&id=%s (%s) %s \n", 
         $server, $city, $cities->{$city}->{name}, $msg);
 }
 
@@ -18,7 +18,6 @@
 $i->login;
 $cities = $i->check;
 $i->logout;
-print(Dumper($cities));
 
 sub rule_happiness
 {
@@ -35,8 +34,8 @@
 
     # checking goods
     foreach my $good (qw/wood wine marble crystal/) {
-        if($cities->{$id}->{$good} <= 100) {
-            Warn($id, sprintf("Running out %s (%d)!", $good, $cities->{$id}->{$good}));
+        if($cities->{$id}->{resources}->{$good} <= 100) {
+            Warn($id, sprintf("Running out %s (%d)!", $good, $cities->{$id}->{resources}->{$good}));
         }
     }
 }
@@ -50,7 +49,8 @@
         Warn($cityId, sprintf("Wall level (%d) is lower then town hall (%d)!",
                 $cities->{$cityId}->{buildings}->{wall},
                 $cities->{$cityId}->{buildings}->{townHall}
-            ));
+            )) 
+        if($cities->{$cityId}->{construction} != 1);
     }
 }
 
@@ -60,12 +60,27 @@
 
     if($cities->{$cityId}->{space}->{total} == $cities->{$cityId}->{space}->{occupied})
     {
-        Warn($cityId, sprintf("your space is full, upgraded the hall")) if($cities->{$cityId}->{construction} != 1)
+        Warn($cityId, sprintf("your space is full, upgraded the hall")) 
+        if($cities->{$cityId}->{construction} != 1);
+    }
+    return 0;
+}
+
+sub rule_war
+{
+    my $cityId = shift;
+    if($cities->{$cityId}->{force}->{attacks} > 0)
+    {
+        Warn($cityId, sprintf("YOU ARE ATTACKED!!!!")) if($cities->{$cityId}->{construction} != 1)
     }
 }
 
 sub rule_engagement
 {
+    my $cityId = shift;
+    if($cities->{$cityId}->{force}->{wars} == 0) {
+        Warn($cityId, sprintf("Let's Attack someone!"));
+    }
 }
 
 # 貪腐
@@ -78,10 +93,33 @@
     }
 }
 
+print Dumper($cities);
+# show cities.
+foreach my $cityId (keys(%$cities)) {
+    printf("%s http://s2.ikariam.tw/index.php?view=city&id=%d\n", $cities->{$cityId}->{name}, $cityId);
+    printf("construction: %s\n", $cities->{$cityId}->{construction});
+    
+    foreach my $thing (qw/resources space force buildings citizens army fleet/) {
+        printf("%s\n", uc($thing));
+        foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) {
+            printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i});
+        }
+        print("\n");
+    }
+    print("\n");
+}
+
 # space
 # Checking rules
-foreach my $procedure (qw/rule_happiness rule_corruption rule_building rule_space rule_resource/) {
+foreach my $procedure (qw/rule_war rule_happiness rule_corruption rule_building rule_space rule_resource/) {
     foreach my $id (keys(%$cities)) {
         eval(sprintf("%s(%s);", $procedure, $id));
     }
 }
+
+foreach my $procedure (qw/rule_war rule_engagement/) {
+    foreach my $id (keys(%$cities)) {
+        eval(sprintf("%s(%s);", $procedure, $id));
+        last;
+    }
+}