comparison warfare.pl @ 101:592d3b3b7898

implemented the WAR robot
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 29 Oct 2008 12:05:36 +0800
parents
children b369fe3c1992
comparison
equal deleted inserted replaced
100:ed6160630329 101:592d3b3b7898
1 #!/usr/bin/perl
2 use strict;
3 use Ikariam;
4 use Data::Dumper;
5 use Decision::ParseTree q{ParseTree};
6 use YAML qw/LoadFile Dump DumpFile/;
7
8 package Ikariam::Warfare::Rules;
9 use strict;
10 use Data::Dumper;
11
12 Ikariam::User->has_many(cities => 'Ikariam::Cities');
13 Ikariam::User->has_a(ally => 'Ikariam::Ally');
14 Ikariam::Cities->has_a(user => 'Ikariam::User');
15 Ikariam::Cities->has_a(island => 'Ikariam::Island');
16 Ikariam::Cities->set_sql(sheeps => qq {
17 SELECT cities.cityId
18 FROM user, cities
19 WHERE user.id == cities.user
20 AND user.army_score_main == 0
21 AND cities.island IN (SELECT island.id FROM island WHERE island.x <= ? AND island.x >= ? AND island.y <= ? AND island.y >= ? )
22 ORDER BY cities.citylevel * (cities.citylevel - 1) * user.trader_score_secondary / 10000 DESC LIMIT 10
23 }
24 );
25 Ikariam::Report->set_sql(attack => qq {
26 SELECT report.id
27 FROM report
28 WHERE report.city = ?
29 AND report.date >= ?
30 }
31 );
32
33 sub new {
34 my ( $class, $i ) = @_;
35 my $self = {
36 ikariam => $i,
37 };
38 return bless $self, $class;
39 }
40
41 sub is_attacked {
42 my ($self, $city) = @_;
43 return ($self->{'ikariam'}->{'military'}->{attacks} > 0 ) ? 1 : 0;
44 }
45
46 sub is_actionPoint_enough {
47 my ($self, $city) = @_;
48 return ($city->{actionPoints} > ($city->{maxActionPoints}/2) ) ? 1 : 0;
49 }
50
51 sub is_army_available {
52 my ($self, $city) = @_;
53 return ($city->{army}->{Swordsman} > 2 ) ? 1 : 0;
54 }
55
56
57 sub locateVictim {
58 my ($self, $city, $x, $y) = @_;
59 my @cities = Ikariam::Cities->search_sheeps(($x + 6), ($x - 6), ($y + 6), ($y - 6));
60
61 foreach my $city (@cities) {
62 my $sheep = $city->user;
63 my $island = $city->island;
64
65 # Ignore 假期模式
66 next if($city->status eq 'v');
67 unless ($city->status eq 'i') {
68 unless ($sheep->allyId == '0') {
69 my $ally = $sheep->ally;
70 # Ignore 聯盟人數大於10
71 next if(defined($ally) && $ally->members > 10);
72 }
73 }
74
75 # check if we over-attacked
76 my $c = Ikariam::Report->search_attack($city->cityId, time - 24*60*60 - 7*60*60)->count();
77 # check the current plunders
78 foreach ($self->{ikariam}->{'military'}->{'homeland'}) {
79 $c++ if($_{to} eq $city->cityId);
80 }
81 next if($c >= 4);
82
83 # {
84 # if(1) {
85 my $capture = $city->citylevel * ($city->citylevel - 1) * $sheep->trader_score_secondary / 10000;
86 my $line = sprintf("%d %s [%d,%d] %s %s/%s",
87 $capture, $city->status,
88 $island->x, $island->y,
89 $city->cityname,
90 $sheep->name, $sheep->ally);
91 printf("%s\n", $line);
92 # }
93 return $city->cityId;
94 }
95 return undef;
96 }
97
98 sub engagement {
99 my ($self, $cityId, $x, $y) = @_;
100 my $victim = $self->locateVictim($cityId, $x, $y);
101 if(defined($victim)) {
102 $self->{ikariam}->changeCity($cityId);
103 $self->{ikariam}->plunderCity($victim);
104 }
105 }
106
107 1;
108
109 package main;
110
111 if($#ARGV != 1) { die("Usage: $0 x y\n"); }
112 my ($x, $y) = @ARGV;
113
114 our $i = new Ikariam($::server, $::user, $::pass);
115 our $rules = Ikariam::Warfare::Rules->new($i);
116
117 $i->login;
118 my $cities = $i->check;
119 $i->checkMilitaryAdvisorCombatReports();
120 print Dump($i->{'military'});
121
122 foreach my $cityId (keys(%$cities)) {
123 # build and upgrade for cities
124 my $tree = LoadFile('warfare.yaml');
125 $cities->{$cityId}->{parse_path} = [];
126 $cities->{$cityId}->{parse_answer} = undef;
127 if(ParseTree($tree, $rules, $cities->{$cityId}) eq 'engagement') {
128 $rules->engagement($cityId, $x, $y);
129 sleep(30);
130 $i->checkMilitaryAdvisorMilitaryMovements();
131 }
132 print Dump($cities->{$cityId}->{parse_path});
133 }
134 DumpFile("warfare-dump.yaml", $cities);
135
136 $i->logout;