<!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>Untitled Document</title>
</head>

<body>
<?php
session_start
();



$yourScore[0] = $_POST["Y1"];
$yourScore[1] = $_POST["Y2"];
$yourScore[2] = $_POST["Y3"];
$yourScore[3] = $_POST["Y4"];
$yourScore[4] = $_POST["Y5"];
$enemyScore[0] = $_POST["E1"];
$enemyScore[1] = $_POST["E2"];
$enemyScore[2] = $_POST["E3"];
$enemyScore[3] = $_POST["E4"];
$enemyScore[4] = $_POST["E5"];

$matchWinner NULL;
$matchLoser NULL;

$yourWins 0;
$otherWins 0;


$played $_POST["hiddenTime"];


$enemy $_POST["hiddenEnemy"];


$gameWinner NULL;
$gameWinnerScore NULL;
$gameLoser NULL;
$gameLoserScore NULL;
$winnerRank NULL;
$loserRank NULL;
$j 0;

$_SESSION['error'] = NULL;
$_SESSION['errorChal'] = NULL;

$preConnection = new PDO("pgsql:dbname=natesladder host=cs.georgefox.edu password=natepost00 user=ngeorgeson","ngeorgeson","natepost00");
    
$preResult $preConnection->query("select challenger from challenge where challenger = '".$_SESSION['currentUser']."' OR challengee ='".$_SESSION['currentUser']."' AND accepted is not NULL");
    
$preResult $preResult->fetch(PDO::FETCH_BOTH);
$preResult $preResult[0];

if (
$preResult == NULL)
{
    
$_SESSION['error'] = "Score already recorded!";
    
$_SESSION['defaultPanel'] = 1;
    
header ("Location: userhub.php");
}

