comparison scan.pl @ 238:1a7b2d42fcbf

fixed the stupid timeout and cache timing
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 07 Nov 2008 11:30:11 +0800
parents 978a949602e5
children 38e1a82627a9
comparison
equal deleted inserted replaced
237:a254fb798eb9 238:1a7b2d42fcbf
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 Ikariam; 5 use Ikariam;
5 6
6 package main; 7 package main;
7 8
8 sub saveCities 9 sub saveCities
13 warn ("empty cities list.\n"); 14 warn ("empty cities list.\n");
14 return; 15 return;
15 } 16 }
16 17
17 foreach my $h_city (@cities) { 18 foreach my $h_city (@cities) {
19 Carp::carp(sprintf("checking %s\n", $h_city->{'cityname'}));
18 $h_city->{island} = $island; 20 $h_city->{island} = $island;
19 $h_city->{'time'} = time; 21 $h_city->{'time'} = time;
20 my $c = Ikariam::Cities->retrieve($h_city->{cityId}); 22 my $c = Ikariam::Cities->retrieve($h_city->{cityId});
21 if(defined($c)) { 23 if(defined($c)) {
22 foreach (keys(%$h_city)) { 24 foreach (keys(%$h_city)) {
26 $c = Ikariam::Cities->insert($h_city); 28 $c = Ikariam::Cities->insert($h_city);
27 } 29 }
28 $c->update(); 30 $c->update();
29 31
30 my $user = Ikariam::User->retrieve($h_city->{'user'}); 32 my $user = Ikariam::User->retrieve($h_city->{'user'});
31 next if (defined($user) && defined($user->time) && $user->time le (time - 30*60*1)); 33 next if (defined($user) && defined($user->time) && $user->time gt (time - 30*60));
32 34
33 saveUser($h_city->{owner}); 35 saveUser($h_city->{owner});
34 } 36 }
35 } 37 }
36 38
41 43
42 foreach my $x (qw/score army_score_main trader_score_secondary/) { 44 foreach my $x (qw/score army_score_main trader_score_secondary/) {
43 $users = $::i->viewScore($x, $userName, 0); 45 $users = $::i->viewScore($x, $userName, 0);
44 46
45 foreach my $h_user (values(%{$users})) { 47 foreach my $h_user (values(%{$users})) {
46 printf("Saving user %s\n", $h_user->{'name'}); 48 Carp::croak(sprintf("Saving user %s\n", $h_user->{'name'}));
47 $h_user->{'time'} = time; 49 $h_user->{'time'} = time;
48 50
49 my $user = Ikariam::User->retrieve($h_user->{'id'}); 51 my $user = Ikariam::User->retrieve($h_user->{'id'});
50 if(defined($user)) { 52 if(defined($user)) {
51 foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); } 53 foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); }
91 } else { 93 } else {
92 $island = Ikariam::Island->insert($h_island); 94 $island = Ikariam::Island->insert($h_island);
93 } 95 }
94 96
95 # scanning the island 97 # scanning the island
96 if($island->time le (time - 30*60*6)) 98 # 10 minutes cache.
99 if($island->time le (time - 60*10))
97 { 100 {
98 my @cities = $::i->viewIsland($h_island->{id}); 101 my @cities = $::i->viewIsland($h_island->{id});
99 saveCities($h_island->{id}, @cities); 102 saveCities($h_island->{id}, @cities);
100 103
101 $island->set('time', time); 104 $island->set('time', time);
104 107
105 } 108 }
106 } 109 }
107 110
108 111
109 local $SIG{ALRM} = sub { die "timeout\n" }; 112 # local $SIG{ALRM} = sub { die "timeout\n" };
110 alarm 3; 113 # alarm 3;
111 our $i = new Ikariam($::server, $::user, $::pass); 114 our $i = new Ikariam($::server, $::user, $::pass);
112 115
113 if($#ARGV == 1) { 116 if($#ARGV == 1) {
114 $i->login; 117 $i->login;
115 my @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]); 118 my @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);