view scan.pl @ 241:64549ddddf4a

count by ally score
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Fri, 07 Nov 2008 15:18:23 +0800
parents 38e1a82627a9
children a654d43731f0
line wrap: on
line source

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

package main;

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

    if ($#cities == -1) {
        warn ("empty cities list.\n");
        return;
    }

    foreach my $h_city (@cities) {
# Carp::carp(sprintf("checking %s\n", $h_city->{'cityname'}));
        $h_city->{island} = $island;
        $h_city->{'time'} = time;
        my $c = Ikariam::Cities->retrieve($h_city->{cityId});
        if(defined($c)) {
            foreach (keys(%$h_city)) { 
                $c->set($_ => $h_city->{$_}); 
            }
        } else {
            $c = Ikariam::Cities->insert($h_city);
        }
        $c->update();

        my $user = Ikariam::User->retrieve($h_city->{'user'});
        next if (defined($user) && defined($user->time) && $user->time gt (time - 30*60));

        saveUser($h_city->{owner});
    }
}

sub saveUser
{
    my $userName = shift;
    my $users;

    foreach my $x (qw/score army_score_main trader_score_secondary/) {
        $users = $::i->viewScore($x, $userName, 0);

        foreach my $h_user (values(%{$users})) {
# Carp::carp(sprintf("Saving user %s\n", $h_user->{'name'}));
            $h_user->{'time'} = time;

            my $user = Ikariam::User->retrieve($h_user->{'id'});
            if(defined($user)) {
                foreach (keys(%$h_user)) { $user->set($_ => $h_user->{$_}); }
            } else {
                $user = Ikariam::User->insert($h_user);
            }
            $user->update();
            saveAlly($h_user->{allyId});
        }
    }
}

sub saveAlly
{
    my $allyId = shift;
    return unless (defined($allyId) && $allyId ne '0');

    # Save for member of ally 
    my $ally = Ikariam::Ally->retrieve($allyId);
    if(!defined($ally) || $ally->time le (time - 30*60*1)) {
        my $h_ally = $::i->viewAlly($allyId);
        $h_ally->{'time'} = time;
        if(defined($ally)) {
            foreach (keys(%$h_ally)) { $ally->set($_ => $h_ally->{$_}); }
        } else {
            $ally = Ikariam::Ally->insert($h_ally);
        }
    }
}

sub saveIslands {
    my @islands = @_;

    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
        # 10 minutes cache.
        if($island->time le (time - 60*10))
        {
            my @cities = $::i->viewIsland($h_island->{id});
            saveCities($h_island->{id}, @cities);

            $island->set('time', time);
        }
        $island->update();

    }
}


# local $SIG{ALRM} = sub { die "timeout\n" };
# alarm 3;
our $i = new Ikariam($::server, $::user, $::pass);

if($#ARGV == 1) {
    $i->login;
    my @islands = $i->viewWorldMap($ARGV[0], $ARGV[1]);
    saveIslands(@islands);
    $i->logout;
} elsif($#ARGV == 0) {
    $i->login;
    my $island = $ARGV[0];
    my @cities = $i->viewIsland($island);
    saveCities($island, @cities);
    $i->logout;
} elsif($#ARGV == -1) {
    $i->login;
    my $cities = $i->check;
    # random
    foreach my $cityId (keys(%$cities)) {
# Carp::carp(sprintf("Checking island location [%s:%s]\n", $cities->{$cityId}->{island}->{x},  $cities->{$cityId}->{island}->{y}));
        my @islands = $i->viewWorldMap( $cities->{$cityId}->{island}->{x},  $cities->{$cityId}->{island}->{y});
        saveIslands(@islands);
    }
    $i->logout;
} else {
    die("Usage: $0\nUsage: $0 x y\n");
}