tournament in coming

This commit is contained in:
2024-04-18 20:08:13 +02:00
parent 630ef709ab
commit ae0a5abfcd
11 changed files with 265 additions and 287 deletions

View File

@ -1,71 +1,53 @@
import { AExchangeable } from "../AExchangable.js";
import { Client } from "../Client.js";
import { Profile } from "../Profile.js";
class Tourmanent
class Tourmanent extends AExchangeable
{
/**
*
* @param {Client} client
* @param {Number} id the id of the tournament
* @param {Number} name
* @param {Number} nb_players
* @param {Number} nb_players_by_game
* @param {Number} round
* @param {Boolean} started
* @param {Boolean} finished
* @param {[]} rounds
* @param {String} state
*/
constructor(client, id, name = undefined, nb_players = undefined, round = undefined, started = undefined, finished = undefined, rounds = undefined, state = undefined)
constructor(client, id)
{
/**
* @type {Client}
*/
this.client = client;
super();
/**
* @type {String} the name of the tournament
*/
this.name = name || `${nb_players_by_game}x1, ${nb_players} players`;
/**
* @type {Number}
*/
this.nb_players = nb_players;
/**
* @type {Number}
*/
this.round = round;
/**
* @type {Number}
*/
this.started = started;
/**
* @type {Number}
*/
this.finished = finished;
/**
* @type {[]}
*/
this.rounds = rounds;
/**
* @type {String} must be "finished", or "started", or "waiting". Any other return all elements
*/
this.state = state;
/**
* @type {Number}
*/
this.id = id;
/**
* @type {Boolean} if a websocket connection is enabled
* @type {Client}
*/
this.connected = false;
this.client = client;
/**
* @type {Number}
*/
this.nb_participants;
/**
* @type {[Profile]} proutman à encore frappé
*/
this.participantList = []
/**
* @type {Boolean}
*/
this.started;
/**
* @type {Number}
*/
this.finished;
/**
* @type {"finished" | "started" | "waiting"} must be "finished", or "started", or "waiting". Any other return all elements
*/
this.state;
}
/**
@ -81,13 +63,7 @@ class Tourmanent
let response_data = await response.json();
this.name = response_data.name || `${response_data.nb_players} players tournament`;
this.nb_players = response_data.nb_players;
this.round = response_data.round;
this.started = response_data.started;
this.finished = response_data.finished;
this.rounds = response_data.rounds;
this.state = response_data.state;
this.import(response_data);
}
leave(event)
@ -99,18 +75,12 @@ class Tourmanent
this.disconnectHandler(event);
}
toggle_participation()
/**
* @param {Object} data
*/
async _receiveParticipantUpdate(data)
{
if (!this.connected)
return;
this._socket.send(JSON.stringify({participate: ""}));
}
async onPlayersUpdate(data)
{
oldPlayerList = this.par
await this.playersUpdateHandler();
this.par
}
async onError(data)
@ -126,20 +96,22 @@ class Tourmanent
{
const data = JSON.parse(event.data);
if (data?.detail === "error")
if (data.detail === "error")
this.onError(data);
else if (data?.detail === "players_update")
this.onPlayersUpdate(data);
else if (["del_participant", "add_participant"].includes(data.detail))
this._receiveParticipantUpdate(data);
}
/**
* Join the tournament Websocket
* @param {CallableFunction} errorHandler
* @param {CallableFunction} playersUpdateHandler
* @param {CallableFunction} addParticipantHandler called when a participants join the tournament
* @param {CallableFunction} delParticipantHandler called when a participants leave the tournament
* @param {CallableFunction} disconnectHandler
* @param {CallableFunction} goToHandler called when the next game will start
* @returns {?}
*/
async join(playersUpdateHandler, errorHandler, disconnectHandler)
async join(participantsUpdateHandler, errorHandler, goToHandler, disconnectHandler)
{
if (!await this.client.isAuthenticated())
return null;
@ -151,9 +123,10 @@ class Tourmanent
this.connected = true;
this.isParticipating = false;
this.playersUpdateHandler = playersUpdateHandler;
this.participantsUpdateHandler = participantsUpdateHandler;
this.errorHandler = errorHandler;
this.disconnectHandler = disconnectHandler;
this.goToHandler = goToHandler;
this._socket.onmessage = this.onReceive.bind(this);

View File

@ -29,13 +29,13 @@ class Tourmanents
/**
*
* @param {Number} nb_players
* @param {Number} nb_participants
* @param {String} name
* @returns {Response}
*/
async createTournament(nb_players, name = "")
async createTournament(nb_participants, name = "")
{
let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, name: name});
let response = await this.client._post("/api/tournaments/", {nb_participants: nb_participants, name: name});
return response;
}
@ -58,15 +58,9 @@ class Tourmanents
let tournaments = [];``
response_data.forEach(tournament_data => {
tournaments.push(new Tourmanent(this.client,
tournament_data.name,
tournament_data.nb_players,
tournament_data.round,
tournament_data.started,
tournament_data.finished,
tournament_data.rounds,
tournament_data.id,
tournament_data.state));
let tournament = new Tourmanent(this.client, tournament_data.id);
tournament.import(tournament_data);
tournaments.push(tournament);
});
return tournaments;