-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcli.js
executable file
·248 lines (232 loc) · 7.84 KB
/
cli.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
#!/usr/bin/env node
const { initGame, submitWord, getErrorMessage } = require("./src/game");
const fs = require("fs");
const readline = require("readline");
const { stdin: input, stdout: output } = require("process");
const { generateShareText } = require("./src/share");
const { STARTING_LIVES } = require("./src/consts");
const { getCountdownString, getNextDate } = require("./src/datetime");
const { version } = require("./package");
const { loadPreferences, savePreferences, STATE_JSON_FILENAME, PREFERENCES_JSON_FILENAME } = require("./src/storage/cli");
const rl = readline.createInterface({
input,
output,
});
const validDifficulties = ["hard", "easy"];
const { program, Option } = require("commander");
program
.name("wordle-clone")
.description("A clone of Wordle for the command line")
.version(version, "-V, --version")
.option("-v, --verbose", "print extra information")
.addOption(new Option("-d, --difficulty <string>", "change game difficulty").choices(validDifficulties))
.action(() => {});
program.command("data")
.description("outputs the filepath of game state and/or preferences")
.option("-p, --preferences", "output preferences filepath")
.option("-s, --state", "output game state filepath")
.action((options) => {
if (!options.preferences && !options.state) {
console.error("Please specify a file to print (see --help for flags)");
process.exit(1);
}
if (options.preferences) {
console.log(PREFERENCES_JSON_FILENAME)
}
if (options.state) {
console.log(STATE_JSON_FILENAME);
}
process.exit(0);
});
let gameState;
let hardMode = false;
let isVerbose = false;
let highContrast = false;
let day;
let lastSubmission;
let preferences;
// https://stackoverflow.com/a/41407246
FgBlack = "\x1b[30m";
FgRed = "\x1b[31m";
FgWhite = "\x1b[37m";
BgBlack = "\x1b[40m";
BgGreen = "\x1b[42m";
BgYellow = "\x1b[43m";
BgMagenta = "\x1b[45m";
BgCyan = "\x1b[46m";
ResetColour = "\x1b[0m";
const getColouredLetter = (letter, colour) => {
let colourFmt;
switch (colour) {
case "within":
colourFmt = (highContrast) ? `${BgCyan}${FgBlack}` : `${BgYellow}${FgBlack}`;
break;
case "correct":
colourFmt = (highContrast) ? `${BgMagenta}${FgBlack}` : `${BgGreen}${FgBlack}`;
break;
default:
colourFmt = `${BgBlack}${FgWhite}`;
}
return `${colourFmt}${letter}${ResetColour}`;
};
const getHowToPlayText = () => {
const correctGuess = [
getColouredLetter("c","correct"),
getColouredLetter("l","standard"),
getColouredLetter("o","standard"),
getColouredLetter("n","standard"),
getColouredLetter("e","standard"),
].join("");
const withinGuess = [
getColouredLetter("s","standard"),
getColouredLetter("p","within"),
getColouredLetter("i","standard"),
getColouredLetter("c","standard"),
getColouredLetter("e","standard"),
].join("");
const incorrectGuess = [
getColouredLetter("h","standard"),
getColouredLetter("e","standard"),
getColouredLetter("a","standard"),
getColouredLetter("r","incorrect"),
getColouredLetter("t","standard"),
].join("");
return `How to play:
Guess a five-letter word in six tries.\n
${correctGuess}
The letter C is in the correct place.\n
${withinGuess}
The letter P is in the word but in the wrong place.\n
${incorrectGuess}
The letter R is not in the word in any place.\n
Good luck!`;
};
const renderState = (gameState) => {
for (let i = 0; i < 6; i++) {
if (i < gameState.attempts.length) {
console.log(
gameState.attempts[i]
.map((entry) =>
entry.correct
? getColouredLetter(entry.letter, "correct")
: entry.within
? getColouredLetter(entry.letter, "within")
: getColouredLetter(entry.letter, "incorrect")
)
.join("")
);
} else {
console.log("_____");
}
}
};
const eventHandler = (event, data) => {
switch (event) {
case "init":
gameState = data.gameState;
day = data.day;
break;
case "first_time":
console.log(
getHowToPlayText()
);
break;
case "draw":
renderState(gameState);
break;
case "error":
return console.log(getErrorMessage(data, lastSubmission));
case "lose":
return console.log("Game over, word was", data);
case "win":
return console.log("You win!");
}
};
const prompt = async (message = "> ") => {
// Workaround for prompt printing before the game text
await new Promise((resolve) => setTimeout(resolve, 10));
return new Promise((resolve) => {
rl.question(message, resolve);
});
};
const handleCLIArguments = () => {
const options = program.opts();
isVerbose = options.verbose;
if (isVerbose) {
console.log("CLI arguments:", options);
}
if (options.difficulty) {
if (!hardMode && gameState.attempts.length > 0 && !gameState.ended) {
console.error("Cannot switch difficulty while game is in progress");
process.exit(2);
}
hardMode = options.difficulty === "hard";
savePreferences({
hardMode,
highContrast,
});
if (hardMode) {
console.log("hard mode enabled");
} else {
console.log("easy mode enabled");
}
}
};
const runGame = async () => {
// Process arguments
program.parse();
// Load preferences
preferences = loadPreferences();
// Use hard mode setting from preferences first, then CLI can change it if argument is specified
hardMode = preferences.hardMode;
highContrast = preferences.highContrast;
// Initialize game
await initGame(eventHandler);
// Process CLI arguments here, as we need to check current game state to see if they can switch to hard mode
handleCLIArguments();
if (isVerbose) {
console.log("State json:", STATE_JSON_FILENAME);
console.log("Preferences json:", PREFERENCES_JSON_FILENAME);
console.log("Loaded preferences:", preferences);
}
while (!gameState.ended) {
const answer = await prompt();
if (answer === "quit" || answer === "q" || answer === "exit") {
console.log("Bye!");
return;
} else if (answer === "help" || answer === "h") {
console.log(
getHowToPlayText()
);
} else {
lastSubmission = answer;
submitWord(
gameState,
answer,
hardMode && gameState.attempts.length > 0
? gameState.attempts[gameState.attempts.length - 1]
: null,
hardMode
);
}
}
const displayNextWordle = () => {
console.log(`Next Wordle: ${getCountdownString(getNextDate())}`);
};
const answer = await prompt("Would you like to share your results? [Y/n]");
if (answer.toLowerCase() === "" || answer.toLowerCase() === "y" || answer.toLowerCase() === "yes") {
import("clipboardy").then((clipboard) => {
clipboard.default.writeSync(
generateShareText(day, gameState.attempts, STARTING_LIVES, {
hardMode: gameState.wonHardMode,
highContrastMode: highContrast,
})
);
console.log("Copied to clipboard");
displayNextWordle();
});
} else {
displayNextWordle();
}
};
runGame().finally(() => rl.close());