comparison 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
comparison
equal deleted inserted replaced
81:d57920014cb4 82:82acbba33b97
89 package Ikariam; 89 package Ikariam;
90 use strict; 90 use strict;
91 use Data::Dumper; 91 use Data::Dumper;
92 use LWP; 92 use LWP;
93 # use LWP::Debug qw(+ -conns -trace -debug); 93 # use LWP::Debug qw(+ -conns -trace -debug);
94 # use LWP::Debug qw(+trace); 94 use LWP::Debug qw(+trace);
95 use HTTP::Cookies; 95 use HTTP::Cookies;
96 use WWW::Mechanize; 96 use WWW::Mechanize;
97 use HTML::TagParser; 97 use HTML::TagParser;
98 use XML::LibXML qw(:encoding); 98 use XML::LibXML qw(:encoding);
99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; 99 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
370 my @locations = @{$self->{'cities'}->{$cityId}->{locations}}; 370 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
371 foreach (0..$#locations) { 371 foreach (0..$#locations) {
372 $position = $_ if($locations[$_] eq $type); 372 $position = $_ if($locations[$_] eq $type);
373 } 373 }
374 374
375 if($position == -1) 375 if($position == -1) {
376 {
377 foreach (0..$#locations) { 376 foreach (0..$#locations) {
377 # XXX
378 next if($_ <= 2 && ($self->{buildingIDs}->{$type} ne "workshop-fleet" && 378 next if($_ <= 2 && ($self->{buildingIDs}->{$type} ne "workshop-fleet" &&
379 $self->{buildingIDs}->{$type} ne "shipyard")); 379 $self->{buildingIDs}->{$type} ne "shipyard"));
380 if($locations[$_] eq undef) { 380 if($locations[$_] eq undef) {
381 my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=build&id=%s&position=%s&building=%d', 381 my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=build&id=%s&position=%s&building=%d',
382 $self->{server}, $cityId, $_, $self->{buildingIDs}->{$type} )); 382 $self->{server}, $cityId, $_, $self->{buildingIDs}->{$type} ));
383 last; 383 last;
384 } 384 }
385 } 385 }
386 } else { 386 } else {
387 $self->{mech}->add_header( Referer => 387 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
388 sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position)); 388 my $url = Ikariam::Extractor->new(content => $res->content)->find('//a[@title="升級建築物"/@href');
389 my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [ 389 $self->{mech}->get($url);
390 action => 'CityScreen',
391 'function' => 'upgradeBuilding',
392 id => $cityId,
393 position => $position,
394 level => $self->{'cities'}->{$cityId}->{buildings}->{$type},
395 oldView => $type,
396 ]);
397 # my $content;
398 # gunzip \$res->content => \$content
399 # or die "gunzip failed: $GunzipError\n";
400 # print ($content);
401 } 390 }
402 } 391 }
403 392
404 sub run { 393 sub run {
405 my $self = shift; 394 my $self = shift;
698 my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [ 687 my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [
699 name => $self->{user}, 688 name => $self->{user},
700 password => $self->{pass}, 689 password => $self->{pass},
701 ]); 690 ]);
702 691
703 my $c; 692 my @cities = Ikariam::Extractor->new(content => $res->content)->find('//option[@class="avatarCities coords"]/@value');
704 my $status = gunzip \$res->content => \$c 693
705 or die "gunzip failed: $GunzipError\n"; 694 if($#cities<0) {
706 695 die ("login failed\n");
707 if($c =~ /錯誤!/) 696 }
708 { 697 foreach(@cities) {
709 die ("password error\n"); 698 $self->{'cities'}->{$_} = {};
710 } else {
711 my $result = XML::LibXML->new('1.0','utf-8')
712 ->parse_html_string ($c, { suppress_errors => 1 })
713 ->find( '//option[@class="avatarCities coords"]' );
714
715 if ( $result->isa( 'XML::LibXML::NodeList' ) ) {
716 foreach ( @$result ) {
717 $self->{'cities'}->{$_->getAttribute('value')} = {};
718 }
719 }
720 } 699 }
721 } 700 }
722 701
723 sub getElementsByTagName { 702 sub getElementsByTagName {
724 my $element = shift; 703 my $element = shift;