comparison 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
comparison
equal deleted inserted replaced
97:5f9bb3042d0c 98:34749e907405
11 if ($#cities == -1) { 11 if ($#cities == -1) {
12 warn ("empty cities list.\n"); 12 warn ("empty cities list.\n");
13 return; 13 return;
14 } 14 }
15 15
16 foreach my $city (@cities) 16 foreach my $h_city (@cities)
17 { 17 {
18 $city->{island} = $island; 18 $h_city->{island} = $island;
19 $city->{'time'} = time; 19 $h_city->{'time'} = time;
20 if(my $c = Ikariam::Cities->retrieve($city->{cityId})) 20 my $c = Ikariam::Cities->retrieve($h_city->{cityId});
21 { 21 if(defined($c)) {
22 foreach my $i (keys(%$city)) { 22 foreach (keys(%$h_city)) {
23 eval($c->$i($city->{$i})); 23 $c->set($_ => $h_city->{$_});
24 # printf("%s %s ", $i, $city->{$i});
25 } 24 }
26 # print ("\n"); 25 } else {
26 $c = Ikariam::Cities->insert($h_city);
27 }
28 $c->update();
27 29
28 $c->autoupdate(1); 30 my $user = Ikariam::User->retrieve($h_city->{'user'});
29 $c->update(); 31 # next if (defined($user) && $user->time le (time - 60*60*1));
30 } else {
31 Ikariam::Cities->insert($city);
32 }
33 32
34 printf("city %s (%d) at island (%d) saved\n", 33 saveUser($h_city->{owner});
35 $city->{cityname},
36 $city->{cityId},
37 $island);
38
39 my $user = Ikariam::User->retrieve($city->{user});
40 if(!defined($user) || $user->time le (time - 60*60*1)) {
41 # Download user profile.
42 foreach my $x (qw/score army_score_main trader_score_secondary/)
43 {
44 my $users = $::i->viewScore($x, $city->{owner}, 0);
45 saveUser($users);
46 }
47 }
48 } 34 }
49 } 35 }
50 36
51 sub saveUser 37 sub saveUser
52 { 38 {
53 my $users = shift; 39 my $userName = shift;
40 my $users;
54 41
55 if(defined($users)) { 42 foreach my $x (qw/score army_score_main trader_score_secondary/) {
56 foreach my $h_user (values(%{$users})) 43 $users = $::i->viewScore($x, $userName, 0);
57 { 44
58 # print(Dumper($user)); 45 foreach my $h_user (values(%{$users})) {
59 printf("Saving user %s\n", $h_user->{'name'}); 46 printf("Saving user %s\n", $h_user->{'name'});
60 $h_user->{'time'} = time; 47 $h_user->{'time'} = time;
61 my $user; 48
62 if($user = Ikariam::User->retrieve($h_user->{id})) 49 my $user = Ikariam::User->retrieve($h_user->{'id'});
63 { 50 if(defined($user)) {
64 foreach my $i (keys(%$h_user)) { 51 foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); }
65 # eval($c->$i($h_user->{$i}));
66 $user->set($i => $h_user->{$i});
67 }
68 } else { 52 } else {
69 $user = Ikariam::User->insert($h_user); 53 $user = Ikariam::User->insert($h_user);
70 } 54 }
71 $user->update(); 55 $user->update();
56 saveAlly($h_user->{allyId});
57 }
58 }
59 }
60
61 sub saveAlly
62 {
63 my $allyId = shift;
64 return unless (defined($allyId) && $allyId ne '0');
65
66 # Save for member of ally
67 my $ally = Ikariam::Ally->retrieve($allyId);
68 if(!defined($ally) || $ally->time le (time - 60*60*1)) {
69 my $h_ally = $::i->viewAlly($allyId);
70 $h_ally->{'time'} = time;
71 if(defined($ally)) {
72 foreach (keys(%$h_ally)) { $ally->set($_ => $h_ally->{$_}); }
73 } else {
74 $ally = Ikariam::Ally->insert($h_ally);
72 } 75 }
73 } 76 }
74 } 77 }
75 78
76 our $i = new Ikariam($::server, $::user, $::pass); 79 our $i = new Ikariam($::server, $::user, $::pass);
91 $i->login; 94 $i->login;
92 @islands = $i->viewHomeMap(); 95 @islands = $i->viewHomeMap();
93 } else { 96 } else {
94 die("Usage: $0\nUsage: $0 x y\n"); 97 die("Usage: $0\nUsage: $0 x y\n");
95 } 98 }
96
97 foreach my $h_island (@islands) 99 foreach my $h_island (@islands)
98 { 100 {
99 printf("checking island %d\n", $h_island->{id}); 101 printf("checking island %d\n", $h_island->{id});
100 102
101 my $island; 103 my $island;