-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUserManager.js
More file actions
113 lines (96 loc) · 3.36 KB
/
UserManager.js
File metadata and controls
113 lines (96 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
var session_user;
//Define the user class
class User{
//define the variables of the user
username;
password;
fullname;
email;
bday;
bmonth;
byear;
//initialize the parameters from the dictionary received
constructor( parameters={} ) {
this.username = parameters.username;
this.password = parameters.password;
this.fullname = parameters.fullname;
this.email = parameters.email;
this.bday = parameters.bday;
this.bmonth = parameters.bmonth;
this.byear = parameters.byear;
}
}
//init
$(document).ready(function () {
//store users in the session storage
var pUser = new User({username:'p', password:'p', fullname:'p_p', email:'p_p@gmail.com'});
//default user - p
sessionStorage.setItem(pUser.username, JSON.stringify(pUser));
document.getElementById('front_display').innerHTML = "Please login/register to start the game ";
})
function userManagerRegSubmit() {
//get values from the register fields
let passwordR = $('#psw_reg').val();
let usernameR = $('#uname_reg').val();
let fullnameR = $('#fname_reg').val();
let emailR = $('#email_reg').val();
let bdayR = $('#Bday').val();
let bmonthR = $('#Bmonth').val();
let byearR = $('#Byear').val();
debugger
let newuser = new User({username:usernameR, password:passwordR, fullname:fullnameR, email:emailR,
bday:bdayR, bmonth:bmonthR, byear:byearR});
if(!addUserToStorage(newuser)){
setTimeout(function(){
alert("Username already exist!", function(){
console.log("Callback executed");
});
}, 500);
}else{
transferToLogin(usernameR, passwordR);
}
}
function transferToLogin(username,password){
//if a user with such user name and password does not exist
if(!validateUserExists(username, password)){
setTimeout(function(){
alert("The user doesn't exist in the system!", function(){
console.log("Callback executed");
});
}, 500);
}else{ //does exist, connect him
session_user = JSON.parse(sessionStorage.getItem(username));
$('#tabS').prop('disabled', false); //to enable playing the game
document.getElementById('front_display').innerHTML = "The user " + session_user.username + " is currently logged in.";
document.getElementById("settingsform").reset();
replaceWindow(event, 'settings');
}
}
//add a new user to the user_storage
function addUserToStorage(user){
//so such user in the storage, can add
if (JSON.parse(sessionStorage.getItem(user.username)) == undefined){
sessionStorage.setItem(user.username, JSON.stringify(user));
return true;
}else{ //already exist
return false;
}
}
//find the logined user in the existing users
function userManagerLogin(){
//get values from the login fields
let passwordL = $('#psw_log').val();
let usernameL = $('#uname_log').val();
transferToLogin(usernameL, passwordL);
}
//check if the user exists
function validateUserExists(usernameL, passwordL){
var userStored = JSON.parse(sessionStorage.getItem(usernameL));
//if no such user was found
if (userStored == null){
return false;
}else{ //compare their passwords
let pass = userStored.password;
return pass == passwordL;
}
}