diff warfare.pl @ 126:3196b1f5dc13

merged changes.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 31 Oct 2008 00:51:49 +0800
parents 4374173bab47
children a5517f0abb85
line wrap: on
line diff
--- a/warfare.pl	Thu Oct 30 15:10:07 2008 +0800
+++ b/warfare.pl	Fri Oct 31 00:51:49 2008 +0800
@@ -10,18 +10,21 @@
 use Data::Dumper;
 
 Ikariam::User->has_many(cities => 'Ikariam::Cities');
-Ikariam::User->has_a(ally => 'Ikariam::Ally');
 Ikariam::Cities->has_a(user => 'Ikariam::User');
 Ikariam::Cities->has_a(island => 'Ikariam::Island');
 Ikariam::Cities->set_sql(sheeps => qq {
         SELECT cities.cityId
           FROM user, cities 
-         WHERE user.id == cities.user 
-           AND user.army_score_main == 0
+         WHERE user.id = cities.user 
+           AND user.army_score_main = 0
            AND cities.island IN (SELECT island.id FROM island WHERE island.x <= ? AND island.x >= ? AND island.y <= ? AND island.y >= ? )
+           AND cities.tradegood != ?
          ORDER BY cities.citylevel * (cities.citylevel - 1) * user.trader_score_secondary / 10000 DESC LIMIT 30
     }
 );
+# $island->tradegood, 
+# qw/NULL 葡萄酒 大理石 水晶 硫磺/;
+# 不打與自己相同的資源
 Ikariam::Report->set_sql(victim => qq {
         SELECT report.id
           FROM report
@@ -50,23 +53,24 @@
 
 sub is_army_available {
     my ($self, $city) = @_;
-    return ($city->{army}->{Swordsman} > 2 ) ? 1 : 0;
+    return ($city->{army}->{Swordsman} >= 2 ) ? 1 : 0;
 }
 
 sub locateVictim {
     my ($self, $city, $x, $y) = @_;
-    my @cities = Ikariam::Cities->search_sheeps(($x + 6), ($x - 6), ($y + 6), ($y - 6));
+    my @cities = Ikariam::Cities->search_sheeps(($x + 6), ($x - 6), ($y + 6), ($y - 6), $tradegood);
 
     foreach my $city (@cities) {
         my $sheep = $city->user;
         my $island = $city->island;
 
-        # Ignore 假期模式
+        # Ignore the user in vacation which we can not attack.
         next if($city->status eq 'v');
         unless ($city->status eq 'i') {
-            unless ($sheep->allyId == '0') {
-                my $ally = $sheep->ally;
-                # Ignore 聯盟人數大於10
+            unless (!defined($sheep->allyId) || $sheep->allyId == 0) {
+                my $ally = Ikariam::Ally->retrieve($sheep->allyId);
+                # Ignore the ally which have more then 10 members. 
+                # We don't want to piss off the big team.
                 next if(defined($ally) && $ally->members > 10);
             }
         }
@@ -75,19 +79,20 @@
         my $c = Ikariam::Report->search_victim($city->cityId, time - 24*60*60 - 7*60*60)->count();
         # check the current plunders
         foreach (@{$self->{ikariam}->{'military'}->{'homeland'}}) {
-            # 出發跟回家都要計算唷
+            # counting for both leaving and coming back.
             $c++ if($_->{to} == $city->cityId || $_->{from} == $city->cityId);
         }
+        # we attack one city maximum 4 times a day.
         next if($c >= 4);
 
         # {
-        # if(1) {
             my $capture = $city->citylevel * ($city->citylevel - 1) * $sheep->trader_score_secondary / 10000;
-            my $line = sprintf("%d %s [%d,%d] %s (%d) %s/%s. Attacked %d", 
+            my $line = sprintf("%d %s [%d,%d] %s (%d) %s Attacked %d", 
                 $capture, $city->status, 
                 $island->x, $island->y,
                 $city->cityname, $city->cityId,
-                $sheep->name, $sheep->ally, $c);
+                $sheep->name, $c);
+            
             printf("%s\n", $line);
         # }
         return $city->cityId;
@@ -96,8 +101,8 @@
 }
 
 sub engagement {
-    my ($self, $cityId, $x, $y) = @_;
-    my $victim = $self->locateVictim($cityId, $x, $y);
+    my ($self, $cityId, $x, $y, $tradegood) = @_;
+    my $victim = $self->locateVictim($cityId, $x, $y, $tradegood);
     if(defined($victim)) {
         $self->{ikariam}->changeCity($cityId);
         $self->{ikariam}->plunderCity($victim);
@@ -108,9 +113,6 @@
 
 package main;
 
-if($#ARGV != 1) { die("Usage: $0 x y\n"); }
-my ($x, $y) = @ARGV;
-
 our $i = new Ikariam($::server, $::user, $::pass);
 our $rules = Ikariam::Warfare::Rules->new($i);
 
@@ -118,7 +120,6 @@
 my $cities = $i->check;
 $i->checkMilitaryAdvisorCombatReports();
 
-
 foreach my $cityId (keys(%$cities)) {
     print Dump($i->{'military'});
     # build and upgrade for cities
@@ -126,7 +127,8 @@
     $cities->{$cityId}->{parse_path} = [];
     $cities->{$cityId}->{parse_answer} = undef;
     if(ParseTree($tree, $rules, $cities->{$cityId}) eq 'engagement') {
-        $rules->engagement($cityId, $x, $y);
+        my $island = Ikariam::Island->retrive($cities->{$cityId}->{island}->{id});
+        $rules->engagement($cityId, $island->{x}, $island->{y}, $island->tradegood);
         sleep(30);
         $i->checkMilitaryAdvisorMilitaryMovements();
     }