First Local Commit - After Clean up.

Signed-off-by: Rick Hays <rhays@haysgang.com>
This commit is contained in:
2019-12-02 14:54:38 -06:00
commit 10412ab7f6
486 changed files with 123242 additions and 0 deletions

View File

@@ -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)
{
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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;
}