mirror of
https://github.com/MaciejkaG/statki.git
synced 2024-11-30 05:22:55 +01:00
bb17dc47ba
- Fully working ship placement system - Validating ship positions works too - Client side ship data display works as well - Changes to field colors to dark grey instead of light. This improves comfort of use and ensures proper contrast on lower quality displays and ease of use.
71 lines
1.8 KiB
JavaScript
71 lines
1.8 KiB
JavaScript
const socket = io();
|
|
|
|
var playerIdx;
|
|
var timerDestination = null;
|
|
var gamePhase = 'pregame';
|
|
|
|
$('.field').on('click', function () {
|
|
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();
|
|
});
|
|
|
|
socket.on("placed ship", (data) => {
|
|
bsc.placeShip(data);
|
|
shipsLeft[data.type]--;
|
|
refreshBoardView();
|
|
});
|
|
|
|
socket.on('connect', () => {
|
|
$(".cover h1").html("Oczekiwanie na serwer...");
|
|
});
|
|
|
|
socket.on("players ready", () => {
|
|
$(".cover").css({opacity: 0, pointerEvents: "none"});
|
|
});
|
|
|
|
socket.on("player idx", (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); |