import { AExchangeable } from "../AExchangable.js"; import { Client } from "../Client.js"; import { Profile } from "../Profile.js"; class Tourmanent extends AExchangeable { /** * * @param {Client} client * @param {Number} id the id of the tournament */ constructor(client, id) { super(); /** * @type {Number} */ this.id = id; /** * @type {Client} */ this.client = client; /** * @type {Number} */ this.nb_participants; /** * @type {[Profile]} proutman à encore frappé */ this.participantList = [] /** * @type {Boolean} */ this.started; /** * @type {Number} */ this.finished; /** * @type {"finished" | "started" | "waiting"} must be "finished", or "started", or "waiting". Any other return all elements */ this.state; /** * @type {Boolean} the client is a participant of the tournament */ this.is_participating; } /** * @param {Boolean} newParticipation */ async setParticipation(newParticipation) { if (this.isParticipating == newParticipation) return; this.isParticipating = newParticipation; this._socket.send(JSON.stringify({"detail": "update_participating", "is_participating": newParticipation}) ); } /** * * @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.import(response_data); } leave(event) { if (this.connected == false) return; this.connected = false; this._socket.close(); if (this.disconnectHandler != null) this.disconnectHandler(event); } /** * @param {Object} data */ async _receiveAddParticipant(data) { const participant = new Profile(this.client, undefined, data.participant.user_id); participant.import(data.participant) this.participantList.push(participant); await this._addParticipantHandler(this.participantList.length) } /** * @param {Object} data */ async _receiveDelParticipant(data) { const index = this.participantList.indexOf((profile) => profile.id === data.profile.user_id) this.participantList.splice(index, 1); await this._delParticipantHandler(this.participantList.length); } async _receiveError(data) { await this.errorHandler(data); } async _receiveGoTo(data) { await this._goToHandler(data) } /** * * @param {MessageEvent} event */ async onReceive(event) { const data = JSON.parse(event.data); switch (data.detail) { case "error": await this._receiveError(data) break; case "add_participant": await this._receiveAddParticipant(data); break; case "del_participant": await this._receiveDelParticipant(data); break; case "go_to": await this._receiveGoTo(data); break default: break; } } /** * Join the tournament Websocket * @param {CallableFunction} errorHandler * @param {CallableFunction} addParticipantHandler called when a participants join the tournament * @param {CallableFunction} delParticipantHandler called when a participants leave the tournament * @param {CallableFunction} disconnectHandler * @param {CallableFunction} goToHandler called when the next game will start * @param {CallableFunction} startHandler called when tournament start * @param {CallableFunction} finishHandler called when tournament finish * @returns {Promise} */ async join(addParticipantHandler, delParticipantHandler, startHandler, finishHandler, errorHandler, goToHandler, 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._startHandler = startHandler; this._finishHandler = finishHandler; this._addParticipantHandler = addParticipantHandler; this._delParticipantHandler = delParticipantHandler; this._errorHandler = errorHandler; this._disconnectHandler = disconnectHandler; this._goToHandler = goToHandler; this._socket.onmessage = this.onReceive.bind(this); this._socket.onclose = this.leave.bind(this); } } export { Tourmanent };