diff --git a/index.js b/index.js index 2e79496..90b041b 100644 --- a/index.js +++ b/index.js @@ -16,6 +16,7 @@ import { Lang } from './utils/localisation.js'; import { rateLimit } from 'express-rate-limit'; import { RedisStore as LimiterRedisStore } from 'rate-limit-redis'; import SessionRedisStore from 'connect-redis'; +import mysql from 'mysql'; const __filename = fileURLToPath(import.meta.url); const __dirname = path.dirname(__filename); @@ -24,6 +25,8 @@ const app = express(); const flags = process.env.flags ? process.env.flags.split(",") : null; +const langs = [{ id: "en", name: "English" }, { id: "pl", name: "Polish" }]; + app.use(express.json()); app.use(express.urlencoded({ extended: true })); @@ -93,6 +96,9 @@ app.get('/', async (req, res) => { } else if (req.session.nickname == null) { auth.getLanguage(req.session.userId).then(language => { var locale; + + req.session.autoLang = language == null ? true : false; + if (language) { locale = new Lang([language]); req.session.langs = [language]; @@ -351,10 +357,47 @@ io.on('connection', async (socket) => { socket.on('my profile', (callback) => { auth.getProfile(session.userId).then((profile) => { + profile.uid = session.userId; callback(profile); }); }); + socket.on('locale options', (callback) => { + const locale = new Lang(session.langs); + + let userLanguage = langs.find((element) => element.id == locale.lang); + let userLangs = langs.filter((element) => element.id != locale.lang); + + if (session.autoLang) { + userLangs.unshift(userLanguage); + userLangs.unshift({ id: "null", name: "Auto" }); + } else { + userLangs.unshift({ id: "null", name: "Auto" }); + userLangs.unshift(userLanguage); + } + + callback(userLangs); + }); + + socket.on('change locale', (locale, callback) => { + if (locale === "null" || langs.find((element) => element.id == locale)) { + locale = locale === "null" ? null : locale; + const conn = mysql.createConnection({ host: process.env.db_host, user: process.env.db_user, password: process.env.db_pass, database: 'statki' }); + conn.query(`UPDATE accounts SET language = ${conn.escape(locale)} WHERE user_id = ${conn.escape(session.userId)}`, (err) => { + if (err) { callback({ status: 'dbErr' }); return; } + else callback({ status: 'ok' }); + + req.session.reload((err) => { + if (err) return socket.disconnect(); + + req.session.autoLang = locale ? false : true; + req.session.save(); + }); + }); + conn.end(); + } + }); + socket.on('create lobby', (callback) => { if (socket.rooms.size === 1) { let id = genID(); diff --git a/lang/en.json b/lang/en.json index 3201458..b7f7a2f 100644 --- a/lang/en.json +++ b/lang/en.json @@ -57,6 +57,9 @@ "winrate": "winrate", "No matches played": "No matches played" }, + "Settings": { + "General": "General" + }, "General": { "Unknown error occured": "Unknown error occured", "Status:": "Status:" diff --git a/lang/pl.json b/lang/pl.json index b327e8c..4d46cb3 100644 --- a/lang/pl.json +++ b/lang/pl.json @@ -43,7 +43,7 @@ "PvP/Loading": { "PvP / Loading": "PvP / Wczytywanie", "Wait": "Czekaj...", - "You will be redirected soon": "Wkrótce zostaniesz przekierowany(-a)", + "You will be redirected soon": "Wkrótce nastąpi przekierowanie", "Opponent:": "Przeciwnik" }, "Profile": { @@ -56,9 +56,11 @@ "total matches played": "meczy zagranych łącznie", "winrate": "winrate", - "No matches played": "Nie zagrano meczy" + "No matches played": "Nie zagrano żadnych meczy" + }, + "Settings": { + "General": "Ogólne" }, - "General": { "Unknown error occured": "Wystąpił nieznany błąd", "Status:": "Status:" diff --git a/nodemon.json b/nodemon.json index fa31724..82b69fc 100644 --- a/nodemon.json +++ b/nodemon.json @@ -2,6 +2,7 @@ "ignore": [ "public/*", "views/*", - "utils/mail/*" + "utils/mail/*", + "lang/*" ] } \ No newline at end of file diff --git a/public/assets/css/main.css b/public/assets/css/main.css index 5e7b385..127a3b1 100644 --- a/public/assets/css/main.css +++ b/public/assets/css/main.css @@ -246,8 +246,6 @@ nav span:hover { flex-direction: column; gap: 2rem; flex: 2; - - overflow-y: scroll; } #profileView .matchList::-webkit-scrollbar { @@ -323,4 +321,20 @@ nav span:hover { .tippyTemplate { display: none; +} + +#settingsView label { + font-size: 1.2rem; +} + +#settingsView select { + padding: 0.5rem 1rem; + padding: 0.5rem 2rem; + font-size: 1rem; + background-color: black; + border: solid 1px white; + color: white; + border-radius: 15px; + outline: none; + margin-left: 2rem; } \ No newline at end of file diff --git a/public/assets/js/socket.js b/public/assets/js/socket.js index 6603ce7..e5c0f13 100644 --- a/public/assets/js/socket.js +++ b/public/assets/js/socket.js @@ -29,7 +29,38 @@ socket.on("gameReady", (gameId) => { 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)); @@ -59,7 +90,7 @@ socket.emit("my profile", (profile) => { matchHistoryDOM += `