comparison attacker_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 # return minutes.
8 sub travelTime {
9 my ($x1, $y1, $x2, $y2, $speed) = @_;
10 $speed = 60 unless(defined($speed));
11 if($x1 == $x2 && $y1 == $y2) {
12 return 600/$speed;
13 } else {
14 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))) * (1200/$speed));
15 }
16 }
17
18 Ikariam::User->has_many(cities => 'Ikariam::Cities');
19 Ikariam::Cities->has_a(island => 'Ikariam::Island');
20 Ikariam::Cities->set_sql(ally => qq {
21 SELECT cities.cityid
22 FROM cities
23 WHERE cities.ally = ?
24 });
25
26
27 sub allyTownToVicim {
28 my $islandId = shift;
29
30 my $mime = Ikariam::User->retrieve(name => $::user);
31 my $ally = $mime->ally;
32 my $iterator = Ikariam::Cities->search_ally($mime->ally);
33 if(my $island = Ikariam::Island->retrieve($islandId)) {
34
35 my @cities = ();
36 while(my $target = $iterator->next) {
37 my $city;
38 $city->{id} = $target->id;
39 $city->{name} = $target->cityname;
40 $city->{owner} = $target->owner;
41 $city->{island}->{id} = $target->island->id;
42 $city->{island}->{x} = $target->island->x;
43 $city->{island}->{y} = $target->island->y;
44 $city->{distance} =
45 travelTime($target->island->x, $target->island->y, $island->x, $island->y) ;
46
47 push (@cities, $city);
48 }
49
50 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);
51
52 for my $x (0..$#cities) {
53 printf("%s (%s)\t- ", $cities[$x]->{name}, $cities[$x]->{owner});
54 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n",
55 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 60),
56 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 33),
57 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 26),
58 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 40),
59 );
60 # last if($x > 5);
61 }
62 }
63 }
64
65 sub myTownToVicim {
66 my $islandId = shift;
67
68 my @cities = ();
69 foreach (glob("city-*-dump.yaml")) {
70 my $city = LoadFile($_);
71 push (@cities, $city);
72 }
73 my $n = $#cities;
74
75
76 if(my $island = Ikariam::Island->retrieve($islandId)) {
77
78 for my $x (0..$n) {
79 printf("%s\t- ", $cities[$x]->{name});
80 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n",
81 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y),
82 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8),
83 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 6),
84 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 10),
85 );
86 }
87 }
88 }
89
90 sub cityToIsland {
91 my (@city, $island);
92 }
93
94 package main;
95
96 my $userName = $ARGV[0];
97 my @cities = ();
98 if(my $user = Ikariam::User->retrieve(name => $userName)) {
99 foreach my $target ($user->cities) {
100 my $island = $target->island;
101 my $city;
102 $city->{id} = $target->id;
103 $city->{name} = $target->cityname;
104 $city->{island}->{id} = $island->id;
105 $city->{island}->{x} = $island->x;
106 $city->{island}->{y} = $island->y;
107
108 push (@cities, $city);
109 }
110 }
111
112 my $n = $#cities;
113
114 # Print them out.
115 # header
116 printf(" %5s ", undef);
117 for my $y (0..$n) {
118 printf ("%5s ", $cities[$y]->{id});
119 }
120 print "\n";
121
122 # body
123 for my $x (0..$n) {
124 printf(" %5s ", $cities[$x]->{id});
125 my $total = 0;
126 for my $y (0..$n) {
127 my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y});
128 printf("%5s ", $t);
129 $total += $t;
130 }
131 printf(" %s", $cities[$x]->{name});
132 printf("\n");
133 }
134 printf("\n");
135
136
137 for my $i (0..$n) {
138 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});
139 # myTownToVicim($cities[$i]->{island}->{id});
140 # printf("\n");
141 allyTownToVicim($cities[$i]->{island}->{id});
142 printf("\n");
143 }