-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.js
204 lines (163 loc) · 6.1 KB
/
app.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
/*
This is the main module that connects and makes calls to all other modules and functions
*/
// Importing all the required libraries.
const clear = require('clear');
const boxen = require('boxen');
const chalk = require('chalk');
const ora = require('ora');
const Spinner = require('cli-spinner').Spinner;
const readLineSync = require('readline-sync');
// Importing all required modules
/*
- welcome module: welcomes the user to the quiz game, and prints instructions
- questions module: contains 50 questions in the form of an array of object
- generate module: generates 10 random questions and returns in the form of an array of object to this module
- quiz module: displays the chosen 10 questions, allows user to enter their answer, checks the answer and also updates score. Returns final score to this module
- highScore module holds the high scores in the form of an array of object
*/
const welcomeModule = require('./welcome.js');
const questionsModule = require('./questions.js');
const generateQuestionsModule = require('./generate.js');
const quizModule = require('./quiz.js');
const highScore = require('./highscores.js');
// Variables to hold user names and whether user wishes to take the quiz
var userName = "";
var beginQuiz = "";
// Variables to hold score, level users passed, number of questions user has attempted and whether user exited the quiz abruptly.
var score = 0;
var numLevels = 0;
var numQuesAttempted = 0;
var finishedAbruptly = false;
start();
// The main function in the module
function start() {
// Calling the welcome(). Prints instructions and welcome message
const welcomeModuleResults = welcomeModule.welcome();
userName = welcomeModuleResults.userName;
beginQuiz = welcomeModuleResults.beginQuiz;
/*/
If user chooses to "start" the quiz, then:
1. Generate questions
2. Call quiz() to display questions, take user answer input, check answer and update score.
3. Save the quiz results and update "score", "numLevels", "numQuesAttempted" and "finishedAbruptly" Variables
4. Call calculateScore() to print the final score card
*/
if(beginQuiz !== 'exit') {
const generateQuestions = generateQuestionsModule.generateQuestions;
const uniqueQues = generateQuestions(questionsModule.questions);
const quiz = quizModule.quiz;
const quizResults = quiz(uniqueQues);
score = quizResults.score;
numQuesAttempted = quizResults.numQuesAttempted;
numLevels = quizResults.numLevels;
finishedAbruptly = quizResults.finishedAbruptly;
calculateScore();
}
}
// Function that prints score card after checking levels and score
function calculateScore() {
var spinner = new Spinner('Generating Your Score Card... %s');
spinner.setSpinnerString("* -–—–- *");
spinner.start();
levelOneBox = {
padding: 2,
margin: 2,
align: 'center',
borderColor: 'red',
backgroundColor: '#333'
};
levelTwoBox = {
padding: 2,
margin: 2,
align: 'center',
borderColor: 'yellow',
backgroundColor: '#333'
};
levelThreeBox = {
padding: 2,
margin: 2,
align: 'center',
borderColor: 'green',
backgroundColor: '#333'
};
exitBox = {
padding: 2,
margin: 2,
align: 'center',
borderColor: '#fff',
backgroundColor: '#000'
}
setTimeout(() => {
spinner.stop(true);
// clear(false);
// If the user ended the quiz abruptly
if(finishedAbruptly === true) {
var text = chalk.bold.bgWhiteBright.blackBright('YOU ENDED THE QUIZ ABRUPTLY');
text += printScoreCard('cyanBright');
console.log(boxen(text, exitBox))
}
// If the user completed all 10 questions in the quiz
else {
var text = chalk.bold.underline.blackBright.bgWhiteBright("SCORE CARD");
// If user was at evel 0 or 1 when the quiz ended
if(numLevels <= 1) {
text += printScoreCard('redBright') + "\n" + chalk.bold.bgRedBright.black('BETTER LUCK NEXT TIME!');
console.log(boxen(text, levelOneBox));
}
// If user was at leve1 2 when the quiz ended
else if(numLevels > 1 && numLevels <=2) {
text += printScoreCard('yellowBright') + "\n" + chalk.bold.bgYellowBright.black('GREAT JOB!');
console.log(boxen(text, levelTwoBox));
}
// If user was at level other than 1 and 2 when the quiz ended
else {
text += printScoreCard('green') + "\n" + chalk.bold.bgGreenBright.black('CONGRATULATIONS. YOU ARE A POTTERHEAD PRO!!');
console.log(boxen(text, levelThreeBox));
}
checkHighScore();
}
}, 2000);
}
function printScoreCard(color) {
var text = chalk.bold `
\nNUMBER OF QUESTIONS ATTEMPTED: {${color} ${numQuesAttempted}}
\nFINAL LEVEL: {${color} ${numLevels}}
\nFINAL SCORE: {${color} ${score}}
`;
return text.toString();
}
// Checks if user reached high score. If yes congratulates the user, and also Prints other high scores
function checkHighScore() {
const highScores = highScore.highScores;
var flag = false;
// Loop thorugh the high data
highScores.forEach((currScore, index) => {
if(score >= currScore.score && flag===false) {
flag = true;
console.log(chalk.bold.underline.magentaBright('\n\nCONGRATULATIONS! YOU ARE A HIGH SCORER!!\n'));
console.log(chalk.bold.yellow('Please take a screenshot of your score card and send it to me!\n\n'));
}
});
// Print high scorers
const box = {
padding: 1,
margin: {bottom: 2, top: 1, left: 0, right: 0},
borderColor: '#333',
backgroundColor: '#f4f4f4',
border: 'classic'
}
var text = chalk.bold.underline.red('HIGH SCORERS');
highScores.forEach((player) => {
text+= chalk.bold`
\n{black NAME:} {red ${player.userName}}
\n{black NUMBER OF LEVELS PASSED:} {red ${player.numLevels}}
\n{black NUMBER OF QUESTIONS ATTEMPTED:} {red ${player.numQuesAttempted}}
\n{black FINAL SCORE:} {red ${player.score}}
\n
`;
});
setTimeout(() => {
console.log(boxen(text, box));
}, 7000);
}