# HG changeset patch # User "Rex Tsai " # Date 1239699471 -28800 # Node ID ba1643654e6d1669be7cf1df4244e885f75d12cd # Parent fb3cfa5533ac89d3ca5634a0a66bc02f1837a758 added locking for avoid duplicated instance. diff -r fb3cfa5533ac -r ba1643654e6d Ikariam.pm --- a/Ikariam.pm Thu Mar 05 17:36:03 2009 +0800 +++ b/Ikariam.pm Tue Apr 14 16:57:51 2009 +0800 @@ -110,13 +110,14 @@ use Data::Dumper; use LWP; +use Fcntl qw (:flock); use HTTP::Cookies; +use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; +use JSON;; +use POSIX; use WWW::Mechanize; use XML::LibXML qw(:encoding); use YAML qw/LoadFile Dump DumpFile/; -use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ; -use POSIX; -use JSON;; use utf8; sub new @@ -133,6 +134,7 @@ user => $user, pass => $pass, debug => undef, + lock => undef, }; @@ -623,6 +625,64 @@ return $out; } +sub defendPort { + my $self = shift; + my $cityId = shift; + my $fields = shift; + + # defendDuration + $self->{mech}->get(sprintf("http://%s/index.php?view=defendPort&destinationCityId=%d", $self->{server}, $cityId)); + $self->{mech}->form_number(1); + foreach my $k (keys(%{$fields})) { + eval{ + $self->{mech}->field($k, $fields->{$k}) + }; + warn $@ if($@); + } + $self->{mech}->submit(); +} + +sub defendCity { + my $self = shift; + my $cityId = shift; + my $fields = shift; + + # defendDuration + $self->{mech}->get(sprintf("http://%s/index.php?view=defendCity&destinationCityId=%d", $self->{server}, $cityId)); + $self->{mech}->form_number(1); + foreach my $k (keys(%{$fields})) { + eval{ + $self->{mech}->field($k, $fields->{$k}) + }; + warn $@ if($@); + } + $self->{mech}->submit(); +} + +sub deploymentArmy { + my $self = shift; + my $cityId = shift; + my $fields = shift; + $self->deployment($cityId, "army", $fields); +} + +sub deployment { + my $self = shift; + my $cityId = shift; + my $type = shift; + my $fields = shift; + + $self->{mech}->get(sprintf("http://%s/index.php?view=deployment&deploymentType=%s&destinationCityId=%d", $self->{server}, $type, $cityId)); + $self->{mech}->form_number(1); + foreach my $k (keys(%{$fields})) { + eval{ + $self->{mech}->field($k, $fields->{$k}) + }; + warn $@ if($@); + } + $self->{mech}->submit(); +} + sub plunderCity { my $self = shift; my $cityId = shift; @@ -664,7 +724,6 @@ } } - sub blockadeCity { my $self = shift; my $cityId = shift; @@ -785,6 +844,7 @@ $self->{'cities'}->{$cityId}->{transporters}->{sum} = $extractor->find('//span[@id="value_transSum"]/text()'); $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/[\(|\)]//g; $self->{'cities'}->{$cityId}->{maxActionPoints} = $extractor->find('//span[@id="value_maxActionPoints"]');; + return $self->{'cities'}->{$cityId}; } sub checkAcademy { @@ -1260,22 +1320,30 @@ { my $self = shift; $self->{mech}->get(sprintf('http://%s/index.php?action=loginAvatar&function=logout', $self->{server})); + close $self->{lock} or die "Cannot close lock file"; } sub login { my $self = shift; - my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [ - name => $self->{user}, - password => $self->{pass}, - ]); + # lock + open LOCKFILE, ">>.ikariam.lck" or die "Cannot open lock_file"; + flock(LOCKFILE, LOCK_EX); + $self->{lock} = \&LOCKFILE; while (1) { + + my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [ + name => $self->{user}, + password => $self->{pass}, + ]); + my @cities = Ikariam::Extractor->new(content => $res->content)->find('//option[@class="avatarCities coords"]/@value'); if($#cities<0) { warn ("login failed\n"); + sleep(10); next; } foreach (@cities) {