2023-12-16 12:00:38 -05:00
|
|
|
import { Client } from "./client.js";
|
2023-12-12 06:06:13 -05:00
|
|
|
|
|
|
|
class MatchMaking
|
|
|
|
{
|
|
|
|
/**
|
2023-12-16 12:00:38 -05:00
|
|
|
* @param {Client} client
|
2023-12-12 06:06:13 -05:00
|
|
|
*/
|
|
|
|
constructor(client)
|
|
|
|
{
|
|
|
|
/**
|
2023-12-20 13:10:26 -05:00
|
|
|
* @type {Client}
|
2023-12-12 06:06:13 -05:00
|
|
|
*/
|
|
|
|
this.client = client
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = false;
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
async start(func, mode)
|
2023-12-12 06:06:13 -05:00
|
|
|
{
|
|
|
|
if (!await this.client.isAuthentificate())
|
|
|
|
return null;
|
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
let url = `wss://${window.location.host}/ws/matchmaking/${mode}`;
|
2023-12-12 06:06:13 -05:00
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
this._socket = new WebSocket(url);
|
2023-12-12 06:06:13 -05:00
|
|
|
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = true;
|
|
|
|
|
|
|
|
this._socket.onmessage = function (event) {
|
2023-12-12 06:06:13 -05:00
|
|
|
const data = JSON.parse(event.data);
|
|
|
|
func(data.game_id)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
async stop()
|
|
|
|
{
|
2023-12-23 06:54:33 -05:00
|
|
|
this.searching = false;
|
|
|
|
this._socket.close()
|
2023-12-12 06:06:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export {MatchMaking}
|