changeset 81:d57920014cb4

split buliding.yaml to city.yaml and overall.yaml
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 25 Oct 2008 00:42:59 +0800
parents 7ab5fc8c847c
children 82acbba33b97
files Ikariam.pm agent.pl building.yaml city.yaml overall.yaml
diffstat 5 files changed, 240 insertions(+), 150 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Fri Oct 24 21:56:03 2008 +0800
+++ b/Ikariam.pm	Sat Oct 25 00:42:59 2008 +0800
@@ -65,7 +65,7 @@
     return undef unless defined($result);
 
     if ( $result->isa( 'XML::LibXML::NodeList' ) ) {
-        return undef if($result->size() == 0);
+        return () if($result->size() == 0);
         foreach ( @$result ) {
             # $_ is XML::LibXML::Element, XML::LibXML::Node
             my $literal = $_->to_literal() , "\n";
@@ -415,12 +415,6 @@
 
     # check if we are researching the same stuff
     my $res =  $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=changeResearch&id=%s&researchType=%s', $self->{server}, $cityId, $type));
-
-#    my $content;
-#    gunzip \$res->content => \$content 
-#        or die "gunzip failed: $GunzipError\n";
-#
-#    print ($content);
 }
 
 sub checkResearch {
@@ -477,11 +471,11 @@
                 $self->{'cities'}->{$cityId}->{locations}[$i] = $building;
 
                 my $span;
-                if($building eq 'townHall') {
-                    # this is stupid, the new vession give two cityTown information.
-                    $span = ($extractor->find(sprintf('//*[@id="position%s"]//span[@class="textLabel"]/text()', $i)))[1];
+                my @spans = $extractor->find(sprintf('//*[@id="position%s"]//span[@class="textLabel"]/text()', $i));
+                if($#spans>=1) {
+                    $span = $spans[1];
                 } else {
-                    $span = $extractor->find(sprintf('//*[@id="position%s"]//span[@class="textLabel"]/text()', $i));
+                    $span = $spans[0];
                 }
                 my (undef, undef, $level) = split(/ /, $span);
                 $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level;
@@ -499,8 +493,88 @@
 sub checkMilitaryAdvisorMilitaryMovements  {
     my $self = shift;
     # TODO
-    # http://s2.ikariam.tw/index.php?view=militaryAdvisorMilitaryMovements
+    # http://%s/index.php?view=militaryAdvisorMilitaryMovements
     my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}));
+    my $extractor = new Ikariam::Extractor(content => $res->content);
+
+    my @homeland = $extractor->find('//div[@id="homeland"]');
+    my @elsewhere = $extractor->find('//div[@id="elsewhere"]');
+    $self->{'military'}->{attacks} = $#elsewhere+1;
+    $self->{'military'}->{wars} = $#homeland+1;
+
+    # there are some details.
+#<div id="homeland" class="content">
+#                <ul id="battleReports">
+#                    <li class="groundfight">
+#                        <div class="type"/>
+#                        <div class="report">
+#                            <table cellspacing="0" cellpadding="0" class="repTable">
+#                                <tbody><tr>
+#                                    <th colspan="2">
+#                                        攻擊 卍严破碎虛空焱卐                                    </th>
+#                                </tr>
+#                            </tbody></table>
+#                        </div>
+#                        <br class="clearfloat"/>
+#                    </li>
+#					<li class="enroute" style="height: 52px;">
+#						<script type="text/javascript">
+#							Event.onDOMReady(function() {
+#								getCountdown({
+#									enddate: 1224863393,
+#									currentdate: 1224862910,
+#									el: "researchCountDown11396440"
+#									}, 3, " ", "", true, true);
+#								});
+#							</script>
+#                        <div class="type">
+#                            <div id="researchCountDown11396440" class="time">5分 46秒</div><div class="undefined"> Finished 23:49:23</div>
+#                        </div>
+#                        <div class="report">
+#來自 <a href="?view=island&amp;cityId=98496">松鼠城</a> 的 3 兵力正在前往 <a href="?view=island&amp;cityId=82959">卍严破碎虛空焱卐</a> 的途中。                        </div>
+#                        <div class="action">
+#                            <div class="return">
+#                                <a href="?action=transportOperations&amp;function=forceReturn&amp;actionRequest=46cc5fdb56b8cb0e65e2116ff9f61512&amp;eventId=11396440&amp;oldView=militaryAdvisorMilitaryMovements" title="返回">返回</a>
+#                            </div>
+#                        </div>
+#                        <br class="clearfloat"/>
+#                    </li>
+#		        </ul>
+#</div>
+
+
+#    <div id="elsewhere" class="content">
+#                <ul id="battleReports">
+#                    <li class="groundfight">
+#                        <div class="type"/>
+#                        <div class="report">
+#                            <table cellspacing="0" cellpadding="0" class="repTable">
+#                                <tbody><tr>
+#                                    <th colspan="2">
+#                                        攻擊 Rex                                    </th>
+#                                </tr>
+#                            </tbody></table>
+#                        </div>
+#                        <br class="clearfloat"/>
+#                    </li>
+#                    <li class="enroute">
+#                        <script type="text/javascript">
+#												Event.onDOMReady(function() {
+#													getCountdown({
+#														enddate: 1224877160,
+#														currentdate: 1224861666,
+#														el: "researchCountDown159638"
+#														}, 3, " ", "", true, true);
+#													});
+#                        </script>
+#                        <div class="type">
+#                            <div id="researchCountDown159638" class="time">4時 17分 35秒</div>
+#                        </div>
+#                        <div class="report">
+#來自 <a href="?view=island&amp;cityId=6078">水晶交換各種資源</a> 的 3 兵力正在前往 <a href="?view=island&amp;cityId=6077">Rex</a> 的途中。                        </div>
+#                    </li>
+#		        </ul>
+#            </div>
 }
 
 sub checkMilitaryAdvisorCombatReports {
@@ -511,11 +585,6 @@
         or die "gunzip failed: $GunzipError\n";
 
     # TODO
-    # military
-    $self->{'military'}->{wars}->{attacks} = 0;
-    $self->{'military'}->{wars}->{wars} = 0;
-    # $self->{'cities'}->{$cityId}->{wars}->{attacks} = $1 if($content =~ /敵人攻擊: (\d+)/);
-    # $self->{'cities'}->{$cityId}->{wars}->{wars} = $1 if($content =~ /我方軍隊行程: (\d+)/);
 
     # list down reports.
     # /index.php?view=militaryAdvisorReportView&amp;combatId=1887662
@@ -598,6 +667,13 @@
     # MilitaryAdvisor
     $self->checkMilitaryAdvisorMilitaryMovements();
     $self->checkMilitaryAdvisorCombatReports();
+    $self->{'research'} = $self->checkResearch((keys(%{$self->{'cities'}}))[0]);
+
+    # alerts
+    # //li[@id="advCities"]/a[@class="normalalert"]
+    # //li[@id="advMilitary"]/a[@class="normalalert"]
+    # //li[@id="advResearch"]/a[@class="normalalert"]
+    # //li[@id="advDiplomacy"]/a[@class="normalalert"]
 
     # looking for cities
     foreach my $cityId (keys(%{$self->{'cities'}})) 
@@ -605,9 +681,7 @@
         $self->checkCity($cityId);
         $self->checkTownHall($cityId);
         $self->checkArmies($cityId);
-        $self->{'cities'}->{$cityId}->{'research'} = $self->checkResearch($cityId);
     }
