changeset 84:2c6c219dc3ff

fixed the checkCity function, implemneted a human resources palnning function.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 26 Oct 2008 01:43:40 +0800
parents ec087515da38
children f25d13153501
files Ikariam.pm agent.pl city.yaml
diffstat 3 files changed, 55 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- 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;
--- 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;
 
--- 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
             # 要蓋到何種程度?