#!/project/perl/perls/perl-5.20.2/bin/perl


use CGI;


@bai = ("", 300, 200, 100);


my $cgi = CGI->new;

&print_header();

$id    = $cgi->param('id');
$grade = $cgi->param('g');
$race  = $cgi->param('race');

print "<font size=5>$race (G$grade)</font><br><br>\n";

open(IN, "OZZU/$id");

$_ = <IN>;
chomp;
($w1, $w2, $w3) = split;
@w3 = split(',', $w3) if ($w3 =~ /,/);

chomp($tan = <IN>);

$_=<IN>;
chomp;
($f1, $f2, $f3, $f4) = split;

chomp($umaren = <IN>);

$_=<IN>;
chomp;
($wide1, $wide2, $wide3, $wide4, $wide5) = split;

close(IN);

$umatan = $umaren * $tan;
if ($f3) {
    if ($f4) {
        $sanrentan2 = $umaren * $tan * $f4;
    }
    $sanrentan = $umaren * $tan * $f3;
}
else {
    $sanrentan = $umaren * $tan * $tan;
}

if ($f3) {
    if ($f4) {
        $sanrenfuku2 = $umaren * $f4;
    }
    $sanrenfuku = $umaren * $f3;
}
else {
    $sanrenfuku = $umaren * $tan;
}

my $rowstr = '';
if ($#w3 == 1) {
    $f3str = "$f3倍, $f4倍";
    $sanrentanstr = "$sanrentan倍, $sanrentan2倍";
    $sanrenfukustr = "$sanrenfuku倍, $sanrenfuku2倍";
    $rownum = 5;
    $rowstr = "<tr><td align = right>$wide4倍</tr>\n<tr><td align = right>$wide5倍</tr>\n"
}
else {
    $f3str = "$f3倍";
    $sanrentanstr = "$sanrentan倍";
    $sanrenfukustr = "$sanrenfuku倍";
    $rownum = 3;
}

print << "EOF";
<table class="default">
<tr><th>１着</th><td>$w1</tr>
<tr><th>２着</th><td>$w2</tr>
<tr><th>３着</th><td>$w3</tr>
</table>

<br>

<table class="default">
<tr><th>単勝<td align = right>$tan倍</tr>
<tr><th rowspan=3 valign = top>複勝</th><td align = right>$f1倍</tr>
<tr><td align = right>$f2倍</tr>
<tr><td align = right>$f3str</tr>
<tr><th>連単<td align = right>$umatan倍</tr>
<tr><th>連複<td align = right>$umaren倍</tr>
<tr><th>３連単<td align = right>$sanrentanstr</tr>
<tr><th>３連複<td align = right>$sanrenfukustr</tr>
<tr><th rowspan = $rownum>ワイド<td align = right>$wide1倍</tr>
<tr><td align = right>$wide2倍</tr>
<tr><td align = right>$wide3倍</tr>
$rowstr
</table>

<hr>

EOF

print "<table class='default'><th>予想者名<th>◎<th>○<th>▲<th>単勝<th>複勝<th>連単<th>連複<th>３連単<th>３連複<th>ワイド<th>合計\n";
open(IN, "yosou/$id");
while (<IN>)
{
    chomp;
    ($name, $y1, $y2, $y3) = split;

    for $i (0..5)    {
        $get[$i] = 0;
    }

    $get[0] = $tan * $bai[$grade] if $w1 eq $y1;

    $get[1] = $f1 * $bai[$grade] if $w1 eq $y1;
    $get[1] = $f2 * $bai[$grade] if $w2 eq $y1;
    $get[1] = $f3 * $bai[$grade] if $w3 eq $y1;
    if ($#w3 == 1) {
        $get[1] = $f3 * $bai[$grade] if $w3[0] eq $y1;
        $get[1] = $f4 * $bai[$grade] if $w3[1] eq $y1;
    }

    $get[2] = $umatan * $bai[$grade] if ($w1 eq $y1 && ($w2 eq $y2 || $w2 eq $y3));
    
    $get[3] = $umaren * $bai[$grade] if ($get[2] ||
                                ($w1 eq $y2 && $w2 eq $y1) ||
                                ($w1 eq $y2 && $w2 eq $y3) ||
                                ($w1 eq $y3 && $w2 eq $y1) ||
                                ($w1 eq $y3 && $w2 eq $y2));

    if ($#w3 == 1) {
        print_doutyaku();
    }
    else {
        print_normal();
    }

    print "<td align = right>$total</tr>\n";
}
close(IN);

