<?php

# Include files required for the site to work properly.
require_once "config.php";
require_once "functions.php";
# Set the timezone for the date() function
date_default_timezone_set("America/Los_Angeles");
# Retrieve the information for the challenge to accept.
$challenger = cleanInputs($_POST['challenger'], $connection);
$challengee = cleanInputs($_POST['challengee'], $connection);
$challengeDate = $_POST['time'];
$acceptDate = date("Y-m-d");
# Add an accepted date to the challenge to accept.
$acceptSuccess = mysqli_query($connection, "UPDATE challenge SET accepted = '{$acceptDate}' WHERE \n         challenger = '{$challenger}' AND \n         challengee = '{$challengee}' AND \n         scheduled = '{$challengeDate}'") or die(mysqli_error($connection));
# If the update to accept the selected challenge was successful, remove all
# other challenges for the challengee from the database.
if ($acceptSuccess) {
    # Remove other challenges for the challengee and challengerfrom the database.
    $acceptSuccess = mysqli_query($connection, "DELETE FROM challenge WHERE \n            (challengee = '{$challengee}' OR \n            challengee = '{$challenger}' OR\n            challenger = '{$challengee}' OR\n            challenger = '{$challenger}') AND \n            accepted IS NULL") or die(mysqli_error($connection));
}
?>

<!DOCTYPE html>
<html>
<head>
   <title>Wrath of Titans - Accept Challenge</title>
   
   <!-- CSS Inclusion -->
      <link href="../../styles/reset.css" rel="stylesheet" type="text/css" />
      <link href="../../styles/main.css" rel="stylesheet" type="text/css" />
   <!-- /End CSS Inclusion -->
<?php

# Include files required for the site to work properly.
require_once "config.php";
require_once "functions.php";
# Set the timezone for the date() function
date_default_timezone_set("America/Los_Angeles");
# Retrieve the input and clean it for database insertion.
$challenger = cleanInputs($_POST['challenger'], $connection);
$challengee = cleanInputs($_POST['challengee'], $connection);
$issueDate = date("Y-m-d");
# Convert the times received to a timestamp for insertion into the database.
$scheduleDate = cleanInputs($_POST['date'], $connection);
$scheduleTime = cleanInputs($_POST['time'], $connection);
$challengeDate = $scheduleDate . " " . $scheduleTime;
# Before attempting to insert the challenge, perform one final
# check to make sure that the given date is valid.
$toCheck = split("-", $scheduleDate);
$validDate = checkDate($toCheck[1], $toCheck[2], $toCheck[0]);
if ($validDate) {
    # Insert the new challenge into the database.
    $challengeSuccess = mysqli_query($connection, "INSERT INTO challenge (challenger, challengee, issued, scheduled) \n            VALUES ('{$challenger}', '{$challengee}', '{$issueDate}', '{$challengeDate}')");
} else {
    $challengeSuccess = false;
}
?>

<!DOCTYPE html>
<html>
<head>
   <title>Wrath of Titans - Issue Challenge</title>
<?php

# Include files required for the site to work properly.
require_once "config.php";
require_once "functions.php";
# Set a flag to keep track of whether registration was successful, this
# allows for the display of an appopriate message later.
$registerSuccess = true;
# Retreive the user input and clean it for database insertion
$username = cleanInputs($_POST['username'], $connection);
$playerName = cleanInputs($_POST['name'], $connection);
$phone = cleanInputs($_POST['phone'], $connection);
$email = cleanInputs($_POST['email'], $connection);
$password = cleanInputs($_POST['password'], $connection);
# Hash the password for storage in the database.
$hashedPassword = hashPass($username, $password);
# Determine whether there are accounts that use the same username or
# email address.
$sameUsername = mysqli_num_rows(mysqli_query($connection, "SELECT * FROM player WHERE username = '******'")) != 0;
$sameEmail = mysqli_num_rows(mysqli_query($connection, "SELECT * FROM player WHERE email = '{$email}'")) != 0;
# If no accounts were found using the same username or eamil address,
# continue with the registration process.
if (!$sameUsername && !$sameEmail) {
    # Determine what the new account's initial rank should be. This equals
    # the lowest rank in the ladder plus 1.
    $getRanks = mysqli_query($connection, "SELECT MAX(rank) FROM player");
    $maxRank = mysqli_fetch_array($getRanks);
    $maxRank = $maxRank[0];
    $newRank = $maxRank + 1;
    # Insert the new user into the database, using the boolean return result
    # from the query to determine whether registration of the account was