comparison Ikariam.pm @ 84:2c6c219dc3ff

fixed the checkCity function, implemneted a human resources palnning function.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 26 Oct 2008 01:43:40 +0800
parents ec087515da38
children a8acf50bf7bb
comparison
equal deleted inserted replaced
83:ec087515da38 84:2c6c219dc3ff
390 } 390 }
391 } 391 }
392 392
393 sub run { 393 sub run {
394 my $self = shift; 394 my $self = shift;
395 my $param = shift;
395 # defense. 396 # defense.
396 die("Not implemented"); 397 die("run $param not implemented yet.");
397 } 398 }
398 399
399 sub research 400 sub research
400 { 401 {
401 my $self = shift; 402 my $self = shift;
426 sub checkCity { 427 sub checkCity {
427 my $self = shift; 428 my $self = shift;
428 my $cityId = shift; 429 my $cityId = shift;
429 430
430 # search for goods 431 # search for goods
432 $self->{mech}->get(sprintf("http://%s/index.php", $self->{server}));
433 $self->{mech}->submit_form(
434 form_number => 2,
435 fields => {
436 cityId => $cityId,
437 }
438 );
431 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=city&id=%d", $self->{server}, $cityId)); 439 my $res = $self->{mech}->get(sprintf("http://%s/index.php?view=city&id=%d", $self->{server}, $cityId));
432 my $extractor = new Ikariam::Extractor(content => $res->content); 440 my $extractor = new Ikariam::Extractor(content => $res->content);
433 441
434 $self->{'cities'}->{$cityId}->{resources}->{gold} = $extractor->find('//span[@id="value_gold"]/text()'); 442 $self->{'cities'}->{$cityId}->{resources}->{gold} = $extractor->find('//span[@id="value_gold"]/text()');
435 $self->{'cities'}->{$cityId}->{resources}->{gold} =~ s/,//g; 443 $self->{'cities'}->{$cityId}->{resources}->{gold} =~ s/,//g;
452 $self->{'cities'}->{$cityId}->{resources}->{$good} = $extractor->find(sprintf('//span[@id="value_%s"]', $good)); 460 $self->{'cities'}->{$cityId}->{resources}->{$good} = $extractor->find(sprintf('//span[@id="value_%s"]', $good));
453 $self->{'cities'}->{$cityId}->{resources}->{$good} =~ s/,//g; 461 $self->{'cities'}->{$cityId}->{resources}->{$good} =~ s/,//g;
454 } 462 }
455 463
456 foreach my $i (0..14) { 464 foreach my $i (0..14) {
457 my @buildings = $extractor->find(sprintf('//*[@id="position%s"]/@class', $i)); 465 my @buildings = $extractor->find(sprintf('//li[@id="position%s"]/@class', $i));
458 foreach my $building (@buildings) { 466 foreach my $building (@buildings) {
459 if (!($building =~ /buildingGround/) && !($building =~ /townhall/)) { 467 if (!($building =~ /buildingGround/) && !($building =~ /townhall/)) {
460 $self->{'cities'}->{$cityId}->{locations}[$i] = $building; 468 $self->{'cities'}->{$cityId}->{locations}[$i] = $building;
461 469
462 my $span; 470 my $span;
463 my @spans = $extractor->find(sprintf('//*[@id="position%s"]//span[@class="textLabel"]/text()', $i)); 471 my @spans = $extractor->find(sprintf('//li[@id="position%s"]//span[@class="textLabel"]/text()', $i));
464 if($#spans>=1) { 472 if($#spans >= 1) {
465 $span = $spans[1]; 473 $span = $spans[1];
466 } else { 474 } else {
467 $span = $spans[0]; 475 $span = $spans[0];
468 } 476 }
469 my (undef, undef, $level) = split(/ /, $span); 477 my (undef, undef, $level) = split(/ /, $span);
672 $self->checkArmies($cityId); 680 $self->checkArmies($cityId);
673 } 681 }
674 return $self->{'cities'}; 682 return $self->{'cities'};
675 } 683 }
676 684
685 sub getNetIncome {
686 my $self = shift;
687 my $cityId = shift;
688
689 Carp::croak("must assign cityId") if !defined($cityId);
690
691 # 扣除研發每人花費 8 GOLD
692 return ($self->{cities}->{$cityId}->{citizens}->{total}*4 - $self->{cities}->{$cityId}->{citizens}->{scientists}*8 );
693 }
694
695 sub blanceHurmanResource {
696 my $self = shift;
697 my $cityId = shift;
698 my $workersRatio = {
699 'citizens' => 0.4,
700 'specialworkers' => 0.3,
701 'woodworkers' => 0.7,
702 };
703
704 my $netincome = $self->getNetIncome($cityId);
705
706 # --- HR ---
707 # 扣除研發,四成種田生產,剩下 3:7 挖資源
708 # 四成收入中可用兩成做軍事用途
709 # 生產共四成
710 my $produceworkers = int(($netincome * $workersRatio->{citizens}) / 4);
711
712 # 換成生產人力
713 my $freePeople = $self->{cities}->{$cityId}->{citizens}->{total} - ($produceworkers + $self->{cities}->{$cityId}->{scientists});
714
715 # XXX
716 Carp::carp(sprintf("suggested - produce: %s wood %s special %s\n", $produceworkers, int($freePeople*0.3), int($freePeople*0.7)));
717 }
718
677 sub logout 719 sub logout
678 { 720 {
679 my $self = shift; 721 my $self = shift;
680 $self->{mech}->get(sprintf('http://%s/index.php?action=loginAvatar&function=logout', $self->{server})); 722 $self->{mech}->get(sprintf('http://%s/index.php?action=loginAvatar&function=logout', $self->{server}));
681 } 723 }