Merge branch 'main' of codeberg.org:adrien-lsh/ft_transcendence

This commit is contained in:
Namonay
2024-03-25 14:56:57 +01:00
10 changed files with 282 additions and 67 deletions

View File

@ -21,14 +21,12 @@ class MatchMaking
* @param {Number} mode The number of players in a game
* @returns {Promise<?>}
*/
async start(receive_func, disconnect_func, mode, gamemode)
async start(receive_func, disconnect_func, gamemode, mode)
{
if (!await this.client.isAuthenticated())
return null;
this.gamemode = gamemode
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${mode}`;
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/matchmaking/${gamemode}/${mode}`;
this._socket = new WebSocket(url);
@ -51,10 +49,7 @@ class MatchMaking
this.disconnect_func(event);
}
/**
* @returns {Promise<?>}
*/
async stop()
stop()
{
if (this._socket)
this._socket.close();

View File

@ -102,7 +102,7 @@ class Tourmanent
return;
this.connected = false;
this._socket.close();
this.disconnect_func(event);
this.disconnectHandler(event);
}
toggle_participation()
@ -112,13 +112,40 @@ class Tourmanent
this._socket.send(JSON.stringify({participate: ""}));
}
async onParticipantsUpdate(data)
{
oldParticipantList = this.par
await this.participantsUpdateHandler();
}
async onError(data)
{
await this.errorHandler(data);
}
/**
*
* @param {MessageEvent} event
*/
onReceive(event)
{
const data = JSON.parse(event.data);
if (data?.detail === "error")
this.onError(data);
else if (data?.detail === "participants_update")
this.onParticipantsUpdate(data);
}
/**
* Join the tournament Websocket
* @param {CallableFunction} receive_func
* @param {CallableFunction} disconnect_func
* @param {CallableFunction} errorHandler
* @param {CallableFunction} participantsUpdateHandler
* @param {CallableFunction} disconnectHandler
* @returns {?}
*/
async join(receive_func, disconnect_func)
async join(participantsUpdateHandler, errorHandler, disconnectHandler)
{
if (!await this.client.isAuthenticated())
return null;
@ -130,13 +157,11 @@ class Tourmanent
this.connected = true;
this.isParticipating = false;
this.receive_func = receive_func;
this.disconnect_func = disconnect_func;
this.participantsUpdateHandler = participantsUpdateHandler;
this.errorHandler = errorHandler;
this.disconnectHandler = disconnectHandler;
this._socket.onmessage = function (event) {
const data = JSON.parse(event.data);
receive_func(data);
};
this._socket.onmessage = this.onReceive.bind(this);
this._socket.onclose = this.leave.bind(this);
}