-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathchart.js
187 lines (151 loc) · 5.9 KB
/
chart.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
class Chart {
constructor(chartData) {
this.chartData = chartData;
this.currentSection = 0;
this.timings = [];
const totalSections = this.chartData.song.notes.length;
this.defaultDuration = this.constructor.getSectionDuration(this.chartData.song.bpm);
this.dynamicBPM = this.chartData.song.notes.some((n) => n.changeBPM);
if (this.dynamicBPM) {
this.maxBPM = Math.max(
...this.chartData.song.notes.filter((n) => n.changeBPM).map((i) => i.bpm)
);
const durations = [];
const bpm = [];
let currentBPM = this.chartData.song.bpm;
// not using map or reduce functions here to mantain clarity
for (let i = 0; i < totalSections; i++) {
const section = this.chartData.song.notes[i];
if (section.changeBPM) {
currentBPM = section.bpm;
}
bpm.push(currentBPM);
const duration = this.constructor.getSectionDuration(currentBPM);
durations.push(duration);
}
const positions = [0];
for (let i = 1; i < totalSections; i++) {
positions.push(positions[i - 1] + durations[i - 1]);
}
this.timings = durations.map((duration, i) => {
const position = positions[i];
return { start: position, end: positions[i] + duration, duration, bpm: bpm[i] };
});
const lastSection = this.chartData.song.notes.length - 1;
this._songDuration = this.timings[lastSection].end;
} else {
this._songDuration = this.defaultDuration * totalSections;
}
this.overlaps = new Map();
this._findOverlaps();
}
getPosition(section = this.currentSection) {
if (this.dynamicBPM) {
return this.timings[section].start;
}
const { bpm } = this.chartData.song;
return this.constructor.getSectionDuration(bpm) * section;
}
getSectionFromPos(milliseconds) {
if (this.dynamicBPM) {
return this.timings.findIndex((i) => milliseconds >= i.start && milliseconds < i.end);
}
const newSection = Math.floor(milliseconds / this.getDuration());
if (newSection >= this.chartData.song.length || newSection < 0) {
return false;
}
return newSection;
}
getRawSection(section = this.currentSection) {
return this.chartData.song.notes[section];
}
getRawSectionNotes(section = this.currentSection) {
return this.chartData.song.notes[section].sectionNotes;
}
getSectionNotes(section = this.currentSection, relative = false) {
const rawNotes = this.chartData.song.notes[section].sectionNotes;
const bothPlayers = rawNotes.some((n) => n[1] > 3);
const mustHitSection = this.getMustHitSection(section);
let player = []; // BF
let opponent = [];
if (bothPlayers) {
opponent = this._filterNotesFirstHalf(rawNotes);
player = this._filterNotesSecondHalf(rawNotes);
if (mustHitSection) {
const swap = player;
player = opponent;
opponent = swap;
}
} else if (mustHitSection) {
player = this._filterNotesFirstHalf(rawNotes);
} else {
opponent = this._filterNotesFirstHalf(rawNotes);
}
return { player, opponent };
}
_filterNotesFirstHalf(notes) {
return notes
.filter((n) => n[1] < 4)
.map((n) => ({ position: n[0], key: n[1], sustain: n[2] }));
}
_filterNotesSecondHalf(notes) {
return notes
.filter((n) => n[1] >= 4)
.map((n) => ({ position: n[0], key: n[1] - 4, sustain: n[2] }));
}
getMustHitSection(section = this.currentSection) {
return this.chartData.song.notes[section].mustHitSection;
}
// TODO: confusing naming, needs refactoring...
// this is for a single section
getDuration(section = this.currentSection) {
if (this.dynamicBPM) {
return this.timings[section].duration;
}
return this.defaultDuration;
}
getSongDuration() {
return this._songDuration;
}
getBPM(section = this.currentSection) {
if (this.dynamicBPM) {
return this.timings[section].bpm;
}
return this.chartData.song.bpm;
}
// dumb, inefficient way to find sustain notes that overlap over sections
_findOverlaps() {
const sections = this.chartData.song.notes.map((section) => section.sectionNotes);
sections.forEach((section, i) => {
const position = this.getPosition(i);
const duration = this.getDuration(i);
const noteDuration = duration / 16;
const end = position + duration;
section.forEach((note) => {
if (!(note[2] > 0)) {
return;
}
const notePosition = note[0];
const sustainDuration = note[2];
const fullNoteDuration = notePosition + noteDuration + sustainDuration;
if (end > fullNoteDuration) {
return;
}
const overlapsUntil = this.getSectionFromPos(fullNoteDuration);
for (let k = i + 1; k <= overlapsUntil; k++) {
if (this.overlaps.has(k)) {
this.overlaps.get(k).add(i);
} else {
const overlapSections = new Set();
overlapSections.add(i);
this.overlaps.set(k, overlapSections);
}
}
});
});
}
static getSectionDuration(bpm) {
return 4 * ((1000 * 60) / bpm);
}
}
export { Chart };