comparison Ikariam.pm @ 87:4c82cf844aa8

counting navy expenditure
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 26 Oct 2008 15:02:06 +0800
parents a8acf50bf7bb
children 0fa9cd836e1e
comparison
equal deleted inserted replaced
86:a8acf50bf7bb 87:4c82cf844aa8
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) ;
155 offset => $offset, 155 offset => $offset,
156 searchUser => $user, 156 searchUser => $user,
157 view => 'highscore' 157 view => 'highscore'
158 ]); 158 ]);
159 159
160 # TODO rewrite with Xpath
160 my $c; 161 my $c;
161 my $status = gunzip \$res->content => \$c 162 my $status = gunzip \$res->content => \$c
162 or die "gunzip failed: $GunzipError\n"; 163 or die "gunzip failed: $GunzipError\n";
163 164
164 my %users; 165 my %users;
646 foreach my $x (qw/army fleet/) { 647 foreach my $x (qw/army fleet/) {
647 $self->{'cities'}->{$cityId}->{$x} = {}; 648 $self->{'cities'}->{$cityId}->{$x} = {};
648 my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=cityMilitary-%s&id=%d', $self->{server}, $x, $cityId)); 649 my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=cityMilitary-%s&id=%d', $self->{server}, $x, $cityId));
649 my @numbers = Ikariam::Extractor->new(content => $res->content)->find('//table//tr/td'); 650 my @numbers = Ikariam::Extractor->new(content => $res->content)->find('//table//tr/td');
650 foreach my $j (0..$#{$force_types{$x}}) { 651 foreach my $j (0..$#{$force_types{$x}}) {
652 next if(!defined($force_types{$x}[$j]));
651 if ($numbers[$j] == '-') { 653 if ($numbers[$j] == '-') {
652 $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = 0; 654 $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = 0;
653 } else { 655 } else {
654 $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = $numbers[$j]; 656 $self->{'cities'}->{$cityId}->{$x}->{$force_types{$x}[$j]} = $numbers[$j];
655 } 657 }
688 690
689 Carp::croak("must assign cityId") if !defined($cityId); 691 Carp::croak("must assign cityId") if !defined($cityId);
690 692
691 # 扣除研發每人花費 8 GOLD 693 # 扣除研發每人花費 8 GOLD
692 return ($self->{cities}->{$cityId}->{citizens}->{total}*4 - $self->{cities}->{$cityId}->{citizens}->{scientists}*8 ); 694 return ($self->{cities}->{$cityId}->{citizens}->{total}*4 - $self->{cities}->{$cityId}->{citizens}->{scientists}*8 );
695 }
696
697 sub getNavyExpenditure {
698 my $self = shift;
699 my $cityId = shift;
700 my $cities = $self->{cities};
701
702 my $ships = {
703 CargoShip => {n => "CargoShip",a => 0,d => 0,s => 4,c => "Steamship",v => 20,A => 0,D => 0},
704 'Ram-Ship' => {n => "Ram-Ship",p => 6,w => 56,S => 21,b => "34m",u => 20,m => 1,o => 3,a => 16,d => 16,A => 4,D => 4,s => 10,c => "Sailer",v => 10},
705 BallistaShip => {n => "BallistaShip",p => 5,w => 72,S => 29,b => "47m",u => 24,m => 3,o => 5,a => 20,d => 28,A => 5,D => 7,s => 11,c => "Sailer",v => 8,x => "Resistance"},
706 Flamethrower => {n => "Flamethrower",p => 5,w => 105,S => 77,b => "1h 55m",u => 45,m => 5,o => 7,a => 40,d => 40,A => 10,D => 10,s => 12,c => "Steamship",v => 8,x => "Assault"},
707 CatapultShip => {n => "CatapultShip",p => 10,w => 173,S => 76,b => "3h 11m",u => 57,m => 7,o => 10,a => 60,d => 60,A => 12,D => 12,s => 16,c => "Sailer",v => 6},
708 MortarShip => {n => "MortarShip",p => 22,w => 456,S => 282,b => "3h 38m",u => 130,m => 12,o => 15,a => 160,d => 160,A => 35,D => 35,s => 14,c => "Steamship",v => 4},
709 PaddleWheelRam => {n => "PaddleWheelRam",i => "steamboat",p => 12,w => 513,S => 167,b => "4h 8m",u => 114,m => 10,o => 13,a => 100,d => 90,A => 20,D => 18,s => 13,c => "Steamship",v => 8,x => "Assault"},
710 DivingBoat => {n => "DivingBoat",i => "submarine",p => 16,w => 493,C => 378,b => "5h 5m",u => 126,m => 15,o => 16,a => 110,d => 155,A => 20,D => 30,s => 10,c => "Steamship",v => 2,x => "Resistance"}
711 };
712 my $cost = 0;
713 foreach(keys(%{$cities->{$cityId}->{fleet}})) {
714 $cost += $cities->{$cityId}->{fleet}->{$_} * $ships->{$_}->{u};
715 }
716 return $cost;
693 } 717 }
694 718
695 sub getMilityExpenditure { 719 sub getMilityExpenditure {
696 my $self = shift; 720 my $self = shift;
697 my $cityId = shift; 721 my $cityId = shift;
726 Doctor => {n=>"Doctor",i=>"medic",p=>6,w=>640,C=>361,b=>"1h 2m",u=>244,m=>11,o=>12,a=>4,d=>28,A=>0,D=>0,s=>14,c=>"Human",v=>20,x=>"Healer"}, 750 Doctor => {n=>"Doctor",i=>"medic",p=>6,w=>640,C=>361,b=>"1h 2m",u=>244,m=>11,o=>12,a=>4,d=>28,A=>0,D=>0,s=>14,c=>"Human",v=>20,x=>"Healer"},
727 Cook => {n=>"Cook",p=>4,w=>520,W=>103,b=>"38m",u=>138,m=>8,o=>8,a=>6,d=>26,A=>0,D=>0,s=>16,c=>"Human",v=>20,x=>"Regeneration"} 751 Cook => {n=>"Cook",p=>4,w=>520,W=>103,b=>"38m",u=>138,m=>8,o=>8,a=>6,d=>26,A=>0,D=>0,s=>16,c=>"Human",v=>20,x=>"Regeneration"}
728 }; 752 };
729 753
730 my $cost = 0; 754 my $cost = 0;
731
732 foreach(keys(%{$cities->{$cityId}->{army}})) { 755 foreach(keys(%{$cities->{$cityId}->{army}})) {
733 # printf("%s %d = %d\n", $_, $cities->{$cityId}->{army}->{$_}, $cities->{$cityId}->{army}->{$_} * $troops->{$_}->{u}); 756 # printf("%s %d = %d\n", $_, $cities->{$cityId}->{army}->{$_}, $cities->{$cityId}->{army}->{$_} * $troops->{$_}->{u});
734 $cost += $cities->{$cityId}->{army}->{$_} * $troops->{$_}->{u}; 757 $cost += $cities->{$cityId}->{army}->{$_} * $troops->{$_}->{u};
735 } 758 }
736 return $cost; 759 return $cost;