# HG changeset patch # User "Rex Tsai " # Date 1224956620 -28800 # Node ID 2c6c219dc3ff7e0cd8a12c937d8e7bac5cf0c19c # Parent ec087515da38546c3e839b1dad9dd73be6d0e010 fixed the checkCity function, implemneted a human resources palnning function. diff -r ec087515da38 -r 2c6c219dc3ff Ikariam.pm --- a/Ikariam.pm Sat Oct 25 01:18:00 2008 +0800 +++ b/Ikariam.pm Sun Oct 26 01:43:40 2008 +0800 @@ -392,8 +392,9 @@ sub run { my $self = shift; + my $param = shift; # defense. - die("Not implemented"); + die("run $param not implemented yet."); } sub research @@ -428,6 +429,13 @@ my $cityId = shift; # search for goods + $self->{mech}->get(sprintf("http://%s/index.php", $self->{server})); + $self->{mech}->submit_form( + form_number => 2, + fields => { + cityId => $cityId, + } + ); my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=city&id=%d", $self->{server}, $cityId)); my $extractor = new Ikariam::Extractor(content => $res->content); @@ -454,14 +462,14 @@ } foreach my $i (0..14) { - my @buildings = $extractor->find(sprintf('//*[@id="position%s"]/@class', $i)); + my @buildings = $extractor->find(sprintf('//li[@id="position%s"]/@class', $i)); foreach my $building (@buildings) { if (!($building =~ /buildingGround/) && !($building =~ /townhall/)) { $self->{'cities'}->{$cityId}->{locations}[$i] = $building; my $span; - my @spans = $extractor->find(sprintf('//*[@id="position%s"]//span[@class="textLabel"]/text()', $i)); - if($#spans>=1) { + my @spans = $extractor->find(sprintf('//li[@id="position%s"]//span[@class="textLabel"]/text()', $i)); + if($#spans >= 1) { $span = $spans[1]; } else { $span = $spans[0]; @@ -674,6 +682,40 @@ return $self->{'cities'}; } +sub getNetIncome { + my $self = shift; + my $cityId = shift; + + Carp::croak("must assign cityId") if !defined($cityId); + + # 扣除研發每人花費 8 GOLD + return ($self->{cities}->{$cityId}->{citizens}->{total}*4 - $self->{cities}->{$cityId}->{citizens}->{scientists}*8 ); +} + +sub blanceHurmanResource { + my $self = shift; + my $cityId = shift; + my $workersRatio = { + 'citizens' => 0.4, + 'specialworkers' => 0.3, + 'woodworkers' => 0.7, + }; + + my $netincome = $self->getNetIncome($cityId); + + # --- HR --- + # 扣除研發,四成種田生產,剩下 3:7 挖資源 + # 四成收入中可用兩成做軍事用途 + # 生產共四成 + my $produceworkers = int(($netincome * $workersRatio->{citizens}) / 4); + + # 換成生產人力 + my $freePeople = $self->{cities}->{$cityId}->{citizens}->{total} - ($produceworkers + $self->{cities}->{$cityId}->{scientists}); + + # XXX + Carp::carp(sprintf("suggested - produce: %s wood %s special %s\n", $produceworkers, int($freePeople*0.3), int($freePeople*0.7))); +} + sub logout { my $self = shift; diff -r ec087515da38 -r 2c6c219dc3ff agent.pl --- a/agent.pl Sat Oct 25 01:18:00 2008 +0800 +++ b/agent.pl Sun Oct 26 01:43:40 2008 +0800 @@ -4,7 +4,7 @@ use Ikariam; use Data::Dumper; use Decision::ParseTree q{ParseTree}; -use YAML qw/LoadFile Dump/; +use YAML qw/LoadFile Dump DumpFile/; package Ikariam::Cities::Rules; use strict; @@ -172,7 +172,7 @@ package main; -my $verbose = undef; +my $verbose = 1; our $i = new Ikariam($::server, $::user, $::pass); $i->login; @@ -187,7 +187,7 @@ $decision->{parse_answer} = undef; my $action = ParseTree($tree, $rules, $decision); triggerAction($action, (keys(%$cities))[0]) if(defined($action)); -print STDERR Dumper($decision) if(defined($verbose)); +print STDERR Dump($decision) if(defined($verbose)); # show cities. @@ -205,6 +205,7 @@ print("\n"); } print("\n"); + $i->blanceHurmanResource($cityId); # build and upgrade for cities $tree = LoadFile('city.yaml'); @@ -220,9 +221,10 @@ } } # Debug - print(Dumper($cities->{$cityId}->{parse_path})) if(defined($verbose)); - print(Dumper($cities)) if(defined($verbose)); + # print(Dump($cities->{$cityId}->{parse_path})) if(defined($verbose)); } +# print(Dump($cities)) if(defined($verbose)); +DumpFile("dump.yaml", $cities); $i->logout; diff -r ec087515da38 -r 2c6c219dc3ff city.yaml --- a/city.yaml Sat Oct 25 01:18:00 2008 +0800 +++ b/city.yaml Sun Oct 26 01:43:40 2008 +0800 @@ -49,6 +49,8 @@ - is_drydock_researched: # requireed more army ? 1: build_shipyard + +# 應該把 Barracks 建到最佳等級 # 1: # - build one board # 要蓋到何種程度?