-
Notifications
You must be signed in to change notification settings - Fork 5
/
Parity.js
259 lines (228 loc) · 8.33 KB
/
Parity.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
const cutDirections = ['up', 'down', 'left', 'right', 'upLeft', 'upRight', 'downLeft', 'downRight', 'dot'];
// bombs are type 3 for some reason
const types = {
0: 'red',
1: 'blue',
3: 'bomb'
};
const lineIndices = ['left', 'middleLeft', 'middleRight', 'right'];
const lineLayers = ['bottom', 'middle', 'top'];
// the minimum time between the last note or bomb for a bomb to be considered for each saber
// make user configurable?
const bombMinTime = 0.25;
// the tolerance when making float comparisons
// needed because different editors round in different ways
const comparisonTolerance = 1 / 128;
const cuts = {
blue: {
good: {
forehand: ['down', 'left', 'downLeft', 'downRight', 'dot'],
backhand: ['up', 'right', 'upLeft', 'upRight', 'downRight', 'dot']
},
borderline: {
forehand: ['right', 'upLeft'],
backhand: ['left']
}
},
red: {
good: {
forehand: ['down', 'right', 'downRight', 'downLeft', 'dot'],
backhand: ['up', 'left', 'upRight', 'upLeft', 'downLeft', 'dot']
},
borderline: {
forehand: ['left', 'upRight'],
backhand: ['right']
}
}
};
function Parity() {
this.red = 'forehand';
this.blue = 'forehand';
}
Parity.prototype.invert = function(color) {
this[color] === 'forehand' ? this[color] = 'backhand' : this[color] = 'forehand';
}
Parity.prototype.init = function(notes) {
let firstRed;
let firstBlue;
for (let note of notes) {
if (!(firstRed) && types[note._type] === 'red') {
firstRed = note;
} else if (!(firstBlue) && types[note._type] === 'blue') {
firstBlue = note;
}
}
if (firstRed && cuts.red.good.forehand.includes(cutDirections[firstRed._cutDirection])) {
this.red = 'forehand';
} else {
this.red = 'backhand';
}
if (firstBlue && cuts.blue.good.forehand.includes(cutDirections[firstBlue._cutDirection])) {
this.blue = 'forehand';
} else {
this.blue = 'backhand';
}
}
var difficultyString;
var sliderPrecision = Infinity;
var ready = false;
/*const fileInput = document.getElementById('file-input');
const sliderPrecisionInput = document.getElementById('slider-precision');
const submit = document.getElementById('submit');
const output = document.getElementById('output');
fileInput.addEventListener('change', readFile);
sliderPrecisionInput.addEventListener('change', readSliderPrecision);
submit.addEventListener('click', main);
function readFile() {
ready = false;
let fr = new FileReader();
fr.readAsText(fileInput.files[0]);
fr.addEventListener('load', function () {
difficultyString = fr.result;
ready = true;
});
}
function readSliderPrecision() {
sliderPrecision = parseInt(sliderPrecisionInput.value) || Infinity;
}
function getDifficultyObject() {
return JSON.parse(difficultyString);
}
function getNotes(obj) {
let notes = obj._notes;
notes.sort(function (a, b) {
return a._time - b._time;
})
// filter out invalid note types
notes = notes.filter(function (note) {
return types[note._type] !== undefined;
});
return notes;
}*/
function logNote(note, parity) {
let time = note._time;
let type = types[note._type];
let cutDirection = cutDirections[note._cutDirection];
let column = lineIndices[note._lineIndex];
let row = lineLayers[note._lineLayer];
if (type === 'bomb') {
return ('Bomb at beat ' + time + ' -- ' + column + ' -- ' + row);
} else {
return ('Note at beat ' + time + ' -- ' + type + ' -- ' + cutDirection + ' -- ' + column + ' -- ' + row + ' -- ' + parity[type]);
}
}
function outputMessage(text, type) {
/*let element = document.createElement('div');
let textNode = document.createElement('pre');
textNode.innerHTML = text;
element.classList.add('outline', type);
element.appendChild(textNode);
output.appendChild(element);*/
log("[GMParity] [" + type + "]" + text);
}
function clearOutput() {
while (output.lastChild) {
output.removeChild(output.lastChild);
}
}
function performCheck(data) {
notes = data.notes;
/*clearOutput();
if (!ready) {
outputMessage('File loading not ready, try again', 'error');
return;
}
let notes = getNotes(getDifficultyObject());*/
let parity = new Parity();
parity.init(notes);
for (let i = 0; i < notes.length; i++) {
let note = notes[i];
let type = types[note._type];
let cutDirection = cutDirections[note._cutDirection];
let column = lineIndices[note._lineIndex];
let row = lineLayers[note._lineLayer];
if (type === 'bomb') {
// this is super ugly, I'm hoping to come up with a better way later
if (!(['middleLeft', 'middleRight'].includes(column)) || !(['bottom', 'top'].includes(row))) {
continue;
}
// for each saber: ignore the bomb if it's within bombMinTime after a note or own-side bomb that says otherwise
let setParity = {
red: true,
blue: true
};
let offset = -1;
let offsetNote = notes[i + offset];
while ((i + offset) >= 0 &&
(note._time - offsetNote._time - bombMinTime) <= comparisonTolerance) {
switch (types[offsetNote._type]) {
case 'bomb':
if (lineIndices[offsetNote._lineIndex] === 'middleLeft') {
setParity.red = false;
} else if (lineIndices[offsetNote._lineIndex] === 'middleRight') {
setParity.blue = false;
}
break;
case 'red':
setParity.red = false;
break;
case 'blue':
setParity.blue = false;
break;
}
offset--;
offsetNote = notes[i + offset];
}
// invert parity if needed and log the bomb if so
let logString = '';
for (let color in setParity) {
if (setParity[color]) {
if (row === 'bottom' && parity[color] === 'backhand') {
parity.invert(color);
logString += '\n' + color + ' parity set to ' + parity[color];
}
if (row === 'top' && parity[color] === 'forehand') {
parity.invert(color);
logString += '\n' + color + ' parity set to ' + parity[color];
}
}
}
if (logString) {
logString = logNote(note, parity) + logString;
outputMessage(logString, 'info');
}
} else {
if (cuts[type].good[parity[type]].includes(cutDirection)) {
parity.invert(type);
} else if (cuts[type].borderline[parity[type]].includes(cutDirection)) {
addWarning(note, "Borderline hit, not all players might read or be able to play this correctly");
outputMessage(logNote(note, parity) +
'\nBorderline hit, not all players might read or be able to play this correctly', 'warning');
parity.invert(type);
} else {
addError(note, "Bad hit, wrist reset is necessary");
outputMessage(logNote(note, parity) + '\nBad hit, wrist reset is necessary', 'error');
}
// invert parity again if there's a same-color note within sliderPrecision
let offset = 1;
let offsetNote = notes[i + offset];
while ((i + offset) < notes.length &&
(offsetNote._time - note._time - (1 / sliderPrecision)) <= comparisonTolerance) {
if (note._type === offsetNote._type) {
parity.invert(type);
break;
}
offset++;
offsetNote = notes[i + offset];
}
}
}
/*if (document.getElementsByClassName('warning').length === 0 && document.getElementsByClassName('error').length === 0) {
outputMessage('No errors found', 'success');
}*/
}
module.exports = {
name: "GM Parity Checker",
params: {},
performCheck: performCheck
};