-
Notifications
You must be signed in to change notification settings - Fork 5
/
index.js
429 lines (328 loc) · 11.4 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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
module.exports = function () {
var Stopwatch = require('timer-stopwatch');
var keypress = require('keypress');
var clc = require('cli-color');
var charm = require('charm')();
var Scrambo = require('scrambo');
var threebythree = new Scrambo();
var prettyMs = require('pretty-ms');
var this_scramble, last_scramble, this_solve, stats = { };
// Helper functions
var prettifyVerbose = require('./utils.js').prettifyVerbose;
const STATS_LINES = 11;
function prettify (ms) {
return prettyMs(ms, {secDecimalDigits: 2});
}
function botSay (phrase) {
console.log(clc.red('Bot: ') + phrase);
}
function userSay (phrase) {
console.log(clc.blue('You: ') + phrase);
}
function prepNewSolve () {
userSay('Press space or click your mouse to initiate a solve.');
this_scramble = threebythree.get(1).join(' ');
botSay(this_scramble);
return 2;
}
function eraseInspectSolveLines () {
charm.position(1, start_inspect);
charm.erase('end');
charm.position(1, start_inspect + 1);
charm.erase('end');
}
function resetForNextSolve () {
stopwatch.stop();
stopwatch.reset(0);
post_inspect.reset(0);
inspect.reset(0);
solving = false;
inspecting = false;
post_inspecting = false;
eraseInspectSolveLines();
penalty = 0;
}
function addToStatsModule (solveTime) {
if (typeof solveTime !== 'number') {
return;
}
this_solve = (solveTime / 1000.0).toFixed(2);
solves_today.push(parseFloat(this_solve));
num_solves += 1;
stats = calcStats(solves_today);
ao5 = stats.ao5;
ao12 = stats.ao12;
ao_session = stats.ao_session;
best_time = stats.best_time;
worst_time = stats.worst_time;
}
function end_session() {
console.log("\n\n" + clc.green("SESSION ENDED. Session stats follow:") + "\n\n");
if (post_solving) {
acceptSolve(last_solve, last_scramble, last_solve_timestamp);
}
print_stats();
return process.exit(0);
}
function acceptSolve(solveTime, scramble, solve_ts) {
solve_rep = 'DNF';
if (typeof solveTime === 'number') {
solve_rep = (solveTime/1000).toFixed(2);
addToStatsModule(solveTime);
}
solve_ts = typeof solve_ts === 'number' ? solve_ts : -1;
writeLocal(solve_rep, scramble, solve_ts);
}
function print_stats () {
console.log('Session statistics');
console.log('Session started at ' + start_time);
console.log('You have been cubing for ' + prettifyVerbose(total_time.ms));
console.log('Total solves: ' + clc.blue(num_solves));
if (best_time !== undefined) {
console.log(clc.green('Best solve: ') + clc.blue(prettifyVerbose(best_time)));
}
if (worst_time !== undefined) {
console.log(clc.green('Worst solve: ') + clc.blue(prettifyVerbose(worst_time)));
}
var ret = { solve: 0, inspect: 0 };
if (num_solves >= 5) {
console.log('Your current ' + clc.red('AO5') + ' is ' + clc.blue(prettifyVerbose(ao5)));
ret.solve ++;
ret.inspect ++;
}
if (num_solves >= 12) {
console.log('Your current ' + clc.red('AO12') + ' is ' + clc.blue(prettifyVerbose(ao12)));
ret.solve ++;
ret.inspect ++;
}
console.log('Your current ' + clc.red('Session average') + ' is ' + clc.blue(prettifyVerbose(ao_session)));
return ret;
}
function print_help (left_position, right_position) {
var help_message = [
{ func: clc.green, msg: 'Keyboard shortcuts' },
{ func: clc.green, msg: 'Press E or Q to exit cli-cube-timer' },
{ func: clc.red.underline, msg: 'Press Space or click your mouse to start a solve' },
{ func: clc.blue, msg: 'Press S to see your session statistics' },
{ func: clc.blue, msg: 'Press T to trash a solve while the solve timer is running' },
{ func: clc.blue, msg: 'Press D after a solve to change it to a DNF' },
{ func: clc.blue, msg: 'Press P after a solve to add a penalty of 2 seconds' },
{ func: clc.green, msg: 'Press H to print this list of keyboard shortcuts' }
];
for (var init = 0; init < help_message.length; init++) {
charm.position(left_position, right_position + init);
console.log(help_message[init].func(help_message[init].msg))
}
return help_message.length + 1;
}
function space_or_mouse () {
if (!inspecting && !post_inspecting && !solving) {
// A new solve has been initiated
// Last solve has been accepted by user! Let's write it to the local
// file!
if (post_solving) {
// User didn't add penalty to the last solve or make it a DNF!
post_solving = false;
acceptSolve(last_solve, last_scramble, last_solve_timestamp);
}
// Now start inspection for the new solve
inspect.start();
inspecting = true;
} else if (inspecting && !post_inspecting && !solving) {
// Inspection ends, solving begins
inspect.stop();
inspect.reset(0);
stopwatch.start();
inspecting = false;
solving = true;
} else if (!inspecting && post_inspecting && !solving) {
// Inspection has ended, with a penalty of +2
// Solving begins
post_inspect.stop();
inspect.reset(0);
post_inspect.reset(0);
stopwatch.start();
post_inspecting = false;
solving = true;
penalty = 2000;
} else if (!inspecting && !post_inspecting && solving) {
// Solve has ended
var solveTime = stopwatch.ms;
solveTime = solveTime + penalty;
charm.position(1, start_inspect);
botSay('That solve was ' + clc.green(prettify(solveTime)) +
(penalty === 0 ? ' (OK)' : clc.red(' (+2)')));
if (num_solves > 1) {
charm.position(right_row_num, start_inspect);
console.log(clc.red(num_solves < 5 ? 'Previous solve: ' : "This session's AO5: ") +
clc.blue(typeof last_solve === 'number' ? prettify(num_solves < 5 ? last_solve : ao5) : 'DNF'));
}
last_solve = solveTime;
last_scramble = this_scramble;
last_solve_timestamp = (new Date()) - 0;
prepNewSolve();
start_inspect += 3;
// The user can still decide to reject this solve!
post_solving = true;
resetForNextSolve();
}
}
charm.pipe(process.stdout);
keypress(process.stdin);
var inspect_options = {
refreshRateMS: 1000,
almostDoneMS: 8000
};
var inspect = new Stopwatch(15000, inspect_options);
var post_inspect = new Stopwatch(2000);
var stopwatch = new Stopwatch();
inspect.on('time', function (time) {
if (!inspect.hasBeenStopped) {
charm.position(1, start_inspect).write('Inspecting: ' + String('00' + (time.ms / 1000).toFixed()).slice(-2));
}
});
inspect.on('done', function () {
charm.position(1, start_inspect);
charm.erase('end');
charm.position(1, start_inspect + 1);
charm.erase('end');
charm.position(1, start_inspect);
console.log(clc.red('Penalty! +2s'));
inspecting = false;
post_inspecting = true;
post_inspect.start();
});
post_inspect.on('done', function () {
charm.position(1, start_inspect);
charm.erase('end');
charm.position(1, start_inspect + 1);
charm.erase('end');
charm.position(1, start_inspect);
console.log(clc.red('This solve is a DNF.'));
post_solving = true;
resetForNextSolve();
charm.position(1, start_inspect);
botSay('That solve was ' + clc.green('DNF'));
prepNewSolve();
start_inspect += 3;
last_solve = 'DNF';
last_solve_timestamp = (new Date()) - 0;
});
stopwatch.on('time', function (time) {
if (!solving) {
return;
}
charm.position(1, start_inspect + 1).write('Solving: ' + (time.ms / 1000).toFixed(2));
});
var stats = require('./solvestats-module.js');
var calcStats = stats.calcStats;
var push = require('./file-module.js');
var writeLocal = push.writeLocal;
var solving = false;
var post_solving = false;
var inspecting = false;
var post_inspecting = false;
var start_inspect = 0;
var last_solve = -1;
var last_solve_timestamp = -1;
var penalty = 0;
var right_row_num = 50;
var num_solves = 0;
var solves_today = [];
var ao5 = 0.0;
var ao12 = 0.0;
var ao_session = 0.0;
var best_time = 0.0;
var worst_time = 0.0;
process.stdin.on('keypress', function (ch, key) {
if (key === undefined) {
return;
}
switch (key.name) {
case 'e':
case 'q':
return end_session();
case 's':
charm.erase('line');
charm.left(1);
var printed = print_stats();
userSay('Press space or click your mouse to initiate a new solve');
start_inspect += (STATS_LINES + printed.inspect);
break;
case 'space':
space_or_mouse();
break;
case 't':
if (!inspecting && !post_inspecting && solving) {
// Solve has been trashed by the solver
// (Probably because they were disturbed during the solve)
// Reset everything and show a new scramble to the solver
charm.position(1, start_inspect);
botSay('That solve was trashed');
prepNewSolve();
start_inspect += 3;
resetForNextSolve();
} else if (!inspecting && !post_inspecting && !solving && post_solving) {
// Solve completed, but the user would like to trash this solve
post_solving = false;
botSay("The previous solve was trashed");
start_inspect += 2;
}
break;
case 'd':
case 'p':
if (!inspecting && !post_inspecting && !solving && post_solving) {
// User has decided to either add +2 to this solve time and write it
// to file or make it a DNF
post_solving = false;
if (key.name === 'd') {
botSay("The previous solve was changed to DNF");
acceptSolve('DNF', last_scramble, last_solve_timestamp);
start_inspect += 2;
} else if (key.name === 'p' && typeof last_solve === 'number') {
// The 2nd check ensures that the addition below will not cause any
// error, when we reach this position through the "DNF because solve
// didn't start within 17 seconds" flow
last_solve += 2000;
botSay("A penalty of 2 seconds was added to the previous solve");
acceptSolve(last_solve, last_scramble, last_solve_timestamp);
start_inspect += 2;
}
}
break;
case 'h':
if (!solving && !inspecting && !post_inspecting) {
var diff = print_help(0, start_inspect);
start_inspect += diff;
}
break;
default:
break;
}
if (key.ctrl && key.name === 'c') {
return end_session();
}
});
process.stdin.setRawMode(true);
process.stdin.on('mousepress', function (click) {
if (click.release) {
space_or_mouse();
}
});
process.on('exit', function () {
//disable mouse on exit, so that the state is back to normal for the terminal.
keypress.disableMouse(charm);
});
process.stdin.resume();
charm.reset();
keypress.enableMouse(charm);
botSay("Hey! Let's start solving!");
botSay('The session starts now!');
start_inspect = print_help(right_row_num, 1);
charm.position(1, start_inspect)
start_inspect += prepNewSolve();
start_time = (new Date()).toTimeString().split(' ')[0]
var total_time = new Stopwatch();
total_time.start();
charm.position(1, start_inspect);
};