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

89 lines
1.9 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;
}
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 }