From 23fb8e123998c28aacbe5d544e709248453d4959 Mon Sep 17 00:00:00 2001 From: toast-ts <96593068+toast-ts@users.noreply.github.com> Date: Sat, 17 Feb 2024 18:58:34 +1100 Subject: [PATCH] Update CanvasBuilder to use methods instead of functions --- src/commands/mp.ts | 4 ++-- src/commands/rank.ts | 4 ++-- .../{CanvasGraph.ts => CanvasBuilder.ts} | 16 +++++----------- 3 files changed, 9 insertions(+), 15 deletions(-) rename src/components/{CanvasGraph.ts => CanvasBuilder.ts} (93%) diff --git a/src/commands/mp.ts b/src/commands/mp.ts index 6d5fa31..ccad14d 100644 --- a/src/commands/mp.ts +++ b/src/commands/mp.ts @@ -1,7 +1,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Logger from '../helpers/Logger.js'; -import CanvasBuilder from '../components/CanvasGraph.js'; +import CanvasBuilder from '../components/CanvasBuilder.js'; import RanIntoHumor from '../helpers/RanIntoHumor.js'; import MessageTool from '../helpers/MessageTool.js'; import PalletLibrary from '../helpers/PalletLibrary.js'; @@ -43,7 +43,7 @@ export default class MP { if (!DSS) return console.log('Endpoint failed - players'); const PDArr = await client.MPServer.fetchPlayerData(choiceSelector); - const canvas = await new CanvasBuilder().generateGraph(PDArr.slice(-120), 'players'); + const canvas = await CanvasBuilder.generateGraph(PDArr.slice(-120), 'players'); const players:string[] = []; let embedColor:Discord.ColorResolvable; diff --git a/src/commands/rank.ts b/src/commands/rank.ts index 6e3c750..49fc66f 100644 --- a/src/commands/rank.ts +++ b/src/commands/rank.ts @@ -3,7 +3,7 @@ import Discord from 'discord.js'; import TClient from '../client.js'; import Formatters from '../helpers/Formatters.js'; import MessageTool from '../helpers/MessageTool.js'; -import CanvasBuilder from '../components/CanvasGraph.js'; +import CanvasBuilder from '../components/CanvasBuilder.js'; export default class Rank { static async run(client: TClient, interaction: Discord.ChatInputCommandInteraction<'cached'>){ if (interaction.guildId !== client.config.dcServer.id) return interaction.reply({content: 'This command doesn\'t work in this server.', ephemeral: true}); @@ -32,7 +32,7 @@ export default class Rank { }); if (data.length < 2) return interaction.reply('Not enough data to generate graph.'); - const graph = await new CanvasBuilder().generateGraph(data, 'leaderboard'); + const graph = await CanvasBuilder.generateGraph(data, 'leaderboard'); interaction.reply({ embeds: [new client.embed().setColor(client.config.embedColor).setTitle('Leaderboard') .setDescription(MessageTool.concatMessage( diff --git a/src/components/CanvasGraph.ts b/src/components/CanvasBuilder.ts similarity index 93% rename from src/components/CanvasGraph.ts rename to src/components/CanvasBuilder.ts index e77c28d..6e8f42f 100644 --- a/src/components/CanvasGraph.ts +++ b/src/components/CanvasBuilder.ts @@ -2,10 +2,10 @@ import {createCanvas, Canvas, SKRSContext2D} from '@napi-rs/canvas'; import {Config} from 'src/interfaces'; import ConfigHelper from '../helpers/ConfigHelper.js'; export default class CanvasBuilder { - private readonly canvas: Canvas; - private readonly ctx: SKRSContext2D; - private readonly config: Config; - private readonly palette = { + private static readonly canvas: Canvas = createCanvas(1500, 750); + private static readonly ctx: SKRSContext2D = this.canvas.getContext('2d'); + private static readonly config: Config = ConfigHelper.readConfig(); + private static readonly palette = { // Color palette for the graph -- The variables are named exactly what it shows in graph to make it easier to be referenced to. oddHorizontal: '#555B63', evenHorizontal: '#3E4245', @@ -16,13 +16,7 @@ export default class CanvasBuilder { greenLine: '#57F287' }; - constructor() { - this.canvas = createCanvas(1500, 750); - this.ctx = this.canvas.getContext('2d'); - this.config = ConfigHelper.readConfig() as Config; - } - - public async generateGraph(data:number[], type:'players'|'leaderboard'):Promise { + public static async generateGraph(data:number[], type:'players'|'leaderboard'):Promise { // Handle negative for (const [i, change] of data.entries()) if (change < 0) data[i] = data[i - 1] || data[i + 1] || 0;