changeset 98:34749e907405

we now scan for ally
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 29 Oct 2008 02:43:04 +0800
parents 5f9bb3042d0c
children b35c7aaef8ca
files Ikariam.pm ikariam.sql scan.pl
diffstat 3 files changed, 63 insertions(+), 42 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Wed Oct 29 01:14:49 2008 +0800
+++ b/Ikariam.pm	Wed Oct 29 02:43:04 2008 +0800
@@ -8,7 +8,7 @@
 use Class::DBI::AutoLoader (
     dsn       => 'dbi:SQLite:dbname=ikariam.sqlite',
     options   => { RaiseError => 1 },
-    tables    => ['cities', 'island', 'user'],
+    tables    => ['cities', 'island', 'user', 'ally'],
     use_base  => 'Class::DBI::SQLite',
     namespace => 'Ikariam',
 );
@@ -143,6 +143,24 @@
     return bless $self, $class;
 }
 
+sub viewAlly
+{
+    my $self = shift;
+    my $allyId = shift;
+
+    my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=allyPage&allyId=%d", $self->{server}, $allyId));
+    my $extractor = new Ikariam::Extractor(content => $res->content);
+
+    my %ally;
+    $ally{'id'} = $allyId;
+    $ally{'name'} = $extractor->find("//table[\@id='allyinfo']/tr[1]/td[2]/text()");
+    $ally{'members'} = $extractor->find("//table[\@id='allyinfo']/tr[2]/td[2]/text()");
+    $ally{'url'} = $extractor->find("//table[\@id='allyinfo']/tr[5]/td[2]/text()");
+    delete($ally{'url'}) if($ally{'url'} eq '-');
+
+    return \%ally;
+}
+
 sub viewScore
 {
     my $self = shift;
@@ -171,7 +189,7 @@
 
             $user{'ally'} = $extractor->find('//td[@class="allytag"]/a/text()');
             my $allyHref = $extractor->find('//td[@class="allytag"]/a/@href');
-            if($allyHref =~  /\?view=allyPage&allyId=(\d+)/) {
+            if($allyHref =~ /\?view=allyPage&oldView=highscore&allyId=(\d+)/) {
                 $user{'allyId'} = $1;
             }
             $user{$type} = $extractor->find('//td[@class="score"]/text()');
--- a/ikariam.sql	Wed Oct 29 01:14:49 2008 +0800
+++ b/ikariam.sql	Wed Oct 29 02:43:04 2008 +0800
@@ -1,3 +1,4 @@
+CREATE TABLE "ally" ("id" INTEGER PRIMARY KEY  NOT NULL , "name" TEXT, "members" INTEGER, "url" TEXT, "time" DATETIME);
 CREATE TABLE cities (
  cityId INTEGER PRIMARY KEY UNIQUE,
  citylevel INTEGER,
--- a/scan.pl	Wed Oct 29 01:14:49 2008 +0800
+++ b/scan.pl	Wed Oct 29 02:43:04 2008 +0800
@@ -13,62 +13,65 @@
         return;
     }
 
-    foreach my $city (@cities)
+    foreach my $h_city (@cities)
     {
-        $city->{island} = $island;
-        $city->{'time'} = time;
-        if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
-        {
-            foreach my $i (keys(%$city)) {
-                eval($c->$i($city->{$i}));
-                # printf("%s %s ", $i, $city->{$i});
+        $h_city->{island} = $island;
+        $h_city->{'time'} = time;
+        my $c = Ikariam::Cities->retrieve($h_city->{cityId});
+        if(defined($c)) {
+            foreach (keys(%$h_city)) { 
+                $c->set($_ => $h_city->{$_}); 
             }
-            # print ("\n");
-
-            $c->autoupdate(1);
-            $c->update();
         } else {
-            Ikariam::Cities->insert($city);
+            $c = Ikariam::Cities->insert($h_city);
         }
-
-        printf("city %s (%d) at island (%d) saved\n", 
-            $city->{cityname},
-            $city->{cityId}, 
-            $island);
+        $c->update();
 
-        my $user = Ikariam::User->retrieve($city->{user});
-        if(!defined($user) || $user->time le (time - 60*60*1)) {
-            # Download user profile.
-            foreach my $x (qw/score army_score_main trader_score_secondary/)
-            {
-                my $users = $::i->viewScore($x, $city->{owner}, 0);
-                saveUser($users);
-            }
-        }
+        my $user = Ikariam::User->retrieve($h_city->{'user'});
+        # next if (defined($user) && $user->time le (time - 60*60*1));
+
+        saveUser($h_city->{owner});
     }
 }
 
 sub saveUser
 {
-    my $users = shift;
+    my $userName = shift;
+    my $users;
 
-    if(defined($users)) {
-        foreach my $h_user (values(%{$users}))
-        {
-            # print(Dumper($user));
+    foreach my $x (qw/score army_score_main trader_score_secondary/) {
+        $users = $::i->viewScore($x, $userName, 0);
+
+        foreach my $h_user (values(%{$users})) {
             printf("Saving user %s\n", $h_user->{'name'});
             $h_user->{'time'} = time;
-            my $user;
-            if($user = Ikariam::User->retrieve($h_user->{id}))
-            {
-                foreach my $i (keys(%$h_user)) {
-                    # eval($c->$i($h_user->{$i}));
-                    $user->set($i => $h_user->{$i});
-                }
+
+            my $user = Ikariam::User->retrieve($h_user->{'id'});
+            if(defined($user)) {
+                foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); }
             } else {
                 $user = Ikariam::User->insert($h_user);
             }
             $user->update();
+            saveAlly($h_user->{allyId});
+        }
+    }
+}
+
+sub saveAlly
+{
+    my $allyId = shift;
+    return unless (defined($allyId) && $allyId ne '0');
+
+    # Save for member of ally 
+    my $ally = Ikariam::Ally->retrieve($allyId);
+    if(!defined($ally) || $ally->time le (time - 60*60*1)) {
+        my $h_ally = $::i->viewAlly($allyId);
+        $h_ally->{'time'} = time;
+        if(defined($ally)) {
+            foreach (keys(%$h_ally)) { $ally->set($_ => $h_ally->{$_}); }
+        } else {
+            $ally = Ikariam::Ally->insert($h_ally);
         }
     }
 }
@@ -93,7 +96,6 @@
 } else {
     die("Usage: $0\nUsage: $0 x y\n");
 }
-
 foreach my $h_island (@islands)
 {
     printf("checking island %d\n", $h_island->{id});