ft_transcendence/frontend/static/js/views/TournamentPageView.js

99 lines
2.6 KiB
JavaScript
Raw Normal View History

2023-12-26 12:24:23 -05:00
import {client, navigateTo} from "../index.js";
2023-12-20 18:21:18 -05:00
import AbstractAuthentifiedView from "./abstracts/AbstractAuthentifiedView.js";
2023-12-21 05:35:47 -05:00
export default class extends AbstractAuthentifiedView
{
constructor(params)
{
2023-12-20 18:21:18 -05:00
super(params, "Tournament");
this.id = params.id;
}
pressButton()
{
this.tournament.toggle_participation();
}
async receive(data)
{
if (data.detail === "nb_participants" || data.detail === "update_participants")
document.getElementById("nb_participants").innerText = `${data.nb_participants} / ${this.tournament.nb_players}`
2023-12-26 12:24:23 -05:00
if (data.detail === "go_to")
navigateTo(data.url);
if (data.detail === "is_participant")
this.updateParticipating(data.is_participant)
if (data.detail === "error")
document.getElementById("display").innerText = data.error_message
}
async updateParticipating(state)
{
document.getElementById("button").value = state ? `Leave ${this.tournament.name}` : `Join ${this.tournament.name}`;
document.getElementById("display").innerText = state ? "You are a particpant" : "You are not a participant";
}
async ondisconnect(event)
{
}
2023-12-20 18:21:18 -05:00
async postInit()
{
this.tournament = await client.tournaments.getTournament(this.id);
2023-12-20 18:21:18 -05:00
if (this.tournament === null)
2023-12-20 18:21:18 -05:00
return 1;
this.tournament.join(this.receive.bind(this), this.ondisconnect.bind(this));
let button = document.getElementById("button")
button.onclick = this.pressButton.bind(this);
2023-12-20 18:21:18 -05:00
document.getElementById("name").innerText = this.tournament.name;
document.getElementById("nb_players").innerText = this.tournament.nb_players;
document.getElementById("nb_players_by_game").innerText = this.tournament.nb_players_by_game;
document.getElementById("level").innerText = this.tournament.level;
document.getElementById("state").innerText = this.tournament.state;
if (this.tournament.state === "waiting")
button.disabled = false;
2023-12-20 18:21:18 -05:00
}
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>Number of player</td>
<td id="nb_participants">Loading...</td>
</tr>
2023-12-20 18:21:18 -05:00
<tr>
<td>status</td>
<td id="state">Loading...</td>
</tr>
</tbody>
</table>
<input type="button" id="button" value="Join tournament" disabled>
2023-12-26 12:24:23 -05:00
<span id="display"></span>
`
2023-12-20 18:21:18 -05:00
}
}