add: tournament page

This commit is contained in:
starnakin 2023-12-21 00:21:18 +01:00
parent 8e0514514b
commit 6295c5120f
5 changed files with 153 additions and 11 deletions

View File

@ -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;
}

View File

@ -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
}
}

View File

@ -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("");
}
}

View File

@ -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 },

View File

@ -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 `
<table>
<thead>
<tr>
<th id="name">Loading...</th>
</tr>
</thead>
<tbody>
<tr>
<td>Number of players</td>
<td id="nb_players">Loading...</td>
</tr>
<tr>
<td>Number of players by game</td>
<td id="nb_players_by_game">Loading...</td>
</tr>
<tr>
<td>Number of round</td>
<td id="level">Loading...</td>
</tr>
<tr>
<td>status</td>
<td id="state">Loading...</td>
</tr>
</tbody>
</table>
`
}
}