diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index ec3d54b..54fd1a4 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -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; } diff --git a/frontend/static/js/api/tournament/tournament.js b/frontend/static/js/api/tournament/tournament.js index 7ecce9c..141ae57 100644 --- a/frontend/static/js/api/tournament/tournament.js +++ b/frontend/static/js/api/tournament/tournament.js @@ -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 + } } diff --git a/frontend/static/js/api/tournament/tournaments.js b/frontend/static/js/api/tournament/tournaments.js index 60a92a7..8424500 100644 --- a/frontend/static/js/api/tournament/tournaments.js +++ b/frontend/static/js/api/tournament/tournaments.js @@ -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(""); } } diff --git a/frontend/static/js/index.js b/frontend/static/js/index.js index f81cf96..f4d8510 100644 --- a/frontend/static/js/index.js +++ b/frontend/static/js/index.js @@ -14,6 +14,7 @@ import AbstractRedirectView from "./views/abstracts/AbstractRedirectView.js"; import MeView from "./views/MeView.js"; import ProfilePageView from "./views/ProfilePageView.js"; import MatchMakingView from "./views/MatchMakingView.js"; +import TournamentPageView from "./views/TournamentPageView.js"; let client = new Client(location.protocol + "//" + location.host) @@ -52,6 +53,7 @@ const router = async (uri) => { const routes = [ { path: "/", view: Dashboard }, { path: "/profiles/:id", view: ProfilePageView }, + { path: "/tournaments/:id", view: TournamentPageView }, { path: "/login", view: LoginView }, { path: "/logout", view: LogoutView }, { path: "/register", view: RegisterView }, diff --git a/frontend/static/js/views/TournamentPageView.js b/frontend/static/js/views/TournamentPageView.js new file mode 100644 index 0000000..92c4afd --- /dev/null +++ b/frontend/static/js/views/TournamentPageView.js @@ -0,0 +1,55 @@ +import {client} from "../index.js"; +import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js"; + +export default class extends AbstractAuthentifiedView { + constructor(params) { + super(params, "Tournament"); + this.id = params.id; + } + + async postInit() + { + let tournament = await client.tournaments.getTournament(this.id); + + if (tournament === null) + return 1; + + document.getElementById("name").innerText = tournament.name || `${tournament.nb_players_by_game}x1, ${tournament.nb_players} players`; + document.getElementById("nb_players").innerText = tournament.nb_players; + document.getElementById("nb_players_by_game").innerText = tournament.nb_players_by_game; + document.getElementById("level").innerText = tournament.level; + document.getElementById("state").innerText = tournament.state; + + } + + async getHtml() + { + return ` + + + + + + + + + + + + + + + + + + + + + + + + +
Loading...
Number of playersLoading...
Number of players by gameLoading...
Number of roundLoading...
statusLoading...
+ ` + } +}