-    print Dumper($self->{'cities'});
     return $self->{'cities'};
 }
 
--- a/agent.pl	Fri Oct 24 21:56:03 2008 +0800
+++ b/agent.pl	Sat Oct 25 00:42:59 2008 +0800
@@ -11,17 +11,16 @@
 use Data::Dumper;
 
 sub new {
-    my ( $class ) = @_;
-    my $self = {};
+    my ( $class, $i ) = @_;
+    my $self = {
+        ikariam => $i,
+    };
     return bless $self, $class;
 }
 
 sub is_attacked {
     my ($self, $city) = @_;
-    # XXX
-    # $self->{'military'}->{wars}->{attacks}
-    # return ($city->{wars}->{attacks} > 0 ) ? 1 : 0;
-    return 0;
+    return ($self->{'ikariam'}->{'military'}->{attacks} > 0 ) ? 1 : 0;
 }
 
 sub is_constructing {
@@ -32,6 +31,7 @@
 sub is_wall_enough {
     my ($self, $city) = @_;
     # http://ikariam.wikia.com/wiki/Wall_Defense
+    # Basic rule - the wall level must equale or large then townHall level.
     return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ?  1 : 0);
 }
 
@@ -97,47 +97,48 @@
 
 sub is_expansion_researched {
     my ($self, $city) = @_;
-    return (defined($city->{research}->{1030}) ?  1 : 0);
+    # $self->{'ikariam'}->
+    return (defined($self->{'ikariam'}->{research}->{1030}) ?  1 : 0);
 }
 
 sub is_culturalexchange_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{1040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{1040}) ?  1 : 0);
 }
 
 sub is_wealth_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{2030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{2030}) ?  1 : 0);
 }
 
 sub is_winepress_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{2040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{2040}) ?  1 : 0);
 }
 
 sub is_paper_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3020}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3020}) ?  1 : 0);
 }
 
 sub is_espionage_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3030}) ?  1 : 0);
 }
 
 sub is_invention_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{3040}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{3040}) ?  1 : 0);
 }
 
 sub is_professionalarmy_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{4030}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{4030}) ?  1 : 0);
 }
 
 sub is_drydock_researched {
-    my ($self, $city) = @_;
-    return (defined($city->{research}->{4010}) ?  1 : 0);
+    my ($self) = @_;
+    return (defined($self->{'ikariam'}->{research}->{4010}) ?  1 : 0);
 }
 
 sub is_barracks_level_enough {
@@ -145,7 +146,7 @@
     return 0 if(!defined($city->{buildings}->{barracks}));
     # 方陣兵需要 level 4
     # optimum is 5
-    return ($city->{buildings}->{barracks} >= 3 ? 1 : 0);
+    return ($city->{buildings}->{barracks} >= 4 ? 1 : 0);
 }
 
 sub is_shipyard_level_enough {
@@ -159,24 +160,36 @@
 sub rule_engagement
 {
     my ($self, $city) = @_;
-    # XXX
+    # TODO
     # 計算距離, 可搶劫比例, 是否有聯盟, 軍事分數 (win rate)
 }
 
 sub rule_resource
 {
-    # XXX
+    # TODO 
 }
 1; 
 
 
 package main;
+my $verbose = undef;
+
 our $i = new Ikariam($::server, $::user, $::pass);
 $i->login;
 my $cities = $i->check;
-my $rules = Ikariam::Cities::Rules->new;
-my $tree  = LoadFile('building.yaml');
-# print Dumper($tree);
+
+# Genereic rules for both overall and city level arranagement.
+my $rules = Ikariam::Cities::Rules->new($i);
+# blanace resources, arrange defance 
+my $tree  = LoadFile('overall.yaml');
+my $decision;
+$decision->{parse_path} = [];
+$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));
+
+
 # show cities.
 foreach my $cityId (keys(%$cities)) {
     printf("%s http://%s/index.php?view=city&id=%d\n", 
@@ -192,25 +205,23 @@
         print("\n");
     }
     print("\n");
-    # print(Dumper($cities));
 
-    # make decisions
-
-    # for the Decision Tree
+    # build and upgrade for cities
+    $tree  = LoadFile('city.yaml');
     $cities->{$cityId}->{parse_path} = [];
     $cities->{$cityId}->{parse_answer} = undef;
     while (my $action = ParseTree($tree, $rules, $cities->{$cityId}))
     {
-        # TODO: remove the last rule, if the result is fallback
         triggerAction($action, $cityId);
         if($cities->{$cityId}->{parse_answer} ne "fallback") {
             last ;
         } else {
-
+            # TODO: remove the last rule, if the result is fallback
         }
     }
     # Debug
-    print(Dumper($cities->{$cityId}->{parse_path}));
+    print(Dumper($cities->{$cityId}->{parse_path})) if(defined($verbose));
+    print(Dumper($cities)) if(defined($verbose));
 }
 
 $i->logout;
