#!/usr/bin/perl
#
# $Header: /home/whelan/cvsroot/joetbrw/2002/scripts/ranking_comparison,v 1.6 2002/03/03 8:24:01 whelan Exp $
#

use KRACH;
use Fcntl;
use Time::Local;
require 'common.pl';
require 'modpairwise.pl';

use Getopt::Long;
Getopt::Long::config('auto_abbrev');

GetOptions('i|scores=s' => \$infile, # Score input file (default STDIN)
	   'quick' => \$quickflag, # Only calculate PWCs
	   'refweight=f'=> \$refweight, # Weight for ref games in KRACH
	                                # (default 0)
	   'onetol=f' => \$onetol, # tolerance for one-shot KPct soln
	   'systol=f' => \$systol, # tolerance for KPct system soln
	   'nolink' => \$nlflag, # Suppress links in PWC table(s)
	   'date=i' => \$stopdate, # Only use scores before $stopdate
	   'folder=s' => \$tabsdir, # put tables in this directory
	   ); 

if (defined $onetol) {
    Criterion->tol($onetol);
}
if (defined $systol) {
    Group->tol($systol);
}
unless (defined $stopdate) {
        $stopdate = 30001231;
}


%name=(
Ab => 'Albany', 
AF => 'Air Force', 
Ar => 'Army', 
BC => 'Boston Coll',
Bh => 'Binghamton', 
Bk => 'Bucknell', 
Br => 'Brown', 
Bu => 'Butler', 
Cg => 'Colgate', 
Cn => 'Canisius', 
Cr => 'Cornell', 
Da => 'Dartmouth', 
De => 'Delaware', 
Dk => 'Duke', 
Dr => 'Drexel', 
DU => 'Denver', 
Ff => 'Fairfield', 
Gt => 'Georgetown', 
Ha => 'Harvard', 
Hb => 'Hobart', 
HC => 'Holy Cross', 
Hf => 'Hartford', 
Ho => 'Hofstra', 
JH => 'Johns Hopkins',
Le => 'Lehigh', 
Lf => 'Lafayette', 
Lo => 'Loyola', 
Ma => 'UMass', 
MB => 'UMBC', 
Md => 'Maryland', 
Mh => 'Manhattan', 
Mr => 'Marist', 
MS => 'Mt St Mary\'s', 
Na => 'Navy', 
NC => 'No. Carolina', 
ND => 'Notre Dame', 
OS => 'Ohio State', 
Pn => 'Pennsylvania', 
Pr => 'Princeton', 
PS => 'Penn State', 
Pv => 'Providence', 
Qn => 'Quinnipiac', 
Ru => 'Rutgers', 
SB => 'Stony Brook', 
SH => 'Sacred Heart', 
Si => 'Siena', 
SJ => 'St. Joseph\'s', 
Sy => 'Syracuse',
To => 'Towson', 
Va => 'Virginia', 
Vi => 'Villanova', 
VM => 'VMI',
Vt => 'Vermont', 
Wa => 'Wagner', 
       Ya => 'Yale');

%class=(1=>' class="won"','-1'=>' class="lost"',0=>'','-0'=>'');
	     
%conf_team=(AC=>[qw(Dk Md NC Va)],
	    AE=>[qw(Ab Bh Hf Qn SB Vt)],
	    CA=>[qw(De Dr Ho Lo SH To Vi)],
	    EC=>[qw(Gt Ma MB Na PS Ru)],
	    GW=>[qw(AF Bu DU Ff ND OS)],
	    IL=>[qw(Br Cr Da Ha Pn Pr Ya)],
	    MA=>[qw(Cn Mh Mr MS Pv Si SJ Wa)],
	    PL=>[qw(Ar Bk Cg Hb HC Le Lf)],
	    Indy=>[qw(JH Sy)]);    

@conf=keys %conf_team;

foreach $conf (@conf) {
    foreach (@{$conf_team{$conf}}) {
	$conf{$_}=$conf;
    }
}

%html_name=%name;

while (($abbr,$name)=each %name) {
    new Team(abbr=>$abbr, name=>$name);
}


$date=&initscores($infile);

warn "Caching RPI" if $^W;
foreach $team (Team->team) {
    $team->rpi(.25,.5,.25);
}

@team = Team->team;

$UMBC = Team->team('MB');
    
warn "Sorting by winning percentage" if $^W;

