view enemy.pl @ 375:0ec57ac68a4c

one more parameter for ship speed.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Tue, 14 Apr 2009 17:01:49 +0800
parents 2cfa03edc22e
children 6ca0677a361e
line wrap: on
line source

#!/usr/bin/perl
use strict;
use Ikariam;
use Data::Dumper;
use YAML qw/Dump LoadFile/;
use POSIX qw(strftime);

package main;
my @tradegoodText = qw/NULL 葡萄酒 大理石 水晶 硫磺/;
my @wonderText = qw/NULL 赫菲斯拓斯的熔爐 蓋亞的神殿 狄奧尼索斯的花園 雅典娜的神殿 赫秘士的神殿 阿瑞斯的要塞 波賽頓的神殿 克羅瑟斯的神殿/;

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 travelMatrix {
    my $userName = shift;
    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;
            if(defined($island)) {
                $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");
}

sub myTownToVicim {
    my $islandId = shift;

    if(my $island = Ikariam::Island->retrieve($islandId)) {
        my @cities = ();
        foreach (glob("city-*-dump.yaml")) {
            my $city = LoadFile($_);
            $city->{distance} = travelTime($island->x, $island->y, $city->{island}->{x}, $city->{island}->{y}, 60);
            push (@cities, $city);
        }
        @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);

        my $n = $#cities;
        for my $x (0..$n) {
            printf("%s (%d)\t- ", $cities[$x]->{name}, $cities[$x]->{id});
            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),
                  );
        }
    }
}

package main;

if($#ARGV != 0) {
    die("Usage: $0 nickname\n");
}

my ($u) = Ikariam::User->search('name' => $ARGV[0]);
if(defined($u)) {
    foreach my $i (qw/name score trader_score_secondary army_score_main ally/) {
        printf("%s: %s\n", $i, $u->get($i));
    }
    printf("time: %s\n",  strftime ("%Y-%m-%d %H:%M", localtime($u->get('time'))));

    travelMatrix($ARGV[0]);

    printf("\nCities\n");
    foreach my $city ($u->cities) {
        my $island = Ikariam::Island->retrieve($city->island);
        next if(!defined($island));

        printf("Name: %s [%s:%s]\n", $city->cityname,  $island->x, $island->y);
        if(defined($city->status)) {
            printf("Status: <<<%s>>>\n", $city->status);
        }
        printf("Location: [%s:%s] http://%s/index.php?view=island&id=%s&selectCity=%d\n", 
                $island->x, $island->y, $::server, $island->id, $city->cityId);
        if(-f sprintf("spy-%d-dump.yaml", $city->cityId)) {
            my $spydata = LoadFile(sprintf("spy-%d-dump.yaml", $city->cityId));
            if($spydata->{city}->{buildings}->{townHall} ne $city->citylevel) {
                printf("Hall: %s", $city->citylevel);
                printf("\n*OUTDATED* ");
            }
            printf("Hall: %s, Wall: %s, Port: %s, Safehouse: %s, Risk: %s\n", 
                    $spydata->{city}->{buildings}->{townHall}, 
                    $spydata->{city}->{buildings}->{wall}, 
                    $spydata->{city}->{buildings}->{port}, 
                    $spydata->{city}->{buildings}->{safehouse}, 
                    $spydata->{risks}[4]);
        } else {
            printf("Level: %s, Rsik %d\n", $city->citylevel, $city->risk);
        }

        printf("Tradegood: %s, Wonder: %s\n", 
                $tradegoodText[$island->tradegood], $wonderText[$island->wonder]);
        # printf("Island: %s\n", $island->name);

        myTownToVicim($island->id);
        printf("\n");
    }
    print "-"x80 . "\n";
}