import { Client } from "../Client.js"; import { Tourmanent } from "./Tournament.js"; class Tourmanents { /** * @param {Client} client */ constructor(client) { /** * @type {Client} */ this.client = client; } /** * * @param {Number} id * @returns {Promise} */ async getTournament(id) { let tournament = new Tourmanent(this.client, id); if (await tournament.init()) return null; return tournament; } /** * * @param {Number} nb_participants * @param {String} name * @returns {Response} */ async createTournament(nb_participants, name = "") { let response = await this.client._post("/api/tournaments/", {nb_participants: nb_participants, name: name}); return response; } /** * @param {String} state must be "finished", or "started", or "waiting". Any other return all elements * @returns {?Promise<[Tourmanent]>} */ async search(state) { let response = await this.client._get(`/api/tournaments/search/${state}`); let response_data = await response.json(); if (response.status === 403) { this.client._update_logged(false); return null; } let tournaments = [];`` response_data.forEach(tournament_data => { let tournament = new Tourmanent(this.client, tournament_data.id); tournament.import(tournament_data); tournaments.push(tournament); }); return tournaments; } /** * Get all tournaments * @returns {?Promise<[Tourmanent]>} */ async all() { return await this.search(""); } } export { Tourmanents };