changeset 174:0cfc7a19a4d2

branch merged
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 04:12:23 +0800
parents e8a244ce5a1d (diff) 2efa901871e5 (current diff)
children 9f248c8460ce 8afd380c20ac dba0543daf36
files Ikariam.pm
diffstat 3 files changed, 65 insertions(+), 10 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Sun Nov 02 03:01:24 2008 +0800
+++ b/Ikariam.pm	Sun Nov 02 04:12:23 2008 +0800
@@ -1,4 +1,3 @@
-#!/usr/bin/env perl
 BEGIN {
     foreach (((getpwuid($<))[7], $ENV{HOME}, $ENV{LOGDIR}, ".")) {
         require "$_/.eagleeye.pm" if (-f "$_/.eagleeye.pm");
@@ -373,6 +372,32 @@
     }
 }
 
+# for tavern only
+sub set {
+    my $self = shift;
+    my $type = shift;
+    my $cityId = shift;
+
+    die ("we don't know about this city") unless(defined($self->{'cities'}->{$cityId}));
+
+    my $position = -1;
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    foreach (0..$#locations) {
+        $position = $_ if($locations[$_] eq $type);
+    }
+
+     if($position != -1) {
+         $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
+         $self->{mech}->submit_form(
+                 form_number => 1,
+                 fields      => {
+                 amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
+                 }
+                 );
+ 
+    }
+}
+
 sub build {
     my $self = shift;
     my $type = shift;
@@ -535,10 +560,29 @@
     $self->{'cities'}->{$cityId}->{maxActionPoints} = $extractor->find('//span[@id="value_maxActionPoints"]');;
 }
 
+sub checkTavern {
+    my $self = shift;
+    my $cityId = shift;
+
+    my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
+    # init
+    $self->{'cities'}->{$cityId}->{'tavern'}->{'maxValue'} = 0;
+    $self->{'cities'}->{$cityId}->{'tavern'}->{'iniValue'} = 0;
+    foreach (0..$#locations) {
+        if($locations[$_] eq 'tavern') {
+            my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=tavern&id=%d&position=%d', $self->{server}, $cityId, $_ ));
+            my $extractor = Ikariam::Extractor->new(content => $res->content);
+            if($extractor->{doc}->toString(0) =~ /maxValue : (\d+),\s+overcharge : \d+,\s+iniValue : (\d+),/) {
+                $self->{'cities'}->{$cityId}->{'tavern'}->{'maxValue'} = $1;
+                $self->{'cities'}->{$cityId}->{'tavern'}->{'iniValue'} = $2;
+            }
+            last;
+        }
+    }
+}
 
 sub checkMilitaryAdvisorMilitaryMovements  {
     my $self = shift;
-    # TODO
     my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}));
     my $extractor = new Ikariam::Extractor(content => $res->content);
 
@@ -800,6 +844,7 @@
         $self->checkCity($cityId);
         $self->checkTownHall($cityId);
         $self->checkArmies($cityId);
+        $self->checkTavern($cityId);
     }
     return $self->{'cities'};
 }
--- a/agent.pl	Sun Nov 02 03:01:24 2008 +0800
+++ b/agent.pl	Sun Nov 02 04:12:23 2008 +0800
@@ -52,10 +52,7 @@
 
 sub is_happiness {
     my ($self, $city) = @_;
-    # FIXME.
-    # we should update the tavern setting.
 
-    # we should update the travl
     return ($city->{growth} >= 5 ?  1 : 0) 
         if($city->{buildings}->{townHall} <= 10);
 
@@ -64,6 +61,16 @@
     return ($city->{happiness} >= 2 ?  1 : 0);
 }
 
+sub is_tavern_available {
+    my ($self, $city) = @_;
+    return (defined($city->{buildings}->{tavern}) ? 1 : 0);
+}
+
+sub is_bacchanal {
+    my ($self, $city) = @_;
+    return ($city->{tavern}->{maxValue} == $city->{tavern}->{iniValue} ? 1 : 0);
+}
+
 sub is_warehouse_enough {
     my ($self, $city) = @_;
     # TODO 以速率計算容納率
@@ -162,7 +169,6 @@
     return (defined($self->{'ikariam'}->{research}->{4010}) ?  1 : 0);
 }
 
-
 sub is_barracks_level_enough {
     my ($self, $city) = @_;
     return 0 if(!defined($city->{buildings}->{barracks}));
@@ -246,7 +252,6 @@
 
 our $i = new Ikariam($::server, $::user, $::pass);
 $i->login;
-$i->checkMilitaryAdvisorCombatReports();
 my $cities = $i->check;
 
 # Genereic rules for both overall and city level arranagement.
--- a/city.yaml	Sun Nov 02 03:01:24 2008 +0800
+++ b/city.yaml	Sun Nov 02 04:12:23 2008 +0800
@@ -23,10 +23,15 @@
             - is_warehouse_enough:
                0: build_warehouse
             - is_happiness:
-               0: 
+               0:
                   - is_winepress_researched:
-                     # FIXME: we should update the tavern setting.
-                     1: build_tavern
+                     1:
+                        - is_tavern_available:
+                           0: build_tavern
+                           1: 
+                              - is_bacchanal:
+                                 0: set_tavern
+                                 1: build_tavern
             # TODO
             # http://ikariam.wikia.com/wiki/List_of_buildings
             # http://ikariam.wikia.com/wiki/Technology_Tree