@@ -220,7 +231,7 @@
     my ($action, $cityId) = @_;
 
     my ($func, $param) = split(/_/,$action);
-    printf('$i->%s("%s", %s);'. "\n\n", $func, $param, $cityId);
+    printf('$i->%s("%s", %s);'. "\n", $func, $param, $cityId);
     # eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
     # warn $@ if $@;
 }
--- a/building.yaml	Fri Oct 24 21:56:03 2008 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,94 +0,0 @@
----
-# 基本建設規則
-# 若糟攻擊則不建設
-- is_attacked:
-    # we are in Peace :D
-    0:
-      - is_constructing:
-         # already building something.
-         1:
-           # adjust human resources
-           # 
-           # 相關基礎研究 (technologies tree)
-           # 財富
-            - is_wealth_researched:
-               0: research_economy
-            # 擴張
-            - is_expansion_researched:
-               0: research_seafaring
-            # 造紙
-            - is_paper_researched:
-               0: research_knowledge
-            # 正規軍
-            - is_professionalarmy_researched:
-               0: resaerch_military
-            # 釀酒
-            - is_winepress_researched:
-               0: research_economy
-            # 發明 (兵工廠)
-            - is_invention_researched:
-               0: research_knowledge
-            # 異國文化
-            - is_culturalexchange_researched:
-               0: research_seafaring
-            # is_reousrce_balanced.
-               # 酒館
-               # 學院
-            # balance resources
-            # 從其他城移動資源到特定城市
-            # 預先計算可能需要的資源
-         # let's find something to build up
-         0:
-            # XXX: 這樣會一口氣買同等城市數量的船耶
-            # - is_gold_enoughforcargo:
-               # 1: increaseTransporter
-            - is_wall_enough:
-               0: build_wall
-            - is_space_enough:
-               0: 
-                  - is_resource_enoghforHall:
-                     1: build_townHall
-            - is_corruption:
-               1: build_governorsresidence
-            # 倉庫庫存量
-            - is_warehouse_enough:
-               0: build_warehouse
-            - is_happiness:
-               0: 
-                  - is_winepress_researched:
-                     1: build_tavern
-
-            # TODO
-            # http://ikariam.wikia.com/wiki/List_of_buildings
-            # http://ikariam.wikia.com/wiki/Technology_Tree
-            # is_conservation_researched
-            #    -build_warehouse
-            # build_academy
-            # build_palace (Expansion, Capital only)
-            # build_embassy (副城, 不建)
-
-            - is_risk:
-               1: 
-                  - is_professionalarmy_researched:
-                     1:
-                        - is_shipyard_level_enough:
-                           0: build_shipyard
-                        - is_barracks_level_enough:
-                           0: build_barracks
-                  - is_shipyard_level_enough:
-                     0: 
-                        - is_drydock_researched: 
-                           # requireed more army ?
-                           1: build_shipyard
-                     # 1:
-                     #    - build one board
-            # 要蓋到何種程度?
-            # is_invention_researched
-            #    1: 
-            #       - build_workshop 
-            # is_espionage_researched
-            #    1: 
-            #       - build_hideout
-            # is_culturalexchange_resaerched
-            #    1: 
-            #       - build_museum
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/city.yaml	Sat Oct 25 00:42:59 2008 +0800
@@ -0,0 +1,63 @@
+---
+# city level
+# 基本建設規則
+# 若糟攻擊則暫停建設
+- is_attacked:
+    # we are in Peace :D
+    0:
+      # adjust human resources
+      - is_constructing:
+         0:
+            # XXX: 這樣會一口氣買同等城市數量的船耶
+            # - is_gold_enoughforcargo:
+               # 1: increaseTransporter
+            - is_wall_enough:
+               0: build_wall
+            - is_space_enough:
+               0: 
+                  - is_resource_enoghforHall:
+                     1: build_townHall
+            - is_corruption:
+               1: build_governorsresidence
+            # 倉庫庫存量
+            - is_warehouse_enough:
+               0: build_warehouse
+            - is_happiness:
+               0: 
+                  - is_winepress_researched:
+                     1: build_tavern
+
+            # TODO
+            # http://ikariam.wikia.com/wiki/List_of_buildings
+            # http://ikariam.wikia.com/wiki/Technology_Tree
+            # is_conservation_researched
+            #    -build_warehouse
+            # build_academy
+            # build_palace (Expansion, Capital only)
+            # build_embassy (副城, 不建)
+
+            - is_risk:
+               1: 
+                  - is_professionalarmy_researched:
+                     1:
+                        - is_shipyard_level_enough:
+                           0: build_shipyard
+                        - is_barracks_level_enough:
+                           0: build_barracks
+                  - is_shipyard_level_enough:
+                     0: 
+                        - is_drydock_researched: 
+                           # requireed more army ?
+                           1: build_shipyard
+# 1:
+#    - build one board
+            # 要蓋到何種程度?
+            # is_invention_researched
+            #    1: 
+            #       - build_workshop 
+            # is_espionage_researched
+            #    1: 
+            #       - build_hideout
+            # is_culturalexchange_resaerched
+            #    1: 
+            #       - build_museum
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/overall.yaml	Sat Oct 25 00:42:59 2008 +0800
@@ -0,0 +1,36 @@
+---
+# country level management.
+- is_attacked:
+   # we are in Peace :D
+   0:
+     # 相關基礎研究 (technologies tree)
+     # 財富
+     - is_wealth_researched:
+        0: research_economy
+     # 擴張
+     - is_expansion_researched:
+        0: research_seafaring
+     # 造紙
+     - is_paper_researched:
+        0: research_knowledge
+     # 正規軍
+     - is_professionalarmy_researched:
+        0: resaerch_military
+     # 釀酒
+     - is_winepress_researched:
+        0: research_economy
+     # 發明 (兵工廠)
+     - is_invention_researched:
+        0: research_knowledge
+     # 異國文化
+     - is_culturalexchange_researched:
+        0: research_seafaring
+     # is_reousrce_balanced.
+        # 酒館
+        # 學院
+     # balance resources
+     # 從其他城移動資源到特定城市
+     # 預先計算可能需要的資源
+   # let's find something to build up
+   1: run_defence
+   # we are being attacked!!!!!!!!!!!!!