statki/public/assets/js/socket.js

152 lines
4.9 KiB
JavaScript
Raw Normal View History

const socket = io();
// Handling server-sent events
socket.on("joined", (nick) => {
2024-03-25 12:41:02 +01:00
returnLock = false;
lockUI(true);
$("#oppNameField").html(nick);
switchView("preparingGame");
lockUI(false);
2024-01-08 16:53:12 +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;
2024-03-19 22:33:14 +01:00
2024-03-24 19:51:49 +01:00
socket.emit("my profile", (profile) => {
// General profile data
let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
$("#playerSince").html(new Date(profile.profile.account_creation).toLocaleDateString("pl-PL", options));
$("#nickname").html(profile.profile.nickname);
// Profile stats
$("#monthlyPlayed").html(profile.stats.monthly_matches);
$("#totalPlayed").html(profile.stats.alltime_matches);
2024-03-25 12:41:02 +01:00
$("#winrate").html(profile.stats.winrate !== null ? `${profile.stats.winrate}%` : "-");
2024-03-24 19:51:49 +01:00
2024-03-24 22:00:05 +01:00
// Match history
2024-03-24 19:51:49 +01:00
var matchHistory = profile.matchHistory;
var matchHistoryDOM = "";
options = { hour: '2-digit', minute: '2-digit', time: 'numeric', weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' };
for (let i = 0; i < matchHistory.length; i++) {
const match = matchHistory[i];
let date = new Date(match.date).toLocaleDateString("pl-PL", options);
const minutes = Math.floor(match.duration / 60).toLocaleString('pl-PL', { minimumIntegerDigits: 2, useGrouping: false });
const seconds = (match.duration - minutes * 60).toLocaleString('pl-PL', { minimumIntegerDigits: 2, useGrouping: false });
const duration = `${minutes}:${seconds}`;
matchHistoryDOM += `<div class="match" data-matchid="${match.match_id}"><div><h1 class="dynamic${match.won === 1 ? "" : " danger"}">${match.won === 1 ? "Zwycięstwo" : "Porażka"}</h1><span> vs. ${match.match_type === "pvp" ? match.opponent : "AI"}</span></div><h2 class="statsButton">Kliknij by wyświetlić statystyki</h2><span>${date}</span><br><span>${duration}</span></div>`;
}
2024-03-25 12:41:02 +01:00
if (matchHistoryDOM === "") {
matchHistoryDOM = "<h2>Brak zagranych meczów</h2>";
}
2024-03-24 19:51:49 +01:00
$(".matchList").html(matchHistoryDOM);
2024-03-19 22:33:14 +01:00
});
socket.emit("whats my nick", (myNickname) => {
nickname = myNickname;
2024-03-19 22:33:14 +01:00
$("#profileButton").html(nickname);
console.log(nickname);
});
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");
2024-03-19 22:33:14 +01:00
returnLock = true;
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);
break;
}
});
});
$("#leaveGameButton").on("click", function () {
lockUI(true);
window.location.reload();
// socket.emit("leave lobby", (response) => {
// switch (response.status) {
// case "ok":
// switchView("mainMenuView");
// lockUI(false);
// break;
2024-01-08 16:53:12 +01:00
// case "youreNotInLobby":
// switchView("mainMenuView");
// lockUI(false);
// break;
2024-01-08 16:53:12 +01:00
// default:
// alert(`Wystąpił nieznany problem\nStatus: ${response.status}`);
// switchView("mainMenuView");
// lockUI(false);
// break;
// }
// });
2024-01-08 16:53:12 +01:00
});
$("#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":
$("#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 = '';
}
});