-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay.js
168 lines (136 loc) · 4.32 KB
/
play.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
const chalk = require('chalk');
const inquirer = require('inquirer');
const figlet = require('figlet');
// allow user to pass in episode as an argument
const episodeNumber = process.argv[2] | '1'
const boardData = require(`./episode-data/season-35/episode-${episodeNumber}.json`);
const { categories, clues } = boardData;
let score = 0;
// a list of answered clue ids to prevent selecting a clue twice
let answeredClueIds = [];
function beginGame() {
const greeting = `${chalk.white('This is...')}\n` + figlet.textSync('JEOPARDY', {
horizontalLayout: 'full'
});
console.log(greeting);
promptCategorySelection();
}
function promptCategorySelection() {
// reformat the categories with properties expected by inquirer
const remainingCateogries = getRemainingCategories(categories, clues);
if (remainingCateogries.length === 0) {
handleGameComplete();
return;
}
const categoryInputs = remainingCateogries.map(category => {
return {
name: category.name,
value: category.id
}
});
const questions = [{
name: 'selectedCategoryId',
type: 'list',
choices: categoryInputs,
message: 'Please select a category'
}];
inquirer
.prompt(questions)
.then(handleCategoryPrompt)
.catch(handleError);
}
function getRemainingCategories(categories, clues) {
// only return categories that have clues remaining
// get a list of remaining category ids then filter categories that match those ids
clues = clues.filter(clue => !answeredClueIds.includes(clue.id));
const remainingCategoryIds = clues.reduce((accClueIds, clue) => {
return accClueIds.concat(clue.categoryId);
}, []);
return categories.filter(category => remainingCategoryIds.includes(category.id));
}
function handleCategoryPrompt(answer) {
const selectedId = answer.selectedCategoryId;
const categoryClues = clues.filter(clue => clue.categoryId === selectedId);
promptForDollarAmount(categoryClues);
}
function promptForDollarAmount(clues) {
// remove options that have already been answered
clues = clues.filter(clue => !answeredClueIds.includes(clue.id));
const availableAmounts = clues.map(clue => {
return {
name: `$${clue.value}`,
value: clue.value
}
});
const questions = [{
name: 'selectedAmount',
type: 'list',
choices: availableAmounts,
message: 'For how much money?'
}];
inquirer
.prompt(questions)
.then(handleAmountSelection.bind(this, clues))
.catch(handleError);
}
function handleAmountSelection(clues, answer) {
const { selectedAmount } = answer;
const selectedClue = clues.find(clue => clue.value === selectedAmount);
promptQuestion(selectedClue);
}
function promptQuestion(clue) {
const cluePrompt = {
name: 'response',
type: 'input',
message: clue.clueText,
};
inquirer
.prompt(cluePrompt)
.then(handleClueResponse.bind(this, clue))
.catch(handleError);
}
function handleClueResponse(clue, answer) {
const { value, id, correctResponse } = clue;
if (responseIsCorrect(answer.response, correctResponse)) {
handleCorrectResponse(value);
} else {
handleIncorrectResponse(value);
}
answeredClueIds.push(id);
console.log(`Correct response: ${clue.correctResponse}`);
console.log(`Score: $${score}`);
setTimeout(promptCategorySelection, 3000);
}
function responseIsCorrect(response, correctResponse) {
// if the user's answer matches any part of the answer let's call it correct
// if they want to guess "e" every time then they are terrible people and I don't care
const responsePattern = new RegExp(response, 'i');
return responsePattern.test(correctResponse);
}
function handleCorrectResponse(clueValue) {
score += clueValue;
console.log('Correct!');
}
function handleIncorrectResponse(clueValue) {
score -= clueValue;
console.log('Sorry, incorrect response.');
}
function handleGameComplete() {
console.log(`${chalk.white(figlet.textSync('Game over', {
horizontalLayout: 'full'
}))}\n${chalk.white.bold('Thanks for playing')}`);
const scoreColor = score >= 0 ? 'green' : 'red';
const scoreMessage = `${chalk.white.bold('Final score:')}`
const displayedScore = `${chalk[scoreColor](figlet.textSync('$' + score, {
horizontalLayout: 'full'
}))}`;
console.log(`${scoreMessage}\n${displayedScore}`);
process.exit();
}
function handleError(err) {
console.log(chalk`{red Fatal internal error:}`);
console.log(chalk`{red ${err}}`)
console.log(chalk`{red exiting...}`);
process.exit();
}
beginGame();