comparison inference.pl @ 27:dd85b55eec2a

implemented basic inference engine.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 10 Oct 2008 03:38:49 +0800
parents
children 99723b8f348b
comparison
equal deleted inserted replaced
26:d8117792c6f5 27:dd85b55eec2a
1 #!/usr/bin/perl
2 use strict;
3 use Ikariam;
4 use Data::Dumper;
5
6 package main;
7
8 our $cities;
9 our $server = 's2.ikariam.tw';
10 sub Warn
11 {
12 my ($city, $msg) = @_;
13 printf("WARNING: http://%s/index.php?view=city&id=%s (%s) %s \n",
14 $server, $city, $cities->{$city}->{name}, $msg);
15 }
16
17 our $i = new Ikariam($server, "chihchun", "c795d57d");
18 $i->login;
19 $cities = $i->check;
20 $i->logout;
21 print(Dumper($cities));
22
23 sub rule_happiness
24 {
25 my $id = shift;
26
27 if($cities->{$id}->{happiness} > 3) {
28 Warn($id, sprintf("is not happy (%s)!", $cities->{$id}->{happiness_text}));
29 }
30 }
31
32 sub rule_resource
33 {
34 my $id = shift;
35
36 # checking goods
37 foreach my $good (qw/wood wine marble crystal/) {
38 if($cities->{$id}->{$good} <= 100) {
39 Warn($id, sprintf("Running out %s!", $good));
40 }
41 }
42 }
43
44 # Checking rules
45 foreach my $procedure (qw/rule_happiness rule_resource/) {
46 foreach my $id (keys(%$cities)) {
47 eval(sprintf("%s(%s);", $procedure, $id));
48 }
49 }