aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/ts/gameBoard.ts
diff options
context:
space:
mode:
authorMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-25 12:07:36 +0200
committerMaks Jopek <maksymilian.jopek@prym-soft.pl>2021-04-25 12:07:36 +0200
commit013319d7a4651d1708280f556e39c2f03839426a (patch)
tree704e51c6d4d96360611819628be3c78edec2f9d8 /frontend/ts/gameBoard.ts
parent13dcc2c8fc1d11e3b94432fffc4c4f186bd94d29 (diff)
downloadfia-013319d7a4651d1708280f556e39c2f03839426a.tar.gz
fia-013319d7a4651d1708280f556e39c2f03839426a.tar.zst
fia-013319d7a4651d1708280f556e39c2f03839426a.zip
Almost going in a circle
Diffstat (limited to 'frontend/ts/gameBoard.ts')
-rw-r--r--frontend/ts/gameBoard.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/frontend/ts/gameBoard.ts b/frontend/ts/gameBoard.ts
new file mode 100644
index 0000000..da5e8d6
--- /dev/null
+++ b/frontend/ts/gameBoard.ts
@@ -0,0 +1,81 @@
+import { Coordinates, GameBoard, HomesOrBases, tColors } from "../../helpers/helpersBack";
+import gameTable from "./gameTable.js";
+import StartGame from "./startGame.js";
+
+export default class gameBoardClass {
+ static drawGameBoard(gameBoard: GameBoard, drawChequer: Function): void {
+ gameBoardClass.removeGameBoard(gameBoard);
+ for (let square of gameBoard.map) {
+ if (square.chequers.length !== 0) {
+ for (let chequer of square.chequers)
+ drawChequer({ x: square.x, y: square.y }, chequer.color, chequer);
+ }
+ }
+ for (let baseOfColor in gameBoard.bases) {
+ for (let square of gameBoard.bases[baseOfColor as unknown as keyof HomesOrBases]) {
+ if (square.chequer > -1)
+ drawChequer({ x: square.x, y: square.y }, square.chequer, square);
+ }
+ }
+ for (let baseOfColor in gameBoard.homes) {
+ for (let square of gameBoard.homes[baseOfColor as unknown as keyof HomesOrBases]) {
+ if (square.chequer > -1)
+ drawChequer({ x: square.x, y: square.y }, square.chequer, square);
+ }
+ }
+ }
+ static removeGameBoard(gameBoard: GameBoard): void {
+ for (let square of gameBoard.map) {
+ gameBoardClass.removeChequer({ x: square.x, y: square.y }, true);
+ }
+ for (let baseOfColor in gameBoard.bases) {
+ for (let square of gameBoard.bases[baseOfColor as unknown as keyof HomesOrBases]) {
+ gameBoardClass.removeChequer({ x: square.x, y: square.y }, true);
+ }
+ }
+ for (let baseOfColor in gameBoard.homes) {
+ for (let square of gameBoard.homes[baseOfColor as unknown as keyof HomesOrBases]) {
+ gameBoardClass.removeChequer({ x: square.x, y: square.y }, true);
+ }
+ }
+ }
+ static removeChequer(coords: Coordinates, all: boolean) {
+ let td = gameTable.getTd(coords);
+ gameBoardClass.hideChequer(coords);
+ td.onmouseenter = () => null;
+ td.onmouseleave = () => null;
+ td.onclick = () => null;
+ }
+
+ static showChequer(coords: Coordinates, color: tColors): void {
+ let td = gameTable.getTd(coords);
+ if (td.dataset.count === undefined)
+ throw new Error("td.dataset.count is undefined!!!")
+
+ if (td.dataset.count === '0') {
+ td.classList.add("chequer-" + StartGame.colors[color]);
+ td.dataset.count = '1';
+ td.innerHTML = '';
+ } else {
+ td.dataset.count = (parseInt(td.dataset.count) + 1).toString();
+ td.innerHTML = 'x' + td.dataset.count;
+ }
+ }
+ static hideChequer(coords: Coordinates, all = false) {
+ let td = gameTable.getTd(coords);
+
+ if (td.dataset.count === '0')
+ return;
+ if (td.dataset.count === undefined)
+ throw new Error("td.dataset.count is undefined!!!")
+
+ if (td.dataset.count === '1' || all === true) {
+ td.className = td.className.replace(/chequer-.*/, '');
+ td.dataset.count = '0';
+ td.innerHTML = '';
+ } else {
+ td.dataset.count = (parseInt(td.dataset.count) - 1).toString();
+ td.innerHTML = 'x' + td.dataset.count;
+ }
+ }
+} \ No newline at end of file