First Local Commit - After Clean up.
Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
@@ -0,0 +1,131 @@
|
||||
/**
|
||||
* Game - Main JS file for game play, user, and file save.
|
||||
*
|
||||
* @date 2019-11-08
|
||||
* @author Rick Hays
|
||||
*
|
||||
*/
|
||||
|
||||
let oUsers = null;
|
||||
let oCurrentUser = null;
|
||||
let userID = null;
|
||||
let userName = '';
|
||||
let userWins = 0;
|
||||
let userLoses = 0;
|
||||
let userLastPlayed = '';
|
||||
let userIP = '';
|
||||
let userCity = '';
|
||||
let userCountry = '';
|
||||
|
||||
// Get current JSON file and import into array
|
||||
$(document).ready(function ()
|
||||
{
|
||||
$.getJSON('data/users.json', function (data)
|
||||
{
|
||||
// Pass data out so it can be used.
|
||||
getUsersData(data.Users);
|
||||
// Start of game screen, determine if there is a cookie or not and display correct screen
|
||||
userID = readCookie('userID');
|
||||
userID !== null ? $("#game").toggleClass('d-none') : $("#getuser").toggleClass('d-none');
|
||||
}, 'json');
|
||||
});
|
||||
|
||||
function getUsersData(data)
|
||||
{
|
||||
oUsers = data;
|
||||
}
|
||||
|
||||
function writeUserData(data)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
function submitUserName()
|
||||
{
|
||||
UName = $("#UserName").val();
|
||||
if (UName === '')
|
||||
{
|
||||
$(".boxerror").html('You must enter a user name to play the game.');
|
||||
if ($(".boxerror").hasClass('d-none')) $(".boxerror").toggleClass('d-none');
|
||||
}
|
||||
else
|
||||
{
|
||||
$(".boxerror").toggleClass('d-none');
|
||||
userID = checkUserName(oUsers, UName);
|
||||
// alert('User ID: ' + userID);
|
||||
if (userID === null)
|
||||
{
|
||||
createUser(UName);
|
||||
}
|
||||
else
|
||||
{
|
||||
// Confirm User before loading else ask for new name
|
||||
|
||||
// Load user info from userID
|
||||
userName = oUsers[userID].UserName;
|
||||
userWins = oUsers[userID].Wins;
|
||||
userLoses = oUsers[userID].Loses;
|
||||
userLastPlayed = oUsers[userID].LastDate;
|
||||
userIP = oIP.ip;
|
||||
userCity = oIP.city;
|
||||
userCountry = oIP.country_code;
|
||||
|
||||
alert('User: ' + userName + ' - City: ' + userCity);
|
||||
// userIP = GET IP ADDRESS
|
||||
// create cookie with userID
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function checkUserName(oUsers, name)
|
||||
{
|
||||
for (let i = 0; i < oUsers.length; i++ )
|
||||
{
|
||||
if (oUsers[i].UserName === name)
|
||||
return i;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param UName
|
||||
* @returns {boolean}
|
||||
*/
|
||||
|
||||
function createUser(UName)
|
||||
{
|
||||
userName = UName;
|
||||
userWins = 0;
|
||||
userLoses = 0;
|
||||
userLastPlayed = getCurrentDate();
|
||||
userIP = oIP.ip;
|
||||
userCity = oIP.city;
|
||||
userCountry = oIP.country_code;
|
||||
|
||||
// Create User Object
|
||||
oCurrentUser =
|
||||
{
|
||||
'UserName': userName,
|
||||
"Wins": userWins,
|
||||
"Loses": userLoses,
|
||||
"LastDate": userLastPlayed,
|
||||
"IP": userIP,
|
||||
"City": userCity,
|
||||
"Country": userCountry
|
||||
};
|
||||
// Add to Users Object
|
||||
oUsers.push(oCurrentUser);
|
||||
|
||||
// Get ID of JSON User Position
|
||||
userID = checkUserName(oUsers, UName);
|
||||
|
||||
// Write JSON file back out.
|
||||
|
||||
// create cookie with userID
|
||||
alert('ID: ' + userID + ' - City: ' + oCurrentUser.City + ' - ' + oUsers[2].City);
|
||||
}
|
||||
|
||||
function updateUser(UName)
|
||||
{
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
/**
|
||||
* createCookie - Creates a cookie with Name->Value pair, and will stay active for x hours
|
||||
*
|
||||
* @date 2019-11-08
|
||||
* @author Rick Hays
|
||||
* @param name
|
||||
* @param value
|
||||
* @param hours
|
||||
*/
|
||||
function createCookie(name, value, hours)
|
||||
{
|
||||
let expires = "";
|
||||
if (hours)
|
||||
{
|
||||
let date = new Date();
|
||||
date.setTime(date.getTime()+(hours*60*60*1000));
|
||||
expires='' + date.toUTCString(); //.toGMTString();
|
||||
}
|
||||
document.cookie = name + '=' + value + '; expires=' + expires + "; path=/";
|
||||
}
|
||||
|
||||
/**
|
||||
* readCookie - reads the value of the name->value pair using the name, returns NULL if not found.
|
||||
*
|
||||
* @date 2019-11-08
|
||||
* @author Rick Hays
|
||||
* @param name
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function readCookie(name)
|
||||
{
|
||||
let nameEQ = name + "=";
|
||||
let ca = document.cookie.split(';');
|
||||
for(let i = 0; i < ca.length; i++)
|
||||
{
|
||||
let c = ca[i];
|
||||
while (c.charAt(0)==' ') c = c.substring(1, c.length);
|
||||
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* eraseCookie - erases cookie with the name->vaule pair.
|
||||
*
|
||||
* @date 2019-11-08
|
||||
* @author Rick Hays
|
||||
* @param name
|
||||
*/
|
||||
function eraseCookie(name)
|
||||
{
|
||||
createCookie(name,"",-1);
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
function getCurrentDate()
|
||||
{
|
||||
let today = new Date();
|
||||
let dd = String(today.getDate()).padStart(2, '0');
|
||||
let mm = String(today.getMonth() + 1).padStart(2, '0'); //January is 0!
|
||||
let yyyy = today.getFullYear();
|
||||
return mm + '/' + dd + '/' + yyyy;
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
let oIP = null;
|
||||
|
||||
$(document).ready(function ()
|
||||
{
|
||||
$.getJSON("https://ip-api.io/api/json", function(data)
|
||||
{
|
||||
getIPData(data);
|
||||
}, 'json');
|
||||
});
|
||||
|
||||
function getIPData(data)
|
||||
{
|
||||
oIP = data;
|
||||
}
|
||||
+7013
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
+4435
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
+7
File diff suppressed because one or more lines are too long
File diff suppressed because one or more lines are too long
@@ -0,0 +1,172 @@
|
||||
/***********************************************************************************************************************
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
+2
File diff suppressed because one or more lines are too long
+10598
File diff suppressed because it is too large
Load Diff
+2
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user