view warfare.pl @ 105:c86e2182d3b8

..
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 29 Oct 2008 15:11:24 +0800
parents b369fe3c1992
children 554bdd49c703
line wrap: on
line source

#!/usr/bin/perl
use strict;
use Ikariam;
use Data::Dumper;
use Decision::ParseTree q{ParseTree};
use YAML qw/LoadFile Dump DumpFile/;

package Ikariam::Warfare::Rules;
use strict;
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
           AND cities.island IN (SELECT island.id FROM island WHERE island.x <= ? AND island.x >= ? AND island.y <= ? AND island.y >= ? )
         ORDER BY cities.citylevel * (cities.citylevel - 1) * user.trader_score_secondary / 10000 DESC LIMIT 30
    }
);
Ikariam::Report->set_sql(attack => qq {
        SELECT report.id
          FROM report
         WHERE report.city = ?
           AND report.date >= ?
    }
);

sub new {
    my ( $class, $i ) = @_;
    my $self = {
        ikariam => $i,
    };
    return bless $self, $class;
}

sub is_attacked {
    my ($self, $city) = @_;
    return ($self->{'ikariam'}->{'military'}->{attacks} > 0 ) ? 1 : 0;
}

sub is_actionPoint_enough {
    my ($self, $city) = @_;
    return ($city->{actionPoints} > ($city->{maxActionPoints}/2) ) ? 1 : 0;
}

sub is_army_available {
    my ($self, $city) = @_;
    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));

    foreach my $city (@cities) {
        my $sheep = $city->user;
        my $island = $city->island;

        # Ignore 假期模式
        next if($city->status eq 'v');
        unless ($city->status eq 'i') {
            unless ($sheep->allyId == '0') {
                my $ally = $sheep->ally;
                # Ignore 聯盟人數大於10
                next if(defined($ally) && $ally->members > 10);
            }
        }

        # check if we over-attacked
        my $c = Ikariam::Report->search_attack($city->cityId, time - 24*60*60 - 7*60*60)->count();
        # check the current plunders
        foreach (@{$self->{ikariam}->{'military'}->{'homeland'}}) {
            $c++ if($_->{to} eq $city->cityId);
        }
        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", 
                $capture, $city->status, 
                $island->x, $island->y,
                $city->cityname, $city->cityId,
                $sheep->name, $sheep->ally);
            printf("%s\n", $line);
        # }
        return $city->cityId;
    }
    return undef;
}

sub engagement {
    my ($self, $cityId, $x, $y) = @_;
    my $victim = $self->locateVictim($cityId, $x, $y);
    if(defined($victim)) {
        $self->{ikariam}->changeCity($cityId);
        $self->{ikariam}->plunderCity($victim);
    }
}

1;

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);

$i->login;
my $cities = $i->check;
$i->checkMilitaryAdvisorCombatReports();

foreach my $cityId (keys(%$cities)) {
    print Dump($i->{'military'});
    # build and upgrade for cities
    my $tree  = LoadFile('warfare.yaml');
    $cities->{$cityId}->{parse_path} = [];
    $cities->{$cityId}->{parse_answer} = undef;
    if(ParseTree($tree, $rules, $cities->{$cityId}) eq 'engagement') {
        $rules->engagement($cityId, $x, $y);
        sleep(30);
        $i->checkMilitaryAdvisorMilitaryMovements();
    }
    print Dump($cities->{$cityId}->{parse_path});
}
DumpFile("warfare-dump.yaml", $cities);

$i->logout;