add: tournament page
This commit is contained in:
		@ -3,6 +3,8 @@ import { MatchMaking } from "./matchmaking.js";
 | 
			
		||||
import { Profiles } from "./profiles.js";
 | 
			
		||||
import { Channels } from './chat/channels.js';
 | 
			
		||||
import { MyProfile } from "./MyProfile.js";
 | 
			
		||||
import { navigateTo } from "../index.js"
 | 
			
		||||
import { Tourmanents } from "./tournament/tournaments.js";
 | 
			
		||||
 | 
			
		||||
function getCookie(name)
 | 
			
		||||
{
 | 
			
		||||
@ -22,6 +24,7 @@ class Client
 | 
			
		||||
		this.account = new Account(this);
 | 
			
		||||
		this.profiles = new Profiles(this);
 | 
			
		||||
		this.matchmaking = new MatchMaking(this);
 | 
			
		||||
		this.tournaments = new Tourmanents(this);
 | 
			
		||||
		this._logged = undefined;
 | 
			
		||||
 | 
			
		||||
		this.channels = new Channels(this);
 | 
			
		||||
@ -101,6 +104,10 @@ class Client
 | 
			
		||||
			this.me = new MyProfile(this);
 | 
			
		||||
			await this.me.init();
 | 
			
		||||
		}
 | 
			
		||||
		if (this.logged && !state)
 | 
			
		||||
		{
 | 
			
		||||
			navigateTo("/login");
 | 
			
		||||
		}
 | 
			
		||||
		this.logged = state;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,18 +1,55 @@
 | 
			
		||||
import { Client } from "./client.js";
 | 
			
		||||
import { Client } from "../client.js";
 | 
			
		||||
 | 
			
		||||
class Tourmanent
 | 
			
		||||
{
 | 
			
		||||
	/**
 | 
			
		||||
	 * @param  {Client} client
 | 
			
		||||
	 */
 | 
			
		||||
	constructor(client)
 | 
			
		||||
	constructor(client, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, id = undefined)
 | 
			
		||||
	{
 | 
			
		||||
		/**
 | 
			
		||||
		 * @type {Client}
 | 
			
		||||
		 */
 | 
			
		||||
		this.client = client
 | 
			
		||||
		this.client = client;
 | 
			
		||||
		this.name = name;
 | 
			
		||||
		this.nb_players = nb_players;
 | 
			
		||||
		this.nb_players_by_game = nb_players_by_game;
 | 
			
		||||
		this.level = level;
 | 
			
		||||
		this.started = started;
 | 
			
		||||
		this.finished = finished;
 | 
			
		||||
		this.levels = levels;
 | 
			
		||||
		this.state = this.get_state();
 | 
			
		||||
		this.id = id
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	get_state()
 | 
			
		||||
	{
 | 
			
		||||
		if (this.finished)
 | 
			
		||||
			return "finished";
 | 
			
		||||
		if (this.started)
 | 
			
		||||
			return "started";
 | 
			
		||||
		else
 | 
			
		||||
			return "waiting";
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	async init(id)
 | 
			
		||||
	{
 | 
			
		||||
		let response = await this.client._get(`/api/tournaments/${id}`);
 | 
			
		||||
 | 
			
		||||
		if (response.status === 404)
 | 
			
		||||
			return null;
 | 
			
		||||
 | 
			
		||||
		let response_data = await response.json();
 | 
			
		||||
 | 
			
		||||
		this.name = response_data.name;
 | 
			
		||||
		this.nb_players = response_data.nb_players;
 | 
			
		||||
		this.nb_players_by_game = response_data.nb_players_by_game;
 | 
			
		||||
		this.level = response_data.level;
 | 
			
		||||
		this.started = response_data.started;
 | 
			
		||||
		this.finished = response_data.finished;
 | 
			
		||||
		this.levels = response_data.levels;
 | 
			
		||||
		this.id = response_data.id
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -1,4 +1,5 @@
 | 
			
		||||
import { Client } from "./client.js";
 | 
			
		||||
import { Client } from "../client.js";
 | 
			
		||||
import { Tourmanent } from "./tournament.js";
 | 
			
		||||
 | 
			
		||||
class Tourmanents
 | 
			
		||||
{
 | 
			
		||||
@ -13,19 +14,59 @@ class Tourmanents
 | 
			
		||||
		this.client = client
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	async createTournament(nb_players, online)
 | 
			
		||||
	async getTournament(id)
 | 
			
		||||
	{
 | 
			
		||||
		if (online)
 | 
			
		||||
			return "offline";
 | 
			
		||||
		let response = await this.client._post("/api/tournaments/", {numbers_of_players: nb_players});
 | 
			
		||||
		let response_data = await response.json();
 | 
			
		||||
		let tournament = new Tourmanent(this.client);
 | 
			
		||||
		await tournament.init(id);
 | 
			
		||||
		return tournament;
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
		if (JSON.stringify(response_data) === JSON.stringify({'detail': 'Authentication credentials were not provided.'}))
 | 
			
		||||
	/**
 | 
			
		||||
	 * @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;			
 | 
			
		||||
		}
 | 
			
		||||
		return response_data.tournament_id;
 | 
			
		||||
 | 
			
		||||
		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("");
 | 
			
		||||
	}
 | 
			
		||||
	
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
		Reference in New Issue
	
	Block a user