view attacker_travel.pl @ 364:7274d8f41d5f

refined the timing for sending ships
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sun, 01 Mar 2009 11:10:10 +0800
parents bf59bef30dfc
children
line wrap: on
line source

#!/usr/bin/perl
use strict;

use Data::Dumper;
use Ikariam;
use YAML qw/LoadFile/;
# return minutes.
sub travelTime {
    my ($x1, $y1, $x2, $y2, $speed) = @_;
    $speed = 60 unless(defined($speed));
    if($x1 == $x2 && $y1 == $y2) {
        return 600/$speed;
    } else {
        return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))) * (1200/$speed));
    }
}

Ikariam::User->has_many(cities => 'Ikariam::Cities');
Ikariam::Cities->has_a(island => 'Ikariam::Island');
Ikariam::Cities->set_sql(ally => qq {
        SELECT cities.cityid
          FROM cities
         WHERE cities.ally = ?
    });


sub allyTownToVicim {
    my $islandId = shift;

    my $mime = Ikariam::User->retrieve(name => $::user);
    my $ally = $mime->ally;
    my $iterator = Ikariam::Cities->search_ally($mime->ally);
    if(my $island = Ikariam::Island->retrieve($islandId)) {

        my @cities = ();
        while(my $target = $iterator->next) {
            my $city;
            $city->{id} = $target->id;
            $city->{name} = $target->cityname;
            $city->{owner} = $target->owner;
            $city->{island}->{id} = $target->island->id;
            $city->{island}->{x} = $target->island->x;
            $city->{island}->{y} = $target->island->y;
            $city->{distance} = 
                travelTime($target->island->x, $target->island->y, $island->x, $island->y) ;

            push (@cities, $city);
        }

        @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);

        for my $x (0..$#cities) {
            printf("%s (%s)\t- ", $cities[$x]->{name}, $cities[$x]->{owner});
            printf("Land %3d Ship %3d Catapult %3d Ram %3d\n", 
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 60),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 33),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 26),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 40),
                  );
# last if($x > 5);
        }
    }
}

sub myTownToVicim {
    my $islandId = shift;

    my @cities = ();
    foreach (glob("city-*-dump.yaml")) {
        my $city = LoadFile($_);
        push (@cities, $city);
    }
    my $n = $#cities;


    if(my $island = Ikariam::Island->retrieve($islandId)) {

        for my $x (0..$n) {
            printf("%s\t- ", $cities[$x]->{name});
            printf("Land %3d Ship %3d Catapult %3d Ram %3d\n", 
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 6),
                    travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 10),
                  );
        }
    }
}

sub cityToIsland {
    my (@city, $island);
}

package main;

my $userName = $ARGV[0];
my @cities = ();
if(my $user = Ikariam::User->retrieve(name => $userName)) {
    foreach my $target ($user->cities) {
        my $island = $target->island;
        my $city;
        $city->{id} = $target->id;
        $city->{name} = $target->cityname;
        $city->{island}->{id} = $island->id;
        $city->{island}->{x} = $island->x;
        $city->{island}->{y} = $island->y;

        push (@cities, $city);
    }
}

my $n = $#cities;

# Print them out.
# header
printf(" %5s ", undef);
for my $y (0..$n) {
    printf ("%5s ", $cities[$y]->{id});
}
print "\n";

# body
for my $x (0..$n) {
    printf(" %5s ", $cities[$x]->{id});
    my $total = 0;
    for my $y (0..$n) {
        my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y});
        printf("%5s ", $t);
        $total += $t;
    }
    printf(" %s", $cities[$x]->{name});
    printf("\n");
}
printf("\n");


for my $i (0..$n) {
    printf("To: %s [%d:%d] http://%s/index.php?view=island&cityId=%s\n", $cities[$i]->{name},  $cities[$i]->{island}->{x},  $cities[$i]->{island}->{y}, $::server, $cities[$i]->{id});
# myTownToVicim($cities[$i]->{island}->{id});
# printf("\n");
    allyTownToVicim($cities[$i]->{island}->{id});
    printf("\n");
}