diff Ikariam.pm @ 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 a8acf50bf7bb
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;