ft_transcendence/frontend/static/js/api/tournament/tournaments.js

75 lines
1.5 KiB
JavaScript
Raw Normal View History

2023-12-20 18:21:18 -05:00
import { Client } from "../client.js";
import { Tourmanent } from "./tournament.js";
2023-12-20 13:15:47 -05:00
class Tourmanents
{
/**
* @param {Client} client
*/
constructor(client)
{
/**
* @type {Client}
*/
this.client = client
}
2023-12-20 18:21:18 -05:00
async getTournament(id)
2023-12-20 13:15:47 -05:00
{
2023-12-20 18:21:18 -05:00
let tournament = new Tourmanent(this.client);
if (await tournament.init(id))
return null;
2023-12-20 18:21:18 -05:00
return tournament;
}
2023-12-20 13:15:47 -05:00
2023-12-20 18:21:18 -05:00
/**
* @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)
2023-12-20 13:15:47 -05:00
{
this.client._update_logged(false);
return null;
}
2023-12-20 18:21:18 -05:00
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("");
2023-12-20 13:15:47 -05:00
}
}
export { Tourmanents }