changeset 7:2040ccc95670

implemented scores collection.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 02:50:19 +0800
parents 2975a94aa7a2
children e4b3168d0319
files Ikariam.pm scan.pl scores.pl
diffstat 3 files changed, 44 insertions(+), 9 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Wed Oct 08 01:52:18 2008 +0800
+++ b/Ikariam.pm	Wed Oct 08 02:50:19 2008 +0800
@@ -3,7 +3,7 @@
 use Class::DBI::AutoLoader (
     dsn       => 'dbi:SQLite:dbname=ikariam.sqlite',
     options   => { RaiseError => 1 },
-    tables    => ['cities', 'islands', 'users'],
+    tables    => ['cities', 'island', 'users'],
     use_base  => 'Class::DBI::SQLite',
     namespace => 'Ikariam',
 );
@@ -12,7 +12,8 @@
 
 use Data::Dumper;
 use LWP;
-# use LWP::Debug qw(+ -conns);
+# use LWP::Debug qw(+ -conns -trace -debug);
+use LWP::Debug qw(+trace);
 use HTTP::Cookies;
 use WWW::Mechanize;
 use HTML::TagParser;
@@ -43,7 +44,7 @@
     my $user = shift || '';
     my $offset = shift || 0;
 
-    my $res = $self->{mech}->post(sprintf("http://%s/index.php?view=worldmap_iso", $self->{server}), [
+    my $res = $self->{mech}->post(sprintf("http://%s/index.php", $self->{server}), [
         highscoreType => $type,
         offset => $offset,
         searchUser => $user,
--- a/scan.pl	Wed Oct 08 01:52:18 2008 +0800
+++ b/scan.pl	Wed Oct 08 02:50:19 2008 +0800
@@ -17,12 +17,12 @@
 foreach my $island (@islands)
 {
     printf("checking island %d\n", $island->{id});
-    if(my $c = Ikariam::Islands->retrieve($island->{id})) {
+    if(my $c = Ikariam::Island->retrieve($island->{id})) {
         foreach my $i (keys(%$island)) {
             eval($c->$i($island->{$i}));
         }
     } else {
-        Ikariam::Islands->insert($island);
+        Ikariam::Island->insert($island);
     }
 
     # scanning the island
--- a/scores.pl	Wed Oct 08 01:52:18 2008 +0800
+++ b/scores.pl	Wed Oct 08 02:50:19 2008 +0800
@@ -10,6 +10,7 @@
     my $users = shift;
     foreach my $user (values(%{$users}))
     {
+        printf("Saving %s\n", $user->{'name'});
         if(my $c = Ikariam::Users->retrieve($user->{id}))
         {
             foreach my $i (keys(%$user)) {
@@ -24,7 +25,8 @@
 }
 
 my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
-# $i->login;
+$i->login;
+
 if($#ARGV == 0) {
     my $users;
     foreach my $x (qw/score army_score_main trader_score_secondary/)
@@ -32,10 +34,42 @@
         $users = $i->viewScore($x, $ARGV[0], 0);
         saveUser($users);
     }
+} elsif ($#ARGV == 1) {
+    # search the islands and cities nearby
+    my ($x, $y) = @ARGV;
+    my $offset = 5;
+
+    Ikariam::Island->has_many(cities => 'Ikariam::Cities');
+
+    my @islands = Ikariam::Island->retrieve_from_sql(
+        "x >= " . ($x - $offset) . " AND " . " x <= " . ($x + $offset) . 
+        " AND " .
+        "y >= " . ($y - $offset) . " AND " . " y <= " . ($y + $offset)
+    );
+
+    my %owners;
+    foreach my $island (@islands)
+    {
+        my @cities = $island->cities;
+        foreach my $city (@cities)
+        {
+            $owners{$city->owner} += 1;
+        }
+    }
+
+
+    foreach my $o (keys(%owners))
+    {
+        foreach my $x (qw/score army_score_main trader_score_secondary/)
+        {
+            my $users = $i->viewScore($x, $o, 0);
+            saveUser($users);
+        }
+    }
 } else {
-    my $users = $i->viewScore('army_score_main');
-    saveUser($users);
-    # die("Usage: $0 nickname\n");
+    # my $users = $i->viewScore('army_score_main');
+    # saveUser($users);
+    die("Usage: $0 nickname\n");
 }
 
 # highscoreType