-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
215 lines (191 loc) · 5.57 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
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
const blessed = require('blessed')
const fs = require('fs')
const chalk = require('chalk')
const path = require('path')
const { highlight } = require('@acarl005/cli-highlight')
// a runtime option for truning on some helpful internal info
const DEBUG = false
const highlightOptions = {
language: 'javascript',
theme: {
string: chalk.magenta,
comment: chalk.gray,
keyword: chalk.green.bold
}
}
// create the one and only screen we'll need
const screen = blessed.screen({
cursor: {
artificial: true,
shape: 'block',
color: 'green'
}
})
// these are all the keys that the player pressed
const keyStack = []
// pick a random javascript file in there
const srcDir = path.join(__dirname, 'code', 'javascript', 'jquery')
const files = fs.readdirSync(srcDir).filter(name => /\.js$/.test(name))
const file = path.join(srcDir, files[Math.floor(Math.random() * files.length)])
// and read in the source code
const code = fs.readFileSync(file, 'utf8')
// this box will show the main game window
const box = blessed.box({
parent: screen,
// give is the code and highlight the first character green!
content: chalk.bgGreen(code[0]) + code.slice(1),
height: DEBUG ? '50%' : '100%',
scrollable: true,
border: {
type: 'line'
}
})
// use this to display the keystack in DEBUG mode
const debug = blessed.box({
top: '50%',
height: '50%-1',
border: {
type: 'line'
}
})
// this will be for arbitrary messages in DEBUG mode
const alert = blessed.box({
top: '100%-1',
height: 2
})
// the message for passing to the alert box
let alertMsg = ''
// State for the current session
let state = {
startTime: null,
numBackspaces: 0,
errors: []
}
if (DEBUG) {
screen.append(debug)
screen.append(alert)
}
// the initial render!
screen.render()
// give us an out
screen.key([ 'C-c' ], () => process.exit(0))
// the handler for all other keypresses
screen.on('keypress', (ch, key) => {
// save time of key press
let now = Date.now()
if (!state.startTime) {
state.startTime = now
}
// add some debug info
alertMsg = JSON.stringify(key) + JSON.stringify(ch)
// default to the key press is correct
let isCorrect = true
// ctrl+r is for removing the alert message in DEBUG mode
if (key.ctrl && key.name === 'r') {
alertMsg = ''
// on mac, pressing enter triggers TWO keypresses, one called 'enter' and one called 'return'.
// we only want to respond to one of those. so lets ignore 'return'
} else if (
key.name === 'return' ||
key.ctrl
) {
'ignore'
return
} else if (key.name === 'backspace') {
state.numBackspaces++
keyStack.pop()
// handle all the other typing
} else {
const keyObj = {
key: ch
}
if (key.name === 'enter') {
keyObj.key = '\n'
isCorrect = code[keyStack.length] === '\n'
while (/\s/.test(code[keyStack.length + 1])) {
keyStack.push({
key: code[keyStack.length + 1],
error: false
})
}
} else {
isCorrect = code[keyStack.length] === ch
}
keyStack.push(keyObj)
keyObj.error = !isCorrect
}
// highlight based on the current state, have they got everything correct up to this point?
let highlighted
let errors = state.errors
let wasCorrect = state.correct
// fix the vertical scroll position, so that the screen moves downward as the game advances
const lineNum = (code.slice(0, keyStack.length).match(/\n/g) || []).length
// need to subtract the borders, which take up a width of 1 each
const innerBoxHeight = box.height - 2
// lets try to keep things centered
const boxCenter = Math.floor(innerBoxHeight / 2)
box.scrollTo(lineNum + boxCenter)
// check for incorrect key press
state.correct = keyStack.every(keyObj => !keyObj.error)
if (state.correct) {
if (!wasCorrect && errors.length > 0) {
errors[errors.length - 1].end = now
}
if (code.length === keyStack.length && wasCorrect) {
state.endTime = now
const minutes = (state.endTime - state.startTime) / 1000 / 60
// User has finished the test, what to do?
blessed.box({
parent: screen,
top: '25%',
left: '25%',
height: '50%',
width: '50%',
content: `
Done!
${keyStack.length / minutes / 5} WPM
`,
border: {
type: 'line'
}
})
return screen.render()
}
// if so, the "cursor" is green
highlighted = highlight(code.slice(0, keyStack.length), highlightOptions)
+ chalk.bgGreen(wrapWhitespace(code[keyStack.length]))
+ code.slice(keyStack.length + 1)
} else {
// check if already in error state
if (wasCorrect) {
errors.push({
count: 0,
start: now
})
} else if (key.name !== 'backspace' && errors.length > 0) {
errors[errors.length - 1].count++
}
// if not, the cursor is yellow and all the characters since the first error are red
const errIndex = keyStack.findIndex(keyObj => keyObj.error)
highlighted = highlight(code.slice(0, errIndex), highlightOptions)
+ chalk.bgRed(code.slice(errIndex, keyStack.length))
+ chalk.bgYellow(wrapWhitespace(code[keyStack.length]))
+ code.slice(keyStack.length + 1)
}
// update and re-render everything
box.setContent(highlighted)
debug.setContent(keyStack.map(obj => obj.key).join(''))
alert.setContent(String(alertMsg) + JSON.stringify(state))
screen.render()
})
// newlines are invlisible. lets give them a little arrow emoji to look at instead
function wrapWhitespace(ch) {
switch (ch) {
case '\n':
return '↩︎ \n'
case '\t':
return '<tb>'
default:
return ch
}
}