# HG changeset patch # User "Rex Tsai " # Date 1223581129 -28800 # Node ID dd85b55eec2a6d14d84a6758ea100b4c0503a8e6 # Parent d8117792c6f5b50c4b034753c9075f3a35796220 implemented basic inference engine. diff -r d8117792c6f5 -r dd85b55eec2a Ikariam.pm --- 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; diff -r d8117792c6f5 -r dd85b55eec2a enemy.pl --- 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)) { diff -r d8117792c6f5 -r dd85b55eec2a inference.pl --- /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)); + } +}