This repository has been archived by the owner on Apr 22, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
debugging.js
94 lines (79 loc) · 2.01 KB
/
debugging.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
const fs = require('fs')
/**
* @type {boolean}
*/
const ENABLED = true
/**
* @typedef Data
* @property failed {object[]}
* @property success {object[]}
* @property deltas {object}
* @property challenges {object}
* @property challenges.all {string[]}
* @property challenges.ok {string[]}
* @property challenges.failed {string[]}
*/
/**
* @type {?Data}
*/
const DATA = ENABLED ? (function () {
try {
return JSON.parse(fs.readFileSync('./debug.json').toString())
} catch (e) {
return {challenges: {all: [], ok: [], failed: []}, deltas: {}, success: [], failed: []}
}
})() : null
function addFailedAttempt(ctx) {
if (!ENABLED) return
DATA.failed.push(ctx)
_update(ctx, false)
_save()
}
function addSuccessfulAttempt(ctx) {
if (!ENABLED) return
DATA.success.push(ctx)
_update(ctx, true)
_save()
}
function _update(ctx, success) {
const list = listChallengesIn(ctx)
for (let j = 0; j < list.length; j++) {
const id = list[j]
if (!(id in DATA.deltas)) DATA.deltas[id] = 0
DATA.deltas[id] += success ? 1 : -1
if (DATA.challenges.all.indexOf(id) === -1) {
DATA.challenges.all.push(id)
DATA.challenges.all.sort()
}
if (success) {
if (DATA.challenges.ok.indexOf(id) === -1) {
DATA.challenges.ok.push(id)
DATA.challenges.ok.sort()
}
let index = DATA.challenges.failed.indexOf(id)
if (index !== -1) DATA.challenges.failed.splice(index, 1)
} else {
if (DATA.challenges.ok.indexOf(id) === -1 && DATA.challenges.failed.indexOf(id) === -1) {
DATA.challenges.failed.push(id)
DATA.challenges.failed.sort()
}
}
}
}
function _save() {
fs.writeFileSync('./debug.json', JSON.stringify(DATA))
}
function listChallengesIn(ctx) {
const list = []
for (let j = 0; j < ctx.chLog.c; j++) {
const id = ctx.chLog[j.toString()].i
if (typeof id === 'string' && j.toString() in ctx.chLog && list.indexOf(id) === -1)
list.push(id)
}
return list
}
module.exports = {
addFailedAttempt: addFailedAttempt,
addSuccessfulAttempt: addSuccessfulAttempt,
listChallengesIn: listChallengesIn
}