view scan.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 8b7bc598ba06
line wrap: on
line source

#!/usr/bin/perl
use strict;
use Ikariam;

package main;

my $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");
$i->login;
my @islands;
if($#ARGV == 1) {
    @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
} elsif($#ARGV == 0) {
    my $island = $ARGV[0];

    my @cities = $i->viewIsland($island);
    saveCities($island, @cities);
    $i->logout;
    exit;
} elsif($#ARGV == -1) {
    @islands = $i->viewHomeMap();
} else {
    die("Usage: $0\nUsage: $0 x y\n");
}

foreach my $island (@islands)
{
    printf("checking island %d\n", $island->{id});
    if(my $c = Ikariam::Island->retrieve($island->{id})) {
        foreach my $i (keys(%$island)) {
            eval($c->$i($island->{$i}));
        }
    } else {
        Ikariam::Island->insert($island);
    }

    # scanning the island
    my @cities = $i->viewIsland($island->{id});
    saveCities($island->{id}, @cities);
}

sub saveCities
{
    my ($island, @cities) = @_;

    foreach my $city (@cities)
    {
        $city->{island} = $island;
        if(my $c = Ikariam::Cities->retrieve($city->{cityId}))
        {
            foreach my $i (keys(%$city))
            {
                eval($c->$i($city->{$i}));
            #    printf("%s %s ", $i, $city->{$i});
            }
            #print ("\n");

            $c->autoupdate(1);
            $c->update();
        } else {
            Ikariam::Cities->insert($city);
        }
        printf("city %d %s saved\n", $city->{cityId}, $city->{cityname});
    }
}
$i->logout;
# $i->getCityInfo();