Skip to content

Commit

Permalink
validate ui state during UI thread execution (#138)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
fumba authored Jul 4, 2021
1 parent d42ecbe commit 807adfa
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 4 deletions.
1 change: 1 addition & 0 deletions .github/workflows/node.js.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
14 changes: 11 additions & 3 deletions src/js/core/Board.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 - <a href="https://bawo.zone">https://bawo.zone</a>
Expand Down Expand Up @@ -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.`
);
}
}
Expand Down
17 changes: 16 additions & 1 deletion src/js/screens/PlayScreen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions test/TestHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ class TestHelper {
}
return seeds;
}
case "PlayerUI": {
return ["top", "bottom"];
}
default:
/* istanbul ignore next */
return [];
Expand Down

0 comments on commit 807adfa

Please sign in to comment.