comparison scan.pl @ 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 f590b5ea5e55
children 59f2c435760c
comparison
equal deleted inserted replaced
13:bb6eebd349fc 14:8b7bc598ba06
1 #!/usr/bin/perl 1 #!/usr/bin/perl
2 use strict; 2 use strict;
3 use Data::Dumper;
3 use Ikariam; 4 use Ikariam;
4 5
5 package main; 6 package main;
7 sub saveCities
8 {
9 my ($island, @cities) = @_;
6 10
7 my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d"); 11 foreach my $city (@cities)
8 $i->login; 12 {
13 $city->{island} = $island;
14 if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
15 {
16 foreach my $i (keys(%$city)) {
17 eval($c->$i($city->{$i}));
18 # printf("%s %s ", $i, $city->{$i});
19 }
20 # print ("\n");
21
22 $c->autoupdate(1);
23 $c->update();
24 } else {
25 Ikariam::Cities->insert($city);
26 }
27
28 printf("city %d %s saved\n", $city->{cityId}, $city->{cityname});
29
30 # Download user profile.
31 foreach my $x (qw/score army_score_main trader_score_secondary/)
32 {
33 my $users = $::i->viewScore($x, $city->{owner}, 0);
34 saveUser($users);
35 }
36 }
37 }
38
39 sub saveUser
40 {
41 my $users = shift;
42
43 foreach my $user (values(%{$users}))
44 {
45 # print(Dumper($user));
46 printf("Saving user %s\n", $user->{'name'});
47 if(my $c = Ikariam::User->retrieve($user->{id}))
48 {
49 foreach my $i (keys(%$user)) {
50 eval($c->$i($user->{$i}));
51 }
52 $c->autoupdate(1);
53 $c->update();
54 } else {
55 Ikariam::User->insert($user);
56 }
57 }
58 }
59
60 our $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
61
9 my @islands; 62 my @islands;
10 if($#ARGV == 1) { 63 if($#ARGV == 1) {
64 $i->login;
11 @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]); 65 @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
12 } elsif($#ARGV == 0) { 66 } elsif($#ARGV == 0) {
67 $i->login;
13 my $island = $ARGV[0]; 68 my $island = $ARGV[0];
14 69
15 my @cities = $i->viewIsland($island); 70 my @cities = $i->viewIsland($island);
16 saveCities($island, @cities); 71 saveCities($island, @cities);
17 $i->logout; 72 $i->logout;
18 exit; 73 return;
19 } elsif($#ARGV == -1) { 74 } elsif($#ARGV == -1) {
75 $i->login;
20 @islands = $i->viewHomeMap(); 76 @islands = $i->viewHomeMap();
21 } else { 77 } else {
22 die("Usage: $0\nUsage: $0 x y\n"); 78 die("Usage: $0\nUsage: $0 x y\n");
23 } 79 }
24 80
35 91
36 # scanning the island 92 # scanning the island
37 my @cities = $i->viewIsland($island->{id}); 93 my @cities = $i->viewIsland($island->{id});
38 saveCities($island->{id}, @cities); 94 saveCities($island->{id}, @cities);
39 } 95 }
40
41 sub saveCities
42 {
43 my ($island, @cities) = @_;
44
45 foreach my $city (@cities)
46 {
47 $city->{island} = $island;
48 if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
49 {
50 foreach my $i (keys(%$city))
51 {
52 eval($c->$i($city->{$i}));
53 # printf("%s %s ", $i, $city->{$i});
54 }
55 #print ("\n");
56
57 $c->autoupdate(1);
58 $c->update();
59 } else {
60 Ikariam::Cities->insert($city);
61 }
62 printf("city %d %s saved\n", $city->{cityId}, $city->{cityname});
63 }
64 }
65 $i->logout; 96 $i->logout;
66 # $i->getCityInfo(); 97 # $i->getCityInfo();