diff scan.pl @ 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 86402d28544f
children b35c7aaef8ca
line wrap: on
line diff
--- 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});