view scan.pl @ 20:5635e75c92d0

the user profile is also cached for 1 hour.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 08 Oct 2008 16:56:22 +0800
parents fd44e3a1f800
children 552528bb4917
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});
        if(my $user = Ikariam::User->retrieve($city->{user}))
        {
            if($user->time le (time - 60*60)) {
                # 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 $h_user (values(%{$users}))
    {
        # print(Dumper($user));
        printf("Saving user %s\n", $h_user->{'name'});
        $h_user->{'time'} = time;
        my $user;
        if($user = Ikariam::User->retrieve($h_user->{id}))
        {
            foreach my $i (keys(%$h_user)) {
                # eval($c->$i($h_user->{$i}));
                $user->set($i => $h_user->{$i});
            }
        } else {
            $user = Ikariam::User->insert($h_user);
        }
        $user->update();
    }
}

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 $h_island (@islands)
{
    printf("checking island %d\n", $h_island->{id});

    my $island;
    if($island = Ikariam::Island->retrieve($h_island->{id})) {
        foreach my $i (keys(%$h_island)) {
            $island->set($i => $h_island->{$i});
        }
    } else {
        $island = Ikariam::Island->insert($h_island);
    }

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

    $island->set('time', time);
    $island->update();
}
$i->logout;
# $i->getCityInfo();