2024-01-06 21:09:41 +01:00
|
|
|
const socket = io();
|
|
|
|
|
2024-03-08 19:18:53 +01:00
|
|
|
// Handling server-sent events
|
2024-02-29 21:55:56 +01:00
|
|
|
socket.on("joined", (nick) => {
|
|
|
|
lockUI(true);
|
|
|
|
$("#oppNameField").html(nick);
|
|
|
|
switchView("preparingGame");
|
|
|
|
lockUI(false);
|
2024-01-08 16:53:12 +01:00
|
|
|
});
|
|
|
|
|
2024-03-01 22:24:30 +01:00
|
|
|
socket.on("player left", () => {
|
|
|
|
lockUI(true);
|
|
|
|
switchView("mainMenuView");
|
|
|
|
lockUI(false);
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("gameReady", (gameId) => {
|
|
|
|
setTimeout(() => {
|
|
|
|
window.location.replace("/game?id=" + gameId);
|
|
|
|
}, 2000);
|
|
|
|
});
|
|
|
|
|
|
|
|
var nickname;
|
|
|
|
|
|
|
|
socket.emit("whats my nick", (myNickname) => {
|
|
|
|
nickname = myNickname;
|
|
|
|
});
|
|
|
|
|
|
|
|
socket.on("game start", (gameInfo) => {
|
|
|
|
let opp;
|
|
|
|
if (gameInfo.players[0]!==nickname) {
|
|
|
|
opp = gameInfo.players[0];
|
|
|
|
} else {
|
|
|
|
opp = gameInfo.players[1];
|
|
|
|
}
|
|
|
|
|
|
|
|
alert(`Grasz przeciwko: ${opp}`);
|
|
|
|
});
|
|
|
|
|
2024-01-08 16:53:12 +01:00
|
|
|
$("#createGameButton").on("click", function () {
|
|
|
|
lockUI(true);
|
|
|
|
socket.emit("create lobby", (response) => {
|
|
|
|
switch (response.status) {
|
|
|
|
case "ok":
|
|
|
|
$("#createGameCode").val(response.gameCode);
|
|
|
|
switchView("pvpCreateView");
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "alreadyInLobby":
|
|
|
|
$("#createGameCode").val(response.gameCode);
|
|
|
|
switchView("pvpCreateView");
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert(`Wystąpił nieznany problem\nStatus: ${response.status}`);
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#leaveGameButton").on("click", function () {
|
|
|
|
lockUI(true);
|
|
|
|
socket.emit("leave lobby", (response) => {
|
|
|
|
switch (response.status) {
|
|
|
|
case "ok":
|
|
|
|
switchView("mainMenuView");
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "youreNotInLobby":
|
|
|
|
switchView("mainMenuView");
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert(`Wystąpił nieznany problem\nStatus: ${response.status}`);
|
|
|
|
switchView("mainMenuView");
|
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$("#pvpMenuButton").on("click", function () {
|
|
|
|
switchView('pvpMenuView');
|
|
|
|
});
|
|
|
|
|
|
|
|
const form = document.getElementById('pvpJoinForm');
|
|
|
|
const input = document.getElementById('pvpJoinCode');
|
|
|
|
|
|
|
|
form.addEventListener('submit', (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
if (input.value && input.value.length === 6) {
|
|
|
|
lockUI(true);
|
|
|
|
socket.emit("join lobby", input.value, (response) => {
|
|
|
|
switch (response.status) {
|
|
|
|
case "ok":
|
2024-02-29 21:55:56 +01:00
|
|
|
$("#oppNameField").html(response.oppNickname);
|
|
|
|
switchView("preparingGame");
|
2024-01-08 16:53:12 +01:00
|
|
|
lockUI(false);
|
|
|
|
break;
|
|
|
|
|
|
|
|
//case "alreadyInLobby":
|
|
|
|
// $("#createGameCode").val(response.gameCode);
|
|
|
|
// switchView("pvpCreateView");
|
|
|
|
// lockUI(false);
|
|
|
|
// break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
alert(`Wystąpił nieznany problem\nStatus: ${response.status}`);
|
|
|
|
lockUI(false);
|
|
|
|
switchView("mainMenuView");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
input.value = '';
|
|
|
|
}
|
|
|
|
});
|