annotate enemy.pl @ 283:34fd5bcd4cf8

add some explan in README
author billy3321@f3svr.f3.csu.edu.tw.f3.csu.edu.tw
date Fri, 05 Dec 2008 15:39:49 +0800
parents 2590bcd517a5
children 2ddd2bd21ef2
rev   line source
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
1 #!/usr/bin/perl
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
2 use strict;
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
3 use Ikariam;
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
4 use Data::Dumper;
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
5 use YAML qw/LoadFile/;
274
2590bcd517a5 added time information in output
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 273
diff changeset
6 use POSIX qw(strftime);
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
7
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
8 package main;
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
9 my @tradegoodText = qw/NULL 葡萄酒 大理石 水晶 硫磺/;
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
10 my @wonderText = qw/NULL 赫菲斯拓斯的熔爐 蓋亞的神殿 狄奧尼索斯的花園 雅典娜的神殿 赫秘士的神殿 阿瑞斯的要塞 波賽頓的神殿 克羅瑟斯的神殿/;
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
11
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
12 Ikariam::User->has_many(cities => 'Ikariam::Cities');
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
13
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
14 sub travelTime {
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
15 my ($x1, $y1, $x2, $y2, $speed) = @_;
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
16 $speed = 20 unless(defined($speed));
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
17 return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))+1) * (400/$speed));
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
18 }
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
19
273
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
20 sub travelMatrix {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
21 my $userName = shift;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
22 my @cities = ();
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
23 if(my $user = Ikariam::User->retrieve(name => $userName)) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
24
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
25 foreach my $target ($user->cities) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
26 my $island = Ikariam::Island->retrieve($target->island);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
27 my $city;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
28 $city->{id} = $target->id;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
29 $city->{name} = $target->cityname;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
30 $city->{island}->{id} = $island->id;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
31 $city->{island}->{x} = $island->x;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
32 $city->{island}->{y} = $island->y;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
33
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
34 push (@cities, $city);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
35 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
36 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
37
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
38 my $n = $#cities;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
39
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
40 # Print them out.
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
41 # header
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
42 printf("Defence\n");
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
43 printf(" %5s ", undef);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
44 for my $y (0..$n) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
45 printf ("%5s ", $cities[$y]->{id});
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
46 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
47 print "\n";
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
48
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
49 # body
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
50 for my $x (0..$n) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
51 printf(" %5s ", $cities[$x]->{id});
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
52 my $total = 0;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
53 for my $y (0..$n) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
54 my $t = travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $cities[$y]->{island}->{x}, $cities[$y]->{island}->{y});
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
55 printf("%5s ", $t);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
56 $total += $t;
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
57 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
58 printf(" %s", $cities[$x]->{name});
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
59 printf("\n");
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
60 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
61 printf("\n");
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
62 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
63
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
64 sub myTownToVicim {
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
65 my $islandId = shift;
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
66
273
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
67 if(my $island = Ikariam::Island->retrieve($islandId)) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
68 my @cities = ();
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
69 foreach (glob("city-*-dump.yaml")) {
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
70 my $city = LoadFile($_);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
71 $city->{distance} = travelTime($island->x, $island->y, $city->{island}->{x}, $city->{island}->{y}, 20);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
72 push (@cities, $city);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
73 }
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
74 @cities = sort { $a->{distance} <=> $b->{distance}; } (@cities);
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
75
273
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
76 my $n = $#cities;
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
77 for my $x (0..$n) {
273
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
78 printf("%s (%d)\t- ", $cities[$x]->{name}, $cities[$x]->{id});
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
79 printf("Land %3d Ship %3d Catapult %3d Ram %3d\n",
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
80 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y),
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
81 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 8),
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
82 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 6),
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
83 travelTime($cities[$x]->{island}->{x}, $cities[$x]->{island}->{y}, $island->x, $island->y, 10),
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
84 );
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
85 }
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
86 }
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
87 }
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
88
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
89 package main;
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
90
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
91 if($#ARGV != 0) {
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
92 die("Usage: $0 nickname\n");
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
93 }
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
94
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
95 my ($u) = Ikariam::User->search('name' => $ARGV[0]);
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
96 if(defined($u)) {
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
97 foreach my $i (qw/name score trader_score_secondary army_score_main ally/) {
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
98 printf("%s: %s\n", $i, $u->get($i));
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
99 }
274
2590bcd517a5 added time information in output
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 273
diff changeset
100 printf("time: %s\n", strftime ("%Y-%m-%d %H:%M", localtime($u->get('time'))));
273
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
101
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
102 travelMatrix($ARGV[0]);
f81c4f9d267f make the travel time is sort by distance
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 272
diff changeset
103
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
104 printf("\nCities\n");
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
105 foreach my $city ($u->cities) {
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
106 my $island = Ikariam::Island->retrieve($city->island);
44
4f7bca0d6190 fixed typo.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 38
diff changeset
107 my $capture = $city->citylevel * ($city->citylevel - 1) * $u->trader_score_secondary / 10000;
38
0863b32e1a05 counting for the capture we can get from the enemy
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 27
diff changeset
108
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
109 printf("Name: %s\n", $city->cityname);
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
110 if(defined($city->status)) {
20
5635e75c92d0 the user profile is also cached for 1 hour.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 18
diff changeset
111 printf("Status: <<<%s>>>\n", $city->status);
5635e75c92d0 the user profile is also cached for 1 hour.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 18
diff changeset
112 }
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
113 printf("Level: %s\n", $city->citylevel);
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
114 printf("Island: %s\n", $island->name);
78
4120f560f214 fixed the server name.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 44
diff changeset
115 printf("Location: [%s:%s] http://%s/index.php?view=island&id=%s&selectCity=%d\n",
4120f560f214 fixed the server name.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 44
diff changeset
116 $island->x, $island->y, $::server, $island->id, $city->cityId);
18
fd44e3a1f800 fixed the cache.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 8
diff changeset
117 printf("Tradegood: %s\n", $tradegoodText[$island->tradegood]);
38
0863b32e1a05 counting for the capture we can get from the enemy
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 27
diff changeset
118 printf("Wonder: %s\n", $wonderText[$island->wonder]);
272
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
119 printf("Capture: %d\n", $capture);
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
120 myTownToVicim($island->id);
3730817e3079 show travel time from my cities to enemy's cities
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 97
diff changeset
121 printf("\n");
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
122 }
97
5f9bb3042d0c refined the output.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents: 78
diff changeset
123 print "-"x80 . "\n";
8
e4b3168d0319 implemented sheep and enemy scripts.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff changeset
124 }