comparison Ikariam.pm @ 183:2362c8c8909e

implemented military builder.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 02 Nov 2008 21:14:55 +0800
parents 0cfc7a19a4d2
children c6c6db6eea76
comparison
equal deleted inserted replaced
182:d86186ae4329 183:2362c8c8909e
140 } 140 }
141 }; 141 };
142 142
143 143
144 # if debug 144 # if debug
145 # LWP::Debug::level('+trace'); 145 LWP::Debug::level('+trace');
146 146
147 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1)); 147 $self->{mech}->cookie_jar(HTTP::Cookies->new(file => "/tmp/ikariam-cookies.txt", autosave => 1));
148 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate'); 148 $self->{mech}->default_headers->push_header('Accept-Encoding', 'deflate');
149 149
150 return bless $self, $class; 150 return bless $self, $class;
310 $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i)); 310 $info{'ally'} = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/text()', $i));
311 if($info{'ally'} eq '-') { 311 if($info{'ally'} eq '-') {
312 delete($info{'ally'}) 312 delete($info{'ally'})
313 } else { 313 } else {
314 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/@href', $i)); 314 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//li[@class="ally"]/a/@href', $i));
315 # http://s2.ikariam.tw/index.php?view=allyPage&allyId=264&oldView=island&id=569
316 if($href =~ /&allyId=(\d+)&/) { 315 if($href =~ /&allyId=(\d+)&/) {
317 $info{'allyId'} = $1; 316 $info{'allyId'} = $1;
318 } 317 }
319 } 318 }
320 319
321 # Ally Id 320 # Ally Id
322
323 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i)); 321 my $href = $extractor->find(sprintf('//li[@id="cityLocation%s"]//a[@class="messageSend"]/@href', $i));
324 # ?view=sendMessage&type=0&with=20204&destinationCityId=64165&oldView=island
325 if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) { 322 if ($href =~ /with=(\d+)&destinationCityId=(\d+)/) {
326 $info{'user'} = $1; 323 $info{'user'} = $1;
327 $info{'cityId'} = $2; 324 $info{'cityId'} = $2;
328 # ?view=sendAllyMessage&allyId=1192&oldView=island&id=721
329 } else { 325 } else {
330 # 聯盟 326 # 聯盟 this is me.
331 # this is me.
332 my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i)); 327 my $id = $extractor->find(sprintf('//li[@id="cityLocation%s"]/a/@id', $i));
333 if($id =~ /city_(\d+)/) { 328 if($id =~ /city_(\d+)/) {
334 $info{'user'} = undef; # FIXME 329 $info{'user'} = undef; # FIXME
335 $info{'cityId'} = $1; 330 $info{'cityId'} = $1;
336 } 331 }
370 $self->{server}, $cityId, $_)); 365 $self->{server}, $cityId, $_));
371 } 366 }
372 } 367 }
373 } 368 }
374 369
370
375 # for tavern only 371 # for tavern only
376 sub set { 372 sub set {
377 my $self = shift; 373 my $self = shift;
378 my $type = shift; 374 my $type = shift;
379 my $cityId = shift; 375 my $cityId = shift;
389 if($position != -1) { 385 if($position != -1) {
390 $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position)); 386 $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
391 $self->{mech}->submit_form( 387 $self->{mech}->submit_form(
392 form_number => 1, 388 form_number => 1,
393 fields => { 389 fields => {
394 amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue}, 390 amount => $self->{'cities'}->{$cityId}->{$type}->{maxValue},
395 } 391 }
396 ); 392 );
397 393
394 }
395 }
396
397 sub is_shipyard_upgrading {
398 my $self = shift;
399 my $cityId = shift;
400 my $type = "shipyard";
401
402 my $position = -1;
403 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
404 foreach (1..2) {
405 $position = $_ if($locations[$_] eq $type);
406 }
407
408 if($position != -1) {
409 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
410 if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
411 return 1;
412 } else {
413 return 0;
414 }
415 }
416 return 0;
417 }
418
419 sub is_navy_trainning {
420 my $self = shift;
421 my $cityId = shift;
422 my $type = "shipyard";
423
424 my $position = -1;
425 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
426 foreach (1..2) {
427 $position = $_ if($locations[$_] eq $type);
428 }
429
430 if($position != -1) {
431 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
432 if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
433 return 1;
434 } else {
435 return 0;
436 }
437 }
438 # FIXME we can not found the shipyard
439 return 0;
440 }
441
442 sub buildShips {
443 my $self = shift;
444 my $shipType = shift;
445 my $cityId = shift;
446 my $type = 'shipyard';
447
448 warn("buildShips $shipType");
449 my $position = -1;
450 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
451 foreach (1..2) {
452 $position = $_ if($locations[$_] eq $type);
453 }
454
455 if($position != -1) {
456 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
457 $self->{mech}->submit_form(
458 form_number => 1,
459 fields => {
460 $shipType => 1,
461 }
462 );
463 }
464 }
465
466 sub is_army_trainning {
467 my $self = shift;
468 my $cityId = shift;
469 my $type = "barracks";
470
471 my $position = -1;
472 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
473 foreach (2..$#locations) {
474 $position = $_ if($locations[$_] eq $type);
475 }
476
477 if($position != -1) {
478 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
479 if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@id="unitConstructionList"]//h4'))) {
480 return 1;
481 } else {
482 return 0;
483 }
484 }
485 # FIXME we can not found the shipyard
486 return 0;
487 }
488
489 sub is_barracks_upgrading {
490 my $self = shift;
491 my $cityId = shift;
492 my $type = 'barracks';
493
494 my $position = -1;
495 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
496 foreach (2..$#locations) {
497 $position = $_ if($locations[$_] eq $type);
498 }
499
500 if($position != -1) {
501 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
502 if(defined(Ikariam::Extractor->new(content => $res->content)->find('//div[@class="isUpgrading"]'))) {
503 return 1;
504 } else {
505 return 0;
506 }
507 }
508 return 0;
509 }
510
511 sub buildUnits {
512 my $self = shift;
513 my $unitType = shift;
514 my $cityId = shift;
515 my $type = 'barracks';
516
517 warn("buildShips $unitType");
518 my $position = -1;
519 my @locations = @{$self->{'cities'}->{$cityId}->{locations}};
520 foreach (2..$#locations) {
521 $position = $_ if($locations[$_] eq $type);
522 }
523
524 if($position != -1) {
525 my $res = $self->{mech}->get (sprintf("http://%s/index.php?view=%s&id=%s&position=%d", $self->{server}, $type, $cityId, $position));
526 $self->{mech}->submit_form(
527 form_number => 1,
528 fields => {
529 $unitType => 1,
530 }
531 );
398 } 532 }
399 } 533 }
400 534
401 sub build { 535 sub build {
402 my $self = shift; 536 my $self = shift;
431 565
432 sub run { 566 sub run {
433 my $self = shift; 567 my $self = shift;
434 my $param = shift; 568 my $param = shift;
435 # defense. 569 # defense.
436 die("run $param not implemented yet."); 570 warn ("run $param not implemented yet.");
437 } 571 }
438 572
439 sub research 573 sub research
440 { 574 {
441 my $self = shift; 575 my $self = shift;
472 my $treaty = Ikariam::Extractor->new(content => $res->content)->find('//div[@class="warning"]'); 606 my $treaty = Ikariam::Extractor->new(content => $res->content)->find('//div[@class="warning"]');
473 if(!defined($treaty)) { 607 if(!defined($treaty)) {
474 $self->{mech}->submit_form( 608 $self->{mech}->submit_form(
475 form_number => 1, 609 form_number => 1,
476 fields => { 610 fields => {
477 cargo_army_302 => '2', 611 cargo_army_302 => '2',
478 } 612 }
479 ); 613 );
480 } else { 614 } else {
481 warn ($treaty); 615 warn ($treaty);
482 } 616 }
483 } 617 }
605 } else { 739 } else {
606 $self->{'military'}->{attack}++; 740 $self->{'military'}->{attack}++;
607 } 741 }
608 } 742 }
609 } 743 }
610 # print(Dumper($self->{'military'}));
611 } 744 }
612 745
613 sub checkMilitaryAdvisorReportView { 746 sub checkMilitaryAdvisorReportView {
614 my $self = shift; 747 my $self = shift;
615 my $combatId = shift; 748 my $combatId = shift;
1000 if($#cities<0) { 1133 if($#cities<0) {
1001 die ("login failed\n"); 1134 die ("login failed\n");
1002 } 1135 }
1003 foreach(@cities) { 1136 foreach(@cities) {
1004 $self->{'cities'}->{$_} = {}; 1137 $self->{'cities'}->{$_} = {};
1138 $self->{'cities'}->{$_}->{id} = $_;
1005 } 1139 }
1006 } 1140 }
1007 1141
1008 1142
1009 1; 1143 1;