fix: matchmaking required min 1 player #5

This commit is contained in:
2024-02-20 14:04:38 +01:00
parent 6b42a99404
commit 4d2d4dfb98
3 changed files with 48 additions and 9 deletions

View File

@ -1,8 +1,8 @@
export function clear(property_name, elements_id)
{
elements_id.forEach(element_id => {
let element = document.getElementById(element_id)
element[property_name] = ""
let element = document.getElementById(element_id);
element[property_name] = "";
});
}

View File

@ -3,6 +3,7 @@ import { clear, fill_errors } from "../utils/formUtils.js";
import AbstractAuthenticatedView from "./abstracts/AbstractAuthenticatedView.js";
export default class extends AbstractAuthenticatedView {
constructor(params)
{
super(params, "Matchmaking");
@ -10,6 +11,7 @@ export default class extends AbstractAuthenticatedView {
async press_button()
{
clear("innerText", ["detail"]);
if (client.matchmaking.searching)
{
client.matchmaking.stop();
@ -27,8 +29,6 @@ export default class extends AbstractAuthenticatedView {
ondisconnect(event)
{
if (event.code === 1000)
clear("innerText", ["detail"]);
document.getElementById("button").value = "Find a game";
}
@ -50,13 +50,42 @@ export default class extends AbstractAuthenticatedView {
async postInit()
{
document.getElementById("button").onclick = this.press_button.bind(this);
let button = document.getElementById("button");
button.onclick = this.press_button.bind(this);
let input = document.getElementById("nb_players-input");
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";
});
let update = () => {
if (input.value < 2)
button.disabled = true;
else
button.disabled = false;
};
["change", "oninput"].forEach((event_name) => {
input.addEventListener(event_name, update);
});
}
async getHtml() {
return `
<h1>Select mode</h1>
<input type="number" value="2" id="nb_players-input">
<input type="number" value="2" min="1" id="nb_players-input">
<input type="button" value="Find a game" id="button">
<span id="detail"></span>
`;