From 923c601292cf9264fd8b30cfa7baec9acefb45d8 Mon Sep 17 00:00:00 2001 From: mxrcode <123785508+mxrcode@users.noreply.github.com> Date: Mon, 5 Feb 2024 04:23:08 +0200 Subject: [PATCH] Clamp the value between 0 and 1 to avoid tinygradient errors. (#343) * Clamp the value between 0 and 1 to avoid tinygradient errors. Previously, we could get a coefficient greater than 1, causing SquadJS to crash. One case is described [here](https://github.com/Team-Silver-Sphere/SquadJS/issues/342). Now we clamp the ratio between 0 and 1, which prevents an error related to tinygradient * Removed unnecessary indentation via lint --- squad-server/plugins/discord-server-status.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/squad-server/plugins/discord-server-status.js b/squad-server/plugins/discord-server-status.js index 180c6f693..a3c9d4a6b 100644 --- a/squad-server/plugins/discord-server-status.js +++ b/squad-server/plugins/discord-server-status.js @@ -99,6 +99,10 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater { // Set footer. embed.setFooter(COPYRIGHT_MESSAGE); + // Clamp the ratio between 0 and 1 to avoid tinygradient errors. + const ratio = this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots); + const clampedRatio = Math.min(1, Math.max(0, ratio)); + // Set gradient embed color. embed.setColor( parseInt( @@ -107,7 +111,7 @@ export default class DiscordServerStatus extends DiscordBaseMessageUpdater { { color: '#ffff00', pos: 0.5 }, { color: '#00ff00', pos: 1 } ]) - .rgbAt(this.server.a2sPlayerCount / (this.server.publicSlots + this.server.reserveSlots)) + .rgbAt(clampedRatio) .toHex(), 16 )