diff --git a/frontend/static/js/lang/en.json b/frontend/static/js/lang/en.json index 7d47ddf..5104587 100644 --- a/frontend/static/js/lang/en.json +++ b/frontend/static/js/lang/en.json @@ -42,5 +42,9 @@ "ruleTitle" : "Rules", "ruleBase" : "1. Win on one of the 9 tictactoe to win the game", "ruleMovement" : "2. You start on the central tictactoe, and play on the one corresponding to your choice on the next turn", - "ruleDraw" : "3. If your play cause a tictactoe to be full and a draw, you lose the game" + "ruleDraw" : "3. If your play cause a tictactoe to be full and a draw, you lose the game", + "matchmakingTitle": "Matchmaking", + "matchmakingStartSearch": "Find a game", + "matchmakingStopSearch": "Stop matchmaking", + "matchmakingNbPlayers": "Number of players" } diff --git a/frontend/static/js/views/MatchMakingView.js b/frontend/static/js/views/MatchMakingView.js index 40f9922..d85d894 100644 --- a/frontend/static/js/views/MatchMakingView.js +++ b/frontend/static/js/views/MatchMakingView.js @@ -1,4 +1,4 @@ -import { client, navigateTo } from "../index.js"; +import { client, lang, navigateTo } from "../index.js"; import { clear, fill_errors } from "../utils/formUtils.js"; import AbstractAuthenticatedView from "./abstracts/AbstractAuthenticatedView.js"; @@ -7,56 +7,36 @@ export default class extends AbstractAuthenticatedView { constructor(params) { super(params, "Matchmaking"); - this.game_mode = 0; // 0 -> 2D; 1 -> 3D } - async press_button() + async toggle_search() { clear("innerText", ["detail"]); if (client.matchmaking.searching) { client.matchmaking.stop(); - document.getElementById("button").value = "Find a game"; + this.button.innerHTML = lang.get("matchmakingStartSearch"); } else { - let nb_players = document.getElementById("nb_players-input").value; + let nb_players = this.input.value; await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), nb_players); - document.getElementById("button").value = "Stop matchmaking"; + this.button.innerHTML = lang.get("matchmakingStopSearch"); } } - async press_button_game_mode() - { - if(this.game_mode === 0) - { - document.getElementById("game-mode").value = "3D"; - this.game_mode = 1; - } - else - { - document.getElementById("game-mode").value = "2D"; - this.game_mode = 0; - } - } - ondisconnect(event) { - let button = document.getElementById("button") - - if (button === null) - return - - button.value = "Find a game"; + this.button.innerHTML = lang.get("matchmakingStartSearch"); } onreceive(data) { if (data.detail === "game_found") { - navigateTo(`/games/${data.game_id}/${this.game_mode}`); + navigateTo(`/games/${data.game_id}`); return; } this.display_data(data); @@ -70,46 +50,44 @@ export default class extends AbstractAuthenticatedView { async postInit() { - let button = document.getElementById("button"); + this.button = document.getElementById("toggle-search"); + this.input = document.getElementById("nb-players-input"); - button.onclick = this.press_button.bind(this); + this.button.onclick = this.toggle_search.bind(this); - let input = document.getElementById("nb_players-input"); + this.input.addEventListener('keydown', async ev => { - input.addEventListener('keydown', async ev => { if (ev.key !== 'Enter') return; - if (client.matchmaking.searching) - client.matchmaking.stop(); - - let nb_players = document.getElementById("nb_players-input").value; - - await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), nb_players); - - document.getElementById("button").value = "Stop matchmaking"; + await this.toggle_search.bind(this); }); let update = () => { - if (input.value < 2 || input.value > 4) - button.disabled = true; - else - button.disabled = false; + button.disabled = (input.value < 2 || input.value > 4); }; ["change", "oninput"].forEach((event_name) => { - input.addEventListener(event_name, update); + this.input.addEventListener(event_name, update); }); - document.getElementById("game-mode").onclick = this.press_button_game_mode.bind(this); } async getHtml() { - return ` -

Select mode

- - - - + return /* HTML */ ` +
+
+

${lang.get("matchmakingTitle")}

+
+ + + +
+
+ + +
+
+
`; }