changeset 8:e4b3168d0319

implemented sheep and enemy scripts.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 04:23:01 +0800
parents 2040ccc95670
children ae412d1f7761
files Ikariam.pm enemy.pl ikariam.sql scan.pl scores.pl sheep.pl
diffstat 6 files changed, 93 insertions(+), 17 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Wed Oct 08 02:50:19 2008 +0800
+++ b/Ikariam.pm	Wed Oct 08 04:23:01 2008 +0800
@@ -3,7 +3,7 @@
 use Class::DBI::AutoLoader (
     dsn       => 'dbi:SQLite:dbname=ikariam.sqlite',
     options   => { RaiseError => 1 },
-    tables    => ['cities', 'island', 'users'],
+    tables    => ['cities', 'island', 'user'],
     use_base  => 'Class::DBI::SQLite',
     namespace => 'Ikariam',
 );
@@ -68,7 +68,7 @@
         $e = getElementsByAttribute($elem, "class", "action");
         $e = getElementsByTagName($e, "a");
 
-        if($e->getAttribute('href') =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/)
+        if(defined ($e) && $e->getAttribute('href') =~ /index\.php\?view=sendMessage&with=(\d+)&oldView=highscore/)
         {
             $user{'id'} = $1;
 
@@ -200,7 +200,7 @@
         @e = getElementsByAttribute($elem, "class", "messageSend");
         if ( $e[0]->getAttribute("href") =~ /with=(\d+)&destinationCityId=(\d+)/)
         {
-            $info{'ownerId'} = $1;
+            $info{'user'} = $1;
             $info{'cityId'} = $2;
         }
         push @cities, \%info;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/enemy.pl	Wed Oct 08 04:23:01 2008 +0800
@@ -0,0 +1,19 @@
+#!/usr/bin/perl
+use strict;
+use Ikariam;
+use Data::Dumper;
+
+package main;
+
+if($#ARGV != 0) {
+    die("Usage: $0 nickname\n");
+}
+
+Ikariam::User->has_many(cities => 'Ikariam::Cities');
+my ($u) = Ikariam::User->search('name' => $ARGV[0]);
+if(defined($u)) {
+    print Dumper($u->_data_hash);
+    foreach my $c ($u->cities) {
+        print Dumper($c->_data_hash);
+    }
+}
--- a/ikariam.sql	Wed Oct 08 02:50:19 2008 +0800
+++ b/ikariam.sql	Wed Oct 08 04:23:01 2008 +0800
@@ -1,11 +1,12 @@
 CREATE TABLE cities (
- cityId BIGINT PRIMARY KEY UNIQUE,
+ cityId INTEGER PRIMARY KEY UNIQUE,
  citylevel INTEGER,
  cityname TEXT,
- ownerId BIGINT,
+ user INTEGER,
  owner TEXT,
- ally TEXT, "island" INTEGER);
-CREATE TABLE islands (
+ ally TEXT,
+ island INTEGER);
+CREATE TABLE "island" (
  id BIGINT PRIMARY KEY UNIQUE,
  x INTEGER,
  y INTEGER,
@@ -13,8 +14,7 @@
  tradegood INTEGER,
  wonder INTEGER,
  people INTEGER);
-
-CREATE TABLE users (
+CREATE TABLE "user" (
   id INTEGER PRIMARY KEY UNIQUE,
   name VARCHAR,
   ally VARCHAR,
@@ -26,4 +26,3 @@
   research_score_secondary INTEGER,
   army_score_main INTEGER,
   trader_score_secondary INTEGER);
-
--- a/scan.pl	Wed Oct 08 02:50:19 2008 +0800
+++ b/scan.pl	Wed Oct 08 04:23:01 2008 +0800
@@ -7,13 +7,15 @@
 my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
 $i->login;
 
-my @islands;
-if($#ARGV >= 1) {
+if($#ARGV == 1) {
     @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
+} elsif($#ARGV == -1) {
+    @islands = $i->viewHomeMap();
 } else {
-    @islands = $i->viewHomeMap();
+    die("Usage: $0\nUsage: $0 x y\n");
 }
 
+my @islands;
 foreach my $island (@islands)
 {
     printf("checking island %d\n", $island->{id});
--- a/scores.pl	Wed Oct 08 02:50:19 2008 +0800
+++ b/scores.pl	Wed Oct 08 04:23:01 2008 +0800
@@ -8,10 +8,14 @@
 sub saveUser
 {
     my $users = shift;
+
+    my $u;
     foreach my $user (values(%{$users}))
     {
+        next if($u == $user->{'id'}); $u = $user->{'id'};
+        
         printf("Saving %s\n", $user->{'name'});
-        if(my $c = Ikariam::Users->retrieve($user->{id}))
+        if(my $c = Ikariam::User->retrieve($user->{id}))
         {
             foreach my $i (keys(%$user)) {
                 eval($c->$i($user->{$i}));
@@ -19,15 +23,16 @@
             $c->autoupdate(1);
             $c->update();
         } else {
-            Ikariam::Users->insert($user);
+            Ikariam::User->insert($user);
         }
     }
 }
 
 my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
-$i->login;
 
 if($#ARGV == 0) {
+    $i->login;
+
     my $users;
     foreach my $x (qw/score army_score_main trader_score_secondary/)
     {
@@ -35,6 +40,8 @@
         saveUser($users);
     }
 } elsif ($#ARGV == 1) {
+    $i->login;
+
     # search the islands and cities nearby
     my ($x, $y) = @ARGV;
     my $offset = 5;
@@ -69,7 +76,7 @@
 } else {
     # my $users = $i->viewScore('army_score_main');
     # saveUser($users);
-    die("Usage: $0 nickname\n");
+    die("Usage: $0 nickname\nUsage: $0 x y");
 }
 
 # highscoreType	
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/sheep.pl	Wed Oct 08 04:23:01 2008 +0800
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use strict;
+use Ikariam;
+use Data::Dumper;
+
+package main;
+
+Ikariam::User->has_many(cities => 'Ikariam::Cities');
+Ikariam::User->set_sql(sheeps => qq {
+    SELECT user.id 
+      FROM user, cities, island
+      WHERE user.id == cities.user
+        AND cities.island == island.id
+        AND island.x <= ?
+        AND island.x >= ?
+        AND island.y <= ?
+        AND island.y >= ?
+        AND user.score >= 500 
+        AND user.army_score_main <= 20
+    }
+);
+
+if($#ARGV != 1) {
+    die("Usage: $0 x y\n");
+}
+my ($x, $y) = @ARGV;
+
+my @sheeps = Ikariam::User->search_sheeps(($x + 6), ($x - 6), ($y + 6), ($y - 6));
+my $s;
+foreach my $sheep (sort (@sheeps)) {
+    # avoid duplicate
+    next if($sheep->id == $s); $s = $sheep->id;
+
+    # 查聯盟數量
+    next if(Ikariam::User->search(allyId => $sheep->allyId)->count() > 3);
+
+    printf("%s (%s),%d,%d\n", $sheep->name, $sheep->ally, $sheep->score, $sheep->army_score_main);
+    # print Dumper($sheep->_data_hash);
+    foreach my $c ($sheep->cities) {
+        # print Dumper($c->_data_hash);
+        my $island = Ikariam::Island->retrieve($c->island);
+        printf("%s,%d,(%d,%d),http://s2.ikariam.tw/index.php?view=island&id=%d\n", $c->cityname, $c->citylevel, 
+            $island->x, $island->y,
+            $island->id);
+    }
+    printf("\n");
+}
+
+# find_or_create