diff Ikariam.pm @ 26:d8117792c6f5

collecting more data from townHall
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 10 Oct 2008 03:20:36 +0800
parents 54ab0becd730
children dd85b55eec2a
line wrap: on
line diff
--- a/Ikariam.pm	Fri Oct 10 03:20:05 2008 +0800
+++ b/Ikariam.pm	Fri Oct 10 03:20:36 2008 +0800
@@ -34,7 +34,7 @@
         pass => $pass,
     };
 
-    $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "./cookies.txt", autosave => 1));
+    $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
     $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
  
     return bless $self, $class;
@@ -81,7 +81,7 @@
             $e = getElementsByAttribute($elem, "class", "allytag");
             $user{'ally'} = $e->innerText();
 
-            $e  = getElementsByTagName($e, "a");
+            $e = getElementsByTagName($e, "a");
             if($e->getAttribute('href') =~ /\?view=allyPage&allyId=(\d+)/)
             {
                 $user{'allyId'} = $1;
@@ -239,60 +239,146 @@
 {
     my $self = shift;
 
-    my $aaaa = $self->{'cities'};
-
-    # 運輸船
-    # CargoShip 
-    foreach my $i (keys(%{$self->{'cities'}})) 
+    # looking for cities
+    foreach my $cityId (keys(%{$self->{'cities'}})) 
     {
-
         # search for goods
         my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
             action => 'header',
-            cityId => $i,
+            cityId => $cityId,
             function => 'changeCurrentCity',
-            id => $i,
+            id => $cityId,
             oldView => 'city',
         ]);
-        my $c;
-        my $status = gunzip \$res->content => \$c 
+
+        my $content;
+        gunzip \$res->content => \$content 
+            or die "gunzip failed: $GunzipError\n";
+
+        my $html = HTML::TagParser->new($content);
+        my @elems;
+
+        my ($elem) = $html->getElementsByAttribute("id", "value_gold");
+        $self->{'cities'}->{$cityId}->{gold} = $elem->innerText();
+        $self->{'cities'}->{$cityId}->{gold} =~ s/,//g;
+
+        my ($elem) = $html->getElementsByAttribute("class", "city");
+        $self->{'cities'}->{$cityId}->{name} = $elem->innerText();
+
+        # 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;
+        }
+
+        # check townHall
+        $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";
 
-        my $html = HTML::TagParser->new($c);
-        my @elems;
+        # check happiness
+        # Happiness = Basic bonuses (196 + Capital Bonus + Holiday Bonus(25)) + 
+        #             Wine (Tavern Base(12*level) + 
+        #             Tavern Bonus(80*step)) + 
+        #             Culture (Museum Base(20*level) + 
+        #             Cultural Goods Bonus(50*Cultural Goods)) - 
+        #             Population (population) - 
+        #             Corruption (Corruption rate * population)
+        #
+        # Growth Rate = Happiness * 0.02
+        $html = HTML::TagParser->new($content);
 
