From 347ec6972f34bb95564f92c36830b4e1956bfcfa Mon Sep 17 00:00:00 2001 From: Ilion Beyst Date: Fri, 11 Nov 2022 12:05:10 +0100 Subject: [PATCH] visualizer: ensure that turn number remains within bounds --- web/pw-visualizer/src/index.ts | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/web/pw-visualizer/src/index.ts b/web/pw-visualizer/src/index.ts index f97bd2b..74b2319 100644 --- a/web/pw-visualizer/src/index.ts +++ b/web/pw-visualizer/src/index.ts @@ -51,6 +51,16 @@ export function set_loading(loading: boolean) { } } +function clamp(min: number, max: number, value: number): number { + if (value < min) { + return min; + } + if (value > max) { + return max; + } + return value; +} + const ELEMENTS: any = {}; var CANVAS: any; var RESOLUTION: any; @@ -561,7 +571,7 @@ export class GameInstance { } updateTurn(turn: number) { - this.frame = Math.max(0, turn); + this.frame = clamp(0, this.turn_count-1, turn); this.game.update_turn(this.frame); this._update_state(); this.updateTurnCounters();