From 807adfa35a41a1358c8a1a679b02152c8163b20c Mon Sep 17 00:00:00 2001 From: Fumba Chibaka Date: Sat, 3 Jul 2021 23:43:22 -0600 Subject: [PATCH] validate ui state during UI thread execution (#138) * validate ui state during UI thread execution * fixup! validate ui state during UI thread execution * fixup! validate ui state during UI thread execution * fixup! validate ui state during UI thread execution * fixup! validate ui state during UI thread execution --- .github/workflows/node.js.yml | 1 + src/js/core/Board.ts | 14 +++++++++++--- src/js/screens/PlayScreen.ts | 17 ++++++++++++++++- test/TestHelper.ts | 3 +++ 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/.github/workflows/node.js.yml b/.github/workflows/node.js.yml index a4dd11e3..c285308d 100644 --- a/.github/workflows/node.js.yml +++ b/.github/workflows/node.js.yml @@ -21,6 +21,7 @@ jobs: steps: - uses: actions/checkout@v2 + - uses: c-hive/gha-yarn-cache@v1 - name: Use Node.js ${{ matrix.node-version }} uses: actions/setup-node@v2 with: diff --git a/src/js/core/Board.ts b/src/js/core/Board.ts index 9fbee7ed..e328fe62 100644 --- a/src/js/core/Board.ts +++ b/src/js/core/Board.ts @@ -12,6 +12,7 @@ import { isEmpty } from "lodash"; import SeedUI from "../ui_entities/SeedUI"; import Utility from "../Utility"; import { Queue } from "queue-typescript"; +import PlayerUI from "../ui_entities/PlayerUI"; /* * bawo.zone - https://bawo.zone @@ -430,10 +431,17 @@ class Board { */ public validateUiState(): void { if (this.isInGraphicsMode()) { - const seedCount = this.me.game.world.getChildByType(SeedUI).length; - if (seedCount != AppConstants.MAX_SEED_COUNT) { + const seedUiCount = this.me.game.world.getChildByType(SeedUI).length; + if (seedUiCount != AppConstants.MAX_SEED_COUNT) { throw new Error( - `UI State has ${seedCount} seeds present. There should always be 64 seeds in play` + `UI State has ${seedUiCount} seeds present. There should always be 64 seeds in play` + ); + } + const playerUiCount = this.me.game.world.getChildByType(PlayerUI).length; + /* istanbul ignore next */ + if (playerUiCount != 2) { + throw new Error( + `UI State has ${playerUiCount} players on board. There should always be 2.` ); } } diff --git a/src/js/screens/PlayScreen.ts b/src/js/screens/PlayScreen.ts index 921b649c..dbb9fc22 100644 --- a/src/js/screens/PlayScreen.ts +++ b/src/js/screens/PlayScreen.ts @@ -82,18 +82,33 @@ class PlayScreen extends me.Stage { } } else { const draggingSeedGroup = UiHelper.getCurrentDraggingSeedGroup(me); - //re-render all holes on board if (!draggingSeedGroup && refreshHoleSleepingState == true) { + // re-render all holes on board UiHelper.forEachBoardHole(this.board, (hole: Hole) => { hole.ui.label.setText(hole.ui.seedCount()); hole.ui.sleepStateUI(); }); refreshHoleSleepingState = false; + + // ui and non-ui game board should always be in sync + this.validateUiState(); } } }, gameSpeed); } + public validateUiState(): void { + UiHelper.forEachBoardHole(this.board, (hole: Hole) => { + if (hole.numSeeds != hole.ui.seedCount()) { + throw new Error( + `UI (${hole.ui.seedCount()}) and non-UI (${ + hole.numSeeds + }) board not in sync : ${hole.toString()}` + ); + } + }); + } + onDestroyEvent(): void { // remove the HUD from the game world me.game.world.removeChild(this.HUD); diff --git a/test/TestHelper.ts b/test/TestHelper.ts index c068efe9..6282c850 100644 --- a/test/TestHelper.ts +++ b/test/TestHelper.ts @@ -37,6 +37,9 @@ class TestHelper { } return seeds; } + case "PlayerUI": { + return ["top", "bottom"]; + } default: /* istanbul ignore next */ return [];