diff Ikariam.pm @ 82:82acbba33b97

fixed the build function.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 25 Oct 2008 01:12:20 +0800
parents d57920014cb4
children ec087515da38
line wrap: on
line diff
--- a/Ikariam.pm	Sat Oct 25 00:42:59 2008 +0800
+++ b/Ikariam.pm	Sat Oct 25 01:12:20 2008 +0800
@@ -91,7 +91,7 @@
 use Data::Dumper;
 use LWP;
 # use LWP::Debug qw(+ -conns -trace -debug);
-# use LWP::Debug qw(+trace);
+use LWP::Debug qw(+trace);
 use HTTP::Cookies;
 use WWW::Mechanize;
 use HTML::TagParser;
@@ -372,9 +372,9 @@
         $position = $_ if($locations[$_] eq $type);
     }
 
-    if($position == -1)
-    {
+    if($position == -1) {
         foreach (0..$#locations) {
+            # XXX
             next if($_ <= 2 && ($self->{buildingIDs}->{$type} ne "workshop-fleet" &&
                     $self->{buildingIDs}->{$type} ne "shipyard"));
             if($locations[$_] eq undef) {
@@ -384,20 +384,9 @@
             }
         }
     } else {
-        $self->{mech}->add_header( Referer =>  
-            sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
-        my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
-            action => 'CityScreen',
-            'function' => 'upgradeBuilding',
-            id => $cityId,
-            position => $position,
-            level => $self->{'cities'}->{$cityId}->{buildings}->{$type},
-            oldView => $type,
-            ]);
-#        my $content;
-#        gunzip \$res->content => \$content 
-#            or die "gunzip failed: $GunzipError\n";
-#        print ($content);
+        my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+        my $url = Ikariam::Extractor->new(content => $res->content)->find('//a[@title="升級建築物"/@href');
+        $self->{mech}->get($url);
     }
 }
 
@@ -700,23 +689,13 @@
         password => $self->{pass},
         ]);
 
-    my $c;
-    my $status = gunzip \$res->content => \$c 
-        or die "gunzip failed: $GunzipError\n";
+    my @cities = Ikariam::Extractor->new(content => $res->content)->find('//option[@class="avatarCities coords"]/@value');
 
-    if($c =~ /錯誤!/)
-    {
-        die ("password error\n");
-    } else {
-        my $result = XML::LibXML->new('1.0','utf-8')
-                                 ->parse_html_string ($c, { suppress_errors => 1 })
-                                 ->find( '//option[@class="avatarCities coords"]' );
-
-        if ( $result->isa( 'XML::LibXML::NodeList' ) ) {
-            foreach ( @$result ) {
-                $self->{'cities'}->{$_->getAttribute('value')} = {};
-            }
-        }
+    if($#cities<0) {
+        die ("login failed\n");
+    }
+    foreach(@cities) {
+        $self->{'cities'}->{$_} = {};
     }
 }