changeset 5:dbb97c4265ba

implemented the scores search tool
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 01:51:28 +0800
parents c0287e8ff168
children 2975a94aa7a2
files Ikariam.pm ikariam.sql scan.pl scores.pl
diffstat 4 files changed, 150 insertions(+), 24 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Wed Oct 08 01:51:08 2008 +0800
+++ b/Ikariam.pm	Wed Oct 08 01:51:28 2008 +0800
@@ -3,7 +3,7 @@
 use Class::DBI::AutoLoader (
     dsn       => 'dbi:SQLite:dbname=ikariam.sqlite',
     options   => { RaiseError => 1 },
-    tables    => ['cities', 'islands'],
+    tables    => ['cities', 'islands', 'users'],
     use_base  => 'Class::DBI::SQLite',
     namespace => 'Ikariam',
 );
@@ -36,6 +36,70 @@
     return bless $self, $class;
 }
 
+sub viewScore
+{
+    my $self = shift;
+    my $type = shift || 'score';
+    my $user = shift || '';
+    my $offset = shift || 0;
+
+#    my $res = $self->{mech}->post(sprintf("http://%s/index.php?view=worldmap_iso", $self->{server}), [
+#        highscoreType => $type,
+#        offset => $offset,
+#        searchUser => $user,
+#        view => 'highscore'
+#        ]);
+#
+#    my $c;
+#    my $status = gunzip \$res->content => \$c 
+#        or die "gunzip failed: $GunzipError\n";
+
+    my $c;
+    open(IN, "f");
+    while(<IN>) { $c .= $_;}
+    close(IN);
+    my $html = HTML::TagParser->new($c);
+
+    my ($table) = $html->getElementsByAttribute("class", "table01");
+    my @elems = getElementsByTagName($table, "tr");
+
+    my %users;
+    foreach my $elem (@elems) {
+        my $e;
+        my %user;
+
+        $e = getElementsByAttribute($elem, "class", "action");
+        $e = getElementsByTagName($e, "a");
+
+        if($e->getAttribute('href') =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/)
+        {
+            $user{'id'} = $1;
+
+            $e = getElementsByAttribute($elem, "class", "name");
+            $user{'name'} = $e->innerText();
+
+            $e = getElementsByAttribute($elem, "class", "allytag");
+            $user{'ally'} = $e->innerText();
+
+            $e  = getElementsByTagName($e, "a");
+            if($e->getAttribute('href') =~ /\?view=allyPage&allyId=(\d+)/)
+            {
+                $user{'allyId'} = $1;
+            }
+
+            $e = getElementsByAttribute($elem, "class", "score");
+            $user{$type} = $e->innerText();
+            $user{$type} =~ s/,//;
+
+            $users{$user{'id'}} = \%user;
+        } else {
+            next;
+        }
+    }
+
+    return \%users;
+}
+
 sub viewWorldMap
 {
     my $self = shift;
@@ -134,6 +198,8 @@
         $info{'citylevel'} = substr($e[1]->innerText(), 14);
         $info{'owner'} = substr($e[2]->innerText(), 8);
         $info{'ally'} = substr($e[3]->innerText(), 8);
+        delete($info{'ally'}) if($info{'ally'} eq '-');
+
         @e = getElementsByAttribute($elem, "class", "messageSend");
         if ( $e[0]->getAttribute("href") =~ /with=(\d+)&destinationCityId=(\d+)/)
         {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ikariam.sql	Wed Oct 08 01:51:28 2008 +0800
@@ -0,0 +1,29 @@
+CREATE TABLE cities (
+ cityId BIGINT PRIMARY KEY UNIQUE,
+ citylevel INTEGER,
+ cityname TEXT,
+ ownerId BIGINT,
+ owner TEXT,
+ ally TEXT, "island" INTEGER);
+CREATE TABLE islands (
+ id BIGINT PRIMARY KEY UNIQUE,
+ x INTEGER,
+ y INTEGER,
+ name TEXT,
+ tradegood INTEGER,
+ wonder INTEGER,
+ people INTEGER);
+
+CREATE TABLE users (
+  id INTEGER PRIMARY KEY UNIQUE,
+  name VARCHAR,
+  ally VARCHAR,
+  allyId INTEGER,
+  score INTEGER,
+  building_score_main INTEGER,
+  building_score_secondary INTEGER,
+  research_score_main INTEGER,
+  research_score_secondary INTEGER,
+  army_score_main INTEGER,
+  trader_score_secondary INTEGER);
+
--- a/scan.pl	Wed Oct 08 01:51:08 2008 +0800
+++ b/scan.pl	Wed Oct 08 01:51:28 2008 +0800
@@ -8,12 +8,11 @@
 $i->login;
 
 my @islands;
-if($#ARGV >= 2) {
+if($#ARGV >= 1) {
     @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
 } else {
     @islands = $i->viewHomeMap();
 }
-# my @islands = $i->viewWorldMap(50, 34);
 
 foreach my $island (@islands)
 {
@@ -33,11 +32,11 @@
         $city->{island} = $island->{id};
         if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
         {
-            #foreach my $i (keys(%$city))
-            #{
-            #    eval($c->$i($city->{$i}));
+            foreach my $i (keys(%$city))
+            {
+                eval($c->$i($city->{$i}));
             #    printf("%s %s ", $i, $city->{$i});
-            #}
+            }
             #print ("\n");
 
             $c->autoupdate(1);
@@ -49,21 +48,4 @@
     }
 }
 
-
-# TODO
-# $i->worldmap($x, $y);
-# http://s2.ikariam.tw/index.php?view=worldmap_iso
-# xajax getMapData
-# xajaxargs[]   54
-# xajaxargs[]   30
-# xajaxr        1223302744553
-# $i->getAcount() account database.
-
-# 經濟, 軍事
-# http://s2.ikariam.tw/index.php
-# view	highscore
-# highscoreType	score
-# offset	-1
-# searchUser	chihchun
-
 # $i->getCityInfo();
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/scores.pl	Wed Oct 08 01:51:28 2008 +0800
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use strict;
+use Ikariam;
+use Data::Dumper;
+
+package main;
+
+sub saveUser
+{
+    my $users = shift;
+    foreach my $user (values(%{$users}))
+    {
+        if(my $c = Ikariam::Users->retrieve($user->{id}))
+        {
+            foreach my $i (keys(%$user)) {
+                eval($c->$i($user->{$i}));
+            }
+            $c->autoupdate(1);
+            $c->update();
+        } else {
+            Ikariam::Users->insert($user);
+        }
+    }
+}
+
+my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
+# $i->login;
+if($#ARGV == 0) {
+    my $users;
+    foreach my $x (qw/score army_score_main trader_score_secondary/)
+    {
+        $users = $i->viewScore($x, $ARGV[0], 0);
+        saveUser($users);
+    }
+} else {
+    my $users = $i->viewScore('army_score_main');
+    saveUser($users);
+    # die("Usage: $0 nickname\n");
+}
+
+# highscoreType	
+# <option value="score">`n</option>
+# <option value="building_score_main">ؿvjv</option>
+# <option value="building_score_secondary">ؿv</option>
+# <option value="research_score_main">ǥH</option>
+# <option value="research_score_secondary">s</option>
+# <option value="army_score_main" selected="selected">ԪNx</option>
+# <option value="trader_score_secondary">sq</option>
+# my $users = $i->viewScore('score', 'chihchun', '0');