[Home]TargetingChallenge/CalculatorCode

Robo Home | TargetingChallenge | Changes | Preferences | AllPages

No diff available--this is the first major revision. (no other diffs)
Since this is a code sharing wiki here's the code for the TCCalc CGI script. It's just a quick hack and doesn't show how my coding principles work. =) It's good to have the code public for when there's need to add features to it and I might need help with that or ironing out bugs. Please feel free to use the code if you find it useful for any automation of your calculations on RoboLeague results. Of course, if you do that, feel free to host the resulting CGI on the wiki. -- PEZ

#!/usr/bin/perl -w
use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
use strict;
use XML::DOM;
use CGI qw/:standard/;

print header,
start_html('TC Calculator'),
h1('A TC Calculator'),
start_multipart_form,
table(
Tr(td('RoboLeague battle file'), td(filefield('uploaded_file', '', 50))),
Tr(td('Author'), td(textfield('author')))),
submit,
end_form,
hr,
"\n\n";

if (param('uploaded_file')) {
    my $author = param('author');
    my $file = upload('uploaded_file');
    my @bots = ('SandboxDT 1.91',
		'AspidMovement 1.0',
		'TheArtOfWar 1.2TC',
		'Sparrow 2.5TC',
		'Cigaret 1.31TC',
		'Tron 2.01',
		'Fhqwhgads 1.1TC',
		'HTTC 1.0',
		'Yngwie 1.0',
		'FunkyChickenTC 1.0');
    my %scores;
    my $parser = new XML::DOM::Parser;
    my $document;
    my @file = <$file>;
    $document = $parser->parse(join("\n", @file));
    my $league = $document->getElementsByTagName('LEAGUE')->item(0);
    my $rounds = $league->getAttribute('rounds_per_grouping');
    my $challenger = $league->getAttribute('focused_competitor_id');
    my ($challengerName, $version) = ($challenger =~ m/([^ ]+) (.*)/);
    my $seasons = 0;
    my @seasonTotals;
    foreach my $season ($document->getElementsByTagName('SEASON')) {
	$seasons++;
	foreach my $grouping ($season->getElementsByTagName('GROUPING')) {
	    my ($bot, $score);
	    foreach my $results ($grouping->getElementsByTagName('RESULTS')) {
		if ($results->getAttribute('id') eq $challenger) {
		    $score = $results->getAttribute('bullet_damage');
		}
		else {
		    $bot = $results->getAttribute('id');
		    if ($bot eq 'Tron 2.01TC') {
			$bot = 'Tron 2.01';
		    }
		}
	    }
	    $scores{$bot} = $score;
	    $scores{$bot . '-summary'} += $score;
	}
	print "Season: " . $season->getAttribute('number') . br();
	my $total_score;
	print '| [[' . $challengerName . ']] ' . $version . ' | [[' . $author . ']] | ';
	foreach my $bot (@bots) {
	    my $score = 100 * $scores{$bot} / ($rounds * 100);
	    print sprintf("%.2f", $score) . ' | '; 
	    $total_score += $score;
	}
	print "'''" . sprintf("%.2f", $total_score / @bots) . "''' | " . br() . "\n";
	push @seasonTotals, $total_score;
    }
    $document->dispose();

    if ($seasons > 1) {
	print br() . 'Average:' . br();
	my $total_score = 0;
	print '| [[' . $challengerName . ']] ' . $version . ' | [[' . $author . ']] | ';
	foreach my $bot (@bots) {
	    my $score = (100 * $scores{$bot . '-summary'} / ($rounds * 100)) / $seasons;
	    print sprintf("%.2f", $score) . ' | '; 
	    $total_score += $score;
	}
	my $sumDiffSq = 0;
	for (my $i = 0; $i < $seasons; $i++) {
	    $sumDiffSq += (($total_score / @bots) - ($seasonTotals[$i] / @bots)) ** 2;
	}
	my $sd = sqrt($sumDiffSq / ($seasons - 1));
	my $confidenceLevel = $sd / sqrt($seasons);
	print "'''" . sprintf("%.2f", $total_score / @bots) . "''' | " . "\n" . br() . br() .
	    $seasons .  " seasons, confidence interval = " . sprintf("%.3f", $confidenceLevel) . br() . "\n";
    }
}

print end_html();

Comments or questions welcome


Robo Home | TargetingChallenge | Changes | Preferences | AllPages
Edit text of this page | View other revisions
Last edited November 27, 2003 13:08 EST by PEZ (diff)
Search: