42_ft_transcendence/frontend/static/js/api/tournament/Tournament.js
2024-04-16 19:13:18 +02:00

166 lines
3.3 KiB
JavaScript

import { Client } from "../Client.js";
class Tourmanent
{
/**
*
* @param {Client} client
* @param {Number} id the id of the tournament
* @param {Number} name
* @param {Number} nb_players
* @param {Number} nb_players_by_game
* @param {Number} round
* @param {Boolean} started
* @param {Boolean} finished
* @param {[]} rounds
* @param {String} state
*/
constructor(client, id, name = undefined, nb_players = undefined, round = undefined, started = undefined, finished = undefined, rounds = undefined, state = undefined)
{
/**
* @type {Client}
*/
this.client = client;
/**
* @type {String} the name of the tournament
*/
this.name = name || `${nb_players_by_game}x1, ${nb_players} players`;
/**
* @type {Number}
*/
this.nb_players = nb_players;
/**
* @type {Number}
*/
this.round = round;
/**
* @type {Number}
*/
this.started = started;
/**
* @type {Number}
*/
this.finished = finished;
/**
* @type {[]}
*/
this.rounds = rounds;
/**
* @type {String} must be "finished", or "started", or "waiting". Any other return all elements
*/
this.state = state;
/**
* @type {Number}
*/
this.id = id;
/**
* @type {Boolean} if a websocket connection is enabled
*/
this.connected = false;
}
/**
*
* @returns {Promise<?>}
*/
async init()
{
let response = await this.client._get(`/api/tournaments/${this.id}`);
if (response.status !== 200)
return response.status;
let response_data = await response.json();
this.name = response_data.name || `${response_data.nb_players} players tournament`;
this.nb_players = response_data.nb_players;
this.round = response_data.round;
this.started = response_data.started;
this.finished = response_data.finished;
this.rounds = response_data.rounds;
this.state = response_data.state;
}
leave(event)
{
if (this.connected == false)
return;
this.connected = false;
this._socket.close();
this.disconnectHandler(event);
}
toggle_participation()
{
if (!this.connected)
return;
this._socket.send(JSON.stringify({participate: ""}));
}
async onPlayersUpdate(data)
{
oldPlayerList = this.par
await this.playersUpdateHandler();
}
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 === "players_update")
this.onPlayersUpdate(data);
}
/**
* Join the tournament Websocket
* @param {CallableFunction} errorHandler
* @param {CallableFunction} playersUpdateHandler
* @param {CallableFunction} disconnectHandler
* @returns {?}
*/
async join(playersUpdateHandler, errorHandler, disconnectHandler)
{
if (!await this.client.isAuthenticated())
return null;
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/tournaments/${this.id}`;
this._socket = new WebSocket(url);
this.connected = true;
this.isParticipating = false;
this.playersUpdateHandler = playersUpdateHandler;
this.errorHandler = errorHandler;
this.disconnectHandler = disconnectHandler;
this._socket.onmessage = this.onReceive.bind(this);
this._socket.onclose = this.leave.bind(this);
}
}
export { Tourmanent };