$i=0;
foreach (@teambypct = sort {$b->pct<=>$a->pct 
				or $a->name cmp $b->name} @team) {
    $_->data('Pctrk',++$i);
}
    
warn "Sorting by RPI" if $^W;
    
$i=0;
foreach (@teambyrpi = sort {$b->rpi(.25,.5,.25)<=>$a->rpi(.25,.5,.25)
				or $a->name cmp $b->name} @team) {
    $_->data('RPIrk',++$i);
}

warn "Sorting by Opp Pct" if $^W;

$i=0;
foreach (sort {$b->opppct<=>$a->opppct
		   or $a->name cmp $b->name} @team) {
    $_->data('OppPctrk',++$i);
}

warn "Sorting by Opp Opp Pct" if $^W;

$i=0;
foreach (sort {$b->oppopppct<=>$a->oppopppct
		   or $a->name cmp $b->name} @team) {
    $_->data('OppOppPctrk',++$i);
}

warn "Sorting by KSOS" if $^W;

$i=0;

foreach (sort {$b->data('KSOS')<=>$a->data('KSOS')
		   or $a->name cmp $b->name} @team) {
    $_->data('KSOSrk',++$i);
}

warn "Calculating record vs top 5/10/15" if $^W;

foreach $team (@team) {
    $top5w=$top5l=0;
    foreach (@teambyrpi[0..4]) {
	$top5w += $team->w($_);
	$top5l += $team->l($_);
    }
    $top10w=$top5w;
    $top10l=$top5l;
    foreach (@teambyrpi[5..9]) {
	$top10w += $team->w($_);
	$top10l += $team->l($_);
    }
    $top15w=$top10w;
    $top15l=$top10l;
    foreach (@teambyrpi[10..14]) {
	$top15w += $team->w($_);
	$top15l += $team->l($_);
    }
    foreach (qw(top5w top5l top10w top10l top15w top15l)) {
	$team->data($_,${$_});
    }
    if ($top5w+$top5l) {
	$team->data('top5pct',$top5w/($top5w+$top5l));
    } else {
	$team->data('top5pct','00.5');
    }
    if ($top10w+$top10l) {
	$team->data('top10pct',$top10w/($top10w+$top10l));
    } else {
	$team->data('top10pct','00.5');
    }
    if ($top15w+$top15l) {
	$team->data('top15pct',$top15w/($top15w+$top15l));
    } else {
	$team->data('top15pct','00.5');
    }
}

$i=0;

foreach (sort {$b->data('top5pct')<=>$a->data('top5pct')
		   or ($b->data('top5w')-$b->data('top5l')
		       <=> $a->data('top5w')-$a->data('top5l'))
		   or $a->name cmp $b->name} @team) {
    $_->data('top5rk',++$i);
}

$i=0;

foreach (sort {$b->data('top10pct')<=>$a->data('top10pct')
		   or ($b->data('top10w')-$b->data('top10l')
		       <=> $a->data('top10w')-$a->data('top10l'))
		   or $a->name cmp $b->name} @team) {
    $_->data('top10rk',++$i);
}

$i=0;

foreach (sort {$b->data('top15pct')<=>$a->data('top15pct')
		   or ($b->data('top15w')-$b->data('top15l')
		       <=> $a->data('top15w')-$a->data('top15l'))
		   or $a->name cmp $b->name} @team) {
    $_->data('top15rk',++$i);
}

$yesterday=&today(localtime(timelocal(0,0,0,(1*substr($date,6,2)),
				      substr($date,4,2)-1,
				      substr($date,0,4)-1900)
			    ));

warn 'Printing RPI table' if $^W;
	
sysopen (TABLE, "$tabsdir/newrpi.tab",
	 O_WRONLY|O_TRUNC|O_CREAT) or die $!;
print TABLE '<h2>', ( $stopdate == 30001231 ? 'Current ' : () ),
    'RPI (including games of ',$yesterday,')</h2>

<table border="1">
  <thead>
    <tr>
      <th scope="col" rowspan="2">Team</th>
      <th scope="col" colspan="2">
          <acronym title="Ratings Percentage Index">RPI</acronym>
      </th>
      <th scope="col" colspan="3">Record</th>
      <th scope="col" colspan="2">
          <abbr title="Opponents\' Winning Percentage">Opp&nbsp;Pct</abbr>
      </th>
      <th scope="col" colspan="2">
          <abbr
