view warfare.pl @ 114:900535236e34

user: billy3321@f3svr.f3.csu.edu.tw.f3.csu.edu.tw changed usebot.sh
author billy3321@f3svr.f3.csu.edu.tw.f3.csu.edu.tw
date Thu, 30 Oct 2008 12:08:25 +0800
parents d3bece9b06b2
children 11541129a389
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(victim => 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_victim($city->cityId, time - 24*60*60 - 7*60*60)->count();
        # check the current plunders
        foreach (@{$self->{ikariam}->{'military'}->{'homeland'}}) {
            # 出發跟回家都要計算唷
            $c++ if($_->{to} == $city->cityId || $_->{from} == $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. Attacked %d", 
                $capture, $city->status, 
                $island->x, $island->y,
                $city->cityname, $city->cityId,
                $sheep->name, $sheep->ally, $c);
            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;