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