changeset 14:8b7bc598ba06

we now also save user's profile when scanning islands.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 12:18:50 +0800
parents bb6eebd349fc
children e68f4cadfabd
files scan.pl
diffstat 1 files changed, 59 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/scan.pl	Wed Oct 08 12:09:54 2008 +0800
+++ b/scan.pl	Wed Oct 08 12:18:50 2008 +0800
@@ -1,22 +1,78 @@
 #!/usr/bin/perl
 use strict;
+use Data::Dumper;
 use Ikariam;
 
 package main;
+sub saveCities
+{
+    my ($island, @cities) = @_;
 
-my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
-$i->login;
+    foreach my $city (@cities)
+    {
+        $city->{island} = $island;
+        if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
+        {
+            foreach my $i (keys(%$city)) {
+                eval($c->$i($city->{$i}));
+                # printf("%s %s ", $i, $city->{$i});
+            }
+            # print ("\n");
+
+            $c->autoupdate(1);
+            $c->update();
+        } else {
+            Ikariam::Cities->insert($city);
+        }
+
+        printf("city %d %s saved\n", $city->{cityId}, $city->{cityname});
+
+        # Download user profile.
+        foreach my $x (qw/score army_score_main trader_score_secondary/)
+        {
+            my $users = $::i->viewScore($x, $city->{owner}, 0);
+            saveUser($users);
+        }
+    }
+}
+
+sub saveUser
+{
+    my $users = shift;
+
+    foreach my $user (values(%{$users}))
+    {
+        # print(Dumper($user));
+        printf("Saving user %s\n", $user->{'name'});
+        if(my $c = Ikariam::User->retrieve($user->{id}))
+        {
+            foreach my $i (keys(%$user)) {
+                eval($c->$i($user->{$i}));
+            }
+            $c->autoupdate(1);
+            $c->update();
+        } else {
+            Ikariam::User->insert($user);
+        }
+    }
+}
+
+our $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
+
 my @islands;
 if($#ARGV == 1) {
+    $i->login;
     @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
 } elsif($#ARGV == 0) {
+    $i->login;
     my $island = $ARGV[0];
 
     my @cities = $i->viewIsland($island);
     saveCities($island, @cities);
     $i->logout;
-    exit;
+    return;
 } elsif($#ARGV == -1) {
+    $i->login;
     @islands = $i->viewHomeMap();
 } else {
     die("Usage: $0\nUsage: $0 x y\n");
@@ -37,30 +93,5 @@
     my @cities = $i->viewIsland($island->{id});
     saveCities($island->{id}, @cities);
 }
-
-sub saveCities
-{
-    my ($island, @cities) = @_;
-
-    foreach my $city (@cities)
-    {
-        $city->{island} = $island;
-        if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
-        {
-            foreach my $i (keys(%$city))
-            {
-                eval($c->$i($city->{$i}));
-            #    printf("%s %s ", $i, $city->{$i});
-            }
-            #print ("\n");
-
-            $c->autoupdate(1);
-            $c->update();
-        } else {
-            Ikariam::Cities->insert($city);
-        }
-        printf("city %d %s saved\n", $city->{cityId}, $city->{cityname});
-    }
-}
 $i->logout;
 # $i->getCityInfo();