-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
103 lines (85 loc) · 2.66 KB
/
index.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
#!/usr/bin/env node
/*
/$$$$$ /$$$$$$ /$$ /$$ /$$$$$$$ /$$$$$$
|__ $$ /$$__ $$ | $$ /$$/| $$__ $$ /$$__ $$
| $$| $$ \__/ | $$ /$$/ | $$ \ $$| $$ \__/
| $$| $$$$$$ /$$$$$$| $$$$$/ | $$$$$$$ | $$
/$$ | $$ \____ $$|______/| $$ $$ | $$__ $$| $$
| $$ | $$ /$$ \ $$ | $$\ $$ | $$ \ $$| $$ $$
| $$$$$$/| $$$$$$/ | $$ \ $$| $$$$$$$/| $$$$$$/
\______/ \______/ |__/ \__/|_______/ \______/
*/
import chalk from 'chalk';
import inquirer from 'inquirer';
import gradient from 'gradient-string';
import chalkAnimation from 'chalk-animation';
import figlet from 'figlet';
import { createSpinner } from 'nanospinner';
import questions from './questions/javascript.js'
// Randomly select 15 questions from questions array.
const selectedQuestions = questions.sort(() => 0.5 - Math.random()).slice(0, 15);
let playerName;
const sleep = (ms = 2000) => new Promise((r) => setTimeout(r, ms));
async function welcome() {
const rainbowTitle = chalkAnimation.rainbow(
'Who Wants To Win The JavaScript KBC ? \n'
);
await sleep();
rainbowTitle.stop();
console.log(`
${chalk.bgBlueBright('HOW TO PLAY')}
I Am A Process On Your Terminal...
If You Provide Any Wrong Answer , I Will Be ${chalk.bgRed('KILLED ')} 💀
So Try To Give All Correct Answers...
`);
}
async function handleAnswer(isCorrect) {
const spinner = createSpinner('Checking answer...').start();
await sleep();
if (isCorrect) {
spinner.success({ text: `Nice Work ${playerName}. That's A legit Answer` });
} else {
spinner.error({
text: `💀💀💀 Game Over, Unfortunately You Lost The Prize Money . We're Sorry For You , ${playerName}!`,
});
process.exit(1);
}
}
async function askName() {
const answers = await inquirer.prompt({
name: 'player_name',
type: 'input',
message: 'What Is Your Name?',
default() {
return 'Player';
},
});
playerName = answers.player_name;
}
function winner() {
console.clear();
figlet(
`Congrats , ${playerName} !\n Rs. 1 0 , 0 0 0 , 0 0 0`,
(err, data) => {
console.log(gradient.pastel.multiline(data) + '\n');
console.log(
chalk.green(
`Programming Is Not Solely About Your Knowledge; It's About Adding A Touch Of Flair To The Command Line !`
)
);
process.exit(0);
}
);
}
const ask = async () => {
for (const question of selectedQuestions) {
const ans = await inquirer.prompt(question);
await handleAnswer(question.name === ans[question.name]);
}
};
// Run it with top-level await
console.clear();
await welcome();
await askName();
await ask();
winner();