-
Notifications
You must be signed in to change notification settings - Fork 0
/
trivia-logic-module.js
303 lines (258 loc) · 10.2 KB
/
trivia-logic-module.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
let games = []; //load initial games
let users = [] //load initial users require("./users.json");
//let quizzes = {}; // initially no quizzes
// console.log(games);
let nextGameID = -1;
for(qid in games){
if(Number(games[qid].id) >= nextGameID){
nextGameID = Number(games[qid].id) + 1;
}
}
//let nextGameID = 0; //don't need initialization, because we are assuming no quizzes to start
//Helper function to verify a user object exists, has a username and that a user with that name exists
function isValidUser(userObj){
if(!userObj){
return false;
}
if(!userObj.username || !users.hasOwnProperty(userObj.username)){
return false;
}
return true;
}
function createUser(newUser){
//Check the object is valid
//This just ensures the object has a username and password
//You may have more complex logic for your project
if(!newUser.username || !newUser.password){
return null;
}
if(users.hasOwnProperty(newUser.username)){
//There is a user with that name already
return null;
}
//Set initial values for the new user
//newUser.questions_created = [];
newUser.gameHistory = []
//newUser.friends = [];
users.push(newUser);
//users[newUser.username] = newUser;
return users[newUser.username];
}
/*
Allows somebody to search all users in the system and get an array of users they can see. We are assuming a user will be logged in to do this. We will assume a requesting user can access themself and any of their friends. Other users should not be returned in the result.
Inputs:
requestingUser - the object representing the user making the request (we use this to decide whether the request should be successful or not)
searchTerm - a string the user is searching for
Outputs:
An array of users that match the search term and are accessible by the requesting user.
*/
function searchUsers(requestingUser, searchTerm){
let results = [];
//If the user is not valid, return an empty array.
//You could return null to indicate an error or any other value to signify the requesting user was not valid.
if(!isValidUser(requestingUser)){
return results;
}
//If users was an array, you could use a nice one line filter function call
for(username in users){
let user = users[username];
//If this user matches the search term
if(user.username.toLowerCase().indexOf(searchTerm.toLowerCase()) >= 0){
//If the requesting user is allowed to access the matching user
if(user.username === requestingUser.username || requestingUser.friends.includes(user.username)){
results.push(user);
}
}
}
return results;
}
/*
Function to make friends. This is a helper function for simplicity in this case.
In your project system, you may have a function for proposing friendships, checking friend requests, and accepting. You would also want a requestingUser input parameter (a user should only be able to update their own friend data)
The function will add each user to the other's friend list, if they aren't already friends
Inputs:
userA/userB - two user IDs that you want to make friends
Outputs:
Nothing.
*/
function makeFriends(userA, userB){
//If one of the user IDs doesn't exist, stop
let user, user2;
users.forEach(value => {
if(value.username == userA) {
user = value;
}
if(value.username == userB) {
user2 = value;
}
});
if(!user || !user2) {
return;
}
let indexUser1 = users.findIndex(i => i.username === user.username);
let indexUser2 = users.findIndex(i => i.username === user2.username);
//If the users are already friends, stop
if(users[indexUser1].friends.includes(user2)){
return;
}
//Update both so they are now friends
users[indexUser1].friends.push(users[indexUser2]);
users[indexUser2].friends.push(users[indexUser1]);
if(users[indexUser1].FriendRequests.includes(user2)) {
let indexFriend1 = users[indexUser1].FriendRequests.findIndex(i => i === user2);
users[indexUser1].FriendRequests.splice(indexFriend1,1);
}
else if(users[indexUser2].FriendRequests.includes(user)) {
let indexFriend2 = users[indexUser2].FriendRequests.findIndex(i => i === user);
users[indexUser2].FriendRequests.splice(indexFriend2,1);
}
}
function deleteFriends(userA, userB) {
//If one of the user IDs doesn't exist, stop
let user, user2;
users.forEach(value => {
if(value.username == userA) {
user = value;
}
if(value.username == userB) {
user2 = value;
}
});
if(!user || !user2) {
return;
}
let indexUser1 = users.findIndex(i => i.username === user.username);
let indexUser2 = users.findIndex(i => i.username === user2.username);
//If the users are already friends, stop
if(users[indexUser1].friends.includes(user2)){
//this means they are friends, we need to remove from both lists
let indexFriend1 = users[indexUser1].friends.findIndex(i => i === user2);
let indexFriend2 = users[indexUser2].friends.findIndex(i => i === user);
users[indexUser1].friends.splice(indexFriend1,1);
users[indexUser2].friends.splice(indexFriend2,1);
}
}
function colorMatchCheck(one, two, three, four){
return (one === two && one === three && one === four && one !== 0 && one !== undefined);
}
function horizontal(arr){
for (let row = 0; row < 6; row++){
for (let col =0; col < 4; col++){
//console.log(arr[row][col] + " " + arr[row][col+1] + " " + arr[row][col+2] + " " + arr[row][col+3])
if (colorMatchCheck(arr[row][col],arr[row][col+1], arr[row][col+2], arr[row][col+3])){
return true;
}
}
}
}
function vertical(arr){
for (let col = 0; col < 6; col++){
for (let row = 0; row < 3; row++){
//console.log(arr[row][col] + " " + arr[row+1][col] + " " + arr[row+2][col] + " " + arr[row+3][col])
if (colorMatchCheck(arr[row][col], arr[row+1][col], arr[row+2][col], arr[row+3][col])){
return true;
};
}
}
}
function diagonal(arr){
for(let col = 0; col < 4; col++){
for (let row = 0; row < 3; row++){
// console.log(arr[row][col] + " " + arr[row+1][col+1] + " " + arr[row+2][col+2] + " " + arr[row+3][col+3])
if (colorMatchCheck(arr[row][col], arr[row+1][col+1], arr[row+2][col+2], arr[row+3][col+3])){
return true;
}
}
}
}
function diagonal2(arr){
for(let col = 0; col < 4; col++){
for (let row = 5; row > 2; row--){
//console.log(arr[row][col] + " " + arr[row-1][col+1] + " " + arr[row-2][col+2] + " " + arr[row-3][col+3])
if (colorMatchCheck(arr[row][col], arr[row-1][col+1],arr[row-2][col+2],arr[row-3][col+3])){
return true;
}
}
}
}
function drawCheck(arr){
let fullCell = []
for (let i=0; i < arr.length; i++){
for(let j=0;j<7;j++) {
//console.log(arr[i][j])
if(arr[i][j]!=0) {
fullCell.push(arr[i][j])
}
}
}
if (fullCell.length === 42){
return true;
}
}
function winCheck(board) {
if(horizontal(board) || vertical(board) || diagonal2(board) || diagonal(board)) {
return true;
}
}
const assert = require("assert");
let userA = createUser({username: "kowalski", password: "kowalski", profilePic:"https://i.gyazo.com/54ddd66424b6efe51e8acc1d6af67a8c.png",
email:"[email protected]", GamesWon:5, GamesLost:1,FriendRequests:[], friends:[], isPublic:true, isOnline:false,gameRequests:[]});
let userB = createUser({username: "private", password:"private", profilePic:"https://i.ytimg.com/vi/P0hOTR509NQ/maxresdefault.jpg",
email:"[email protected]", GamesWon:4, GamesLost:2,FriendRequests:[], friends:[], isPublic:true, isOnline:false,gameRequests:[]});
let userC = createUser({username: "skipper", password:"skipper", profilePic:"https://i.gyazo.com/6b8505ff99d12178069a671ffbda5cf9.png",
email:"[email protected]", GamesWon:3, GamesLost:3,FriendRequests:[], friends:[], isPublic:true, isOnline:false,gameRequests:[]});
let userD = createUser({username: "rico", password: "rico", profilePic:"https://i.gyazo.com/b2a2fde177e31393df51cc6269d2c7e2.png",
email:"[email protected]", GamesWon:2, GamesLost:4,FriendRequests:[], friends:[], isPublic:true, isOnline:false,gameRequests:[]});
let userE = createUser({username: "jim", password: "jim", profilePic:"https://i2.wp.com/www.sporcle.com/blog/wp-content/uploads/2020/04/4-2.jpg?resize=1280%2C720&ssl=1",
email:"[email protected]", GamesWon:1, GamesLost:5,FriendRequests:[], friends:[], isPublic:false, isOnline:false,gameRequests:[]});
let userF = createUser({username: "ariane", password: "ariane", profilePic:"https://3er1viui9wo30pkxh1v2nh4w-wpengine.netdna-ssl.com/wp-content/uploads/prod/sites/45/2019/11/MS_Penguin-Counting-Story_1900x800.jpg",
email:"[email protected]", GamesWon:1, GamesLost:5,FriendRequests:[], friends:[], isPublic:false, isOnline:false,gameRequests:[]});
let gameA = {gameID:1,isPublic:true,isFO:false,players:[users[0].username,users[1].username],currentTurn:1,isGameDone:false,winner:null,gameBoard:[
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[0,0,0,0,0,0,0],
[2,2,0,1,1,1,2],
[1,2,2,1,2,2,2]
]}
let gameB = {gameID:2,isPublic:true,isFO:false,players:[users[0].username,users[1].username],currentTurn:2,isGameDone:false,winner:null,gameBoard:[
[1,2,1,2,0,1,1],
[1,2,1,1,0,2,1],
[2,1,2,2,1,2,2],
[1,2,1,2,1,2,1],
[2,1,2,2,1,1,2],
[1,2,2,1,2,2,1]
]}
let gameC = {gameID:3,isPublic:true,isFO:false,players:[users[0].username,users[1].username],currentTurn:1,isGameDone:true,winner:users[0].username,gameBoard:[
[1,1,0,0,0,0,0],
[1,1,0,0,0,0,0],
[1,2,0,0,0,0,0],
[2,2,0,0,0,0,0],
[2,1,0,2,1,1,2],
[1,2,2,1,2,2,2]
]}
//users.push(userA,userB,userC,userD,userE)
games.push(gameA,gameB,gameC)
makeFriends("kowalski", "private");
makeFriends("kowalski", "skipper");
// makeFriends("kowalski", "jim");
makeFriends("kowalski", "rico");
makeFriends("kowalski", "jim");
makeFriends("jim", "ariane");
makeFriends("rico","private");
makeFriends("rico","skipper");
users[0].FriendRequests.push(users[5])
deleteFriends("kowalski","private");
makeFriends("kowalski","private")
//console.log("kowalski friends: ")
//console.log(users["kowalski"].friends)
module.exports = {
users,
games,
winCheck,
drawCheck,
createUser,
searchUsers,
deleteFriends,
makeFriends
}