#!/usr/bin/env perl
use strict;
use warnings;
use LWP::Simple; #get()
my %spells;
my $wins = 0;
my $with_spells = 0;
sub main {
my @lines;
print "- Downloading all-players.html.\n";
my $tournament_players = get("http://dobrazupa.org/tournament/0.14/all-players.html");
die unless defined $tournament_players;
@lines = split /\n/, $tournament_players;
my $players_section = 0;
foreach my $line (@lines) {
if ($line =~ m#<tr><th></th><th>Player#) { $players_section = 1; next;}
if ($line =~ m#</table>#) { $players_section = 0; next;}
if ($players_section) {
my @num = ($line =~ m#<td class=\"numeric\">([^<]+)<\/td>#g);
if ($#num) {
my $won = int($num[2]);
if ($won > 0 && $line =~ m#href="([^"]+)"#) {
my $player_url = $1;
print " |\n";
print " |- Downloading $player_url\n";
my $player_profile = get($player_url);
die unless defined $player_profile;
handle_profile($player_profile);
}
}
}
}
# All the information is in %spells
print "Winners : $wins\n";
print "With spells : $with_spells\n";
my $no_spells = $wins - $with_spells;
print "Without spells : $no_spells\n";
my @k = sort { $spells{$a} <=> $spells{$b} } keys %spells;
for (reverse @k) {
print "$_ : $spells{$_}\n";
}
}
sub handle_profile {
my $profile = shift;
my @lines = split /\n/, $profile;
my $wins_section = 0;
foreach my $line (@lines) {
if ($line =~ m#<h3>Wins</h3>#) { $wins_section = 1; next;}
if ($line =~ m#</table>#) { $wins_section = 0; next;}
if ($wins_section) {
if ($line =~ /href="([^"]+)"/) {
$wins++;
my $morgue = $1;
print " | |\n";
print " | |- Downloading $morgue\n";
my $morgue_content = get($morgue);
die unless defined $morgue_content;
handle_morgue($morgue_content);
}
}
}
}
sub handle_morgue {
my $morgue = shift;
my @lines = split /\n/, $morgue;
my ($spell_section, $blank_line, $prev_blank_line) = (0, 0 ,0);
foreach (@lines) {
$prev_blank_line = $blank_line;
$blank_line = length == 0 ? 1 : 0;
$spell_section = 0 if ($prev_blank_line && $blank_line);
if ($spell_section) {
next if not /-/;
my $spell_name = substr($_, 4, 26 - 4);
if (not defined($spells{$spell_name})) {
$spells{$spell_name} = 0;
}
$spells{$spell_name}++;
} elsif (/You knew the following spells:/) {
$spell_section = 1;
$with_spells++;
} else {
next;
}
}
}
main;