fix: matchmaking support start stop, multi connection

This commit is contained in:
2023-12-23 16:47:22 +01:00
parent 6c39a13aca
commit c02c49215d
5 changed files with 92 additions and 32 deletions

View File

@ -14,7 +14,7 @@ class MatchMaking
this.searching = false;
}
async start(func, mode)
async start(receive_func, disconnect_func, mode)
{
if (!await this.client.isAuthentificate())
return null;
@ -25,10 +25,21 @@ class MatchMaking
this.searching = true;
this.receive_func = receive_func;
this.disconnect_func = disconnect_func;
this._socket.onmessage = function (event) {
const data = JSON.parse(event.data);
func(data.game_id)
receive_func(data);
};
this._socket.onclose = this.onclose.bind(this);
}
onclose(event)
{
this.stop();
this.disconnect_func()
}
async stop()

View File

@ -1,11 +1,7 @@
import { client, navigateTo } from "../index.js";
import { clear, fill_errors } from "../utils/formUtils.js";
import AbstractView from "./abstracts/AbstractView.js";
function game_found(game_id)
{
navigateTo(`/games/${game_id}`)
}
export default class extends AbstractView {
constructor(params)
{
@ -21,28 +17,36 @@ export default class extends AbstractView {
}
else
{
await this.matchmaking();
let nb_players = document.getElementById("nb_players-input").value
await client.matchmaking.start(this.display_data, this.ondisconnect, nb_players);
document.getElementById("button").value = "Stop matchmaking"
}
}
async matchmaking()
ondisconnect()
{
let nb_players = document.getElementById("nb_players-input").value
document.getElementById("button").value = "Find a game"
}
client.matchmaking.start(game_found, nb_players);
display_data(data)
{
clear("innerText", ["detail"]);
fill_errors(data, "innerText");
}
async postInit()
{
document.getElementById("button").onclick = this.matchmaking
document.getElementById("button").onclick = this.press_button.bind(this)
}
async getHtml() {
return `
<h1>Select mode<h1>
<h1>Select mode</h1>
<input type="number" value="2" id="nb_players-input">
<input type="button" value="Find a game" id="button">
<span id="detail"></span>
`;
}