-        foreach my $j (qw/value_wood value_wine value_marble value_crystal value_sulfur/) {
-            my ($elem) = $html->getElementsByAttribute("id", $j);
-            $self->{'cities'}->{$i}->{$j} = $elem->innerText();
-            $self->{'cities'}->{$i}->{$j} =~ s/,//g;
+        my @happiness = ("ecstatic", "happy", "neutral", "sad", "outraged");
+        foreach my $j (0..$#happiness) {
+            my ($elem) = $html->getElementsByAttribute("class", sprintf("happiness happiness_%s", $happiness[$j]));
+            if(defined($elem)) {
+                $self->{'cities'}->{$cityId}->{happiness} = $j;
+                $self->{'cities'}->{$cityId}->{happiness_text} = $happiness[$j];
+            }
+        }
+
+        # Space, 房屋數
+        foreach my $j (qw/occupied total/) {
+            my ($elem) = $html->getElementsByAttribute("class", sprintf("value %s", $j));
+            if(defined($elem)) {
+                $self->{'cities'}->{$cityId}->{"space_" . $j} = $elem->innerText();
+            }
         }
 
-        my %types;
-        $types{'army'} = [ qw/undef undef Slinger Swordsman Phalanx Ram Archer Catapult Gunsman Mortar SteamGiant Gyrocopter Bombardier Doctor Cook/ ];
-        $types{'fleet'} = [ qw/undef undef Ram-Ship BallistaShip Flamethrower CatapultShip MortarShip PaddleWheelRam DivingBoat/ ];
+        # Actions
+        # <span id="value_maxActionPoints">1</span>
+
+        # <li class="incomegold incomegold_negative">
+        # <li class="incomegold incomegold_positive">
+        # <span class="value">-178</span>
+
+        # <li class="corruption">
+        # <span class="textLabel">腐敗: </span>
+        # <span class="value positive">
+        # <span title="目前腐敗程度">33%</span> 
+        # </span>
+        # </li>
+        my ($elem) = $html->getElementsByAttribute("title", "目前腐敗程度");
+        if(defined($elem)) {
+            $self->{'cities'}->{$cityId}->{corruption} = $elem->innerText();
+            $self->{'cities'}->{$cityId}->{corruption} =~ s/%//g;
+        }
 
+        # count
+        my $cities_type = {
+            citizens => 8,
+            woodworkers => 10, 
+            specialworkers => 12,
+            scientists => 14,
+        };
+
+        @elems = $html->getElementsByAttribute('class', 'count');
+        $self->{'cities'}->{$cityId}->{'cities'} = {};
+        foreach my $type (keys(%$cities_type))
+        {
+            $self->{'cities'}->{$cityId}->{'cities'}->{$type} = $elems[$cities_type{$type}]->innerText();
+        }
+
+        # production
+        # skin/resources/icon_gold.gif
+        # skin/resources/icon_wood.gif
+        # skin/resources/icon_sulfur.gif (?)
+        # skin/resources/icon_research.gif
+
+#        foreach my $i (0..$#cities_type) {
+#            next if($cities_type[$i] eq 'undef');
+#            if(defined($elems[$i])) {
+#                $self->{'cities'}->{$cityId}->{'cities_'.$type}->{$cities_type[$i]} = $elems[$i]->innerText();
+#            }
+#        }
+
+        # check armies
+        my %force_types;
+        $force_types{'army'} = [ qw/undef undef Slinger Swordsman Phalanx Ram Archer Catapult Gunsman Mortar SteamGiant Gyrocopter Bombardier Doctor Cook/ ];
+        $force_types{'fleet'} = [ qw/undef undef Ram-Ship BallistaShip Flamethrower CatapultShip MortarShip PaddleWheelRam DivingBoat/ ];
         foreach my $x (qw/army fleet/)
         {
-            $self->{'cities'}->{$i}->{$x} = {};
+            $self->{'cities'}->{$cityId}->{$x} = {};
             # search army
-            $res = $self->{mech}->get(sprintf('http://%s/index.php?view=cityMilitary-%s&id=%d', $self->{server}, $x, $i));
-            $status = gunzip \$res->content => \$c 
+            $res = $self->{mech}->get(sprintf('http://%s/index.php?view=cityMilitary-%s&id=%d', $self->{server}, $x, $cityId));
+            gunzip \$res->content => \$content 
                 or die "gunzip failed: $GunzipError\n";
 
-            $html = HTML::TagParser->new($c);
+            $html = HTML::TagParser->new($content);
             @elems = $html->getElementsByTagName('td');
-
-            foreach my $j (0..$#{$types{$x}}) {
-                next if($types{$x}[$j] eq 'undef');
+            foreach my $j (0..$#{$force_types{$x}}) {
+                next if($force_types{$x}[$j] eq 'undef');
                 if($elems[$j]->innerText() == '-') {
-                    $self->{'cities'}->{$i}->{$x}->{$types{$x}[$j]} = 0;
+                    $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = 0;
                 } else {
-                    $self->{'cities'}->{$i}->{$x}->{$types{$x}[$j]} = $elems[$j]->innerText();
+                    $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = $elems[$j]->innerText();
                 }
             }
         }
     }
-    print Dumper($self->{'cities'});
+    # print Dumper($self->{'cities'});
+    return $self->{'cities'};
 }
 
 sub logout