title="Opponents\' Opponents\' Winning Percentage">Opp&nbsp;Opp&nbsp;Pct</abbr>
      </th>
      <th scope="col" colspan="3">
         vs. top 5
      </th>
      <th scope="col" colspan="3">
         vs. top 10
      </th>
      <th scope="col" colspan="3">
         vs. top 15
      </th>
    </tr>
    <tr>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">Rating</th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">W-L-T</th>
      <th scope="col">Pct</th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">
          <abbr title="Opponents\' Winning Percentage">OPct</abbr>
      </th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">
          <abbr title="Opponents\' Opponents\' Winning Percentage">OOPct</abbr>
      </th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">W-L-T</th>
      <th scope="col">Pct</th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">W-L-T</th>
      <th scope="col">Pct</th>
      <th scope="col"><abbr title="Rank">Rk</abbr></th>
      <th scope="col">W-L-T</th>
      <th scope="col">Pct</th>
    </tr>
  </thead>
  <tbody>
';

foreach $team (@teambyrpi) {
    print TABLE '    <tr';
    if (int(($team->data('RPIrk')-1)/5) % 2) {
	print TABLE ' class="tens';
    }
    else {
	print TABLE ' class="fives';
    }
    print TABLE 'cornell' if $team->abbr eq 'Cr';
    print TABLE '">
      <td scope="row">
          <a target="_blank" href="',$tabsdir,
      '/rpi.',
      $team->abbr,
      '.shtml">',$team->name,'</a></td>
      <td>',$team->data('RPIrk'),'</td>
      <td>',&pct($team->rpi(.25,.5,.25),4),'</td>
      <td class="flip">',$team->data('Pctrk'),'</td>
      <td class="flip">',&wlt($team),'</td>
      <td class="flip">',&pct($team->pct,4),'</td>
      <td>',$team->data('OppPctrk'),'</td>
      <td>',&pct($team->opppct,4),'</td>
      <td class="flip">',$team->data('OppOppPctrk'),'</td>
      <td class="flip">',&pct($team->oppopppct,4),'</td>
      <td>',$team->data('top5rk'),'</td>
      <td>',$team->data('top5w'),'-',$team->data('top5l'),'</td>
      <td>',&pct($team->data('top5pct'),4),'</td>
      <td class="flip">',$team->data('top10rk'),'</td>
      <td class="flip">',$team->data('top10w'),'-',$team->data('top10l'),'</td>
      <td class="flip">',&pct($team->data('top10pct'),4),'</td>
      <td>',$team->data('top15rk'),'</td>
      <td>',$team->data('top15w'),'-',$team->data('top15l'),'</td>
      <td>',&pct($team->data('top15pct'),4),'</td>
';

    print TABLE '    </tr>
';
    sysopen (COMP, "$tabsdir/rpi.".
		 $team->abbr.'.shtml', O_WRONLY|O_TRUNC|O_CREAT) or die $!;
    print COMP '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html lang="en-US"><head><title>Opponents of',
	    $team->name,'</title>
    <link href="../pairwise.css" rel="Stylesheet" type="text/css">
</head>
<body><h1>Opponents of ', $team->name,
		    '</h1>
<table border="1"><thead><tr><th scope="col">Opponent</th><th scope="col">Rk</th><th scope="col">RPI</th><th scope="col">Record</th></tr></thead>
<tfoot><tr><th scope="row" colspan="3">Total</th><td>',
			&wlt($team->w,$team->l,$team->t),'</td></tr>
</tfoot>
<tbody>';
    foreach (sort {$b->rpi(.25,.5,.25) <=> $a->rpi(.25,.5,.25)}
	     $team->opponents) {
	print COMP '<tr';
	if ($_->data('RPIrk') < 6) {
	    print COMP ' class="fives"';
	}
	elsif ($_->data('RPIrk') < 11) {
	    print COMP ' class="tens"';
	}
	elsif ($_->data('RPIrk') < 16) {
	    print COMP ' class="fifteens"';
	}
	print COMP '><td scope="row">',$_->name,'</td><td>',
	$_->data('RPIrk'),'</td><td>',
	&pct($_->rpi(.25,.5,.25)),'</td><td>',
	&wlt($team->w($_),$team->l($_),$team->t($_)),'</td>';
	print COMP '</tr>
';
    }
    print COMP '</tbody></table></body>
</html>
';
    close COMP or die $!;
}
	
	print TABLE '  </tbody>
</table>
    ';

close TABLE or die $!;
