Skip to content

Commit

Permalink
Clamp the value between 0 and 1 to avoid tinygradient errors. (#343)
Browse files Browse the repository at this point in the history
* 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](#342). 
Now we clamp the ratio between 0 and 1, which prevents an error related to tinygradient

* Removed unnecessary indentation via lint
  • Loading branch information
mxrcode authored Feb 5, 2024
1 parent 9de44f6 commit 923c601
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion squad-server/plugins/discord-server-status.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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
)
Expand Down

0 comments on commit 923c601

Please sign in to comment.