2024-03-01 22:24:30 +01:00
|
|
|
const socket = io();
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
var playerIdx;
|
|
|
|
var timerDestination = null;
|
2024-03-03 01:29:11 +01:00
|
|
|
var gamePhase = 'pregame';
|
|
|
|
|
|
|
|
$('.field').on('click', function () {
|
|
|
|
console.log("Clicked");
|
|
|
|
socket.emit("place ship", selectedShip, $(this).data('pos-x'), $(this).data('pos-y'), shipRotation);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('toast', (msg) => {
|
|
|
|
Toastify({
|
|
|
|
text: msg,
|
|
|
|
duration: 5000,
|
|
|
|
newWindow: true,
|
|
|
|
gravity: "bottom",
|
|
|
|
position: "right",
|
|
|
|
stopOnFocus: true,
|
|
|
|
className: "bshipstoast",
|
|
|
|
}).showToast();
|
|
|
|
});
|
2024-03-02 13:56:01 +01:00
|
|
|
|
|
|
|
socket.on('connect', () => {
|
|
|
|
$(".cover h1").html("Oczekiwanie na serwer...");
|
|
|
|
});
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
socket.on("players ready", () => {
|
|
|
|
$(".cover").css({opacity: 0, pointerEvents: "none"});
|
2024-03-02 13:56:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("player idx", (idx) => {
|
|
|
|
console.log(idx);
|
|
|
|
playerIdx = idx;
|
2024-03-03 01:29:11 +01:00
|
|
|
console.log(playerIdx);
|
2024-03-02 13:56:01 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
socket.on('turn update', (turnData) => {
|
2024-03-02 18:28:33 +01:00
|
|
|
if (turnData.phase === "preparation") {
|
|
|
|
$("#whosTurn").html("Faza przygotowań");
|
|
|
|
} else {
|
|
|
|
turnData.turn === playerIdx ? $("#whosTurn").html("Twoja tura") : $("#whosTurn").html("Tura przeciwnika");
|
|
|
|
}
|
|
|
|
|
2024-03-02 13:56:01 +01:00
|
|
|
|
|
|
|
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);
|