Mercurial > eagle-eye
comparison victim_travel.pl @ 352:bf59bef30dfc
scripts for counting travel time
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Sun, 15 Feb 2009 14:19:47 +0800 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
348:b245f259776f | 352:bf59bef30dfc |
---|---|
1 #!/usr/bin/perl | |
2 use strict; | |
3 | |
4 use Data::Dumper; | |
5 use Ikariam; | |
6 use YAML qw/LoadFile/; | |
7 | |
8 Ikariam::User->has_many(cities => 'Ikariam::Cities'); | |
9 | |
10 sub travelTime { | |
11 my ($x1, $y1, $x2, $y2, $speed) = @_; | |
12 $speed = 60 unless(defined($speed)); | |
13 if($x1 == $x2 && $y1 == $y2) { | |
14 return 600/$speed; | |
15 } else { | |
16 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))) * (1200/$speed)); | |
17 } | |
18 } | |
19 | |
20 sub myTownToVicim { | |
21 my $islandId = shift; | |
22 | |
23 my @cities = (); | |
24 foreach (glob("city-*-dump.yaml")) { | |
25 my $city = LoadFile($_); | |
26 push (@cities, $city); | |
27 } | |
28 my $n = $#cities; | |
29 | |
30 if(my $island = Ikariam::Island->retrieve($islandId)) { | |
31 for my $x (0..$n) { | |
32 printf("%s\t- ", $cities[$x]->{name}); | |
33 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n", | |
34 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 60), | |
35 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 33), | |
36 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 26), | |
37 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 40), | |
38 ); | |
39 } | |
40 } | |
41 } | |
42 | |
43 sub cityToIsland { | |
44 my (@city, $island); | |
45 } | |
46 | |
47 package main; | |
48 | |
49 my $userName = $ARGV[0]; | |
50 my @cities = (); | |
51 if(my $user = Ikariam::User->retrieve(name => $userName)) { | |
52 | |
53 foreach my $target ($user->cities) { | |
54 my $island = Ikariam::Island->retrieve($target->island); | |
55 my $city; | |
56 $city->{id} = $target->id; | |
57 $city->{name} = $target->cityname; | |
58 $city->{island}->{id} = $island->id; | |
59 $city->{island}->{x} = $island->x; | |
60 $city->{island}->{y} = $island->y; | |
61 | |
62 push (@cities, $city); | |
63 } | |
64 } | |
65 | |
66 my $n = $#cities; | |
67 | |
68 # Print them out. | |
69 # header | |
70 printf("Defence\n"); | |
71 printf(" %5s ", undef); | |
72 for my $y (0..$n) { | |
73 printf ("%5s ", $cities[$y]->{id}); | |
74 } | |
75 print "\n"; | |
76 | |
77 # body | |
78 for my $x (0..$n) { | |
79 printf(" %5s ", $cities[$x]->{id}); | |
80 my $total = 0; | |
81 for my $y (0..$n) { | |
82 my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y}); | |
83 printf("%5s ", $t); | |
84 $total += $t; | |
85 } | |
86 printf(" %s", $cities[$x]->{name}); | |
87 printf("\n"); | |
88 } | |
89 printf("\n"); | |
90 | |
91 | |
92 for my $i (0..$n) { | |
93 printf("To: %s [%d:%d] http://%s/index.php?view=island&id=%s&selectCity=%s\n", | |
94 $cities[$i]->{name}, $cities[$i]->{island}->{x}, $cities[$i]->{island}->{y}, $::server, $cities[$i]->{island}->{id}, $cities[$i]->{id}); | |
95 myTownToVicim($cities[$i]->{island}->{id}); | |
96 printf("\n"); | |
97 } |