view sheep.pl @ 347:b74e3a838b58

do not build military
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Sat, 14 Feb 2009 03:02:46 +0800
parents 85d60a0569bf
children b245f259776f
line wrap: on
line source

#!/usr/bin/perl
use strict;
use Ikariam;
use Data::Dumper;

package main;
my $mime = Ikariam::User->retrieve ('name' => $::user);
my $myAlly = undef;
$myAlly = Ikariam::Ally->retrieve($mime->allyId)
    if(defined($mime->allyId) && $mime->allyId ne '0');

# only challenge the small victims
my $army_score_main = ($mime->army_score_main / 3);

my @tradegoodText = qw/NULL 葡萄酒 大理石 水晶 硫磺/;

Ikariam::User->has_many(cities => 'Ikariam::Cities');
Ikariam::User->has_a(ally => 'Ikariam::Ally');
Ikariam::User->set_sql(inactivity => qq {
    SELECT user.id 
      FROM user, cities, island
      WHERE user.id == cities.user
        AND cities.island == island.id
        AND island.x <= ?
        AND island.x >= ?
        AND island.y <= ?
        AND island.y >= ?
        AND cities.status = 'i'
    }
);

Ikariam::User->set_sql(sheeps => qq {
        SELECT user.id 
          FROM user, cities 
         WHERE user.id == cities.user 
           AND user.trader_score_secondary >= user.army_score_main*3
           AND user.army_score_main <= $army_score_main
           AND cities.island IN (SELECT island.id FROM island WHERE island.x <= ? AND island.x >= ? AND island.y <= ? AND island.y >= ? )
    }
);

sub travelTime {
    my ($x1, $y1, $x2, $y2, $speed) = @_;
    $speed = 60 unless(defined($speed));
    if($x1 == $x2 && $y1 == $y2) {
        return 600/$speed;
    } else {
        return int( (sqrt((abs($x1 - $x2) * abs($x1 - $x2)) + (abs($y1 - $y2) * abs($y1 - $y2)))) * (1200/$speed));
    }
}

sub listSheeps
{
    my @sheeps = @_;
    my %results;

    my $s;
    foreach my $sheep (sort (@sheeps)) {
        # avoid duplicate
        next if($sheep->id == $s); $s = $sheep->id;

        foreach my $c ($sheep->cities) {
            my $line = "";
            # Ignore 假期模式
            next if($c->status eq 'v');

            unless($c->status eq 'i') {
                # 依照影響力區分
                unless ($sheep->allyId == '0') {
                    unless (!defined($sheep->allyId) || $sheep->allyId == 0) {
                        my $ally = Ikariam::Ally->retrieve($sheep->allyId);
                        next if(!defined($ally));
                        next if($ally->score > $myAlly->score);
                    }
                }
            }

            my $island = Ikariam::Island->retrieve($c->island);


            $line = sprintf("%d %s score %d army %d risk %d %s/%s,", 
                travelTime($::x, $::y, $island->x, $island->y),
                # $capture,
                $c->status, $sheep->score, $sheep->army_score_main, $c->risk, $sheep->name, $sheep->ally);

            $line .= sprintf("\"%s\" %d [%d,%d] %s http://%s/index.php?view=island&id=%d&selectCity=%d\n",
                $c->cityname, $c->citylevel, 
                $island->x, $island->y,
                $tradegoodText[$island->tradegood],
                $::server,
                $island->id,
                $c->cityId
            );

            printf("%s", $line);
        }
    }
}

__MAIN__:

if($#ARGV != 1) { die("Usage: $0 x y\n"); }
our ($x, $y) = @ARGV;

listSheeps(Ikariam::User->search_sheeps(($x + 6), ($x - 6), ($y + 6), ($y - 6)));
# listSheeps(Ikariam::User->search_inactivity(($x + 6), ($x - 6), ($y + 6), ($y - 6)));