print "</table>\n";

print << "EOF";

</center>
</BODY>
</html>

EOF


sub print_header
{
    print << "EOF";
Content-type: text/html

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<TITLE>結果！ - 辺境の予想ページ</TITLE>
<link rel="stylesheet" type="text/css" href="/css/style.css" />
</head>

EOF

}


sub print_normal
{
    $get[4] = int($sanrentan * $bai[$grade]) if ($w1 eq $y1 && $w2 eq $y2 && $w3 eq $y3);

    $get[5] = $sanrenfuku * $bai[$grade] if (($w1 eq $y1 || $w1 eq $y2 || $w1 eq $y3) &&
                                    ($w2 eq $y1 || $w2 eq $y2 || $w2 eq $y3) &&
                                    ($w3 eq $y1 || $w3 eq $y2 || $w3 eq $y3));

    print "<tr><td nowrap>$name<td nowrap>$y1<td nowrap>$y2<td nowrap>$y3\n";

    $total = 0;
    for $i (0..5)
    {
        $total += $get[$i];
        print "<td align = right>$get[$i]\n";
    }
    
    if ($wide1 eq "-")
    {
        print "<td align = center>-</td>\n";
    }
    else
    {
        $get_wide = 0;
        
        $get_wide = $wide1 * $bai[$grade] if ($w1 eq $y1 && $w2 eq $y2) || ($w1 eq $y2 && $w2 eq $y1);
        $get_wide = $wide2 * $bai[$grade] if ($w1 eq $y1 && $w3 eq $y2) || ($w1 eq $y2 && $w3 eq $y1);
        $get_wide = $wide3 * $bai[$grade] if ($w2 eq $y1 && $w3 eq $y2) || ($w2 eq $y2 && $w3 eq $y1);

        print "<td align = center>$get_wide</td>\n";

        $total += $get_wide;
    }

}


sub print_doutyaku
{
    $get[4] = $sanrentan * $bai[$grade] if ($w1 eq $y1 && $w2 eq $y2 && $w3[0] eq $y3);
    $get[4] = $sanrentan2 * $bai[$grade] if ($w1 eq $y1 && $w2 eq $y2 && $w3[1] eq $y3);

    $get[5] = $sanrenfuku * $bai[$grade] if (($w1 eq $y1 || $w1 eq $y2 || $w1 eq $y3) &&
                                    ($w2 eq $y1 || $w2 eq $y2 || $w2 eq $y3) &&
                                    ($w3[0] eq $y1 || $w3[0] eq $y2 || $w3[0] eq $y3));
    $get[5] = $sanrenfuku2 * $bai[$grade] if (($w1 eq $y1 || $w1 eq $y2 || $w1 eq $y3) &&
                                    ($w2 eq $y1 || $w2 eq $y2 || $w2 eq $y3) &&
                                    ($w3[1] eq $y1 || $w3[1] eq $y2 || $w3[1] eq $y3));

    print "<tr><td nowrap>$name<td nowrap>$y1<td nowrap>$y2<td nowrap>$y3\n";

    $total = 0;
    for $i (0..5)
    {
        $total += $get[$i];
        print "<td align = right>$get[$i]\n";
    }
    
    if ($wide1 eq "-")
    {
        print "<td align = center>-</td>\n";
    }
    else
    {
        $get_wide = 0;
        
        $get_wide = $wide1 * $bai[$grade] if ($w1 eq $y1 && $w2 eq $y2) || ($w1 eq $y2 && $w2 eq $y1);
        $get_wide = $wide2 * $bai[$grade] if ($w1 eq $y1 && $w3[0] eq $y2) || ($w1 eq $y2 && $w3[1] eq $y1);
        $get_wide = $wide3 * $bai[$grade] if ($w1 eq $y1 && $w3[1] eq $y2) || ($w1 eq $y2 && $w3[1] eq $y1);
        $get_wide = $wide4 * $bai[$grade] if ($w2 eq $y1 && $w3[0] eq $y2) || ($w2 eq $y2 && $w3[0] eq $y1);
        $get_wide = $wide5 * $bai[$grade] if ($w2 eq $y1 && $w3[1] eq $y2) || ($w2 eq $y2 && $w3[1] eq $y1);

        print "<td align = center>$get_wide</td>\n";

        $total += $get_wide;
    }

}
