changeset 27:dd85b55eec2a

implemented basic inference engine.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 10 Oct 2008 03:38:49 +0800
parents d8117792c6f5
children 99723b8f348b
files Ikariam.pm enemy.pl inference.pl
diffstat 3 files changed, 60 insertions(+), 18 deletions(-) [+]
line wrap: on
line diff
--- a/Ikariam.pm	Fri Oct 10 03:20:36 2008 +0800
+++ b/Ikariam.pm	Fri Oct 10 03:38:49 2008 +0800
@@ -9,7 +9,7 @@
 );
 
 package Ikariam;
-
+use strict;
 use Data::Dumper;
 use LWP;
 # use LWP::Debug qw(+ -conns -trace -debug);
@@ -193,7 +193,7 @@
     my %status;
     foreach my $class (qw/inactivity vacation/)
     {
-        @elems = $html->getElementsByAttribute("class", $class);
+        my @elems = $html->getElementsByAttribute("class", $class);
         foreach my $elem (@elems) {
             if($elem->innerText() =~ /^(.*?) \((\w)\)/) {
                 $status{$1} = $2;
@@ -326,18 +326,15 @@
         }
 
         # count
-        my $cities_type = {
-            citizens => 8,
-            woodworkers => 10, 
-            specialworkers => 12,
-            scientists => 14,
-        };
+        my @citizens_type = qw/citizens woodworkers specialworkers scientists/;
+        @elems = $html->getElementsByAttribute('class', 'count');
+        $self->{'cities'}->{$cityId}->{'citizens'} = {};
+        $self->{'cities'}->{$cityId}->{'citizens'}->{total} = 0;
 
-        @elems = $html->getElementsByAttribute('class', 'count');
-        $self->{'cities'}->{$cityId}->{'cities'} = {};
-        foreach my $type (keys(%$cities_type))
+        foreach my $i (0..$#citizens_type)
         {
-            $self->{'cities'}->{$cityId}->{'cities'}->{$type} = $elems[$cities_type{$type}]->innerText();
+            $self->{'cities'}->{$cityId}->{'citizens'}->{$citizens_type[$i]} = $elems[$i]->innerText();
+            $self->{'cities'}->{$cityId}->{'citizens'}->{total} += $elems[$i]->innerText();;
         }
 
         # production
@@ -346,12 +343,6 @@
         # skin/resources/icon_sulfur.gif (?)
         # skin/resources/icon_research.gif
 
-#        foreach my $i (0..$#cities_type) {
-#            next if($cities_type[$i] eq 'undef');
-#            if(defined($elems[$i])) {
-#                $self->{'cities'}->{$cityId}->{'cities_'.$type}->{$cities_type[$i]} = $elems[$i]->innerText();
-#            }
-#        }
 
         # check armies
         my %force_types;
--- a/enemy.pl	Fri Oct 10 03:20:36 2008 +0800
+++ b/enemy.pl	Fri Oct 10 03:38:49 2008 +0800
@@ -11,6 +11,8 @@
     die("Usage: $0 nickname\n");
 }
 
+system('perl scores.pl ' . $ARGV[0]);
+
 Ikariam::User->has_many(cities => 'Ikariam::Cities');
 my ($u) = Ikariam::User->search('name' => $ARGV[0]);
 if(defined($u)) {
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/inference.pl	Fri Oct 10 03:38:49 2008 +0800
@@ -0,0 +1,49 @@
+#!/usr/bin/perl
+use strict;
+use Ikariam;
+use Data::Dumper;
+
+package main;
+
+our $cities;
+our $server = 's2.ikariam.tw';
+sub Warn
+{
+    my ($city, $msg) = @_;
+    printf("WARNING: http://%s/index.php?view=city&id=%s (%s) %s \n", 
+        $server, $city, $cities->{$city}->{name}, $msg);
+}
+
+our $i = new Ikariam($server, "chihchun", "c795d57d");
+$i->login;
+$cities = $i->check;
+$i->logout;
+print(Dumper($cities));
+
+sub rule_happiness
+{
+    my $id = shift;
+
+    if($cities->{$id}->{happiness} > 3) {
+        Warn($id, sprintf("is not happy (%s)!", $cities->{$id}->{happiness_text}));
+    }
+}
+
+sub rule_resource
+{
+    my $id = shift;
+
+    # checking goods
+    foreach my $good (qw/wood wine marble crystal/) {
+        if($cities->{$id}->{$good} <= 100) {
+            Warn($id, sprintf("Running out %s!", $good));
+        }
+    }
+}
+
+# Checking rules
+foreach my $procedure (qw/rule_happiness rule_resource/) {
+    foreach my $id (keys(%$cities)) {
+        eval(sprintf("%s(%s);", $procedure, $id));
+    }
+}