view victim_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/;

Ikariam::User->has_many(cities => 'Ikariam::Cities');

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));
    }
}

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, 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),
                  );
        }
    }
}

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 = Ikariam::Island->retrieve($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("Defence\n");
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&id=%s&selectCity=%s\n",
            $cities[$i]->{name},  $cities[$i]->{island}->{x},  $cities[$i]->{island}->{y}, $::server, $cities[$i]->{island}->{id}, $cities[$i]->{id});
    myTownToVicim($cities[$i]->{island}->{id});
    printf("\n");
}