comparison scan.pl @ 250:a654d43731f0

implement multi-thread Ikariam::Scanner
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 17 Nov 2008 00:26:44 +0800
parents 38e1a82627a9
children d75f26fc1c69
comparison
equal deleted inserted replaced
249:c30080146c05 250:a654d43731f0
1 #!/usr/bin/perl 1 #!/usr/bin/perl
2 use strict; 2 use strict;
3 use Data::Dumper; 3 use Data::Dumper;
4 use Carp; 4 use Carp;
5 use Ikariam; 5 use Ikariam;
6 use Parallel::ForkManager;
6 7
7 package main; 8 package main;
8 9
10 package Ikariam::Scanner;
11 use strict;
12 use Carp;
13 use Ikariam;
14 use Data::Dumper;
15 use Parallel::ForkManager;
16
17 sub new {
18 my($class, $i) = @_;
19 warn("new scanner");
20 my $self = {
21 ikariam => $i,
22 };
23
24 return bless $self, $class;
25 }
26
9 sub saveCities 27 sub saveCities
10 { 28 {
11 my ($island, @cities) = @_; 29 my ($self, $island, @cities) = @_;
12 30
13 if ($#cities == -1) { 31 if ($#cities == -1) {
14 warn ("empty cities list.\n"); 32 warn ("empty cities list.\n");
15 return; 33 return;
16 } 34 }
17 35
18 foreach my $h_city (@cities) { 36 foreach my $h_city (@cities) {
19 # Carp::carp(sprintf("checking %s\n", $h_city->{'cityname'})); 37 # Carp::carp(sprintf("checking %s\n", $h_city->{'cityname'}));
38 print(STDERR sprintf("checking cityname %s\n", $h_city->{'cityname'}));
20 $h_city->{island} = $island; 39 $h_city->{island} = $island;
21 $h_city->{'time'} = time; 40 $h_city->{'time'} = time;
22 my $c = Ikariam::Cities->retrieve($h_city->{cityId}); 41 my $c = Ikariam::Cities->retrieve($h_city->{cityId});
23 if(defined($c)) { 42 if(defined($c)) {
24 foreach (keys(%$h_city)) { 43 foreach (keys(%$h_city)) {
30 $c->update(); 49 $c->update();
31 50
32 my $user = Ikariam::User->retrieve($h_city->{'user'}); 51 my $user = Ikariam::User->retrieve($h_city->{'user'});
33 next if (defined($user) && defined($user->time) && $user->time gt (time - 30*60)); 52 next if (defined($user) && defined($user->time) && $user->time gt (time - 30*60));
34 53
35 saveUser($h_city->{owner}); 54 $self->saveUser($h_city->{owner});
36 } 55 }
37 } 56 }
38 57
39 sub saveUser 58 sub saveUser
40 { 59 {
60 my $self = shift;
41 my $userName = shift; 61 my $userName = shift;
42 my $users; 62 my $users;
43 63
44 foreach my $x (qw/score army_score_main trader_score_secondary/) { 64 foreach my $x (qw/score army_score_main trader_score_secondary/) {
45 $users = $::i->viewScore($x, $userName, 0); 65 my $i = $self->{ikariam}->clone();
66 $users = $i->viewScore($x, $userName, 0);
46 67
47 foreach my $h_user (values(%{$users})) { 68 foreach my $h_user (values(%{$users})) {
48 # Carp::carp(sprintf("Saving user %s\n", $h_user->{'name'})); 69 # Carp::carp(sprintf("Saving user %s\n", $h_user->{'name'}));
49 $h_user->{'time'} = time; 70 $h_user->{'time'} = time;
50
51 my $user = Ikariam::User->retrieve($h_user->{'id'}); 71 my $user = Ikariam::User->retrieve($h_user->{'id'});
52 if(defined($user)) { 72 if(defined($user)) {
73 # check if the arm score is down
53 foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); } 74 foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); }
54 } else { 75 } else {
55 $user = Ikariam::User->insert($h_user); 76 $user = Ikariam::User->insert($h_user);
56 } 77 }
57 $user->update(); 78 $user->update();
58 saveAlly($h_user->{allyId}); 79 $self->saveAlly($h_user->{allyId});
59 } 80 }
60 } 81 }
61 } 82 }
62 83
63 sub saveAlly 84 sub saveAlly
64 { 85 {
86 my $self = shift;
65 my $allyId = shift; 87 my $allyId = shift;
66 return unless (defined($allyId) && $allyId ne '0'); 88 return unless (defined($allyId) && $allyId ne '0');
67 89
68 # Save for member of ally 90 # Save for member of ally
69 my $ally = Ikariam::Ally->retrieve($allyId); 91 my $ally = Ikariam::Ally->retrieve($allyId);
70 if(!defined($ally) || $ally->time le (time - 30*60*1)) { 92 if(!defined($ally) || $ally->time le (time - 30*60*1)) {
71 my $h_ally = $::i->viewAlly($allyId); 93 my $i = $self->{ikariam}->clone();
94 my $h_ally = $i->viewAlly($allyId);
72 $h_ally->{'time'} = time; 95 $h_ally->{'time'} = time;
73 if(defined($ally)) { 96 if(defined($ally)) {
74 foreach (keys(%$h_ally)) { $ally->set($_ => $h_ally->{$_}); } 97 foreach (keys(%$h_ally)) { $ally->set($_ => $h_ally->{$_}); }
75 } else { 98 } else {
76 $ally = Ikariam::Ally->insert($h_ally); 99 $ally = Ikariam::Ally->insert($h_ally);
77 } 100 }
78 } 101 }
79 } 102 }
80 103
81 sub saveIslands { 104 sub saveIslands {
82 my @islands = @_; 105 my ($self, @islands) = @_;
106
107 my $pm = new Parallel::ForkManager(10);
83 108
84 foreach my $h_island (@islands) 109 foreach my $h_island (@islands)
85 { 110 {
86 # printf("checking island %d\n", $h_island->{id}); 111 my $pid = $pm->start and next;
87 112
113 printf("checking island %d\n", $h_island->{id});
88 my $island; 114 my $island;
89 if($island = Ikariam::Island->retrieve($h_island->{id})) { 115 if($island = Ikariam::Island->retrieve($h_island->{id})) {
90 foreach my $i (keys(%$h_island)) { 116 foreach my $i (keys(%$h_island)) {
91 $island->set($i => $h_island->{$i}); 117 $island->set($i => $h_island->{$i});
92 } 118 }
96 122
97 # scanning the island 123 # scanning the island
98 # 10 minutes cache. 124 # 10 minutes cache.
99 if($island->time le (time - 60*10)) 125 if($island->time le (time - 60*10))
100 { 126 {
101 my @cities = $::i->viewIsland($h_island->{id}); 127 my $i = $self->{ikariam}->clone();
102 saveCities($h_island->{id}, @cities); 128 my @cities =
129 $i->viewIsland($h_island->{id});
130 $self->saveCities($h_island->{id}, @cities);
103 131
104 $island->set('time', time); 132 $island->set('time', time);
105 } 133 }
106 $island->update(); 134 $island->update();
107 135 $pm->finish;
108 } 136 }
137 $pm->wait_all_children;
109 } 138 }
110 139
140 1;
141
142 package main;
111 143
112 # local $SIG{ALRM} = sub { die "timeout\n" }; 144 # local $SIG{ALRM} = sub { die "timeout\n" };
113 # alarm 3; 145 # alarm 3;
114 our $i = new Ikariam($::server, $::user, $::pass); 146 our $i = new Ikariam($::server, $::user, $::pass);
115 147
116 if($#ARGV == 1) { 148 if($#ARGV == 1) {
117 $i->login; 149 $i->login;
118 my @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]); 150 my @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
119 saveIslands(@islands); 151 # my $s = new Ikariam::Scanner($i);
120 $i->logout; 152 # $s->saveIslands(@islands);
153 new Ikariam::Scanner($i)->saveIslands(@islands);
121 } elsif($#ARGV == 0) { 154 } elsif($#ARGV == 0) {
122 $i->login; 155 $i->login;
123 my $island = $ARGV[0]; 156 my $island = $ARGV[0];
124 my @cities = $i->viewIsland($island); 157 my @cities = $i->viewIsland($island);
125 saveCities($island, @cities); 158 new Ikariam::Scanner($i)->saveCities($island, @cities);
126 $i->logout;
127 } elsif($#ARGV == -1) { 159 } elsif($#ARGV == -1) {
128 $i->login; 160 $i->login;
129 my $cities = $i->check; 161 my $cities = $i->check;
130 # random 162 # random
131 foreach my $cityId (keys(%$cities)) { 163 foreach my $cityId (keys(%$cities)) {
132 # Carp::carp(sprintf("Checking island location [%s:%s]\n", $cities->{$cityId}->{island}->{x}, $cities->{$cityId}->{island}->{y}));
133 my @islands = $i->viewWorldMap( $cities->{$cityId}->{island}->{x}, $cities->{$cityId}->{island}->{y}); 164 my @islands = $i->viewWorldMap( $cities->{$cityId}->{island}->{x}, $cities->{$cityId}->{island}->{y});
134 saveIslands(@islands); 165 new Ikariam::Scanner($i)->saveIslands(@islands);
135 } 166 }
136 $i->logout;
137 } else { 167 } else {
138 die("Usage: $0\nUsage: $0 x y\n"); 168 die("Usage: $0\nUsage: $0 x y\n");
139 } 169 }
170 $i->logout;