clean: rm online tournament
This commit is contained in:
@ -3,7 +3,6 @@ import { MatchMaking } from "./Matchmaking.js";
|
||||
import { Profiles } from "./Profiles.js";
|
||||
import { Channels } from './chat/Channels.js';
|
||||
import { MyProfile } from "./MyProfile.js";
|
||||
import { Tourmanents } from "./tournament/Tournaments.js";
|
||||
import { Channel } from "./chat/Channel.js";
|
||||
import Notice from "./Notice.js";
|
||||
import LanguageManager from './LanguageManager.js';
|
||||
@ -46,11 +45,6 @@ class Client
|
||||
*/
|
||||
this.matchmaking = new MatchMaking(this);
|
||||
|
||||
/**
|
||||
* @type {Tourmanents}
|
||||
*/
|
||||
this.tournaments = new Tourmanents(this);
|
||||
|
||||
/**
|
||||
* @type {Boolean} A private var represent if the is is log NEVER USE IT use await isAuthenticated()
|
||||
*/
|
||||
|
@ -1,201 +0,0 @@
|
||||
import { AExchangeable } from "../AExchangable.js";
|
||||
import { Client } from "../Client.js";
|
||||
import { Profile } from "../Profile.js";
|
||||
|
||||
class Tourmanent extends AExchangeable
|
||||
{
|
||||
/**
|
||||
*
|
||||
* @param {Client} client
|
||||
* @param {Number} id the id of the tournament
|
||||
*/
|
||||
constructor(client, id)
|
||||
{
|
||||
super();
|
||||
|
||||
/**
|
||||
* @type {Number}
|
||||
*/
|
||||
this.id = id;
|
||||
|
||||
/**
|
||||
* @type {Client}
|
||||
*/
|
||||
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;
|
||||
|
||||
/**
|
||||
* @type {Boolean} the client is a participant of the tournament
|
||||
*/
|
||||
this.is_participating;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Boolean} newParticipation
|
||||
*/
|
||||
async setParticipation(newParticipation)
|
||||
{
|
||||
if (this.isParticipating == newParticipation)
|
||||
return;
|
||||
|
||||
this.isParticipating = newParticipation;
|
||||
|
||||
this._socket.send(JSON.stringify({"detail": "update_participating",
|
||||
"is_participating": newParticipation})
|
||||
);
|
||||
|
||||
}
|
||||
/**
|
||||
*
|
||||
* @returns {Promise<?>}
|
||||
*/
|
||||
async init()
|
||||
{
|
||||
let response = await this.client._get(`/api/tournaments/${this.id}`);
|
||||
|
||||
if (response.status !== 200)
|
||||
return response.status;
|
||||
|
||||
let response_data = await response.json();
|
||||
|
||||
this.import(response_data);
|
||||
}
|
||||
|
||||
leave(event)
|
||||
{
|
||||
if (this.connected == false)
|
||||
return;
|
||||
this.connected = false;
|
||||
this._socket.close();
|
||||
if (this.disconnectHandler != null)
|
||||
this.disconnectHandler(event);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} data
|
||||
*/
|
||||
async _receiveAddParticipant(data)
|
||||
{
|
||||
const participant = new Profile(this.client, undefined, data.participant.user_id);
|
||||
participant.import(data.participant)
|
||||
|
||||
this.participantList.push(participant);
|
||||
|
||||
await this._addParticipantHandler(this.participantList.length)
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Object} data
|
||||
*/
|
||||
async _receiveDelParticipant(data)
|
||||
{
|
||||
const index = this.participantList.indexOf((profile) => profile.id === data.profile.user_id)
|
||||
|
||||
this.participantList.splice(index, 1);
|
||||
|
||||
await this._delParticipantHandler(this.participantList.length);
|
||||
}
|
||||
|
||||
async _receiveError(data)
|
||||
{
|
||||
await this.errorHandler(data);
|
||||
}
|
||||
|
||||
async _receiveGoTo(data)
|
||||
{
|
||||
await this._goToHandler(data)
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {MessageEvent} event
|
||||
*/
|
||||
async onReceive(event)
|
||||
{
|
||||
const data = JSON.parse(event.data);
|
||||
|
||||
switch (data.detail) {
|
||||
case "error":
|
||||
await this._receiveError(data)
|
||||
break;
|
||||
|
||||
case "add_participant":
|
||||
await this._receiveAddParticipant(data);
|
||||
break;
|
||||
|
||||
case "del_participant":
|
||||
await this._receiveDelParticipant(data);
|
||||
break;
|
||||
|
||||
case "go_to":
|
||||
await this._receiveGoTo(data);
|
||||
break
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Join the tournament Websocket
|
||||
* @param {CallableFunction} errorHandler
|
||||
* @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
|
||||
* @param {CallableFunction} startHandler called when tournament start
|
||||
* @param {CallableFunction} finishHandler called when tournament finish
|
||||
* @returns {Promise}
|
||||
*/
|
||||
async join(addParticipantHandler, delParticipantHandler, startHandler, finishHandler, errorHandler, goToHandler, disconnectHandler)
|
||||
{
|
||||
if (!await this.client.isAuthenticated())
|
||||
return null;
|
||||
|
||||
let url = `${window.location.protocol[4] === 's' ? 'wss' : 'ws'}://${window.location.host}/ws/tournaments/${this.id}`;
|
||||
|
||||
this._socket = new WebSocket(url);
|
||||
|
||||
this.connected = true;
|
||||
this.isParticipating = false;
|
||||
|
||||
this._startHandler = startHandler;
|
||||
this._finishHandler = finishHandler;
|
||||
this._addParticipantHandler = addParticipantHandler;
|
||||
this._delParticipantHandler = delParticipantHandler;
|
||||
this._errorHandler = errorHandler;
|
||||
this._disconnectHandler = disconnectHandler;
|
||||
this._goToHandler = goToHandler;
|
||||
|
||||
this._socket.onmessage = this.onReceive.bind(this);
|
||||
|
||||
this._socket.onclose = this.leave.bind(this);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { Tourmanent };
|
@ -1,80 +0,0 @@
|
||||
import { Client } from "../Client.js";
|
||||
import { Tourmanent } from "./Tournament.js";
|
||||
|
||||
class Tourmanents
|
||||
{
|
||||
/**
|
||||
* @param {Client} client
|
||||
*/
|
||||
constructor(client)
|
||||
{
|
||||
/**
|
||||
* @type {Client}
|
||||
*/
|
||||
this.client = client;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} id
|
||||
* @returns {Promise<Tournament>}
|
||||
*/
|
||||
async getTournament(id)
|
||||
{
|
||||
let tournament = new Tourmanent(this.client, id);
|
||||
if (await tournament.init())
|
||||
return null;
|
||||
return tournament;
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @param {Number} nb_participants
|
||||
* @param {String} name
|
||||
* @returns {Response}
|
||||
*/
|
||||
async createTournament(nb_participants, name = "")
|
||||
{
|
||||
let response = await this.client._post("/api/tournaments/", {nb_participants: nb_participants, name: name});
|
||||
|
||||
return response;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {String} state must be "finished", or "started", or "waiting". Any other return all elements
|
||||
* @returns {?Promise<[Tourmanent]>}
|
||||
*/
|
||||
async search(state)
|
||||
{
|
||||
let response = await this.client._get(`/api/tournaments/search/${state}`);
|
||||
let response_data = await response.json();
|
||||
|
||||
if (response.status === 403)
|
||||
{
|
||||
this.client._update_logged(false);
|
||||
return null;
|
||||
}
|
||||
|
||||
let tournaments = [];``
|
||||
|
||||
response_data.forEach(tournament_data => {
|
||||
let tournament = new Tourmanent(this.client, tournament_data.id);
|
||||
tournament.import(tournament_data);
|
||||
tournaments.push(tournament);
|
||||
});
|
||||
|
||||
return tournaments;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all tournaments
|
||||
* @returns {?Promise<[Tourmanent]>}
|
||||
*/
|
||||
async all()
|
||||
{
|
||||
return await this.search("");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
export { Tourmanents };
|
Reference in New Issue
Block a user