Difference between revisions of "User:CommanderC/winners spells.pl"

From CrawlWiki
Jump to: navigation, search
(Some scripts)
 
(Updated to 0.14)
 
Line 9: Line 9:
 
my $wins = 0;
 
my $wins = 0;
 
my $with_spells = 0;
 
my $with_spells = 0;
my $read_from_pastebin = 0;
 
  
 
sub main {
 
sub main {
 
my @lines;
 
my @lines;
if ($read_from_pastebin) {
+
        print "- Downloading all-players.html.\n";
print "- Downloading dcss_0.11_tourney_players.txt.\n";
+
        my $tournament_players = get("http://dobrazupa.org/tournament/0.14/all-players.html");
my $tournament_players = get('http://pastebin.com/raw.php?i=bAU5bAx6');
+
        die unless defined $tournament_players;
die unless defined $tournament_players;
+
        @lines = split /\n/, $tournament_players;
@lines = split /\n/, $tournament_players;
+
        my $players_section = 0;
} else {
+
foreach my $line (@lines) {
print "- Reading from dcss_0.11_tourney_players.txt.\n";
+
if ($line =~ m#<tr><th></th><th>Player#) { $players_section = 1; next;}
open my $file, "< dcss_0.11_tourney_players.txt" or die;
+
if ($line =~ m#</table>#) { $players_section = 0; next;}
@lines = <$file>;
+
                if ($players_section) {
close($file);
+
                        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);
 +
                                }
 +
                        }
 +
                }
 
}
 
}
shift @lines; #skip first line
+
 
foreach my $player (@lines) {
 
my ($player_name, $player_url) = split ',', $player;
 
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
 
# All the information is in %spells
 
print "Winners : $wins\n";
 
print "Winners : $wins\n";
Line 50: Line 52:
 
my $wins_section = 0;
 
my $wins_section = 0;
 
foreach my $line (@lines) {
 
foreach my $line (@lines) {
if ($line =~ m{<h3>Wins</h3>}) { $wins_section = 1; next;}
+
if ($line =~ m#<h3>Wins</h3>#) { $wins_section = 1; next;}
if ($line =~ m{</table>}) { $wins_section = 0; next;}
+
if ($line =~ m#</table>#) { $wins_section = 0; next;}
 
if ($wins_section) {
 
if ($wins_section) {
 
if ($line =~ /href="([^"]+)"/) {
 
if ($line =~ /href="([^"]+)"/) {
Line 74: Line 76:
 
$blank_line = length == 0 ? 1 : 0;
 
$blank_line = length == 0 ? 1 : 0;
 
$spell_section = 0 if ($prev_blank_line && $blank_line);
 
$spell_section = 0 if ($prev_blank_line && $blank_line);
+
 
 
if ($spell_section) {
 
if ($spell_section) {
 
next if not /-/;
 
next if not /-/;

Latest revision as of 19:52, 28 April 2014

#!/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;