else
{

    for (
$i=0$i<5$i++)
    {
        if (
$_SESSION['error'] == NULL && $enemyScore[$i] != NULL && $yourScore[$i] != NULL)
        {
            if (
ctype_digit($enemyScore[$i])==true && ctype_digit($yourScore[$i])==true)
            {
                if ((
$yourScore[$i] == NULL && $enemyScore[$i] == NULL && $j 2) || ($yourScore[$i] != NULL && $enemyScore[$i] != NULL))
                {
                    if (
$yourScore[$i] != $enemyScore[$i] || $j 2)
                    {
                        if (((
$yourScore[$i] == NULL && $enemyScore[$i] == NULL) || $enemyScore[$i] != $yourScore[$i]) || $i <= 2)
                        {
                            if (
$yourScore[$i] >= && $enemyScore[$i] >= 0)
                            {
                                if (
$yourScore[$i] > $enemyScore[$i])
                                {
                                    
$gameWinner $_SESSION['currentUser'];
                                    
$gameLoser $enemy;
                                    
$gameWinnerScore $yourScore[$i];
                                    
$gameLoserScore $enemyScore[$i];
                                    
$yourWins $yourWins 1;
                                    
        
//                            print $gameWinner;
        //                            print $gameLoser;
        //                            print $gameWinnerScore;
        //                            print $gameLoserScore;
                                
}
                                else 
                                {
                                    
$gameWinner $enemy;
                                    
$gameLoser $_SESSION['currentUser'];
                                    
$gameWinnerScore $enemyScore[$i];
                                    
$gameLoserScore $yourScore[$i];
                                    
$otherWins $otherWins 1;
                                    
        
//                            print $gameWinner;
        //                            print $gameLoser;
        //                            print $gameWinnerScore;
        //                            print $gameLoserScore;
                                
}
                                
                                if (
$gameWinnerScore >=15)
                                {
                                    if (
$gameWinnerScore >= $gameLoserScore 2)
                                    {
                                        if ((
$gameWinnerScore 15 && $gameWinnerScore == $gameLoserScore 2) || $gameWinnerScore == 15)
                                        {
                                            
$dataRows[$j] = "INSERT INTO game(winner,loser,played,number,winner_score,loser_score) VALUES('".$gameWinner."', '".$gameLoser."', '".$played."', '".($i+1)."', '".$gameWinnerScore."', '".$gameLoserScore."')";
                                            
$j $j 1;
                                        }
                                        else
                                        {
                                            
$_SESSION['error'] = "Game winner must win by 2 after 15 points!";
                                            
$_SESSION['defaultPanel'] = 1;
                                        }
                                    }
                                    else
                                    {
                                        
$_SESSION['error'] = "Game winner must beat loser by 2 or more!";
                                        
$_SESSION['defaultPanel'] = 1;
                                    }
                                    
                                }
                                else
                                {
                                    
//print $gameWinnerScores;
                                    
$_SESSION['error'] = "Winner score must be 15 or greater!";
                                    
$_SESSION['defaultPanel'] = 1;
                                }
                                
                            }
                            else
                            {
                                
$_SESSION['error'] = "Scores cannot be negative numbers!";
                                
$_SESSION['defaultPanel'] = 1;
                            }
                        }
                        else
                        {
                            
$_SESSION['error'] = "You cannot have a tie2!";
                            
$_SESSION['defaultPanel'] = 1;
                        }
                    }
                    else
                    {
                        
$_SESSION['error'] = "You cannot have a tie!";
                        
$_SESSION['defaultPanel'] = 1;
                    }
                }
                else
                {
                    
$_SESSION['error'] = "You must play at least 3 games and both have scores!";
                    
$_SESSION['defaultPanel'] = 1;
                }
            }
            else
            {
                
$_SESSION['error'] = "Please Input Integers!";
                
$_SESSION['defaultPanel'] = 1;
            }
        }
    }
    
    
$what count($dataRows);
    print 
$what;
    print 
" ";
    
    if (
count($dataRows) >= 3)
    {
        
        if ((
count($dataRows) == && $yourWins == 0) || (count($dataRows) == && $otherWins == 0) || (count($dataRows) == && $otherWins == 1) || (count($dataRows) == && $yourWins == 1) || (count($dataRows) == && $yourWins == 2) || (count($dataRows) == && $otherWins == 2))
        {
    
            if (
$_SESSION['error'] == NULL)
            {
                
$connection = new PDO("pgsql:dbname=natesladder host=cs.georgefox.edu password=natepost00 user=ngeorgeson","ngeorgeson","natepost00");
            
                for (
$i=0$i<5$i++)
                {
                    if (
$dataRows[$i] != NULL)
                    {
                        
$result $connection->query("".$dataRows[$i]."");
                    
                        if (!
$result)
                        {
                            
$_SESSION['error'] = "ERROR recording score!";
                            
$_SESSION['defaultPanel'] = 1;
                            
header ("Location: userhub.php");
                        }
                    }
                }
                
                if (
$yourWins $otherWins)
                {
                    
$matchWinner $_SESSION['currentUser'];
                    
$matchLoser $enemy;
                }
                else
                {
                    
$matchWinner $enemy;
                    
$matchLoser $_SESSION['currentUser'];
                }
                
                
$winnerRank $connection->query("SELECT rank FROM player WHERE username='".$matchWinner."'");
                
$winnerRank $winnerRank->fetch(PDO::FETCH_BOTH);
                
$winnerRank $winnerRank[0];
                
                
$loserRank $connection->query("SELECT rank FROM player WHERE username='".$matchLoser."'");
                
$loserRank $loserRank->fetch(PDO::FETCH_BOTH);
                
$loserRank $loserRank[0];
                
                print 
$winnerRank;
                print 
" ";
                print 
$loserRank;
                print 
" ";
                print 
$yourWins;
                print 
" ";
                print 
$otherWins;
                
                if (
$winnerRank $loserRank)
                {
                
                    
$connection->query("UPDATE player set rank=NULL WHERE username='".$matchWinner."'");
                    
$connection->query("UPDATE player set rank=NULL WHERE username='".$matchLoser."'");
                    
$connection->query("UPDATE player set rank='".$winnerRank."' WHERE username='".$matchLoser."'");
                    
$connection->query("UPDATE player set rank='".$loserRank."' WHERE username='".$matchWinner."'");
                }
                
                
                
//clear challenge
                
$connection->query("DELETE FROM challenge WHERE challenger='".$matchWinner."'");
                
$connection->query("DELETE FROM challenge WHERE challengee='".$matchWinner."'");
                
            
                
                
$_SESSION['defaultPanel'] = 0;
                
header ("Location: userhub.php");
            }
        }
        else
        {
            
$_SESSION['error'] = "Invalid game ratio in best of 5!";
            
$_SESSION['defaultPanel'] = 1;
            
header ("Location: userhub.php");
        }
    }
    else
    {
        
$_SESSION['error'] = "You must play at least 3 games!";
        
$_SESSION['defaultPanel'] = 1;
        
header ("Location: userhub.php");
    }
}

header ("Location: userhub.php");

$connection NULL;

?>
</body>
</html>