annotate warfare.pl @ 103:6804e9c3c589

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