2023-12-12 06:06:13 -05:00
|
|
|
import { client, navigateTo } from "../index.js";
|
2023-12-23 10:47:22 -05:00
|
|
|
import { clear, fill_errors } from "../utils/formUtils.js";
|
2023-12-23 10:49:43 -05:00
|
|
|
import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js";
|
2023-12-11 07:40:01 -05:00
|
|
|
|
2023-12-23 10:49:43 -05:00
|
|
|
export default class extends AbstractAuthentifiedView {
|
2023-12-23 06:54:33 -05:00
|
|
|
constructor(params)
|
|
|
|
{
|
|
|
|
super(params, "Matchmaking");
|
|
|
|
}
|
|
|
|
|
|
|
|
async press_button()
|
|
|
|
{
|
|
|
|
if (client.matchmaking.searching)
|
|
|
|
{
|
|
|
|
client.matchmaking.stop();
|
|
|
|
document.getElementById("button").value = "Find a game"
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-23 10:47:22 -05:00
|
|
|
let nb_players = document.getElementById("nb_players-input").value
|
|
|
|
|
2023-12-23 10:56:04 -05:00
|
|
|
await client.matchmaking.start(this.onreceive.bind(this), this.ondisconnect.bind(this), nb_players);
|
2023-12-23 10:47:22 -05:00
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
document.getElementById("button").value = "Stop matchmaking"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
ondisconnect()
|
2023-12-23 06:54:33 -05:00
|
|
|
{
|
2023-12-23 10:47:22 -05:00
|
|
|
document.getElementById("button").value = "Find a game"
|
|
|
|
}
|
2023-12-23 06:54:33 -05:00
|
|
|
|
2023-12-23 10:56:04 -05:00
|
|
|
onreceive(data)
|
|
|
|
{
|
|
|
|
if (data.detail === "game_found")
|
|
|
|
{
|
|
|
|
navigateTo(`/games/${data.game_id}`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.display_data(data)
|
|
|
|
}
|
|
|
|
|
2023-12-23 10:47:22 -05:00
|
|
|
display_data(data)
|
|
|
|
{
|
|
|
|
clear("innerText", ["detail"]);
|
|
|
|
fill_errors(data, "innerText");
|
2023-12-11 07:40:01 -05:00
|
|
|
}
|
|
|
|
|
2023-12-12 06:06:13 -05:00
|
|
|
async postInit()
|
|
|
|
{
|
2023-12-23 10:47:22 -05:00
|
|
|
document.getElementById("button").onclick = this.press_button.bind(this)
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-11 07:40:01 -05:00
|
|
|
async getHtml() {
|
|
|
|
return `
|
2023-12-23 10:47:22 -05:00
|
|
|
<h1>Select mode</h1>
|
2023-12-23 06:54:33 -05:00
|
|
|
<input type="number" value="2" id="nb_players-input">
|
|
|
|
<input type="button" value="Find a game" id="button">
|
2023-12-23 10:47:22 -05:00
|
|
|
<span id="detail"></span>
|
2023-12-11 07:40:01 -05:00
|
|
|
`;
|
|
|
|
}
|
2023-12-12 06:06:13 -05:00
|
|
|
|
|
|
|
async leavePage()
|
|
|
|
{
|
|
|
|
await client.matchmaking.stop();
|
|
|
|
}
|
2023-12-19 05:27:18 -05:00
|
|
|
}
|