comparison Ikariam.pm @ 54:2d3c394b7940

rules for build new building and buy transporters
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 20 Oct 2008 23:24:15 +0800
parents d2ac1e198ce4
children 6e0d5e781949
comparison
equal deleted inserted replaced
53:336164c9a983 54:2d3c394b7940
35 timeout => 10, 35 timeout => 10,
36 ), 36 ),
37 server => $server, 37 server => $server,
38 user => $user, 38 user => $user,
39 pass => $pass, 39 pass => $pass,
40 buildingIDs => {
41 townHall => 0,
42 townhall => 0,
43 port => 3,
44 academy => 4,
45 shipyard => 5,
46 barracks => 6,
47 warehouse => 7,
48 wall => 8,
49 tavern => 9,
50 museum => 10,
51 palace => 11,
52 embassy => 12,
53 branchOffice => 13,
54 workshop => 15,
55 'workshop-army' => 15,
56 'workshop-fleet' => 15,
57 safehouse => 16,
58 palaceColony => 17,
59 resource => 1,
60 tradegood => 2
61 }
40 }; 62 };
63
41 64
42 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1)); 65 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
43 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate'); 66 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
44 67
45 return bless $self, $class; 68 return bless $self, $class;
239 } 262 }
240 263
241 return @cities; 264 return @cities;
242 } 265 }
243 266
267 sub increaseTransporter {
268 my $self = shift;
269 my $param = shift;
270 my $cityId = shift;
271
272 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
273 foreach (1..2) {
274 if($locations[$_] eq 'port') {
275 my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=increaseTransporter&id=%s&position=%s',
276 $self->{server}, $cityId, $_));
277 }
278 }
279 }
280
244 sub build { 281 sub build {
245 my $self = shift; 282 my $self = shift;
246 my $type = shift; 283 my $type = shift;
247 my $cityId = shift; 284 my $cityId = shift;
248 285
249 die ("only wall are implmeneted") unless ($type eq "wall");
250 die ("we don't know about this city") unless(defined($self->{'cities'}->{$cityId})); 286 die ("we don't know about this city") unless(defined($self->{'cities'}->{$cityId}));
251 287
252 my $position = -1; 288 my $position = -1;
253 my @locations = @{$self->{'cities'}->{$cityId}->{locations}}; 289 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
254 foreach (0..$#locations) { 290 foreach (0..$#locations) {
255 $position = $_ if($locations[$_] == $type); 291 $position = $_ if($locations[$_] eq $type);
256 } 292 }
257 293
258 if($position == -1) 294 if($position == -1)
259 { 295 {
260 warn("we don't have the building yet."); 296 foreach (0..$#locations) {
297 next if($_ <= 2 && ($self->{buildingIDs}->{$type} ne "workshop-fleet" &&
298 $self->{buildingIDs}->{$type} ne "shipyard"));
299 if($locations[$_] eq undef) {
300 my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=build&id=%s&position=%s&building=%d',
301 $self->{server}, $cityId, $_, $self->{buildingIDs}->{$type} ));
302 last;
303 }
304 }
261 } else { 305 } else {
306 $self->{mech}->add_header( Referer =>
307 sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
262 my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [ 308 my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
263 action => 'CityScreen', 309 action => 'CityScreen',
264 'function' => 'upgradeBuilding', 310 'function' => 'upgradeBuilding',
265 id => $cityId, 311 id => $cityId,
266 position => $position, 312 position => $position,
267 level => $self->{'cities'}->{$cityId}->{buildings}->{$type}, 313 level => $self->{'cities'}->{$cityId}->{buildings}->{$type},
268 oldView => $type, 314 oldView => $type,
269 ]); 315 ]);
316 # my $content;
317 # gunzip \$res->content => \$content
318 # or die "gunzip failed: $GunzipError\n";
319 # print ($content);
270 } 320 }
271 } 321 }
272 322
273 sub run { 323 sub run {
274 my $self = shift; 324 my $self = shift;
313 @$out{$1} = $item->innerText(); 363 @$out{$1} = $item->innerText();
314 } 364 }
315 } 365 }
316 } 366 }
317 return $out; 367 return $out;
318 }
319
320 sub checkCity
321 {
322 my $self = shift;
323 my $cityId = shift;
324
325 my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [
326 action => 'header',
327 cityId => $cityId,
328 function => 'changeCurrentCity',
329 id => $cityId,
330 oldView => 'city',
331 ]);
332 # XXX
333 } 368 }
334 369
335 sub check 370 sub check
336 { 371 {
337 my $self = shift; 372 my $self = shift;
382 my $span = getElementsByAttribute($elem, "class", "textLabel"); 417 my $span = getElementsByAttribute($elem, "class", "textLabel");
383 my (undef, undef, $level) = split(/ /, $span->innerText()); 418 my (undef, undef, $level) = split(/ /, $span->innerText());
384 $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level; 419 $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level;
385 } 420 }
386 } 421 }
422
423 # transporters
424 my ($elem) = $html->getElementsByAttribute("class", 'transAvail');
425 $self->{'cities'}->{$cityId}->{transporters}->{avail} = $elem->innerText();
426 my ($elem) = $html->getElementsByAttribute("class", 'transSum');
427 $self->{'cities'}->{$cityId}->{transporters}->{sum} = $elem->innerText();
428 $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/\(//;
429 $self->{'cities'}->{$cityId}->{transporters}->{sum} =~ s/\)//;
387 430
388 $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}, $cityId)); 431 $res = $self->{mech}->get(sprintf('http://%s/index.php?view=militaryAdvisorMilitaryMovements', $self->{server}, $cityId));
389 gunzip \$res->content => \$content 432 gunzip \$res->content => \$content
390 or die "gunzip failed: $GunzipError\n"; 433 or die "gunzip failed: $GunzipError\n";
391 434