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

78 lines
1.6 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-22 08:26:06 -05:00
async createTournament(nb_players, nb_players_by_game, name = "")
2023-12-20 18:21:18 -05:00
{
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();
2023-12-22 08:26:06 -05:00
return response_data;
2023-12-20 18:21:18 -05:00
}
/**
* @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;
}
2023-12-21 05:35:47 -05:00
let tournaments = [];
response_data.forEach(tournament_data => {
tournaments.push(new Tourmanent(this.client,
tournament_data.name,
tournament_data.nb_players,
tournament_data.nb_players_by_game,
tournament_data.level,
tournament_data.started,
tournament_data.finished,
tournament_data.levels,
tournament_data.id));
});
return tournaments;
2023-12-20 18:21:18 -05:00
}
async all()
{
return await this.search("");
2023-12-20 13:15:47 -05:00
}
}
export { Tourmanents }