Mercurial > eagle-eye
comparison Ikariam.pm @ 52:d2ac1e198ce4
implement a new agent based on Decision Tree (Decision::ParseTree)
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Mon, 20 Oct 2008 19:07:53 +0800 |
parents | 96ee35378696 |
children | 2d3c394b7940 |
comparison
equal
deleted
inserted
replaced
51:bc31729f29f4 | 52:d2ac1e198ce4 |
---|---|
239 } | 239 } |
240 | 240 |
241 return @cities; | 241 return @cities; |
242 } | 242 } |
243 | 243 |
244 sub build { | |
245 my $self = shift; | |
246 my $type = shift; | |
247 my $cityId = shift; | |
248 | |
249 die ("only wall are implmeneted") unless ($type eq "wall"); | |
250 die ("we don't know about this city") unless(defined($self->{'cities'}->{$cityId})); | |
251 | |
252 my $position = -1; | |
253 my @locations = @{$self->{'cities'}->{$cityId}->{locations}}; | |
254 foreach (0..$#locations) { | |
255 $position = $_ if($locations[$_] == $type); | |
256 } | |
257 | |
258 if($position == -1) | |
259 { | |
260 warn("we don't have the building yet."); | |
261 } else { | |
262 my $res = $self->{mech}->post(sprintf('http://%s/index.php', $self->{server}), [ | |
263 action => 'CityScreen', | |
264 'function' => 'upgradeBuilding', | |
265 id => $cityId, | |
266 position => $position, | |
267 level => $self->{'cities'}->{$cityId}->{buildings}->{$type}, | |
268 oldView => $type, | |
269 ]); | |
270 } | |
271 } | |
272 | |
273 sub run { | |
274 my $self = shift; | |
275 # defense. | |
276 die("Not implemented"); | |
277 } | |
278 | |
279 sub research | |
280 { | |
281 my $self = shift; | |
282 my $type = shift; | |
283 my $cityId = shift; | |
284 | |
285 # check if we are researching the same stuff | |
286 my $res = $self->{mech}->get(sprintf('http://%s/index.php?action=CityScreen&function=changeResearch&id=%s&researchType=%s', $self->{server}, $cityId, $type)); | |
287 | |
288 # my $content; | |
289 # gunzip \$res->content => \$content | |
290 # or die "gunzip failed: $GunzipError\n"; | |
291 # | |
292 # print ($content); | |
293 } | |
294 | |
295 sub checkResearch { | |
296 my $self = shift; | |
297 my $cityId = shift; | |
298 | |
299 my $res = $self->{mech}->get(sprintf('http://%s/index.php?view=researchOverview&id=%s', $self->{server}, $cityId)); | |
300 | |
301 my $content; | |
302 gunzip \$res->content => \$content | |
303 or die "gunzip failed: $GunzipError\n"; | |
304 my $html = HTML::TagParser->new($content); | |
305 | |
306 my @elems = $html->getElementsByAttribute('class', 'explored'); | |
307 | |
308 my $out = {}; | |
309 foreach my $elem (@elems) { | |
310 my @items = getElementsByTagName($elem, "a"); | |
311 foreach my $item (@items) { | |
312 if($item->getAttribute('href') =~ /view=researchDetail&id=\d+&position=\d+&researchId=(\d+)$/) { | |
313 @$out{$1} = $item->innerText(); | |
314 } | |
315 } | |
316 } | |
317 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 } | |
334 | |
244 sub check | 335 sub check |
245 { | 336 { |
246 my $self = shift; | 337 my $self = shift; |
247 | 338 |
248 # looking for cities | 339 # looking for cities |
284 | 375 |
285 # search locations | 376 # search locations |
286 foreach my $i (0..14) { | 377 foreach my $i (0..14) { |
287 my ($elem) = $html->getElementsByAttribute("id", "position" . $i); | 378 my ($elem) = $html->getElementsByAttribute("id", "position" . $i); |
288 my $building = $elem->getAttribute('class'); | 379 my $building = $elem->getAttribute('class'); |
289 if ($building ne "buildingGround land") { | 380 if (!($building =~ /buildingGround/)) { |
290 $self->{'cities'}->{$cityId}->{locations}[$i] = $building; | 381 $self->{'cities'}->{$cityId}->{locations}[$i] = $building; |
291 my $span = getElementsByAttribute($elem, "class", "textLabel"); | 382 my $span = getElementsByAttribute($elem, "class", "textLabel"); |
292 my (undef, undef, $level) = split(/ /, $span->innerText()); | 383 my (undef, undef, $level) = split(/ /, $span->innerText()); |
293 $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level; | 384 $self->{'cities'}->{$cityId}->{buildings}->{$building} = $level; |
294 } | 385 } |
301 $self->{'cities'}->{$cityId}->{force}->{attacks} = $1 if($content =~ /敵人攻擊: (\d+)/); | 392 $self->{'cities'}->{$cityId}->{force}->{attacks} = $1 if($content =~ /敵人攻擊: (\d+)/); |
302 $self->{'cities'}->{$cityId}->{force}->{wars} = $1 if($content =~ /我方軍隊行程: (\d+)/); | 393 $self->{'cities'}->{$cityId}->{force}->{wars} = $1 if($content =~ /我方軍隊行程: (\d+)/); |
303 # if($content =~ /更新戰鬥報告: (\d+)/); | 394 # if($content =~ /更新戰鬥報告: (\d+)/); |
304 # if($content =~ /新的戰鬥報告: (\d+)/); | 395 # if($content =~ /新的戰鬥報告: (\d+)/); |
305 | 396 |
306 # check townHall | 397 # sub checkTownHall { |
307 $res = $self->{mech}->get(sprintf('http://%s/index.php?view=townHall&id=%d', $self->{server}, $cityId)); | 398 $res = $self->{mech}->get(sprintf('http://%s/index.php?view=townHall&id=%d', $self->{server}, $cityId)); |
308 gunzip \$res->content => \$content | 399 gunzip \$res->content => \$content |
309 or die "gunzip failed: $GunzipError\n"; | 400 or die "gunzip failed: $GunzipError\n"; |
310 | 401 |
311 # check happiness | 402 # check happiness |
349 if(defined($elem)) { | 440 if(defined($elem)) { |
350 $self->{'cities'}->{$cityId}->{corruption} = $elem->innerText(); | 441 $self->{'cities'}->{$cityId}->{corruption} = $elem->innerText(); |
351 $self->{'cities'}->{$cityId}->{corruption} =~ s/%//g; | 442 $self->{'cities'}->{$cityId}->{corruption} =~ s/%//g; |
352 } | 443 } |
353 | 444 |
354 # count | 445 # countCiizens |
355 my @citizens_type = qw/citizens woodworkers specialworkers scientists/; | 446 my @citizens_type = qw/citizens woodworkers specialworkers scientists/; |
356 @elems = $html->getElementsByAttribute('class', 'count'); | 447 @elems = $html->getElementsByAttribute('class', 'count'); |
357 $self->{'cities'}->{$cityId}->{'citizens'} = {}; | 448 $self->{'cities'}->{$cityId}->{'citizens'} = {}; |
358 $self->{'cities'}->{$cityId}->{'citizens'}->{total} = 0; | 449 $self->{'cities'}->{$cityId}->{'citizens'}->{total} = 0; |
359 | 450 |
361 { | 452 { |
362 $self->{'cities'}->{$cityId}->{'citizens'}->{$citizens_type[$i]} = $elems[$i]->innerText(); | 453 $self->{'cities'}->{$cityId}->{'citizens'}->{$citizens_type[$i]} = $elems[$i]->innerText(); |
363 $self->{'cities'}->{$cityId}->{'citizens'}->{total} += $elems[$i]->innerText();; | 454 $self->{'cities'}->{$cityId}->{'citizens'}->{total} += $elems[$i]->innerText();; |
364 } | 455 } |
365 | 456 |
366 # production | 457 # } |
367 # skin/resources/icon_gold.gif | 458 |
368 # skin/resources/icon_wood.gif | 459 $self->{'cities'}->{$cityId}->{'research'} = $self->checkResearch($cityId); |
369 # skin/resources/icon_sulfur.gif (?) | 460 |
370 # skin/resources/icon_research.gif | 461 # sub checkArmies { |
371 | |
372 | |
373 # check armies | |
374 my %force_types; | 462 my %force_types; |
375 $force_types{'army'} = [ qw/undef undef Slinger Swordsman Phalanx Ram Archer Catapult Gunsman Mortar SteamGiant Gyrocopter Bombardier Doctor Cook/ ]; | 463 $force_types{'army'} = [ qw/undef undef Slinger Swordsman Phalanx Ram Archer Catapult Gunsman Mortar SteamGiant Gyrocopter Bombardier Doctor Cook/ ]; |
376 $force_types{'fleet'} = [ qw/undef undef Ram-Ship BallistaShip Flamethrower CatapultShip MortarShip PaddleWheelRam DivingBoat/ ]; | 464 $force_types{'fleet'} = [ qw/undef undef Ram-Ship BallistaShip Flamethrower CatapultShip MortarShip PaddleWheelRam DivingBoat/ ]; |
377 foreach my $x (qw/army fleet/) | 465 foreach my $x (qw/army fleet/) |
378 { | 466 { |
430 } | 518 } |
431 } | 519 } |
432 } | 520 } |
433 | 521 |
434 sub getElementsByTagName { | 522 sub getElementsByTagName { |
435 my $element = shift; | 523 my $element = shift; |
436 my $tagname = lc(shift); | 524 my $tagname = lc(shift); |
437 my ( $flat, $cur ) = @$element; | 525 my ( $flat, $cur ) = @$element; |
438 | 526 |
439 my $out = []; | 527 my $out = []; |
440 for( ; $cur <= $#$flat ; $cur++ ) { | 528 for( ; $cur <= $#$flat ; $cur++ ) { |
441 next if ( $flat->[$cur]->[001] ne $tagname ); | 529 last if ($flat->[ $cur + 1 ]->[001] eq $element->tagName() ); |
442 next if $flat->[$cur]->[000]; # close | 530 next if ($flat->[$cur]->[001] ne $tagname ); |
531 next if $flat->[$cur]->[000]; # close | |
532 | |
443 my $elem = HTML::TagParser::Element->new( $flat, $cur ); | 533 my $elem = HTML::TagParser::Element->new( $flat, $cur ); |
444 return $elem unless wantarray; | 534 return $elem unless wantarray; |
445 push( @$out, $elem ); | 535 push( @$out, $elem ); |
446 } | 536 } |
447 return unless wantarray; | 537 return unless wantarray; |