diff agent.pl @ 52:d2ac1e198ce4

implement a new agent based on Decision Tree (Decision::ParseTree)
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Mon, 20 Oct 2008 19:07:53 +0800
parents
children 2d3c394b7940
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/agent.pl	Mon Oct 20 19:07:53 2008 +0800
@@ -0,0 +1,145 @@
+#!/usr/bin/perl
+
+use strict;
+use Ikariam;
+use Data::Dumper;
+use Decision::ParseTree q{ParseTree};
+use YAML qw/LoadFile Dump/;
+
+package Ikariam::Cities::Rules;
+use strict;
+use Data::Dumper;
+
+sub new {
+    my ( $class ) = @_;
+    my $self = {};
+    return bless $self, $class;
+}
+
+sub is_Attacked {
+    my ($self, $city) = @_;
+    return ($city->{force}->{attacks} > 0 ) ? 1 : 0;
+}
+
+sub is_constructing {
+    my ($self, $city) = @_;
+    return ($city->{construction} > 0 ) ? 1 : 0;
+}
+
+sub is_wall_enough {
+    my ($self, $city) = @_;
+    # TODO 應該以防禦力計算
+    return ($city->{buildings}->{wall} >= $city->{buildings}->{townHall} ?  1 : 0);
+}
+
+sub is_space_enough {
+    my ($self, $city) = @_;
+    # TODO 應該以消耗率/時間計算
+    return ($city->{space}->{total} <= ($city->{space}->{occupied}+6) ?  1 : 0)
+}
+
+sub is_corruption {
+    my ($self, $city) = @_;
+    return 0;
+}
+
+sub is_happiness
+{
+    my ($self, $city) = @_;
+    # TODO 以 fuzzy 取出合理 happiness 值
+    return ($city->{happiness} >= 2 ?  1 : 0)
+}
+
+sub is_warehouse_enough
+{
+    my ($self, $city) = @_;
+    # TODO 以速率計算容納率
+    # XXX: not implemented yet.
+    return 1;
+}
+
+sub is_risk
+{
+    my ($self, $city) = @_;
+    # TODO 計算可搶劫比例, 城牆防護, 軍事分數
+    return 0;
+}
+
+sub is_shipyard
+{
+    return 1;
+}
+
+sub is_drydock_researched {
+    my ($self, $city) = @_;
+    return (defined($city->{research}->{4010}) ?  1 : 0);
+}
+
+sub is_winepress_researched {
+    my ($self, $city) = @_;
+    return (defined($city->{research}->{2040}) ?  1 : 0);
+}
+
+sub rule_engagement
+{
+    my ($self, $city) = @_;
+    # XXX
+    # 計算距離, 可搶劫比例, 是否有聯盟, 軍事分數 (win rate)
+}
+
+sub rule_resource
+{
+    # XXX
+}
+1; 
+
+
+package main;
+our $i = new Ikariam($::server, $::user, $::pass);
+$i->login;
+my $cities = $i->check;
+
+my $rules = Ikariam::Cities::Rules->new;
+my $tree  = LoadFile('building.yaml');
+# print Dumper($tree);
+# show cities.
+foreach my $cityId (keys(%$cities)) {
+    printf("%s http://%s/index.php?view=city&id=%d\n", 
+        $cities->{$cityId}->{name}, $::server, $cityId);
+    printf("construction: %s\n", $cities->{$cityId}->{construction});
+    
+    foreach my $thing (qw/resources space force buildings citizens army fleet/) {
+        printf("<%s>\n", uc($thing));
+        foreach my $i (keys(%{$cities->{$cityId}->{$thing}})) {
+            printf("%s %s, ", $i, $cities->{$cityId}->{$thing}->{$i});
+        }
+        print("\n");
+    }
+    print("\n");
+    # print(Dumper($cities));
+
+    # make decisions
+
+    # for the Decision Tree
+    $cities->{$cityId}->{parse_path} = [];
+    $cities->{$cityId}->{parse_answer} = undef;
+    while (my $action = ParseTree($tree, $rules, $cities->{$cityId}))
+    {
+        # TODO: remove the last rule, if the result is fallback
+        triggerAction($action, $cityId);
+        last;
+    }
+    # Debug
+    print(Dumper($cities->{$cityId}->{parse_path}));
+}
+
+$i->logout;
+
+sub triggerAction {
+    my ($action, $cityId) = @_;
+
+    my ($func, $param) = split(/_/,$action);
+    # printf('$i->%s("%s", %s);\n\n', $func, $param, $cityId);
+    eval(sprintf('$i->%s("%s", %s);', $func, $param, $cityId));
+    warn $@ if $@;
+}