const socket = io(); // Handling server-sent events socket.on("joined", (nick) => { returnLock = false; lockUI(true); $("#oppNameField").html(nick); switchView("preparingGame"); lockUI(false); console.log("Player joined the game:", nick); }); socket.on("player left", () => { lockUI(true); switchView("mainMenuView"); lockUI(false); console.log("Player left the game"); }); socket.on("gameReady", (gameId) => { console.log("Game is ready, redirecting in 2 seconds. Game ID:", gameId); setTimeout(() => { console.log("Redirecting..."); window.location.replace("/game?id=" + gameId); }, 2000); }); var nickname; socket.emit('locale options', (langs) => { console.log("Fetching available locale options"); let menu = ""; langs.forEach(lang => { menu += ``; }); $("#languages").html(menu); console.log("Locale options fetched"); }); $("#languages").on("change", function() { lockUI(true); console.log("Switching language to", $(this).val()); socket.emit("change locale", $(this).val(), (response) => { switch (response.status) { case "ok": console.log("Switched languages, refreshing"); window.location.reload(); break; default: alert(`${window.locale["Unknown error occured"]}\n${window.locale["Status:"]} ${response.status}`); lockUI(false); break; } }); }); socket.emit("my profile", (profile) => { console.log("Received user data. UID:", profile.uid); // General profile data let options = { weekday: 'long', year: 'numeric', month: 'long', day: 'numeric' }; $("#playerSince").html(new Date(profile.profile.account_creation).toLocaleDateString(undefined, options)); $("#nickname").html(profile.profile.nickname); // Profile stats $("#monthlyPlayed").html(profile.stats.monthly_matches); $("#totalPlayed").html(profile.stats.alltime_matches); $("#winrate").html(profile.stats.winrate !== null ? `${profile.stats.winrate}%` : "-"); // Match history 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(undefined, options); const minutes = Math.floor(match.duration / 60).toLocaleString(undefined, { minimumIntegerDigits: 2, useGrouping: false }); const seconds = (match.duration - minutes * 60).toLocaleString(undefined, { minimumIntegerDigits: 2, useGrouping: false }); const duration = `${minutes}:${seconds}`; matchHistoryDOM += `