view sheep.pl @ 10:f590b5ea5e55

fixed bug of sheep.pl
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 09:55:43 +0800
parents e4b3168d0319
children e21b8f303c6a
line wrap: on
line source

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

package main;

Ikariam::User->has_many(cities => 'Ikariam::Cities');
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 user.status = 'i'
    }
);
Ikariam::User->set_sql(sheeps => 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 user.score >= 500 
        AND user.army_score_main <= 25
    }
);

if($#ARGV != 1) {
    die("Usage: $0 x y\n");
}
my ($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)));


sub listSheeps
{
    my @sheeps = @_;

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

        # 假期模式
        next if($sheep->status eq 'v');

        # 查聯盟數量
        my $members = 1;
        unless ($sheep->status eq 'i') {
            $members = Ikariam::User->search(allyId => $sheep->allyId)->count();
            next if($members > 3);
        }

        # print Dumper($sheep->_data_hash);
        foreach my $c ($sheep->cities) {
            # print Dumper($c->_data_hash);

            print("!") if ($c->status eq 'i');

            printf("%d,%d,%d,%s of %s (%d),", $sheep->trader_score_secondary, $sheep->score, $sheep->army_score_main, $sheep->name, $sheep->ally, $members);

            my $island = Ikariam::Island->retrieve($c->island);
            printf("%s,%d,[%d,%d],http://s2.ikariam.tw/index.php?view=island&id=%d\n", $c->cityname, $c->citylevel, 
                $island->x, $island->y,
                $island->id);
        }
        printf("\n");
    }
}
# find_or_create