aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--backend/ts/api/getCurrentGameState.ts8
-rw-r--r--backend/ts/api/login.ts13
-rw-r--r--backend/ts/api/readyStateToggle.ts4
-rw-r--r--backend/ts/api/saveGameBoard.ts5
-rw-r--r--backend/ts/index.ts1
-rw-r--r--backend/ts/startGame.ts7
-rw-r--r--frontend/ts/gameBoard.ts25
-rw-r--r--frontend/ts/gameTable.ts1
-rw-r--r--frontend/ts/ghost.ts25
-rw-r--r--frontend/ts/logic.ts213
-rw-r--r--frontend/ts/login.ts17
-rw-r--r--frontend/ts/startGame.ts113
-rw-r--r--frontend/ts/timer.ts1
-rw-r--r--helpers/helpers.ts5
-rw-r--r--helpers/helpersBack.ts5
-rw-r--r--public/css/fia.css7
-rw-r--r--public/index.html37
-rw-r--r--tsconfig.json10
18 files changed, 270 insertions, 227 deletions
diff --git a/backend/ts/api/getCurrentGameState.ts b/backend/ts/api/getCurrentGameState.ts
index 028cf04..28522e1 100644
--- a/backend/ts/api/getCurrentGameState.ts
+++ b/backend/ts/api/getCurrentGameState.ts
@@ -1,8 +1,7 @@
import { Request, Response } from "express";
-import { Data, makeQuery, getData, API_RES, GameBoard } from "../../../helpers/helpersBack";
+import { Data, makeQuery, API_RES, GameBoard } from "../../../helpers/helpersBack";
export default async function getCurrentGameState(req: Request, res: Response) {
- // console.log("request.session", req.session)
if (req.session === undefined || req.session.gid === undefined) {
res.status(400);
res.send(API_RES.failure);
@@ -13,13 +12,10 @@ export default async function getCurrentGameState(req: Request, res: Response) {
= (await makeQuery("SELECT `gameBoard`, `currentPlayer`, `started`, `data`, `timeTillTurnEnd`"
+ " FROM `fia` WHERE `id` = ?",
[req.session.gid]))[0];
-
if (dbRes.started === 0) {
res.send(JSON.stringify({ data: dbRes.data }));
} else {
res.send(JSON.stringify(dbRes));
}
-
-
-} \ No newline at end of file
+}
diff --git a/backend/ts/api/login.ts b/backend/ts/api/login.ts
index 4b5ff87..a957760 100644
--- a/backend/ts/api/login.ts
+++ b/backend/ts/api/login.ts
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
-import { fiaTable, dbRes, Color, Colors, Helpers, makeQuery, Chequer, tColors, waitForQueue } from "../../../helpers/helpersBack";
+import { fiaTable, Helpers, makeQuery, tColors, waitForQueue } from "../../../helpers/helpersBack";
import startGame from "../startGame";
import { global } from "../index";
@@ -27,24 +27,19 @@ export default async function apiLogin(req: Request, res: Response) {
id: req.session.uid,
index: 0,
ready: false,
- color: Color.red,
+ color: color,
name: req.body.name,
}];
+
req.session.color = color;
await makeQuery('INSERT INTO `fia`(`data`) VALUES (?)', [row.data]);
- req.session.gid = (await makeQuery('SELECT `id` FROM `fia` WHERE `data`= ?', [row.data]) as any)[0].id;
+ req.session.gid = (await makeQuery('SELECT `id` FROM `fia` WHERE `data`= ?', [row.data]))[0].id;
} else {
- // row = Helpers.dbResToFiaTable(dbRes[0]);
row = dbRes[0];
let playersColors: Array<tColors> = [],
colors = [...Array(4).keys()];
- if (row.data.filter(el => el.id !== req.session?.uid).length === 0) {
- res.send(JSON.stringify({ data: row.data, uid: req.session.uid }));
- return;
- }
-
for (let playerData of row.data)
playersColors.push(playerData.color);
diff --git a/backend/ts/api/readyStateToggle.ts b/backend/ts/api/readyStateToggle.ts
index e1b08b2..fc14f72 100644
--- a/backend/ts/api/readyStateToggle.ts
+++ b/backend/ts/api/readyStateToggle.ts
@@ -1,5 +1,5 @@
import { Request, Response } from "express";
-import { makeQuery, Data, getData, API_RES, waitForQueue } from "../../../helpers/helpersBack";
+import { makeQuery, Data, API_RES, waitForQueue } from "../../../helpers/helpersBack";
import startGame from "../startGame";
import {global} from "../index";
@@ -21,7 +21,6 @@ export default async function (req: Request, res: Response) {
started = false;
if (data.started === 0) {
-
for (let player of data.data) {
if (player.id === req.session.uid)
player.ready = req.body.state ? true : false;
@@ -36,7 +35,6 @@ export default async function (req: Request, res: Response) {
startGame(req.session.gid);
started = true;
}
-
}
global.stop = false;
res.send(`{"success": true, "started": ${started}}`);
diff --git a/backend/ts/api/saveGameBoard.ts b/backend/ts/api/saveGameBoard.ts
index df2b7a0..7871ff5 100644
--- a/backend/ts/api/saveGameBoard.ts
+++ b/backend/ts/api/saveGameBoard.ts
@@ -7,11 +7,10 @@ export default async function saveGameBoard(req: Request, res: Response) {
res.send(API_RES.failure);
return;
}
- console.log("saving gameBoard");
+
let query = "UPDATE `fia` SET `currentPlayer` = IF(`currentPlayer` + 1 = `playersCount`, 0, `currentPlayer` + 1), "
- + "`gameBoard` = ?, `timeTillTurnEnd` = ? WHERE `id` = ?"
+ + "`gameBoard` = ?, `timeTillTurnEnd` = ? WHERE `id` = ?";
await makeQuery(query, [req.body.gameBoard, nextTurnEndsAt(), req.session.gid]);
res.send(API_RES.success);
}
-//UPDATE `fia` SET `currentPlayer` = IF( `currentPlayer` + 1 = `playersCount`, 0, `currentPlayer` + 1 ) WHERE `id` = 169 \ No newline at end of file
diff --git a/backend/ts/index.ts b/backend/ts/index.ts
index 32ac644..dcaac7e 100644
--- a/backend/ts/index.ts
+++ b/backend/ts/index.ts
@@ -37,7 +37,6 @@ app.post("/readyStateToggle", readyStateToggle);
app.post("/getCurrentGameState", getCurrentGameState);
app.post("/saveGameBoard", saveGameBoard);
-
app.listen(PORT, () => {
console.log(`[server]: Server is running at https://localhost:${PORT}`);
});
diff --git a/backend/ts/startGame.ts b/backend/ts/startGame.ts
index c4d7131..a247982 100644
--- a/backend/ts/startGame.ts
+++ b/backend/ts/startGame.ts
@@ -1,5 +1,4 @@
-import { } from "../../helpers/helpers";
-import { makeQuery, nextTurnEndsAt, Color, GameBoard, Chequer, Coordinates, Data, getData, HomesOrBases, Square, tColors } from "../../helpers/helpersBack";
+import { makeQuery, nextTurnEndsAt, GameBoard, Data, HomesOrBases } from "../../helpers/helpersBack";
export default async function startGame(gid: number) {
let dbRes: {gameBoard: GameBoard, data: Data} =
@@ -12,6 +11,6 @@ export default async function startGame(gid: number) {
}
}
- await makeQuery("UPDATE `fia` SET `gameBoard` = ?, `started` = 1, `timeTillTurnEnd` = ? WHERE `id`= ?",
- [dbRes.gameBoard, nextTurnEndsAt(), gid]);
+ await makeQuery("UPDATE `fia` SET `gameBoard` = ?, `started` = 1, `timeTillTurnEnd` = ?, `data` = ? WHERE `id`= ?",
+ [dbRes.gameBoard, nextTurnEndsAt(), dbRes.data, gid]);
} \ No newline at end of file
diff --git a/frontend/ts/gameBoard.ts b/frontend/ts/gameBoard.ts
index 14ccac6..fc8cec7 100644
--- a/frontend/ts/gameBoard.ts
+++ b/frontend/ts/gameBoard.ts
@@ -1,4 +1,4 @@
-import { Coordinates, GameBoard, HomesOrBases, tColors } from "../../helpers/helpersBack";
+import { Coordinates, GameBoard, HomesOrBases, OldTd, tColors } from "../../helpers/helpersBack";
import gameTable from "./gameTable.js";
import StartGame from "./startGame.js";
@@ -24,6 +24,7 @@ export default class gameBoardClass {
}
}
}
+
static removeGameBoard(gameBoard: GameBoard): void {
for (let square of gameBoard.map) {
gameBoardClass.removeChequer({ x: square.x, y: square.y }, true);
@@ -47,15 +48,19 @@ export default class gameBoardClass {
td.onclick = () => null;
}
- static showChequer(coords: Coordinates, color: tColors): void {
+ static showChequer(coords: Coordinates, color: tColors, oldColor = 'void'): void {
let td = gameTable.getTd(coords);
if (td.dataset.count === undefined)
throw new Error("td.dataset.count is undefined!!!")
-
- if(td.className === '')
+
+ if (td.className === '')
td.classList.add("chequer");
-
- if (td.dataset.count === '0') {
+
+ if (color === 4 && !td.className.includes(oldColor) && !td.className.includes('pink')) {
+ td.className = 'chequer chequer-' + StartGame.colors[color];
+ td.innerHTML = '';
+ td.dataset.count = '1';
+ } else if (td.dataset.count === '0') {
td.classList.add("chequer-" + StartGame.colors[color]);
td.dataset.count = '1';
td.innerHTML = '';
@@ -64,13 +69,17 @@ export default class gameBoardClass {
td.innerHTML = td.dataset.count === '1' ? '' : 'x' + td.dataset.count;
}
}
- static hideChequer(coords: Coordinates, all = false) {
+ static hideChequer(coords: Coordinates, all = false, oldTd: OldTd = { className: '', innerHTML: '', count: '' }) {
let td = gameTable.getTd(coords);
if (td.dataset.count === undefined)
throw new Error("td.dataset.count is undefined!!!")
- if (all === true || ['0', '1'].includes(td.dataset.count)) {
+ if (oldTd.className !== '' && oldTd.count !== '') {
+ td.className = oldTd.className;
+ td.innerHTML = oldTd.innerHTML;
+ td.dataset.count = oldTd.count;
+ } else if (all === true || ['0', '1'].includes(td.dataset.count)) {
td.className = 'chequer';
td.dataset.count = '0';
td.innerHTML = '';
diff --git a/frontend/ts/gameTable.ts b/frontend/ts/gameTable.ts
index a0f9c6a..94051f8 100644
--- a/frontend/ts/gameTable.ts
+++ b/frontend/ts/gameTable.ts
@@ -16,6 +16,7 @@ export default class gameTable {
gameTable.gameTable.appendChild(tr);
}
}
+
static getTd(coords: Coordinates): HTMLElement {
return gameTable.gameTable.children[coords.y].children[coords.x] as HTMLElement;
}
diff --git a/frontend/ts/ghost.ts b/frontend/ts/ghost.ts
index 208127d..9a6008a 100644
--- a/frontend/ts/ghost.ts
+++ b/frontend/ts/ghost.ts
@@ -1,25 +1,36 @@
-import { Chequer, GameBoard, HoBSquare, HomesOrBases, Square, tColors } from "../../helpers/helpersBack";
+import { Chequer, GameBoard, HoBSquare, HomesOrBases, OldTd, Square, tColors } from "../../helpers/helpersBack";
import gameBoardClass from "./gameBoard.js";
import gameTable from "./gameTable.js";
+import StartGame from "./startGame.js";
export default class ghost {
static ghostsTds: Array<{ td: HTMLElement, class: string }> = [];
static interval: any;
static hide = true;
+ static oldTd: OldTd;
+
static showGhost(chequer: Chequer | HoBSquare, color: tColors) {
if (chequer.ghost === undefined || chequer.ghost.coords === undefined) {
chequer.ghost?.coords === undefined ? console.log("showShost::chequer.ghost.coords === undefined", chequer.ghost?.coords === undefined) : '';
return;
}
- gameBoardClass.showChequer(chequer.ghost.coords, 4)
+ let td = gameTable.getTd(chequer.ghost.coords);
+ if (td.dataset.count === undefined) {
+ throw new Error("td.dataset.count === undefined");
+ }
+ ghost.oldTd = { className: td.className, innerHTML: td.innerHTML, count: td.dataset.count };
+ gameBoardClass.showChequer(chequer.ghost.coords, 4, StartGame.colors[color])
}
+
static hideGhost(chequer: Chequer | HoBSquare): void {
if (chequer.ghost === undefined || chequer.ghost.coords === undefined) {
chequer.ghost?.coords === undefined ? console.log("hideGhost::chequer.ghost.coords === undefined", chequer.ghost?.coords === undefined) : '';
return;
}
- gameBoardClass.hideChequer(chequer.ghost.coords);
+ gameBoardClass.hideChequer(chequer.ghost.coords, false, ghost.oldTd);
+ ghost.oldTd = { className: '', innerHTML: '', count: '' };
}
+
static removeGhosts(gameBoard: GameBoard, color: tColors): void {
for (let square of gameBoard.map) {
for (let chequer of square.chequers) {
@@ -33,6 +44,7 @@ export default class ghost {
chequer.ghost = undefined;
}
}
+
static anyGhosts(gameBoard: GameBoard, color: tColors): boolean {
for (let square of gameBoard.map) {
for (let chequer of square.chequers) {
@@ -50,6 +62,7 @@ export default class ghost {
}
return false;
}
+
static allBlink(gameBoard: GameBoard, color: tColors) {
for (let square of gameBoard.map) {
if (square.chequers.some(el => el.ghost !== undefined) === true)
@@ -64,18 +77,22 @@ export default class ghost {
ghost.blink(chequer);
}
}
+
static blink(chequer: Square | HoBSquare) {
if (ghost.interval === undefined)
ghost.interval = setInterval(ghost.blinking, 700);
let td = gameTable.getTd({ x: chequer.x, y: chequer.y });
ghost.ghostsTds.push({ td, class: td.className });
}
+
static blinking() {
for (let td of ghost.ghostsTds) {
- td.td.className = ghost.hide === true ? '' : td.class;
+ if (!td.td.className.includes("white"))
+ td.td.className = ghost.hide === true ? 'chequer chequer-pink' : td.class;
}
ghost.hide = !ghost.hide;
}
+
static allStopBlink() {
clearInterval(ghost.interval);
ghost.interval = undefined;
diff --git a/frontend/ts/logic.ts b/frontend/ts/logic.ts
index 438bd8c..015226e 100644
--- a/frontend/ts/logic.ts
+++ b/frontend/ts/logic.ts
@@ -1,126 +1,119 @@
import { Coordinates, GameBoard, HomesOrBases, Square, tColors } from "../../helpers/helpersBack";
import Helpers from "../../helpers/helpers.js"
export default class Logic {
- static moveFromBase(gameBoard: GameBoard, color: tColors,) {
- let start: Square, i: number;
+ static moveFromBase(gameBoard: GameBoard, color: tColors,) {
+ let start: Square, i: number;
- gameBoard.map.forEach((square, j) => {
- if (square.start === color) {
- start = square;
- i = j;
- }
- })
+ gameBoard.map.forEach((square, j) => {
+ if (square.start === color) {
+ start = square;
+ i = j;
+ }
+ })
- gameBoard.bases[color as keyof HomesOrBases].forEach((chequer, j) => {
- if (chequer.chequer > -1) {
- chequer.ghost = {
- where: {
- from: "base", oldIndex: j,
- to: "map", newIndex: i
- },
- color: color,
- coords: { x: start.x, y: start.y }
- };
- }
- })
+ gameBoard.bases[color as keyof HomesOrBases].forEach((chequer, j) => {
+ if (chequer.chequer > -1) {
+ chequer.ghost = {
+ where: {
+ from: "base", oldIndex: j,
+ to: "map", newIndex: i
+ },
+ color: color,
+ coords: { x: start.x, y: start.y }
+ };
+ }
+ })
- return gameBoard;
- }
- static moveInMap(gameBoard: GameBoard, color: tColors, number: number) {
- console.log("moveInMap::color ", color)
- gameBoard.map.forEach((square, i) => {
- if (square.chequers.length > 0 && square.chequers[0].color === color) {
- for (let chequer of square.chequers) {
- let x: number | undefined, y: number | undefined, coords: Coordinates | undefined, inMap: boolean,
- homeIndex: number | undefined, befStart: number | undefined;
+ return gameBoard;
+ }
+ static moveInMap(gameBoard: GameBoard, color: tColors, number: number) {
+ gameBoard.map.forEach((square, i) => {
+ if (square.chequers.length > 0 && square.chequers[0].color === color) {
+ for (let chequer of square.chequers) {
+ let x: number | undefined, y: number | undefined, coords: Coordinates | undefined, inMap: boolean,
+ homeIndex: number | undefined, befStart: number | undefined;
- if (color !== Helpers.Color.red) {
- gameBoard.map.forEach((square, i) => {
- if (square.start === color)
- befStart = i - 1;
- });
- } else {
- befStart = gameBoard.map.length - 1;
- }
- if (befStart === undefined)
- throw new Error("before start doesnt exist");
+ if (color !== Helpers.Color.red) {
+ gameBoard.map.forEach((square, i) => {
+ if (square.start === color)
+ befStart = i - 1;
+ });
+ } else {
+ befStart = gameBoard.map.length - 1;
+ }
+ if (befStart === undefined)
+ throw new Error("before start doesnt exist");
- let range = (start: number, end: number) => Array.from(Array(end + 1).keys()).slice(start);
- console.log("range ", range(i, i + number));
- console.log("homeIndex ", befStart);
- // if(i >= befStart && i + number <= befStart) {
- if (range(i, i + number).includes(befStart)) {
- // debugger;
- // homeIndex = gameBoard.map.length - i - 1 - number - 1;
- homeIndex = number - befStart + i - 1;
- inMap = false;
+ let range = (start: number, end: number) => Array.from(Array(end + 1).keys()).slice(start);
+ // if(i >= befStart && i + number <= befStart) {
+ if (range(i, i + number).includes(befStart)) {
+ homeIndex = number - befStart + i - 1;
+ inMap = false;
- if (homeIndex < gameBoard.homes[0].length) {
- x = gameBoard.homes[color as keyof HomesOrBases][homeIndex].x;
- y = gameBoard.homes[color as keyof HomesOrBases][homeIndex].y;
- }
- else {
- x = y = undefined;
- }
- console.log("toHome check ", { x, y, homeIndex, befStart });
- } else {
- if (i + number >= gameBoard.map.length) {
- // debugger;
- number += i - gameBoard.map.length - i; //* -$i 'cause adding $i on 62 && 63
- }// i += number - gameBoard.map.length - 1 - number; //* -$number 'cause adding $number on 62 && 63
- console.log("newIndex: ", i + number)
- x = gameBoard.map[i + number].x;
- y = gameBoard.map[i + number].y;
- inMap = true;
- }
+ if (homeIndex < gameBoard.homes[0].length) {
+ x = gameBoard.homes[color as keyof HomesOrBases][homeIndex].x;
+ y = gameBoard.homes[color as keyof HomesOrBases][homeIndex].y;
+ }
+ else {
+ x = y = undefined;
+ }
+ } else {
+ if (i + number >= gameBoard.map.length)
+ number += i - gameBoard.map.length - i; //* -$i 'cause adding $i on 62 && 63
- if (x === undefined || y === undefined)
- coords = undefined;
- else
- coords = { x, y };
+ x = gameBoard.map[i + number].x;
+ y = gameBoard.map[i + number].y;
+ inMap = true;
+ }
- chequer.ghost = {
- where: {
- from: "map", oldIndex: i,
- to: (inMap === true ? "map" : "home"), newIndex: homeIndex || i + number
- },
- color: color,
- coords,
- }
- }
- }
- })
- return gameBoard;
- }
- static moveInHome(gameBoard: GameBoard, color: tColors, number: number) {
- let home = gameBoard.homes[color as keyof HomesOrBases];
+ if (x === undefined || y === undefined)
+ coords = undefined;
+ else
+ coords = { x, y };
- home.forEach((chequer, i) => {
- if (chequer.chequer > -1) {
- let x: number | undefined, y: number | undefined, coords: Coordinates | undefined;
- if (home[i + number] !== undefined) {
- x = home[i + number].x;
- y = home[i + number].y;
- } else {
- x = y = undefined;
- }
+ chequer.ghost = {
+ where: {
+ from: "map", oldIndex: i,
+ to: (inMap === true ? "map" : "home"),
+ newIndex: homeIndex || i + number
+ },
+ color: color,
+ coords,
+ }
+ }
+ }
+ })
+ return gameBoard;
+ }
+ static moveInHome(gameBoard: GameBoard, color: tColors, number: number) {
+ let home = gameBoard.homes[color as keyof HomesOrBases];
- if (x === undefined || y === undefined)
- coords = undefined;
- else
- coords = { x, y };
+ home.forEach((chequer, i) => {
+ if (chequer.chequer > -1) {
+ let x: number | undefined, y: number | undefined, coords: Coordinates | undefined;
+ if (home[i + number] !== undefined) {
+ x = home[i + number].x;
+ y = home[i + number].y;
+ } else {
+ x = y = undefined;
+ }
- chequer.ghost = {
- where: {
- from: "home", oldIndex: i,
- to: "home", newIndex: i + number
- },
- color: color,
- coords,
- };
- }
- });
+ if (x === undefined || y === undefined)
+ coords = undefined;
+ else
+ coords = { x, y };
+
+ chequer.ghost = {
+ where: {
+ from: "home", oldIndex: i,
+ to: "home", newIndex: i + number
+ },
+ color: color,
+ coords,
+ };
+ }
+ });
- return gameBoard;
- }
+ return gameBoard;
+ }
} \ No newline at end of file
diff --git a/frontend/ts/login.ts b/frontend/ts/login.ts
index 2e50884..c02aafd 100644
--- a/frontend/ts/login.ts
+++ b/frontend/ts/login.ts
@@ -4,11 +4,12 @@ import Helpers from "../../helpers/helpers.js";
class Login {
static html = /* html */ `
<div class="container text-center fs-1" style="margin-top: 45vh;">
- <label for="username">Vad heter du?</label>
- <input type="text" name="username" required>
- <button type="button" class="btn btn-light btn-lg" onclick="Login.login">Starta spelet</button>
+ <label for="username">Vad heter du?</label>
+ <input type="text" name="username" required>
+ <button type="button" class="btn btn-light btn-lg" onclick="Login.login">Starta spelet</button>
</div>
`;
+ static clicked = false;
static async start() {
let res = await (await fetch("/startPoint")).json();
@@ -19,13 +20,17 @@ class Login {
document.getElementsByTagName("button")[0].onclick = () => Login.login();
// !!
- document.getElementsByTagName("input")[0].value = "maks " + Helpers.getRandomInt(1, 100);
- document.title = document.getElementsByTagName("input")[0].value;
- document.getElementsByTagName("button")[0].click();
+ // document.getElementsByTagName("input")[0].value = "maks " + Helpers.getRandomInt(1, 100);
+ // document.title = document.getElementsByTagName("input")[0].value;
+ // document.getElementsByTagName("button")[0].click();
}
}
static async login(username = "") {
+ if(Login.clicked === true)
+ return;
+
+ Login.clicked = true;
if (username === "")
username = document.getElementsByTagName("input")[0].value;
diff --git a/frontend/ts/startGame.ts b/frontend/ts/startGame.ts
index d73b846..883d648 100644
--- a/frontend/ts/startGame.ts
+++ b/frontend/ts/startGame.ts
@@ -29,6 +29,8 @@ export default class StartGame {
btnRollDice!: HTMLButtonElement;
divDice!: HTMLDivElement;
+ voices!: Array<SpeechSynthesisVoice>;
+
mineTurn = false;
gameStarted = false;
toggleStop = false;
@@ -40,6 +42,10 @@ export default class StartGame {
async init(data: { data: Data, uid: string }) {
document.body.innerHTML = await (await fetch("/map.html")).text();
+ if (speechSynthesis.onvoiceschanged !== undefined)
+ speechSynthesis.onvoiceschanged = () => this.voices = speechSynthesis.getVoices();
+ this.voices = speechSynthesis.getVoices();
+
this.uid = data.uid;
this.data = data.data;
this.index = data.data.filter(el => el.id === this.uid)[0].index;
@@ -69,8 +75,9 @@ export default class StartGame {
if (player.ready === true || this.gameStarted === true) {
tds[i].classList.add("bg-" + this.bgColors[player.color]);
- if (this.uid === player.id)
- this.inpStateToggle.checked = true;
+ if (this.uid === player.id) {
+ this.blockInpStateToggle()
+ }
}
else
tds[i].classList.add("bg-secondary");
@@ -87,7 +94,7 @@ export default class StartGame {
}
readyStateToggle = async (e: MouseEvent): Promise<void> => {
- if (this.toggleStop) {
+ if (this.toggleStop === true) {
this.inpStateToggle.checked = true;
return;
}
@@ -104,15 +111,50 @@ export default class StartGame {
this.startInterval();
}
+ blockInpStateToggle() {
+ this.inpStateToggle.checked = true;
+ this.toggleStop = true;
+ }
rollDice = async (e: MouseEvent): Promise<void> => {
this.btnRollDice.style.display = "none";
- // ! let number = Helpers.getRandomInt(1, 7);
- let number = 6;
+ let number = Helpers.getRandomInt(1, 7);
+
+ if((window as any).number !== undefined)
+ number = (window as any).number;
+
+ this.say(number.toString());
this.diceHash;
this.divDice.classList.add(`dice-${number}`);
this.calcMoves(number);
}
+ async say(text: string) {
+ if (!("speechSynthesis" in window)) {
+ // console.log("Client does not support SpeechSynthesis")
+ return;
+ }
+ let utterance = new SpeechSynthesisUtterance(),
+ voice: SpeechSynthesisVoice | undefined = undefined,
+ preferredVoices = ["sv-SE", "se-SE"];
+
+ for (let prefVoice of preferredVoices) {
+ voice = this.voices.find(el => el.lang === prefVoice);
+ if (voice !== undefined)
+ break;
+ }
+ if (voice === undefined)
+ voice = this.voices.find(el => el.default === true);
+
+ if (voice === undefined) {
+ // console.log("Client does not have any voices in SpeechSynthesis");
+ return;
+ }
+
+ utterance.voice = voice;
+ utterance.text = text;
+ speechSynthesis.speak(utterance);
+ }
+
calcMoves(number: number) {
if ([1, 6].includes(number))
Logic.moveFromBase(this.gameBoard, this.color);
@@ -127,13 +169,13 @@ export default class StartGame {
ghost.allBlink(this.gameBoard, this.color);
}
- doTurn(chequer: Chequer | HoBSquare, redraw = true): void {
+ doTurn(chequer: Chequer | HoBSquare): void {
if (chequer.ghost === undefined || chequer.ghost.coords === undefined) {
return;
}
this.mineTurn = false;
let where = chequer.ghost.where;
- let olderGameBoard = JSON.stringify(this.gameBoard);
+
if (where.from === "map") {
this.gameBoard.map[where.oldIndex].chequers.pop();
} else if (where.from === "base") {
@@ -145,23 +187,15 @@ export default class StartGame {
if (where.to === "map") {
let enemyColor = this.gameBoard.map[where.newIndex].chequers[0]?.color;
if (enemyColor !== this.color && enemyColor !== undefined) {
- let enemyBase = this.gameBoard.bases[enemyColor as keyof HomesOrBases];
- for (let chequer of this.gameBoard.map[where.newIndex].chequers) {
- let i = 0;
- for (let c of enemyBase) {
- if (c.chequer < 0) break;
- i++;
- }
- chequer.ghost = {
- color: enemyColor, coords: { x: this.gameBoard.map[where.newIndex].x, y: this.gameBoard.map[where.newIndex].y },
- where: {
- from: "map", oldIndex: where.newIndex,
- to: "base", newIndex: i
- }
- }
- this.doTurn(chequer, true);
+ let enemyBase = this.gameBoard.bases[enemyColor as keyof HomesOrBases],
+ square = this.gameBoard.map[where.newIndex];
+
+ for (let i = 0, j = 0; i < square.chequers.length; i++, j = 0) {
+ while(enemyBase[j].chequer >= 0)
+ j++;
+ enemyBase[j].chequer = enemyColor;
}
- this.gameBoard.map[where.newIndex].chequers = [];
+ square.chequers = [];
}
this.gameBoard.map[where.newIndex].chequers.push({ color: this.color });
} else if (where.to === "base") {
@@ -170,17 +204,18 @@ export default class StartGame {
this.gameBoard.homes[this.color as keyof HomesOrBases][where.newIndex].chequer = this.color;
}
- if (redraw === true) {
- (window as any).clicked = true;
- let won = this.gameHasBeenWon();
- ghost.allStopBlink();
- ghost.removeGhosts(this.gameBoard, this.color);
- gameBoardClass.drawGameBoard(this.gameBoard, this.drawChequer);
- this.sendGameBoard(false);
- // (window as any).clicked = false;
+ (window as any).clicked = true;
+ let won = this.gameHasBeenWon();
+ if (won === true) {
+ alert("WON!WON!WON!WON!WON!WON!");
+ throw new Error("WON!WON!WON!WON!WON!WON!");
}
- }
+ ghost.allStopBlink();
+ ghost.removeGhosts(this.gameBoard, this.color);
+ gameBoardClass.drawGameBoard(this.gameBoard, this.drawChequer);
+ this.sendGameBoard(false);
+ }
async hideDice(wait: boolean) {
let diceHash = this._diceHash;
wait === true ? await Helpers.sleep(2000) : '';
@@ -190,14 +225,15 @@ export default class StartGame {
gameHasBeenWon(): boolean {
let home = this.gameBoard.homes[this.color as keyof HomesOrBases];
- for(let square of home) {
- if(square.chequer < 0)
- return false;
+ for (let square of home) {
+ if (square.chequer < 0)
+ return false;
}
return true;
}
async sendGameBoard(diceWait: boolean) {
+ this.btnRollDice.style.display = "none";
ghost.allStopBlink();
ghost.removeGhosts(this.gameBoard, this.color);
this.timer.stop();
@@ -221,6 +257,7 @@ export default class StartGame {
if (this.gameWasStopped !== true)
this.sendGameBoard(false);
}
+
_diceHash = 0;
get diceHash(): number {
if (this._diceHash >= Number.MAX_SAFE_INTEGER)
@@ -230,19 +267,18 @@ export default class StartGame {
}
async getData() {
- console.log("gettingData");
let data: { gameBoard: GameBoard, currentPlayer: number, started: number, data: Data, timeTillTurnEnd: number }
= await Helpers.mFetch("/getCurrentGameState", {});
+
if (data.started === 1) {
this.gameStarted = true;
if (this.oldIndex !== data.currentPlayer) {
this.oldIndex = data.currentPlayer;
- if (data.currentPlayer === this.index/* && this.mineTurn === false */) {
+ if (data.currentPlayer === this.index) {
this.mineTurn = true;
this.gameWasStopped = false;
this.btnRollDice.style.display = "block";
} /* else if (StartGame.waitForNewTurn === false) { */
- console.log("Redrawing gameBoard - newer from server");
this.gameBoard = data.gameBoard;
gameBoardClass.drawGameBoard(this.gameBoard, this.drawChequer);
// }
@@ -255,7 +291,6 @@ export default class StartGame {
this.setOpponents(this.data, false);
}
- // TODO: Check zbijanie
// TODO: Check gameHasBeenWon()
}
}
diff --git a/frontend/ts/timer.ts b/frontend/ts/timer.ts
index db70356..013d498 100644
--- a/frontend/ts/timer.ts
+++ b/frontend/ts/timer.ts
@@ -25,6 +25,7 @@ export default class Timer {
this.divTimer.innerHTML = secondsElapsed.toString();
}
}
+
stop(): void {
this.divTimer.innerHTML = '';
clearInterval(this.interval);
diff --git a/helpers/helpers.ts b/helpers/helpers.ts
index 3800040..9cd15cc 100644
--- a/helpers/helpers.ts
+++ b/helpers/helpers.ts
@@ -17,9 +17,10 @@ export default class Helpers {
params: params
})
})
- ).text();
+ ).json();
// console.log(t);
- return JSON.parse(t);
+ // return JSON.parse(t);
+ return t;
}
static dbResToFiaTable(dbRes: dbRes): fiaTable {
diff --git a/helpers/helpersBack.ts b/helpers/helpersBack.ts
index 6a3dfd7..b0e9a6d 100644
--- a/helpers/helpersBack.ts
+++ b/helpers/helpersBack.ts
@@ -118,6 +118,11 @@ export interface Ghost {
color: tColors;
coords?: Coordinates;
}
+export interface OldTd {
+ className: string;
+ innerHTML: string;
+ count: string;
+}
export interface Chequer {
color: tColors;
ghost?: Ghost; // number; // 1 | 2 | 3 | 4 | 5 | 6;
diff --git a/public/css/fia.css b/public/css/fia.css
index beeb18f..80d5b61 100644
--- a/public/css/fia.css
+++ b/public/css/fia.css
@@ -29,7 +29,6 @@
margin: 50px auto;
height: 100px;
width: 100px;
- border: 2px solid blue;
background-size: contain;
}
.dice-1 {
@@ -80,4 +79,8 @@
.chequer-white {
background-color: white;
border: 4px solid black;
-} \ No newline at end of file
+}
+.chequer-pink {
+ background-color: rgb(255, 0, 200);
+ border: 4px solid black;
+}
diff --git a/public/index.html b/public/index.html
index a5b174a..617de91 100644
--- a/public/index.html
+++ b/public/index.html
@@ -1,22 +1,19 @@
<!DOCTYPE html>
<html lang="en">
-
-<head>
- <meta charset="UTF-8" />
- <meta http-equiv="X-UA-Compatible" content="IE=edge" />
- <meta name="viewport" content="width=device-width, initial-scale=1.0" />
- <title>Fia</title>
- <link rel="stylesheet" href="css/bootstrap.min.css">
- <link rel="stylesheet" href="css/fia.css">
-</head>
-
-<body class="bg-dark text-white">
- <noscript>
- <div class="container text-center h1" style="margin-top: 45vh;">
- Tyvärr, men för att spela Fia måste du aktivera skript
- </div>
- </noscript>
- <script src="js/frontend/ts/login.js" type="module"></script>
-</body>
-
-</html> \ No newline at end of file
+ <head>
+ <meta charset="UTF-8" />
+ <meta http-equiv="X-UA-Compatible" content="IE=edge" />
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
+ <title>Fia</title>
+ <link rel="stylesheet" href="css/bootstrap.min.css" />
+ <link rel="stylesheet" href="css/fia.css" />
+ </head>
+ <body class="bg-dark text-white">
+ <noscript>
+ <div class="container text-center h1" style="margin-top: 45vh">
+ Tyvärr, men för att spela Fia måste du aktivera skript
+ </div>
+ </noscript>
+ <script src="js/frontend/ts/login.js" type="module"></script>
+ </body>
+</html>
diff --git a/tsconfig.json b/tsconfig.json
index 09abbe6..7cac22e 100644
--- a/tsconfig.json
+++ b/tsconfig.json
@@ -1,7 +1,5 @@
{
"compilerOptions": {
- // "rootDir": "./",
- // "outDir": "./src/js",
"esModuleInterop": true,
"target": "ESNext",
"module": "commonjs",
@@ -21,12 +19,4 @@
"downlevelIteration": true,
/* END MINE */
},
-
- // "include": [
- // "src/ts/**/*"
- // ],
- // "exclude": [
- // "node_modules",
- // ".git"
- // ]
} \ No newline at end of file