ft_transcendence/frontend/static/js/api/tournament/tournaments.js
2023-12-21 00:21:18 +01:00

74 lines
1.5 KiB
JavaScript

import { Client } from "../client.js";
import { Tourmanent } from "./tournament.js";
class Tourmanents
{
/**
* @param {Client} client
*/
constructor(client)
{
/**
* @type {Client}
*/
this.client = client
}
async getTournament(id)
{
let tournament = new Tourmanent(this.client);
await tournament.init(id);
return tournament;
}
/**
* @param {boolean} online if the tournament is online or offline
*/
async createTournament(nb_players, nb_players_by_game, name = "", online = true)
{
if (online === false)
{
let tournament = new Tourmanent(this.client, nb_players, nb_players_by_game, 5, false, false, [], 0);
return tournament;
}
let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, nb_players_by_game: nb_players_by_game, name: name});
if (response.status === 403)
{
this.client._update_logged(false);
return null;
}
let response_data = await response.json();
let tournament = this.getTournament(response_data["id"]);
return tournament;
}
/**
* @param {string} state must be "finished", or "started", or "waiting". Any other return all elements
*/
async search(state)
{
let response = await this.client._get(`/api/tournaments/search/${state}`);
let response_data = await response.json()
if (response.status === 404)
{
this.client._update_logged(false);
return null;
}
return response_data;
}
async all()
{
return await this.search("");
}
}
export { Tourmanents }