<?php
/* What game is it, how many daily plays are there? */
$game_name = "HiLo";
$number_of_plays = 20;
/* This function will help sanitize input to prevent errors. */
function sanitizeInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
/* Find when and who! */
$day = date('z');
$username = sanitizeInput($_POST['username']);
/* If the username matches the cookie from login, proceed, if not, throw an error: */
$cookie_name = 'mysusername';
$cookievalue = $_COOKIE[$cookie_name];
if ($cookievalue != $username) {
$warning = "Please do not exploit the system!";
return $warning;
} else {
/* This sets up the database connection. */
include("../../inc/config.php");
$db = new mysqli(DBHOST, DBUSER, DBPASS, DBNAME);
if ($db->connect_error) { die("Database connection failed!"); }
/* Grab this user's info on this game from the database. */
$game_data = "SELECT * FROM adopts_games WHERE `username` = '{$username}' AND `game` = '{$game_name}'";
$result = mysqli_query($db, $game_data);
$game = mysqli_fetch_array($result);
/* If no data is found with the user having ever played before... create some! */
if (!$game) {
$sql = "INSERT INTO `adopts_games`(`plays`, `username`, `game`, `timestamp`) VALUES ('{$number_of_plays}', '{$username}', '{$game_name}', '{$day}')";
if ($db->query($sql) === FALSE) { echo "Error creating new game data: " . $db->error; }
}
/* If a score is being sent through post data, do this. */
if (isset($_POST['amt'])) {
$score = sanitizeInput($_POST['amt']);
// If there are still plays left for today's game...
if ($game['plays'] > 0){
// Add score to user's money.
$sql = "UPDATE adopts_users SET `money` = money + $score WHERE `username` = '{$username}'";
if ($db->query($sql) === TRUE) { echo "Score updated successfully!"; } else { echo "Error updating score: " . $db->error; }
// Reduce the number of plays left available for this game by one & updates the timestamp to reflect current day of the year.
$plays_left = $game['plays'] - 1;
$sql = "UPDATE adopts_games SET `plays` = '{$plays_left}', `timestamp` = '{$day}' WHERE `username` = '{$username}' AND `game` = '{$game_name}'";
if ($db->query($sql) === TRUE) { echo "Game data updated successfully!"; } else { echo "Error updating game data: " . $db->error; }
}
}
if (isset($_POST['plays'])) {
// Check if today matches the timestamp in the database.
if (date('z') != $game['timestamp']){
// If the timestamp is different, reset plays to max and update the timestamp to today.
$sql = "UPDATE adopts_games SET `plays` = '{$number_of_plays}', `timestamp` = '{$day}' WHERE `username` = '{$username}' AND `game` = '{$game_name}'";
if ($db->query($sql) === TRUE) { echo "{$number_of_plays}"; } else { echo "Error updating time stamp: " . $db->error; }
} else {
// If the timestamp is the same, send back the state of the game.
if ($game['plays'] <= 0){ echo "GameOver"; } else { echo $game['plays']; }
}
}
$db->close();
}
?>
Can you confirm for me that /hilo.php, ../view/hiloview.php, and ../games/.htaccess exist for me in their proper directories? If any of these files are missing or out of place it would cause a redirect.
Well... you've modified the base files by changing the number of plays. Did you at least confirm it was working before you started tinkering with it?
$(function() {
checkPlays();
});
var plays = 20;
var score = 0;
var first = 1 + Math.floor(Math.random()*16);
$('.first').html(first);
$('.guess').click(function(){
if (plays >= 0){
var second = 1 + Math.floor(Math.random()*16);
$('.second').html(second);
if ($(this).hasClass('higher')){
if (first <= second){
resultIs('correct', second);
sendScore(150);
} else {
resultIs('incorrect', second);
sendScore(0);
}
}
if ($(this).hasClass('lower')){
if (first >= second){
resultIs('correct', second);
sendScore(150);
} else {
resultIs('incorrect', second);
sendScore(0);
}
}
}
});
function resultIs(result, second){
$('.result').html(result);
$('.result').fadeIn(1000, function(){
$('.first').fadeOut(500);
$('.second').fadeOut(500, function(){
$('.first').html(second);
first = second;
plays = plays - 1;
$('.first').fadeIn(500);
$('.second').html('?');
$('.second').fadeIn(500);
$('.plays').html(plays);
if (result == 'correct'){
score = score + 150;
current = parseFloat(window.parent.$('.money').text());
window.parent.$('.money').fadeTo(100, 0.1);
window.parent.$('.money').text((current + score));
window.parent.$('.money').fadeTo(100, 1);
}
$('.score').html(score);
if (plays <= 0){
disableGame();
}
});
});
$('.result').fadeOut(500);
}
function sendScore(amt) {
var values = {
'username': $("#username").text(),
'amt': amt
};
$.ajax({
url: "sendscore.php",
type: "POST",
data: values,
}).done(function(status){
if (status == "GameOver"){
disableGame();
} else {
$('.plays').html(status);
plays = status;
}
});
}
function disableGame(){
$('.plays').html("0");
$('.guess').off('click');
$('.guess').css( 'cursor', 'not-allowed' );
$('.arrow-box').fadeTo('slow', 0.3);
$('.first').html('game');
$('.second').html('over');
$('.finalscore').html("<b>Plays Left Today:</b> 0 of 20<h2>See You Tomorrow!</h2>");
}
function checkPlays(){
var values = {
'username': $("#username").text(),
'plays': 'check'
};
$.ajax({
url: "sendscore.php",
type: "POST",
data: values,
}).done(function(status){
if (status == "GameOver"){
disableGame();
} else {
$('.plays').html(status);
plays = status;
}
});
}