HTML-TableBracket

 view release on metacpan or  search on metacpan

TableBracket.pm  view on Meta::CPAN

package HTML::TableBracket;

$HTML::TableBracket::VERSION = '0.11';

use strict;
use POSIX qw(ceil floor);

=head1 NAME

HTML::TableBracket - Tournament Table Bracket Generator

=head1 SYNOPSIS

    use HTML::TableBracket;

    # Create the Bracket, list of names in seeded order
    $temp=HTML::TableBracket->new("Jeek", "Tom", "Dick", "Harry",
				  "Larry", "Curly", "Moe");

    # Process the matches (TEAM1 => SCORE1, TEAM2 => SCORE2)
    $temp->match(Larry => 10, Harry => 20);
    $temp->match(Jeek  => 10, Harry => 20);
    $temp->match(Dick  => 20, Curly => 21);
    $temp->match(Moe   => 10, Tom   => 20);

    # For matches that don't have a score, such as chess matches,
    # Use the round method. (WINNER,LOSER)
    $temp->round("Tom","Curly");

    # Display the table in HTML format
    print $temp->as_html;

    # Display the table in XHTML format
    print $temp->as_xhtml;

    # Display the table in .dot format (name of graph as argument)
    print $temp->as_directed_graph_source("Tournament");

    # Display the table as a directed graph (name of graph as argument)
    print $temp->as_directed_graph("Tournament")->as_png;


=head1 DESCRIPTION

This module generates a tournament bracket drawing for standard
single-elimination-style matchups.

=cut

#my (%element, $lastelement, @person, $numofpeople);

sub new {
    my $class = shift; 
    my (%element, $lastelement, @person, $j);
    my $numofpeople = 0;

    foreach my $name (@_) {
	$person[++$numofpeople] = "$numofpeople $name";
    }

    for ($j = 2; $j < 2 * (2 ** ceil(log($numofpeople) / log(2))); $j++) {
        $element{$j} = 0;
    }

    my $row = 0;
    $element{1} = 1;

    for ($j = 2; $j <= $numofpeople;) {
        my $maxrankincurrentrow = 0;
    	my $maxrankaddress = 0;

        for (my $k = (2 ** $row) / 2; $k < (2 ** $row); $k++) {
            $k = 1 if ($k < 1);

	    if ($element{$k}>$maxrankincurrentrow) {
		$maxrankincurrentrow=$element{$k}; $maxrankaddress=$k;
	    }
	}

	if ($maxrankincurrentrow == 0) {
	    $row++;
	} else {
	    if (($maxrankaddress / 2) == (ceil($maxrankaddress / 2))) {
		if (($maxrankaddress / 4) != (floor($maxrankaddress / 4))) {
		    $element{$maxrankaddress * 2} = $maxrankincurrentrow;
		    $element{($maxrankaddress * 2) + 1} = $j;
		} else {
		    if ($maxrankaddress == 2) {
			$element{4} = $j;
			$element{5} = $maxrankincurrentrow;
		    } else {
			$element{$maxrankaddress * 2} = $maxrankincurrentrow;
			$element{($maxrankaddress * 2) + 1} = $j;



( run in 1.073 second using v1.01-cache-2.11-cpan-119454b85a5 )