@rian8337/osu-strain-graph-generator 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Rian8337
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,11 @@
1
+ # `osu-strain-graph-generator`
2
+
3
+ > TODO: description
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ const osuStrainGraphGenerator = require('osu-strain-graph-generator');
9
+
10
+ // TODO: DEMONSTRATE API
11
+ ```
package/dist/Chart.js ADDED
@@ -0,0 +1,282 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.Chart = void 0;
4
+ const canvas_1 = require("canvas");
5
+ /**
6
+ * Utility to draw a graph with only node-canvas.
7
+ *
8
+ * Used for creating strain graph of beatmaps.
9
+ */
10
+ class Chart {
11
+ /**
12
+ * @param values Initializer options for the graph.
13
+ */
14
+ constructor(values) {
15
+ this.padding = 10;
16
+ this.tickSize = 10;
17
+ this.axisColor = "#555";
18
+ this.font = "12pt Calibri";
19
+ this.axisLabelFont = "bold 11pt Calibri";
20
+ this.fontHeight = 12;
21
+ this.baseLabelOffset = 15;
22
+ this.graphWidth = values.graphWidth;
23
+ this.graphHeight = values.graphHeight;
24
+ this.canvas = (0, canvas_1.createCanvas)(this.graphWidth, this.graphHeight);
25
+ this.context = this.canvas.getContext("2d");
26
+ this.minX = values.minX;
27
+ this.minY = values.minY;
28
+ this.maxX = values.maxX;
29
+ this.maxY = values.maxY;
30
+ this.unitsPerTickX = values.unitsPerTickX;
31
+ this.unitsPerTickY = values.unitsPerTickY;
32
+ this.background = values.background;
33
+ this.xLabel = values.xLabel;
34
+ this.yLabel = values.yLabel;
35
+ this.xValueType = values.xValueType;
36
+ this.yValueType = values.yValueType;
37
+ this.pointRadius = Math.max(0, values.pointRadius ?? 1);
38
+ // Relationships
39
+ this.rangeX = this.maxX - this.minX;
40
+ this.rangeY = this.maxY - this.minY;
41
+ this.numXTicks = Math.round(this.rangeX / this.unitsPerTickX);
42
+ this.numYTicks = Math.round(this.rangeY / this.unitsPerTickY);
43
+ this.x = this.getLongestValueWidth() + this.padding * 2;
44
+ this.y = this.padding * 2;
45
+ this.width = this.canvas.width - this.x - this.padding * 2;
46
+ this.height =
47
+ this.canvas.height - this.y - this.padding - this.fontHeight;
48
+ this.scaleX =
49
+ (this.width - (this.xLabel ? this.baseLabelOffset : 0)) /
50
+ this.rangeX;
51
+ this.scaleY =
52
+ (this.height - (this.yLabel ? this.baseLabelOffset : 0)) /
53
+ this.rangeY;
54
+ // Draw background and X and Y axis tick marks
55
+ this.setBackground();
56
+ this.drawXAxis(true);
57
+ this.drawYAxis(true);
58
+ }
59
+ /**
60
+ * Draws a line graph with specified data, color, and line width.
61
+ *
62
+ * @param data The data to make the graph.
63
+ * @param color The color of the line.
64
+ * @param width The width of the line.
65
+ */
66
+ drawLine(data, color, width) {
67
+ const c = this.context;
68
+ c.save();
69
+ this.transformContext();
70
+ c.lineWidth = width;
71
+ c.strokeStyle = c.fillStyle = color;
72
+ c.beginPath();
73
+ c.moveTo(data[0].x * this.scaleX, data[0].y * this.scaleY);
74
+ for (let n = 0; n < data.length; ++n) {
75
+ const point = data[n];
76
+ // Data segment
77
+ c.lineTo(point.x * this.scaleX, point.y * this.scaleY);
78
+ c.stroke();
79
+ c.closePath();
80
+ if (this.pointRadius) {
81
+ c.beginPath();
82
+ c.arc(point.x * this.scaleX, point.y * this.scaleY, this.pointRadius, 0, 2 * Math.PI, false);
83
+ c.fill();
84
+ c.closePath();
85
+ }
86
+ // Position for next segment
87
+ c.beginPath();
88
+ c.moveTo(point.x * this.scaleX, point.y * this.scaleY);
89
+ }
90
+ c.restore();
91
+ }
92
+ /**
93
+ * Draws an area graph with specified data and color.
94
+ *
95
+ * @param data The data to make the graph.
96
+ * @param color The color of the area.
97
+ */
98
+ drawArea(data, color) {
99
+ const c = this.context;
100
+ c.save();
101
+ this.transformContext();
102
+ c.strokeStyle = c.fillStyle = color;
103
+ c.beginPath();
104
+ data.forEach((d) => c.lineTo(d.x * this.scaleX, d.y * this.scaleY));
105
+ c.stroke();
106
+ c.lineTo(data.at(-1).x * this.scaleX, 0);
107
+ c.lineTo(0, 0);
108
+ c.fill();
109
+ c.restore();
110
+ // Redraw axes since it gets
111
+ // overlapped by chart area
112
+ if (color !== this.axisColor) {
113
+ this.drawXAxis();
114
+ this.drawYAxis();
115
+ }
116
+ }
117
+ /**
118
+ * Returns a Buffer that represents the graph.
119
+ */
120
+ getBuffer() {
121
+ return this.canvas.toBuffer();
122
+ }
123
+ /**
124
+ * Draws the X axis of the graph.
125
+ *
126
+ * @param drawLabel Whether or not to draw the axis label.
127
+ */
128
+ drawXAxis(drawLabel) {
129
+ const c = this.context;
130
+ const labelOffset = this.xLabel ? this.baseLabelOffset : 0;
131
+ const yLabelOffset = this.yLabel ? this.baseLabelOffset : 0;
132
+ c.save();
133
+ if (this.xLabel && drawLabel) {
134
+ c.textAlign = "center";
135
+ c.font = this.axisLabelFont;
136
+ c.fillText(this.xLabel, this.x + this.width / 2, this.y + this.height + labelOffset);
137
+ c.restore();
138
+ }
139
+ c.beginPath();
140
+ c.moveTo(this.x + yLabelOffset, this.y + this.height - labelOffset);
141
+ c.lineTo(this.x + this.width, this.y + this.height - labelOffset);
142
+ c.strokeStyle = this.axisColor;
143
+ c.lineWidth = 2;
144
+ c.stroke();
145
+ // Draw tick marks
146
+ for (let n = 0; n < this.numXTicks; ++n) {
147
+ c.beginPath();
148
+ c.moveTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
149
+ this.x +
150
+ yLabelOffset, this.y + this.height - labelOffset);
151
+ c.lineTo(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
152
+ this.x +
153
+ yLabelOffset, this.y + this.height - labelOffset - this.tickSize);
154
+ c.stroke();
155
+ }
156
+ // Draw labels
157
+ c.font = this.font;
158
+ c.fillStyle = "black";
159
+ c.textAlign = "center";
160
+ c.textBaseline = "middle";
161
+ for (let n = 0; n < this.numXTicks; ++n) {
162
+ const label = Math.round(((n + 1) * this.maxX) / this.numXTicks);
163
+ let stringLabel = label.toString();
164
+ switch (this.xValueType) {
165
+ case "time":
166
+ stringLabel = this.timeString(label);
167
+ break;
168
+ }
169
+ c.save();
170
+ c.translate(((n + 1) * (this.width - yLabelOffset)) / this.numXTicks +
171
+ this.x +
172
+ yLabelOffset, this.y + this.height + this.padding - labelOffset);
173
+ c.fillText(stringLabel, 0, 0);
174
+ c.restore();
175
+ }
176
+ c.restore();
177
+ }
178
+ /**
179
+ * Draws the Y axis of the graph.
180
+ *
181
+ * @param drawLabel Whether or not to draw the axis label.
182
+ */
183
+ drawYAxis(drawLabel) {
184
+ const c = this.context;
185
+ const labelOffset = this.yLabel ? this.baseLabelOffset : 0;
186
+ const xLabelOffset = this.xLabel ? this.baseLabelOffset : 0;
187
+ c.save();
188
+ if (this.yLabel && drawLabel) {
189
+ c.textAlign = "center";
190
+ c.font = this.axisLabelFont;
191
+ c.translate(0, this.graphHeight);
192
+ c.rotate(-Math.PI / 2);
193
+ c.fillText(this.yLabel, this.y + xLabelOffset + this.height / 2, this.x - labelOffset * 2.5);
194
+ c.restore();
195
+ }
196
+ c.beginPath();
197
+ c.moveTo(this.x + labelOffset, this.y);
198
+ c.lineTo(this.x + labelOffset, this.y + this.height - xLabelOffset);
199
+ c.strokeStyle = this.axisColor;
200
+ c.lineWidth = 2;
201
+ c.stroke();
202
+ c.restore();
203
+ // Draw tick marks
204
+ for (let n = 0; n < this.numYTicks; ++n) {
205
+ c.beginPath();
206
+ c.moveTo(this.x + labelOffset, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
207
+ c.lineTo(this.x + labelOffset + this.tickSize, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
208
+ c.stroke();
209
+ }
210
+ // Draw values
211
+ c.font = this.font;
212
+ c.fillStyle = "black";
213
+ c.textAlign = "right";
214
+ c.textBaseline = "middle";
215
+ for (let n = 0; n < this.numYTicks; ++n) {
216
+ const value = Math.round(this.maxY - (n * this.maxY) / this.numYTicks);
217
+ c.save();
218
+ c.translate(this.x + labelOffset - this.padding, (n * (this.height - xLabelOffset)) / this.numYTicks + this.y);
219
+ c.fillText(value.toString(), 0, 0);
220
+ c.restore();
221
+ }
222
+ c.restore();
223
+ }
224
+ /**
225
+ * Transforms the context and move it to the center of the graph.
226
+ */
227
+ transformContext() {
228
+ const c = this.context;
229
+ // Move context to point (0, 0) in graph
230
+ c.translate(this.x + (this.yLabel ? this.baseLabelOffset : 0), this.y + this.height - (this.xLabel ? this.baseLabelOffset : 0));
231
+ // Invert the Y scale so that it
232
+ // increments as we go upwards
233
+ c.scale(1, -1);
234
+ }
235
+ /**
236
+ * Gets the longest width from each label text in Y axis.
237
+ */
238
+ getLongestValueWidth() {
239
+ this.context.font = this.font;
240
+ let longestValueWidth = 0;
241
+ for (let n = 0; n < this.numYTicks; ++n) {
242
+ const value = this.maxY - n * this.unitsPerTickY;
243
+ let stringValue = value.toString();
244
+ switch (this.yValueType) {
245
+ case "time":
246
+ stringValue = this.timeString(value);
247
+ break;
248
+ }
249
+ longestValueWidth = Math.max(longestValueWidth, this.context.measureText(stringValue).width);
250
+ }
251
+ return longestValueWidth;
252
+ }
253
+ /**
254
+ * Sets the background of the graph.
255
+ */
256
+ setBackground() {
257
+ if (!this.background) {
258
+ this.context.globalAlpha = 0.7;
259
+ this.context.fillStyle = "#ffffff";
260
+ this.context.fillRect(0, 0, this.canvas.width, this.canvas.height);
261
+ this.context.fillStyle = "#000000";
262
+ return;
263
+ }
264
+ this.context.globalAlpha = 1;
265
+ this.context.drawImage(this.background, 0, 0, this.canvas.width, this.canvas.height);
266
+ this.context.globalAlpha = 0.8;
267
+ this.context.fillStyle = "#bbbbbb";
268
+ this.context.fillRect(0, 0, 900, 250);
269
+ this.context.globalAlpha = 1;
270
+ this.context.fillStyle = "#000000";
271
+ }
272
+ /**
273
+ * Time string parsing function for axis labels.
274
+ */
275
+ timeString(second) {
276
+ return new Date(1000 * Math.ceil(second))
277
+ .toISOString()
278
+ .substr(11, 8)
279
+ .replace(/^[0:]+/, "");
280
+ }
281
+ }
282
+ exports.Chart = Chart;
package/dist/index.js ADDED
@@ -0,0 +1,66 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const osu_base_1 = require("@rian8337/osu-base");
4
+ const canvas_1 = require("canvas");
5
+ const Chart_1 = require("./Chart");
6
+ /**
7
+ * Generates the strain chart of beatmap beatmap and returns the chart as a buffer.
8
+ *
9
+ * @param beatmap The beatmap to generate the strain graph for.
10
+ * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
11
+ * @param color The color of the graph.
12
+ */
13
+ async function getStrainChart(beatmap, beatmapsetID, color = "#000000") {
14
+ if ([
15
+ beatmap.strainPeaks.aimWithSliders.length,
16
+ beatmap.strainPeaks.aimWithoutSliders.length,
17
+ beatmap.strainPeaks.speed.length,
18
+ beatmap.strainPeaks.flashlight.length,
19
+ ].some((v) => v === 0)) {
20
+ return null;
21
+ }
22
+ const sectionLength = 400;
23
+ const currentSectionEnd = Math.ceil(beatmap.map.objects[0].startTime / sectionLength) *
24
+ sectionLength;
25
+ const strainInformations = new Array(Math.max(beatmap.strainPeaks.aimWithSliders.length, beatmap.strainPeaks.speed.length, beatmap.strainPeaks.flashlight.length));
26
+ for (let i = 0; i < strainInformations.length; ++i) {
27
+ const aimStrain = beatmap.strainPeaks.aimWithSliders[i] ?? 0;
28
+ const speedStrain = beatmap.strainPeaks.speed[i] ?? 0;
29
+ const flashlightStrain = beatmap.strainPeaks.flashlight[i] ?? 0;
30
+ strainInformations[i] = {
31
+ time: (currentSectionEnd + sectionLength * i) / 1000,
32
+ strain: beatmap.mods.some((m) => m instanceof osu_base_1.ModFlashlight)
33
+ ? (aimStrain + speedStrain + flashlightStrain) / 3
34
+ : (aimStrain + speedStrain) / 2,
35
+ };
36
+ }
37
+ const maxTime = strainInformations.at(-1).time ??
38
+ beatmap.objects.at(-1).object.endTime / 1000;
39
+ const maxStrain = Math.max(...strainInformations.map((v) => {
40
+ return v.strain;
41
+ }), 1);
42
+ const maxXUnits = 10;
43
+ const maxYUnits = 10;
44
+ const unitsPerTickX = Math.ceil(maxTime / maxXUnits / 10) * 10;
45
+ const unitsPerTickY = Math.ceil(maxStrain / maxYUnits / 20) * 20;
46
+ const chart = new Chart_1.Chart({
47
+ graphWidth: 900,
48
+ graphHeight: 250,
49
+ minX: 0,
50
+ minY: 0,
51
+ maxX: Math.ceil(maxTime / unitsPerTickX) * unitsPerTickX,
52
+ maxY: Math.ceil(maxStrain / unitsPerTickY) * unitsPerTickY,
53
+ unitsPerTickX,
54
+ unitsPerTickY,
55
+ background: await (0, canvas_1.loadImage)(`https://assets.ppy.sh/beatmaps/${beatmapsetID}/covers/cover.jpg`).catch(() => {
56
+ return undefined;
57
+ }),
58
+ xLabel: "Time",
59
+ yLabel: "Strain",
60
+ pointRadius: 0,
61
+ xValueType: "time",
62
+ });
63
+ chart.drawArea(strainInformations.map((v) => new osu_base_1.Vector2(v.time, v.strain)), color);
64
+ return chart.getBuffer();
65
+ }
66
+ exports.default = getStrainChart;
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@rian8337/osu-strain-graph-generator",
3
+ "version": "1.0.0",
4
+ "description": "A module for generating strain graph of an osu!standard beatmap.",
5
+ "keywords": [
6
+ "osu",
7
+ "osu-strain-graph"
8
+ ],
9
+ "author": "Rian8337 <52914632+Rian8337@users.noreply.github.com>",
10
+ "homepage": "https://github.com/Rian8337/osu-droid-module#readme",
11
+ "license": "MIT",
12
+ "main": "dist/index.js",
13
+ "types": "typings/index.d.ts",
14
+ "files": [
15
+ "dist/**",
16
+ "typings/**"
17
+ ],
18
+ "repository": {
19
+ "type": "git",
20
+ "url": "git+https://github.com/Rian8337/osu-droid-module.git"
21
+ },
22
+ "scripts": {
23
+ "build": "tsc",
24
+ "prepare": "npm run build",
25
+ "test": "echo \"No tests for this module\""
26
+ },
27
+ "bugs": {
28
+ "url": "https://github.com/Rian8337/osu-droid-module/issues"
29
+ },
30
+ "dependencies": {
31
+ "@rian8337/osu-base": "^1.0.0",
32
+ "@rian8337/osu-difficulty-calculator": "^1.0.0",
33
+ "@rian8337/osu-rebalance-difficulty-calculator": "^1.0.0",
34
+ "canvas": "^2.9.0"
35
+ },
36
+ "publishConfig": {
37
+ "access": "public"
38
+ },
39
+ "gitHead": "e4fdb9c1ed6f90e70651c1aedfdb964b61cb240d"
40
+ }
@@ -0,0 +1,21 @@
1
+ declare module "@rian8337/osu-strain-graph-generator" {
2
+ import { StarRating } from "@rian8337/osu-difficulty-calculator";
3
+ import { StarRating as RebalanceStarRating } from "@rian8337/osu-rebalance-difficulty-calculator";
4
+
5
+ //#region Functions
6
+
7
+ /**
8
+ * Generates the strain chart of beatmap beatmap and returns the chart as a buffer.
9
+ *
10
+ * @param beatmap The beatmap to generate the strain graph for.
11
+ * @param beatmapsetID The beatmapset ID to get background image from. If omitted, the background will be plain white.
12
+ * @param color The color of the graph.
13
+ */
14
+ export default function getStrainChart(
15
+ beatmap: StarRating | RebalanceStarRating,
16
+ beatmapsetID?: number,
17
+ color?: string
18
+ ): Promise<Buffer | null>;
19
+
20
+ //#endregion
21
+ }