Mercurial > eagle-eye
annotate inference.pl @ 39:65e847de5187
refined the defending rules
author | "Rex Tsai <chihchun@kalug.linux.org.tw>" |
---|---|
date | Thu, 16 Oct 2008 21:43:20 +0800 |
parents | 91e387b51aa0 |
children | 5849b6fdc76c |
rev | line source |
---|---|
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
1 #!/usr/bin/perl |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
2 use strict; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
3 use Ikariam; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
4 use Data::Dumper; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
5 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
6 package main; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
7 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
8 our $cities; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
9 our $server = 's2.ikariam.tw'; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
10 sub Warn |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
11 { |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
12 my ($city, $msg) = @_; |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
13 printf("http://%s/index.php?view=city&id=%s (%s) %s \n", |
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
14 $server, $city, $cities->{$city}->{name}, $msg); |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
15 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
16 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
17 our $i = new Ikariam($server, "chihchun", "c795d57d"); |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
18 $i->login; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
19 $cities = $i->check; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
20 $i->logout; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
21 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
22 sub rule_happiness |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
23 { |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
24 my $id = shift; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
25 |
28
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
26 if($cities->{$id}->{happiness} >= 3) { |
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
27 Warn($id, sprintf("is not happy (%s)!", $cities->{$id}->{happiness_text})); |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
28 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
29 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
30 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
31 sub rule_resource |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
32 { |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
33 my $id = shift; |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
34 |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
35 # checking goods |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
36 foreach my $good (qw/wood wine marble crystal/) { |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
37 if($cities->{$id}->{resources}->{$good} <= 100) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
38 Warn($id, sprintf("Running out %s (%d)!", $good, $cities->{$id}->{resources}->{$good})); |
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
39 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
40 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
41 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
42 |
29
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
43 sub rule_building |
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
44 { |
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
45 my $cityId = shift; |
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
46 |
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
47 # 城鎮跟圍牆 |
39
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
48 if($cities->{$cityId}->{construction} != 1) { |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
49 if($cities->{$cityId}->{buildings}->{wall} < $cities->{$cityId}->{buildings}->{townHall}) { |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
50 Warn($cityId, sprintf("Wall level (%d) is lower then town hall (%d)!", |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
51 $cities->{$cityId}->{buildings}->{wall}, |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
52 $cities->{$cityId}->{buildings}->{townHall} |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
53 )) |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
54 } else { |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
55 Warn($cityId, sprintf("is not building.")); |
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
56 } |
29
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
57 } |
30
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
58 } |
29
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
59 |
30
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
60 sub rule_space |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
61 { |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
62 my $cityId = shift; |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
63 |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
64 if($cities->{$cityId}->{space}->{total} == $cities->{$cityId}->{space}->{occupied}) |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
65 { |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
66 Warn($cityId, sprintf("your space is full, upgraded the hall")) |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
67 if($cities->{$cityId}->{construction} != 1); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
68 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
69 return 0; |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
70 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
71 |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
72 sub rule_war |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
73 { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
74 my $cityId = shift; |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
75 if($cities->{$cityId}->{force}->{attacks} > 0) |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
76 { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
77 Warn($cityId, sprintf("YOU ARE ATTACKED!!!!")) if($cities->{$cityId}->{construction} != 1) |
30
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
78 } |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
79 } |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
80 |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
81 sub rule_engagement |
de5de6d472f9
added space checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
29
diff
changeset
|
82 { |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
83 my $cityId = shift; |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
84 if($cities->{$cityId}->{force}->{wars} == 0) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
85 Warn($cityId, sprintf("Let's Attack someone!")); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
86 } |
29
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
87 } |
dbec53e754e3
added rule_building
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
28
diff
changeset
|
88 |
28
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
89 # 貪腐 |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
90 sub rule_corruption |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
91 { |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
92 my $id = shift; |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
93 |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
94 if($cities->{$id}->{corruption} > 0) { |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
95 Warn($id, sprintf("corruption is %d", $cities->{$id}->{corruption})); |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
96 } |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
97 } |
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
98 |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
99 print Dumper($cities); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
100 # show cities. |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
101 foreach my $cityId (keys(%$cities)) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
102 printf("%s http://s2.ikariam.tw/index.php?view=city&id=%d\n", $cities->{$cityId}->{name}, $cityId); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
103 printf("construction: %s\n", $cities->{$cityId}->{construction}); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
104 |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
105 foreach my $thing (qw/resources space force buildings citizens army fleet/) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
106 printf("%s\n", uc($thing)); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
107 foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
108 printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i}); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
109 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
110 print("\n"); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
111 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
112 print("\n"); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
113 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
114 |
28
99723b8f348b
added rule_corruption
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
27
diff
changeset
|
115 # space |
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
116 # Checking rules |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
117 foreach my $procedure (qw/rule_war rule_happiness rule_corruption rule_building rule_space rule_resource/) { |
27
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
118 foreach my $id (keys(%$cities)) { |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
119 eval(sprintf("%s(%s);", $procedure, $id)); |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
120 } |
dd85b55eec2a
implemented basic inference engine.
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
diff
changeset
|
121 } |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
122 |
39
65e847de5187
refined the defending rules
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
34
diff
changeset
|
123 foreach my $procedure (qw/rule_engagement/) { |
34
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
124 foreach my $id (keys(%$cities)) { |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
125 eval(sprintf("%s(%s);", $procedure, $id)); |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
126 last; |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
127 } |
91e387b51aa0
added more rules checking
"Rex Tsai <chihchun@kalug.linux.org.tw>"
parents:
30
diff
changeset
|
128 } |