statki/public/assets/js/socket-game.js
MaciejkaG 72da84c3d6 Major changes
- Huge improvements in UI design and animations
- Improvements in code organisation (separated multiple functions into an util file)
- Multiple bug fixes and improvements to stability
- Client side events improved
2024-03-02 18:28:33 +01:00

50 lines
1.3 KiB
JavaScript

const socket = io();
var playerIdx;
var timerDestination = null;
var gamePhase = "pregame";
socket.on('connect', () => {
$(".cover h1").html("Oczekiwanie na serwer...");
});
socket.on("players ready", () => {
$(".cover").css({opacity: 0, pointerEvents: "none"});
});
socket.on("player idx", (idx) => {
console.log(idx);
playerIdx = idx;
});
socket.on('turn update', (turnData) => {
if (turnData.phase === "preparation") {
$("#whosTurn").html("Faza przygotowań");
} else {
turnData.turn === playerIdx ? $("#whosTurn").html("Twoja tura") : $("#whosTurn").html("Tura przeciwnika");
}
timerDestination = turnData.timerToUTC;
gamePhase = turnData.phase;
});
socket.on('player left', () => {
window.location.replace("/");
});
// Update timer
setInterval(() => {
if (timerDestination == null) {
$("#timer").html("");
} else {
const UTCNow = Math.floor((new Date()).getTime() / 1000);
const time = Math.abs(UTCNow - timerDestination);
const minutes = Math.floor(time / 60).toLocaleString('pl-PL', {minimumIntegerDigits: 2, useGrouping: false});
const seconds = (time - minutes * 60).toLocaleString('pl-PL', { minimumIntegerDigits: 2, useGrouping: false });
$("#timer").html(`${minutes}:${seconds}`);
}
}, 250);