diff Ikariam.pm @ 150:fab1593cadf1

fixed the combat reading issue.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 01 Nov 2008 03:52:09 +0800
parents b23077d597b0
children 637314cc57ed
line wrap: on
line diff
--- a/Ikariam.pm	Sat Nov 01 02:01:53 2008 +0800
+++ b/Ikariam.pm	Sat Nov 01 03:52:09 2008 +0800
@@ -37,7 +37,6 @@
     return $self;
 }
 
-# parse($Content);
 sub parse {
     my ($self, $content) = @_;
     my $string;
@@ -68,7 +67,10 @@
     return undef unless defined($result);
 
     if ( $result->isa( 'XML::LibXML::NodeList' ) ) {
-        return () if($result->size() == 0);
+        if($result->size() == 0) {
+            return undef unless wantarray;
+            return ();
+        }
         foreach ( @$result ) {
             # $_ is XML::LibXML::Element, XML::LibXML::Node
             my $literal = $_->to_literal();
@@ -577,17 +579,21 @@
     $report{defender} = $extractor->find('//div[@id="troopsReport"]//table[@id="result"]//th[@class="defenders"]');
     $report{winner} = $extractor->find('//div[@id="troopsReport"]//table[@id="result"]//td[@class="winner"]');
 
+    # the combat we win!
     $report{targetCity} = $extractor->find('//div[@id="troopsReport"]/div/h3/a/text()');
     my $href = $extractor->find('//div[@id="troopsReport"]/div/h3/a/@href');
-    # $report{targetCity} = $extractor->find('//td[@class="battle"]/a/text()');
-    # my $href = $extractor->find('//td[@class="battle"]/a/@href');
+    if(!defined($href)) {
+        # the combat we lost!
+        $report{targetCity} = $extractor->find('//td[@class="battle"]/a/text()');
+        $href = $extractor->find('//td[@class="battle"]/a/@href');
+    }
 
     if($href =~ /index\.php\?view=island&id=(\d+)&selectCity=(\d+)/) {
        $report{island} = $1;
        $report{city} = $2;
     } else {
+        warn($href);
         warn ("can not read combat reprot $combatId");
-        warn ($href);
         return undef;
     }
     $report{gold} = $extractor->find('//div[@id="troopsReport"]//ul[@class="resources"]/li[@class="gold"]');