#!/usr/local/bin/perl
#
# tipmaster: footy tips on the web
#
### --------------------------------------------------------------------- ###
# configurable options
### --------------------------------------------------------------------- ###
#
# Script Names: partial paths from ~/public/html
#
$n_ladder = "/cgi-bin/cgiwrap/ben/ladder";
$n_results = "/cgi-bin/cgiwrap/ben/results";
$n_matches = "/cgi-bin/cgiwrap/ben/matches";
$n_tipsters = "/cgi-bin/cgiwrap/ben/tipsters";
$n_tipsters_2007 = "/cgi-bin/cgiwrap/ben/tipsters-2007";
$n_tips = "/cgi-bin/cgiwrap/ben/tips";
$n_ratios = "/cgi-bin/cgiwrap/ben/ratios";
$n_averages = "/cgi-bin/cgiwrap/ben/averages";
# Full paths to tipping commands
#
$home = "/home/ben";
$bin = "$home/bin";
$ladder_cmd = "$bin/ladder";
$results_cmd = "$bin/results";
$matches_cmd = "$bin/matches";
$tipsters_cmd = "$bin/tipsters";
$tips_cmd = "$bin/tips";
$listips = "$bin/listips";
$aver_cmd = "$bin/tipaverage";
$tips_passwd = "/home/ben/footy/web-tipsters.passwd";
$tipsters_db = "/home/ben/footy/tips/tipsters";
%teams = (
'a','Adelaide',
'b','Brisbane',
'c','Carlton',
'd','Fremantle',
'e','Essendon',
'f','Footscray',
'g','Geelong',
'h','Hawthorn',
'k','St. Kilda',
'm','Melbourne',
'n','North Melbourne',
'r','Richmond',
's','Sydney',
'w','Collingwood',
'x','West Coast',
'y','Greater Western Sydney',
'p','Port Adelaide',
'q','Gold Coast',
'D','Draw'
);
### --------------------------------------------------------------------- ###
# end of configurable options
### --------------------------------------------------------------------- ###
#
$http_header = "Content-type: text/html\n\n";
$top = "\n
\nBen's Footy Tips!\n\n
Ben's Footy Tips
\n";
$bottom = "
Back to Footy Tips Home Page
Maintainer: Ben Warden, ben\@unimelb.edu.au
\n
Disclaimer: This page, its contents and style,
are the responsibility of the author and do not necessarily
represent the views, policies or opinions of The University of Melbourne.
\n
\n\n";
$method = $ENV{REQUEST_METHOD};
$name = $ENV{SCRIPT_NAME};
if( $method eq "GET" )
{
if( $name eq $n_ladder )
{
$cmd = "/home/ben/bin/ladder";
$html = "\n" .
" | Team | Played | \n" .
"Won | Lost | Drawn | \n" .
"Points For | Pts Against | Percent | \n" .
"Points | Match Ratio
| \n
\n";
open(TMP,"$cmd |");
while( )
{
chop;
next if /Team/;
next if /^$/;
($rank,$team,$play,$won,$lost,$drawn,$for,$against,$pct,$pts,$mr) =
/(..)\.\ \ (.................)\s*(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+.\d)\s+(\d+)\s+(\d+.\d+)\s*$/;
$html .= "\n" .
"$rank | \n" .
"$team | \n" .
"$play | \n" .
"$won | \n" .
"$lost | \n" .
"$drawn | \n" .
"$for | \n" .
"$against | \n" .
"$pct | \n" .
"$pts | \n" .
"$mr
| \n" .
"
\n";
}
$html .= "
\n";
close(TMP);
$body = "Premiership Ladder
\n$html";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_results )
{
&do_results();
}
elsif( $name eq $n_matches )
{
&do_matches();
}
elsif( $name eq $n_tipsters )
{
$cmd = "/home/ben/bin/tipsters";
$html = "";
open(TMP,"$cmd |");
while( )
{
$html .= "$_";
}
$html .= "
\n";
close(TMP);
$body = "Tipsters Ladder
\n$html";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_tipsters_2007 )
{
$cmd = "/home/ben/bin/tipsters-2007";
$html = "";
open(TMP,"$cmd |");
while( )
{
$html .= "$_";
}
$html .= "
\n";
close(TMP);
$body = "Final Tipsters Ladder - 2007
\n$html";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_tips )
{
$thisweek = `cat $home/footy/tips/thisweek`;
$body = "On-line Tipping Interface
\n";
$body .= "
" .
"\n";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_ratios )
{
$body = "Tipping Ratios
\n";
$body .= "
" .
"\n";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_averages )
{
$body = "Average Tipped
\n";
$body .= "
" .
"\n";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
else
{
$body = "Unknown command: $name
";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
}
elsif( $method eq "POST" )
{
%vars = get_input("POST");
if( $name eq $n_results )
{
$n = $vars{'roundNumber'};
$n =~ s/[^\d]//g;
&do_results($n);
}
elsif( $name eq $n_matches )
{
$n = $vars{'roundNumber'};
$n =~ s/[^\d]//g;
&do_matches($n);
}
elsif( $name eq $n_ratios )
{
$n = $vars{'roundNumber'};
$n =~ s/[^\d]//g;
unless (($n >= 1) && ($n <= 22))
{
$html = "Invalid round number entered.
";
goto PRINT;
}
if( -e "$home/footy/tips/tips.$n" && -w "$home/footy/tips/tips.$n" )
{
$html = "Tipping has not closed for that round yet.
";
goto PRINT;
}
open(TIPS,"$listips $n |");
while( )
{
$count++;
@tips=split();
if( $tips[1] eq "-" )
{
$ratters++;
next;
}
$match1{$tips[1]}++;
$match2{$tips[2]}++;
$match3{$tips[3]}++;
$match4{$tips[4]}++;
$match5{$tips[5]}++;
$match6{$tips[6]}++;
$match7{$tips[7]}++;
$match8{$tips[8]}++;
$match9{$tips[9]}++;
$tipsters++;
}
$html = "";
$html .= "- Match 1:
";
foreach $val (keys(%match1))
{
$pct = sprintf "%3.1f", $match1{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match1{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 2:
";
foreach $val (keys(%match2))
{
$pct = sprintf "%3.1f", $match2{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match2{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 3:
";
foreach $val (keys(%match3))
{
$pct = sprintf "%3.1f", $match3{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match3{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 4:
";
foreach $val (keys(%match4))
{
$pct = sprintf "%3.1f", $match4{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match4{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 5:
";
foreach $val (keys(%match5))
{
$pct = sprintf "%3.1f", $match5{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match5{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 6:
";
foreach $val (keys(%match6))
{
$pct = sprintf "%3.1f", $match6{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match6{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 7:
";
foreach $val (keys(%match7))
{
$pct = sprintf "%3.1f", $match7{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match7{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 8:
";
foreach $val (keys(%match8))
{
$pct = sprintf "%3.1f", $match8{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match8{$val})";
$html .= "- $str
";
}
$html .= "\n- Match 9:
";
foreach $val (keys(%match9))
{
$pct = sprintf "%3.1f", $match9{$val}/$tipsters*100;
$str = sprintf "%-30s","$teams{$val}: $pct% ($match9{$val})";
$html .= "- $str
";
}
$html .= "
";
$pct = sprintf "%d", $ratters/$count*100;
$str = sprintf "\n%s","$ratters tippers ($pct%) didn't enter tips.\n";
$html .= "$str
";
PRINT: $body = "Tipping Ratios for Round $n
\n$html";
$body .= "
" .
"\n";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
elsif( $name eq $n_tips )
{
$n = $vars{'roundNumber'}; $user = $vars{'username'}; $pass = $vars{'password'};
$thisrnd = $vars{'thisround'};
$thisusr = $vars{'thisuser'};
$n =~ s/[^\d]//g; $user =~ s/[^a-zA-Z0-9]//g;
$tiplist = $vars{'tiplist'};
if(!$tiplist)
{
if($vars{match_1})
{
$tiplist = "$vars{match_1}" .
"$vars{match_2}" .
"$vars{match_3}" .
"$vars{match_4}" .
"$vars{match_5}" .
"$vars{match_6}" .
"$vars{match_7}" .
"$vars{match_8}" .
"$vars{match_9}";
}
}
$tiplist =~ s/[^abcdefghkmnpqrswxyD]//g;
$tips = &space_tips($tiplist);
&do_tips($n,$user,$pass,$tips,$thisrnd,$thisusr);
}
elsif ( $name eq $n_averages )
{
$n = $vars{'roundNumber'};
$n =~ s/[^\d]//g;
$body = "Average Number tipped for Round $n
";
open (TMP, "/home/ben/bin/tipaverage $n|");
$body .="";
$body .=;
$body .="
";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
else
{
$body = "Unknown command: $name
";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
}
sub do_results
{
local($round) = pop(@_);
$cmd = "$results_cmd $round";
$html = "\n";
open(TMP,"$cmd |");
while( )
{
($team1,$goals1,$behinds1,$score1,$team2,$goals2,$behinds2,$score2) =
/^(.................)\s*(\d+).\s*(\d+).\s*(\d+)\s*(................)\s*(\d+).\s*(\d+).\s*(\d+)\s*$/;
if( $score1 > $score2 )
{
$html .= "\n" .
"$team1 | $goals1 . $behinds1
| " .
"$score1 | | " .
" beat | | " .
"$team2 | $goals2 . $behinds2
| " .
"$score2
|
";
}
elsif( $score1 < $score2 )
{
$html .= "\n" .
"$team1 | $goals1 . $behinds1
| " .
"$score1 | | " .
" lost to | | " .
"$team2 | $goals2 . $behinds2
| " .
"$score2
|
";
}
else
{
$html .= "\n" .
"$team1 | $goals1 . $behinds1 | " .
"$score1 | | " .
" drew with | | " .
"$team2 | $goals2 . $behinds2 | " .
"$score2
|
";
}
}
$html .= "
\n";
close(TMP);
if( $round eq "") { $body = "Results for last week:
\n$html";}
else { $body = "Results for round $round:
\n$html";}
$body .= "
" .
"\n
";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
sub do_matches
{
local($round) = pop(@_);
$cmd = "$matches_cmd $round";
$html = "\n" .
"No. | Home Team | | Away Team | \n" .
"Location | Day
| \n" .
"
\n";
open(TMP,"$cmd |");
while( )
{
next if /Matches/;
next if /^$/;
($num,$team1,$team2,$location,$day) =
/(.)\.\ \ (.................)vs\ \ (.................)\(([^)]+)\)\ \((...)\)\s+$/;
$html .= "\n" .
"$num. | \n" .
"$team1 | \n" .
" vs | \n" .
"$team2 | \n" .
"$location | \n" .
"$day
| \n" .
"
\n";
}
$html .= "
\n";
close(TMP);
$body = "Matches for next week:
\n$html" if(!$round);
$body = "Matches for round $round:
\n$html" if($round);
$body .= "
" .
"\n
";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
sub do_tips
{
local($round,$user,$pass,$tiplist,$thisrnd,$thisusr) = (@_);
local($passok) = "no";
local($userfound) = "no";
open(PASS,"$tips_passwd");
while( )
{
chop;
($p_user,$p_pass) = split(/:/);
if( $p_user eq $user)
{
$userfound = "yes";
if( $p_pass eq $pass )
{
$passok = "yes";
}
}
}
if( $userfound eq "no" )
{
$body = "Error!
Username $user does not exist.
";
print $http_header, $top, $body, $bottom;
exit(0);
}
else
{
if( $passok eq "no")
{
$body = "Error!
Invalid password for username $user.
";
print $http_header, $top, $body, $bottom;
exit(0);
}
}
$mtiplist = $tiplist;
$mtiplist =~ s/\ //g;
$num = length($mtiplist);
$nteams = `$tips_cmd +$user $round | egrep '^[0-9]' | wc -l | sed -e 's/ *//'`;
$nteams = $nteams * 2 - 1;
# RNS DEBUG
#open(D, ">> /tmp/footy-cgi.log");
#print D scalar localtime(), "\n";
#print D "DEBUG: user = $user\n";
#print D "DEBUG: thisrnd = $thisrnd\n";
#print D "DEBUG: round = $round\n";
#print D "DEBUG: tiplist = $tiplist\n";
#print D "DEBUG: length(tiplist) = ", length($tiplist), "\n";
#print D "DEBUG: nteams = $nteams\n";
#print D "--------------------------------------------------------\n";
#close(D);
if ($tiplist && (length($tiplist) != $nteams) && ($thisrnd == $round)) {
$body =<<_EOT;
Error!
Please enter all tips for the round. You only entered $num
_EOT
print $http_header, $top, $body, $bottom;
exit(0);
}
$file = "/home/ben/footy/tips/tips.$round";
if(! -w $file) {
$html .= "Tips are closed for round $round
";
$cmd = "$tips_cmd +$user $round";
} else {
$cmd = "$tips_cmd +$user $round" if(($thisrnd ne $round) && ($thisusr ne $user));
$cmd = "$tips_cmd +$user $round" if(($thisrnd ne $round) && ($thisusr eq $user));
$cmd = "$tips_cmd +$user $round $tiplist" if(($thisrnd eq $round) && ($thisusr eq $user));
$newtips = "true" if(($thisrnd eq $round) && ($thisusr eq $user));
$cmd = "$tips_cmd +$user $round" if(($thisrnd eq $round) && ($thisusr ne $user));
# Write an audit log entry
$full_name = "UNKNOWN";
open(TIPSTERS, "$tipsters_db");
while () {
@F = split(":", $_);
$full_name = $F[1] if ($F[0] eq $user);
}
close(TIPSTERS);
$tips_log = "/home/ben/footy/web-tips.log.$thisrnd";
open(TIPSLOG, ">> $tips_log");
print TIPSLOG scalar localtime(), ": ", $user, ": [", $full_name, "]", ": ", $tiplist, "\n";
close(TIPSLOG);
}
$html .= "\n";
close(TMP);
if( $notips ne "true" )
{
$body = "You have successfully entered your tips for round $round.
\n" if($newtips eq "true");
$body .= "WARNING: You have tipped a DRAW for one or more matches.\n" if ( $tiplist =~ /D/ );
$body .= "${user}'s tips for round $round:
\n" .
"
Note: If you're using a computer in a public area, be sure to quit" .
" your browser once you've finished.
\n";
print $http_header;
print $top;
print $body;
print $bottom;
exit(0);
}
sub get_input{
$method = pop(@_);
local( $len, $postinput, $param, $value, $item, @INPUT, %INPUT_ARRAY );
if( $method eq "GET")
{
$QUERY = $ENV{QUERY_STRING};
$QUERY =~ s/\+/ /g; # Change +'s to spaces
$QUERY =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
@QUERY_LIST = split( /&/, $QUERY);
foreach $item (@QUERY_LIST)
{
($param, $value) = split( /=/, $item);
$INPUT_ARRAY{$param} .= $value;
}
}
elsif( $method eq "POST")
{
$len = $ENV{CONTENT_LENGTH};
$postinput=;
$postinput =~ s/\n|\r//g;
$postinput =~ s/\+//g;
@INPUT = split( /&/, $postinput);
foreach $item (@INPUT)
{
($param, $value) = split( /=/, $item);
$value =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
$param =~ s/%([\da-f]{1,2})/pack(C,hex($1))/eig;
$INPUT_ARRAY{$param} = $value;
}
}
return (%INPUT_ARRAY);
}
sub space_tips
{
# puts spaces between list of tips
# i.e. abcdefg -> a b c d e f g
local($_) = shift;
s/^(\S)(\S+)/$1 $2/;
1 while s/(\ \S)(\S+)$/$1 $2/;
return $_;
}