-
Notifications
You must be signed in to change notification settings - Fork 1
/
backup_account.js
184 lines (154 loc) Β· 4.79 KB
/
backup_account.js
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
//document.cookie = "uid="+""+";";
const fs = require('fs')
function setPwdVisible() {
let x = document.getElementById("pwd");
if (x.type === "password") {
x.type = "text";
} else {
x.type = "password";
}
}
function login() {
console.log("Logging in");
if (checkCookieExists("uname")) {
console.log("Logged in already")
return "Logged in already";
}
let uname = document.getElementById("uname");
let pwd = document.getElementById("pwd");
let accounts = getAccounts();
let accObj = accounts.find(account=>account.username===uname)
if(!accObj) return "no account w/ that usname"
if(accObj.password===pwd){
}
for (let i = 0; i < currAccs.length; i++) {
if (currAccs[i][0] === uname) {
if (currAccs[i][1] === pwd) {
document.cookie = "uname="+uname+";";
console.log("Successfully logged in");
return "Successfully logged in. "
} else {
console.log("Wrong username or password")
return "Wrong username or password";
}
}
}
/*
//account doesn't exist yet, create it
const data = uname+","+pwd+",\n";
fs.appendFile('accountData1357908642.csv', data, (err) => {
if (err) throw err;
console.log('Account created');
});
//let file = new File([""], "accountData1357908642.csv");*/
}
function makeAcc() {
console.log("Making account");
if (checkCookieExists("uname")) {
console.log("Logged in already")
return "Logged in already";
}
let uname = document.getElementById("uname");
let pwd = document.getElementById("pwd");
let accounts = getAccounts();
let accObj = accounts.find(account=>account.username===uname)
if(accObj) {
console.log("Account with that username already exists");
return "Account with that username already exists";
}
let newAccounts = new Array(accounts.length+1);
for (let i=0; i<accounts.length; i++) {
newAccounts[i] = accounts[i];
}
newAccounts[-1] = [uname, pwd, []];
writeData(newAccounts);
//let file = new File([""], "accountData1357908642.csv");
}
function addHist(uname, score) {
accounts = getAccounts();
//let accObj = accounts.find(account=>account.username===uname)
//accObj
for (let i=0; i<accounts.length; i++) {
if (accounts[i].username === uname) {
accounts[i].history.push(score);
}
break;
}
writeData(accounts);
}
/*function editData(data) {
if (!checkCookieExists("uname")) {
return;
}
let uname = getCookieValue("uname");
let allAccs = getAccounts();
let success = false;
for (let i=0; i<allAccs.length; i++) {
if (allAccs[i][0] === uname) {
allAccs[i] = data;
success = true;
break;
}
}
if (!success) {
return;
}
}*/
function writeData(data) {
console.log("Writing data into file");
fs.writeFileSync(JSON.stringify(data))
}
/*function getHist() {
if (!checkCookieExists("uname")) {
return;
}
let uname = getCookieValue("uname");
let allAccs = getAccounts();
let index = -1;
for (let i=0; i<allAccs.length; i++) {
if (allAccs[i][0] === uname) {
index = i;
break;
}
}
if (index == -1) {
return;
}
return allAccs[index].slice(2, allAccs[index].length);
}*/
function getAccounts(){
console.log("Getting current accounts");
/*let file = new File([""], "accountData1357908642.csv");
let reader = new FileReader();
reader.readAsText(file);
reader.onload = function(event) {
let csvdata = event.target.result;
let rowData = csvdata.split('\n');
let splitData = new Array(rowData.length);
console.log(csvdata);
//let rowColData = [];
for (let row = 0; row < rowData.length; row++) {
let rowColData = rowData[row].split(',');
splitData[row] = rowColData;
*/
/*for (let col = 0; col < rowColData.length; col++) {
let newCell = newRow.insertCell();
newCell.innerHTML = rowColData[col];
}*/ /*
}
console.log("success");
console.log(splitData);
return splitData;
};*/
//const fs = require("fs")
let raw = fs.readFileSync("accountData.json");
let data = JSON.parse(raw) // []
return data
}
function checkCookieExists(str) {
return document.cookie.split(';').some((item) => item.trim().startsWith(str + '='));
}
function getCookieValue(str) {
return document.cookie.split('; ').find((row) => row.startsWith(str + '='))?.split('=')[1];
}
console.log(getAccounts())