#!/usr/local/bin/perl 
my $INCLUDE_HEADER=($ENV{'DOCUMENT_ROOT'}||"/home/httpd/html");
require "/home/sites/site3/lokad/header1.inc";
print "<head>";
print "<title>Submit gamefinish</title>
</head>
International Axis and Allies Players Association Database</i></H1>
<P><BR><BR>
<H2>Submit game results</H2>
</center>";
use CGI;
$q=new  CGI;
$gameno = $q->param('gameno');
$alias = $q->param('alias');
$playercode = $q->param('playercode');
$qParent = $gameno; 
$Qgt="";
$Qpt="";
if ($gameno eq '' || $playercode eq '' || $alias eq '' ) {
     print("<FORM METHOD=POST ACTION=/cgi-local/axis/confirmgamefinish.cgi><TABLE>");
     print("<TD>Game number <TD><input name=gameno value=\"$gameno\"><TR>"); 
     print("<TD>Your aliasname at IAAPA <TD><input name=alias value=\"$alias\"><TR>"); 
     print("<TD>Password <TD><input name=playercode value=\"$playercode\"><TR>");
     print("<TD> <INPUT TYPE=submit VALUE='Get game'></table></FORM>");}
else
{
$pquery = "select playernumber, emailaddress from players where alias = '$alias' and playercode = $playercode";
die "prepare\n" unless ($pcursor = $dbh->prepare($pquery)); $pcursor->execute;
if (@plrow = $pcursor->fetchrow){
@gmx = split("@",@plrow[1]);
$gm = $alias;
$query="select winner, player1, player2, site, gm, bidwins, start, finish, victype, rank1, rank2,rated, captured, lastturn, bidlose, gametype, playtype, tournament, gameno, postmortem from games where gameno = $gameno and confirmed = 'N' and ( pl2nr = $plrow[0] or gm LIKE '$gm%' )";
#print "$query";
die "prepare\n" unless ($cursor = $dbh->prepare($query)); $cursor->execute;
if (@row = $cursor->fetchrow){
                @Qgame = @row; 

print "<FORM METHOD=post ACTION=/cgi-local/axis/confirm.cgi>

<INPUT TYPE=hidden NAME=gameno VALUE=\"$gameno\">
<INPUT TYPE=hidden NAME=playercode VALUE=\"$playercode\">
<INPUT TYPE=hidden NAME=playernr VALUE=\"@plrow[0]\">
<INPUT TYPE=hidden NAME=gm VALUE=\"$gm\">
<INPUT TYPE=hidden NAME=winner VALUE=\"@row[0]\">
<INPUT TYPE=hidden NAME=site VALUE=\"@row[3]\">
<INPUT TYPE=hidden NAME=type VALUE=\"@row[8]\">

<BR>
<CENTER>
<H3>Please fill in the details of the games below.
</H3>
<p>
<br><br>
<TABLE>
<TD><H4>Game NO</H4>
<TD> <H3>$gameno </H3>
<TD>Winning side
<TD>@Qgame[3]
<TR>
<TD><H4>Player1</H4>
<TD> \"@Qgame[1]\"<input type=hidden name=Player1 value=\"@Qgame[1]\">
<TD><H4>Opponent</H4>
<TD> @Qgame[2]<input type=hidden name=Player2 value=\"@Qgame[2]\"><TR>
<TD><H4>Winner</H4>
<TD>@Qgame[0]<TR>
<TD><H4>Start date (yyyy-mm-dd)</H4>
<TD> @Qgame[6]
<TD><H4>End date (yyyy-mm-dd)</H4>
<TD> @Qgame[7]
<TR>
<TD><H4>Capitals captured</H4>
<TD> @Qgame[12]
<TR>
<TD><H4>End turn</H4>
<TD> @Qgame[13]
<TR>
<TD><H4>Victory type </H4>
<TD>@Qgame[8]
<TR>
<TD>
<TR>
<TD><H4>Techs</H4>
</TABLE>";
print "</center><TABLE>";
$tryit="select * from aatechinfo where tigameno ='$gameno'";
die "prepare\n" unless ($cursor = $dbh->prepare($tryit)); $cursor->execute; 
while(@row = $cursor->fetchrow){
		printf("<TD> %s <TD> %s <TR>",@row[1],@row[2]); 
	} close(MYSQL);
print "</TABLE><BR>IC builds <BR>";
print "<TABLE>";
$tryit="select * from aaic where icgame =$gameno";
$newc="1";
die "prepare\n" unless ($cursor = $dbh->prepare($tryit)); $cursor->execute;
while(@row = $cursor->fetchrow){
        if ($newc eq "1") {print "<TD>@row[1]"; $br=@row[1]; $newc="0";}
	if ($br eq @row[1]) {
		printf("<TD> %s ",@row[2]);}
	else {
		printf("<TR><TD> %s <TD> %s ",@row[1],@row[2]);$br=@row[1];}
	}
print "</TABLE>
<CENTER>
<H3>Comments </H3><p><br>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH=\"60%\" NOSAVE >
<TD>";
#$lf = "$ENV{'DOCUMENT_ROOT'}/clubdb/comments/comments.".$gameno;
#if( open(COMMENTFILE, $lf)){
  print "<p>";
#while (<COMMENTFILE>) { 
   print @Qgame[19]; 
#   }
#close(COMMENTFILE);
#}
print "</table>
<p>
<br>
<INPUT TYPE=submit VALUE=\"Confirm results\">
</FORM>
<A HREF=/cgi-local/axis/players.cgi?playernr=@plrow[0]&playercode=$playercode>
<IMG src=/images/back.gif></A>\n"; }
else
{
    print("Sorry the database does not have your playernumber as either player or gamemaster of this game<p>Please report the results to the Minister of Stats");
    print("<FORM METHOD=POST ACTION=/cgi-local/axis/confirmgamefinish.cgi><TABLE>");
    print("<TD>Game number <TD><input name=gameno value=\"$gameno\"><TR>"); 
    print("<TD>Your aliasname at IAAPA <TD><input name=alias value=\"$alias\"><TR>"); 
    print("<TD>Password <TD><input name=playercode value=\"$playercode\"><TR>");
    print("<TD> <INPUT TYPE=submit VALUE='Get game'></table></FORM>");
print "<BR><BR> <A HREF=/cgi-local/axis/players.cgi?playernr=@plrow[0]&playercode=$playercode><IMG src=/images/back.gif></A>"; }}
else
{    print("Your alias name and your playercode do not match<br><br>");
     print("<FORM METHOD=POST ACTION=/cgi-local/axis/confirmgamefinish.cgi><TABLE>");
     print("<TD>Game number <TD><input name=gameno value=\"$gameno\"><TR>"); 
     print("<TD>Your aliasname at IAAPA <TD><input name=alias value=\"$alias\"><TR>"); 
     print("<TD>Password <TD><input name=playercode value=\"$playercode\"><TR>");
     print("<TD> <INPUT TYPE=submit VALUE='Get game'></table></FORM>");
print "<BR><BR> <A HREF=/cgi-local/axis/players.cgi?playernr=@plrow[0]&playercode=$playercode><IMG src=/images/back.gif></A>"; }
}
