view scan.pl @ 16:59f2c435760c

implemented cached by timing.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 15:24:21 +0800
parents 8b7bc598ba06
children fd44e3a1f800
line wrap: on
line source

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

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

    foreach my $city (@cities)
    {
        $city->{island} = $island;
        $city->{'time'} = time;
        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});

        # Download user profile.
        foreach my $x (qw/score army_score_main trader_score_secondary/)
        {
            my $users = $::i->viewScore($x, $city->{owner}, 0);
            saveUser($users);
        }
    }
}

sub saveUser
{
    my $users = shift;

    foreach my $user (values(%{$users}))
    {
        # print(Dumper($user));
        printf("Saving user %s\n", $user->{'name'});
        $user->{'time'} = time;
        if(my $c = Ikariam::User->retrieve($user->{id}))
        {
            foreach my $i (keys(%$user)) {
                eval($c->$i($user->{$i}));
            }
            $c->autoupdate(1);
            $c->update();
        } else {
            Ikariam::User->insert($user);
        }
    }
}

our $i = new Ikariam("s2.ikariam.tw", "chihchun", "c795d57d");

my @islands;
if($#ARGV == 1) {
    $i->login;
    @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
} elsif($#ARGV == 0) {
    $i->login;
    my $island = $ARGV[0];

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

foreach my $island (@islands)
{
    printf("checking island %d\n", $island->{id});

    # scanning the island
    if($island->{time} < (time - 60*60*12 ))
    {
        my @cities = $i->viewIsland($island->{id});
        saveCities($island->{id}, @cities);

        $island->{'time'} = time;
        if(my $c = Ikariam::Island->retrieve($island->{id})) {
            foreach my $i (keys(%$island)) {
                eval($c->$i($island->{$i}));
            }
        } else {
            Ikariam::Island->insert($island);
        }
    }
}
$i->logout;
# $i->getCityInfo();