You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

172 lines
4.3 KiB

/***********************************************************************************************************************
RPSLS - Rock, Paper, Scissors, Lizard, Spock Game
@file public/assets/js/game.js
@description Handles all javascript for the game.
@author Rick Hays
@date 11-17-2019
@license MIT
@copyright Copyright © 2019 - Rick Hays, All Rights Reserved.
Revisions:
YYYY-MM-DD XX Description
***********************************************************************************************************************/
/**
* ALL Processes that require the Document to be ready before running.
*/
$(document).ready(function ()
{
/**
* Capture the Enter key when in User Name Form
*/
$("#UserName").keypress(function(e)
{
if (e.which == 13)
{
e.preventDefault(); // Stops Enter Key Action
submitUserName();
}
});
/**
* hideElements - Hides the game play elements from view.
*/
function hideElements()
{
$('#textRock, #textPaper, #textScissors, #textLizard, #textSpock, #textComputerHas, #textComputerPick, #textWinLoseTie, #textTryAgain, #btnTryAgain, #btnNoThanks').each(function ()
{
$(this).hide();
});
}
/**
* TryAgain Button - This is for phone (small) screens, hides game play elements and moves the screen back to the
* top for user to play again.
*/
$("#btnTryAgain").click(function(evt)
{
hideElements();
$('html, body').animate({scrollTop: $(".container").offset().top});
});
/**
* NoThanks Button - Runs endGame from Controller, this removes session and takes you back to home screen.
*/
$("#btnNoThanks").click(function(evt)
{
window.location.href = '/endGame';
});
// Game Play Buttons..
$(".gameButton").click(function(evt)
{
hideElements();
// Get Button Selected
let gameButton = $(this).attr("id");
// AJAX - Play! Pass Option Selected.
jQuery.ajax(
{
url: 'play/' + gameButton,
type: 'get',
dataType: "json",
success: function(data)
{
$('#textComputerPick h2').text(data.ComputerPick);
$("#textWinLoseTie").removeAttr('class');
switch(data.Result)
{
case 'WINNER':
$("#textWinLoseTie").addClass("text-success");
break;
case 'LOSER':
$("#textWinLoseTie").addClass("text-danger");
break;
case 'TIE':
$("#textWinLoseTie").addClass("text-info");
break;
default:
$("#textWinLoseTie").addClass("text-light");
}
$('#textWinLoseTie h2').text(data.Result + ' - ' + data.ResultText);
$('#textRock, #textPaper, #textScissors, #textLizard, #textSpock, #textComputerHas, #textComputerPick, #textWinLoseTie, #textTryAgain, #btnTryAgain, #btnNoThanks').each(function(fadeInDiv)
{
$(this).delay(fadeInDiv * 500).fadeIn(1000);
$('html, body').animate({scrollTop:$(document).height()}, '100');
});
// Scores
$('#totalWins').html(data.Wins);
$('#totalLoses').html(data.Loses);
$('#totalTies').html(data.Ties);
},
error: function(e)
{
console.log(e.lineNumber + ' - ' + e.script + ': ERROR - ' + e.message);
console.trace();
}
});
});
});
/**
* validateUName
* @description
* Simple validation for user name.
*
* @param UName - User Name to validate.
* @returns {boolean} True/False if the name validates to the regex
*
*/
function validateUName(UName)
{
var regex = /^([0-9a-zA-Z-_@\.]*)$/;
return (regex.test(UName)) ? true : false;
}
/**
* submitUserName
* @description
* Handles the form submission.
*
*/
function submitUserName()
{
UName = $("#UserName").val();
if (UName === '')
{
$(".boxerror").html('You must enter a user name to play the game.');
}
else if(!validateUName(UName))
{
$(".boxerror").html('User Name can only contain characters: (a-z, A-Z, 0-9, - _ @ .) Only. ');
}
else
{
// AJAX to see if name exists and confirm before
jQuery.ajax(
{
url: 'testUser/' + UName,
type: 'get',
async: false,
success: function(data)
{
if (data !== '0')
{
let answer = confirm("User Name is already in the Database. \nPress: \nOK - To use this name. \nCANCEL - To pick a new name.");
if (answer == true)
window.location.href = 'createUser/'+UName+'/'+data;
}
else
window.location.href = 'createUser/'+UName+'/'+data;
},
error: function(e)
{
alert('84-game.js: ERROR - ' + e.message);
}
});
}
}