comparison Ikariam.pm @ 370:ba1643654e6d

added locking for avoid duplicated instance.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 14 Apr 2009 16:57:51 +0800
parents d9d3cdff27c6
children 5d6f429e5f19
comparison
equal deleted inserted replaced
369:fb3cfa5533ac 370:ba1643654e6d
108 package Ikariam; 108 package Ikariam;
109 use strict; 109 use strict;
110 use Data::Dumper; 110 use Data::Dumper;
111 use LWP; 111 use LWP;
112 112
113 use Fcntl qw (:flock);
113 use HTTP::Cookies; 114 use HTTP::Cookies;
115 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
116 use JSON;;
117 use POSIX;
114 use WWW::Mechanize; 118 use WWW::Mechanize;
115 use XML::LibXML qw(:encoding); 119 use XML::LibXML qw(:encoding);
116 use YAML qw/LoadFile Dump DumpFile/; 120 use YAML qw/LoadFile Dump DumpFile/;
117 use IO::Uncompress::Gunzip qw(gunzip $GunzipError) ;
118 use POSIX;
119 use JSON;;
120 use utf8; 121 use utf8;
121 122
122 sub new 123 sub new
123 { 124 {
124 my ($class, $server, $user, $pass) = @_; 125 my ($class, $server, $user, $pass) = @_;
131 ), 132 ),
132 server => $server, 133 server => $server,
133 user => $user, 134 user => $user,
134 pass => $pass, 135 pass => $pass,
135 debug => undef, 136 debug => undef,
137 lock => undef,
136 }; 138 };
137 139
138 140
139 # if debug 141 # if debug
140 LWP::Debug::level('+trace'); 142 LWP::Debug::level('+trace');
621 } 623 }
622 } 624 }
623 return $out; 625 return $out;
624 } 626 }
625 627
628 sub defendPort {
629 my $self = shift;
630 my $cityId = shift;
631 my $fields = shift;
632
633 # defendDuration
634 $self->{mech}->get(sprintf("http://%s/index.php?view=defendPort&destinationCityId=%d", $self->{server}, $cityId));
635 $self->{mech}->form_number(1);
636 foreach my $k (keys(%{$fields})) {
637 eval{
638 $self->{mech}->field($k, $fields->{$k})
639 };
640 warn $@ if($@);
641 }
642 $self->{mech}->submit();
643 }
644
645 sub defendCity {
646 my $self = shift;
647 my $cityId = shift;
648 my $fields = shift;
649
650 # defendDuration
651 $self->{mech}->get(sprintf("http://%s/index.php?view=defendCity&destinationCityId=%d", $self->{server}, $cityId));
652 $self->{mech}->form_number(1);
653 foreach my $k (keys(%{$fields})) {
654 eval{
655 $self->{mech}->field($k, $fields->{$k})
656 };
657 warn $@ if($@);
658 }
659 $self->{mech}->submit();
660 }
661
662 sub deploymentArmy {
663 my $self = shift;
664 my $cityId = shift;
665 my $fields = shift;
666 $self->deployment($cityId, "army", $fields);
667 }
668
669 sub deployment {
670 my $self = shift;
671 my $cityId = shift;
672 my $type = shift;
673 my $fields = shift;
674
675 $self->{mech}->get(sprintf("http://%s/index.php?view=deployment&deploymentType=%s&destinationCityId=%d", $self->{server}, $type, $cityId));
676 $self->{mech}->form_number(1);
677 foreach my $k (keys(%{$fields})) {
678 eval{
679 $self->{mech}->field($k, $fields->{$k})
680 };
681 warn $@ if($@);
682 }
683 $self->{mech}->submit();
684 }
685
626 sub plunderCity { 686 sub plunderCity {
627 my $self = shift; 687 my $self = shift;
628 my $cityId = shift; 688 my $cityId = shift;
629 my $fields = shift; 689 my $fields = shift;
630 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=plunder&destinationCityId=%d", $self->{server}, $cityId)); 690 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=plunder&destinationCityId=%d", $self->{server}, $cityId));
661 print OUT $sheep->name . "\n"; 721 print OUT $sheep->name . "\n";
662 close(OUT); 722 close(OUT);
663 Carp::carp($treaty); 723 Carp::carp($treaty);
664 } 724 }
665 } 725 }
666
667 726
668 sub blockadeCity { 727 sub blockadeCity {
669 my $self = shift; 728 my $self = shift;
670 my $cityId = shift; 729 my $cityId = shift;
671 my $fields = shift; 730 my $fields = shift;
783 842
784 $self->{'cities'}->{$cityId}->{transporters}->{avail} = $extractor->find('//span[@id="value_transAvail"]/text()'); 843 $self->{'cities'}->{$cityId}->{transporters}->{avail} = $extractor->find('//span[@id="value_transAvail"]/text()');
785 $self->{'cities'}->{$cityId}->{transporters}->{sum} = $extractor->find('//span[@id="value_transSum"]/text()'); 844 $self->{'cities'}->{$cityId}->{transporters}->{sum} = $extractor->find('//span[@id="value_transSum"]/text()');
786 $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/[\(|\)]//g; 845 $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/[\(|\)]//g;
787 $self->{'cities'}->{$cityId}->{maxActionPoints} = $extractor->find('//span[@id="value_maxActionPoints"]');; 846 $self->{'cities'}->{$cityId}->{maxActionPoints} = $extractor->find('//span[@id="value_maxActionPoints"]');;
847 return $self->{'cities'}->{$cityId};
788 } 848 }
789 849
790 sub checkAcademy { 850 sub checkAcademy {
791 my $self = shift; 851 my $self = shift;
792 my $cityId = shift; 852 my $cityId = shift;
1258 1318
1259 sub logout 1319 sub logout
1260 { 1320 {
1261 my $self = shift; 1321 my $self = shift;
1262 $self->{mech}->get(sprintf('http://%s/index.php?action=loginAvatar&function=logout', $self->{server})); 1322 $self->{mech}->get(sprintf('http://%s/index.php?action=loginAvatar&function=logout', $self->{server}));
1323 close $self->{lock} or die "Cannot close lock file";
1263 } 1324 }
1264 1325
1265 sub login 1326 sub login
1266 { 1327 {
1267 my $self = shift; 1328 my $self = shift;
1268 1329
1269 my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [ 1330 # lock
1270 name => $self->{user}, 1331 open LOCKFILE, ">>.ikariam.lck" or die "Cannot open lock_file";
1271 password => $self->{pass}, 1332 flock(LOCKFILE, LOCK_EX);
1272 ]); 1333 $self->{lock} = \&LOCKFILE;
1273 1334
1274 while (1) { 1335 while (1) {
1336
1337 my $res = $self->{mech}->post(sprintf("http://%s/index.php?action=loginAvatar&function=login", $self->{server}), [
1338 name => $self->{user},
1339 password => $self->{pass},
1340 ]);
1341
1275 my @cities = Ikariam::Extractor->new(content => $res->content)->find('//option[@class="avatarCities coords"]/@value'); 1342 my @cities = Ikariam::Extractor->new(content => $res->content)->find('//option[@class="avatarCities coords"]/@value');
1276 1343
1277 if($#cities<0) { 1344 if($#cities<0) {
1278 warn ("login failed\n"); 1345 warn ("login failed\n");
1346 sleep(10);
1279 next; 1347 next;
1280 } 1348 }
1281 foreach (@cities) { 1349 foreach (@cities) {
1282 $self->{'cities'}->{$_} = {}; 1350 $self->{'cities'}->{$_} = {};
1283 $self->{'cities'}->{$_}->{id} = $_; 1351 $self->{'cities'}->{$_}->{id} = $_;