import { client, lang, navigateTo } from "../index.js"; import { clearIds, fill_errors } from "../utils/formUtils.js"; import AbstractAuthenticatedView from "./abstracts/AbstractAuthenticatedView.js"; export default class extends AbstractAuthenticatedView { constructor(params) { super(params, "Matchmaking"); } async toggle_search() { clearIds("innerText", ["detail"]); if (client.matchmaking.searching) { client.matchmaking.stop(); this.button.innerHTML = lang.get("matchmakingStartSearch"); } else { let nb_players = this.input.value; await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), nb_players); this.button.innerHTML = lang.get("matchmakingStopSearch"); } } ondisconnect(event) { this.button.innerHTML = lang.get("matchmakingStartSearch"); } onreceive(data) { if (data.detail === "game_found") { navigateTo(`/games/${data.game_id}`); return; } this.display_data(data); } display_data(data) { clearIds("innerText", ["detail"]); fill_errors(data, "innerText"); } async postInit() { this.button = document.getElementById("toggle-search"); this.input = document.getElementById("nb-players-input"); let container = document.getElementById("nb-players-container"); let gameChoice = document.getElementById("game-choice"); this.button.onclick = this.toggle_search.bind(this); this.input.addEventListener('keydown', async ev => { if (ev.key !== 'Enter') return; await this.toggle_search.bind(this); }); gameChoice.addEventListener("change", function() { if (this.value === "TicTacToe") { container.style.display = 'none'; document.getElementById("nb-players-input").value = 2; } else container.style.display = 'block'; }) let update = () => { this.button.disabled = (this.input.value < 2 || this.input.value > 4); }; ["change", "oninput"].forEach((event_name) => { this.input.addEventListener(event_name, update); }); } async getHtml() { return /* HTML */ `

${lang.get("matchmakingTitle")}

`; } async leavePage() { await client.matchmaking.stop(); } }