aboutsummaryrefslogtreecommitdiffstats
path: root/frontend/ts/gameBoard.ts
blob: da5e8d686a15dd0bc3c393e8937b9de569b93604 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
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;
    }
  }
}