view scores.pl @ 97:5f9bb3042d0c

refined the output.
author "Rex Tsai <chihchun@kalug.linux.org.tw>"
date Wed, 29 Oct 2008 01:14:49 +0800
parents 53c87400183f
children e551351b18de
line wrap: on
line source

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

# highscoreType	
# <option value="score">總積分</option>
# <option value="building_score_main">建築大師</option>
# <option value="building_score_secondary">建築等級</option>
# <option value="research_score_main">科學巨人</option>
# <option value="research_score_secondary">研究等級</option>
# <option value="army_score_main" selected="selected">戰爭將軍</option>
# <option value="trader_score_secondary">黃金存量</option>
# my $users = $i->viewScore('score', 'chihchun', '0');

package main;

sub saveUser
{
    my $users = shift;

    foreach my $user (values(%{$users})) {
        printf("Saving %s\n", $user->{'name'});
        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);
        }
    }
}

my $i = new Ikariam($::server, $::user, $::pass);
$i->login;

if($#ARGV == 0) {
    my $users;
    foreach my $x (qw/score army_score_main trader_score_secondary/)
    {
        $users = $i->viewScore($x, $ARGV[0], 0);
        saveUser($users);
    }
} elsif ($#ARGV == 1) {
    # search the islands and cities nearby
    my ($x, $y) = @ARGV;
    my $offset = 5;

    Ikariam::Island->has_many(cities => 'Ikariam::Cities');

    my @islands = Ikariam::Island->retrieve_from_sql(
        "x >= " . ($x - $offset) . " AND " . " x <= " . ($x + $offset) . 
        " AND " .
        "y >= " . ($y - $offset) . " AND " . " y <= " . ($y + $offset)
    );

    my %owners;
    foreach my $island (@islands)
    {
        my @cities = $island->cities;
        foreach my $city (@cities)
        {
            $owners{$city->owner} += 1;
        }
    }

    foreach my $o (keys(%owners))
    {
        foreach my $type (qw/score army_score_main trader_score_secondary/)
        {
            my $users = $i->viewScore($type, $o, 0);
            saveUser($users);
        }
    }
} else {
    if(0) { 
        # Update all the users
        my @users = Ikariam::User->retrieve_all();
        foreach my $user (@users) 
        {
            printf("%s\n", $user->name);
            foreach my $type (qw/score army_score_main trader_score_secondary/)
            {
                my $users = $i->viewScore($type, $user->name, 0);
                saveUser($users);
            }
        }
    } else {
        # Update owner of cities
        my @cities = Ikariam::Cities->retrieve_all();
        foreach my $city (@cities) 
        {
            # printf("%s\n", $city->owner);
            foreach my $type (qw/score army_score_main trader_score_secondary/)
            {
                my $users = $i->viewScore($type, $city->owner, 0);
                saveUser($users);
            }
        }
    }
    # die("Usage: $0 nickname\nUsage: $0 x y");
}
$i->logout;