From 6fd11d0028e2dda012b7061902ed8ec8f5a98ce7 Mon Sep 17 00:00:00 2001 From: starnakin Date: Mon, 22 Jan 2024 13:21:51 +0100 Subject: [PATCH] game: add: docstring --- frontend/static/js/api/MyProfile.js | 9 ++ frontend/static/js/api/account.js | 22 ++++- frontend/static/js/api/client.js | 90 +++++++++++++++++++ frontend/static/js/api/game/MyPlayer.js | 4 - frontend/static/js/api/game/Player.js | 1 - frontend/static/js/api/matchmaking.js | 14 ++- frontend/static/js/api/profile.js | 16 ++++ frontend/static/js/api/profiles.js | 19 ++++ .../static/js/api/tournament/tournament.js | 51 ++++++++++- .../static/js/api/tournament/tournaments.js | 17 +++- 10 files changed, 231 insertions(+), 12 deletions(-) diff --git a/frontend/static/js/api/MyProfile.js b/frontend/static/js/api/MyProfile.js index a42d83d..208531a 100644 --- a/frontend/static/js/api/MyProfile.js +++ b/frontend/static/js/api/MyProfile.js @@ -1,13 +1,22 @@ +import { Client } from "./client.js"; import { Profile } from "./profile.js"; class MyProfile extends Profile { + /** + * @param {Client} client + */ constructor (client) { super(client, "me") } + /** + * + * @param {*} form_data + * @returns + */ async change_avatar(form_data) { let response = await this.client._patch_file(`/api/profiles/me`, form_data); diff --git a/frontend/static/js/api/account.js b/frontend/static/js/api/account.js index 2247106..6abb89b 100644 --- a/frontend/static/js/api/account.js +++ b/frontend/static/js/api/account.js @@ -12,7 +12,12 @@ class Account */ this.client = client; } - + + /** + * @param {String} username + * @param {String} password + * @returns + */ async create(username, password) { let response = await this.client._post("/api/accounts/register", {username: username, password: password}); @@ -26,6 +31,10 @@ class Account return response_data } + /** + * @param {String} password + * @returns + */ async delete(password) { let response = await this.client._delete("/api/accounts/delete", {password: password}); @@ -41,6 +50,10 @@ class Account return response_data; } + /** + * Get account data (username) + * @returns + */ async get() { let response = await this.client._get("/api/accounts/edit"); @@ -54,6 +67,12 @@ class Account return response_data; } + /** + * + * @param {*} data + * @param {Number} password + * @returns + */ async update(data, password) { data.current_password = password; @@ -65,6 +84,7 @@ class Account this.client._update_logged(false); return null; } + return response_data; } } diff --git a/frontend/static/js/api/client.js b/frontend/static/js/api/client.js index 0340814..455fc22 100644 --- a/frontend/static/js/api/client.js +++ b/frontend/static/js/api/client.js @@ -6,6 +6,7 @@ import { MyProfile } from "./MyProfile.js"; import { navigateTo } from "../index.js" import { Tourmanents } from "./tournament/tournaments.js"; import {Notice} from "./chat/notice.js" +import { Channel } from "./chat/channel.js"; function getCookie(name) { @@ -19,21 +20,62 @@ function getCookie(name) class Client { + /** + * + * @param {String} url + */ constructor(url) { + /** + * @type {String} + */ this._url = url; + + /** + * @type {Account} + */ this.account = new Account(this); + + /** + * @type {Profiles} + */ this.profiles = new Profiles(this); + + /** + * @type {MatchMaking} + */ 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 isAuthentificated() + */ this._logged = undefined; + /** + * @type {Channels} + */ this.channels = new Channels(this); + + /** + * @type {Channel} + */ this.channel = undefined; + /** + * @type {Notice} + */ this.notice = new Notice(this); } + /** + * The only right way to determine is the user is logged + * @returns {Boolean} + */ async isAuthentificate() { if (this._logged == undefined) @@ -41,6 +83,12 @@ class Client return this._logged; } + /** + * Send a GET request to %uri% + * @param {String} uri + * @param {*} data + * @returns {Response} + */ async _get(uri, data) { let response = await fetch(this._url + uri, { @@ -50,6 +98,12 @@ class Client return response; } + /** + * Send a POST request + * @param {String} uri + * @param {*} data + * @returns {Response} + */ async _post(uri, data) { let response = await fetch(this._url + uri, { @@ -63,6 +117,12 @@ class Client return response; } + /** + * Send a DELETE request + * @param {String} uri + * @param {String} data + * @returns {Response} + */ async _delete(uri, data) { let response = await fetch(this._url + uri, { @@ -76,6 +136,12 @@ class Client return response; } + /** + * Send a PATCH request with json + * @param {String} uri + * @param {*} data + * @returns {Response} + */ async _patch_json(uri, data) { let response = await fetch(this._url + uri, { @@ -89,6 +155,12 @@ class Client return response; } + /** + * Send a PATCH request with file + * @param {String} uri + * @param {*} file + * @returns {Response} + */ async _patch_file(uri, file) { let response = await fetch(this._url + uri, { @@ -101,6 +173,11 @@ class Client return response; } + /** + * Change logged state. Use It if you recv an 403 error + * @param {Boolean} state + * @returns + */ async _update_logged(state) { if (this._logged == state) @@ -126,6 +203,12 @@ class Client this._logged = state; } + /** + * Loggin the user + * @param {String} username + * @param {String} password + * @returns + */ async login(username, password) { let response = await this._post("/api/accounts/login", {username: username, password: password}) @@ -135,12 +218,19 @@ class Client return response; } + /** + * Logout the user + */ async logout() { await this._get("/api/accounts/logout"); await this._update_logged(false); } + /** + * Determine if the user is logged. NEVER USE IT, USE isAuthentificated() + * @returns {Boolean} + */ async _test_logged() { let response = await this._get("/api/accounts/logged"); diff --git a/frontend/static/js/api/game/MyPlayer.js b/frontend/static/js/api/game/MyPlayer.js index 552d9f2..b0662ef 100644 --- a/frontend/static/js/api/game/MyPlayer.js +++ b/frontend/static/js/api/game/MyPlayer.js @@ -6,7 +6,6 @@ import { Segment } from "./Segment.js"; class MyPlayer extends Player { /** - * * @param {Client} client * @param {Game} game * @param {Segment} rail @@ -23,9 +22,7 @@ class MyPlayer extends Player } /** - * * @param {[string]} keys_pressed - * @returns */ update_paddle(keys_pressed) { @@ -47,7 +44,6 @@ class MyPlayer extends Player } /** - * * @param {Number} new_position * @param {Number} time */ diff --git a/frontend/static/js/api/game/Player.js b/frontend/static/js/api/game/Player.js index 44ee621..8736db1 100644 --- a/frontend/static/js/api/game/Player.js +++ b/frontend/static/js/api/game/Player.js @@ -5,7 +5,6 @@ import { Segment } from "./Segment.js"; class Player { /** - * * @param {Number} id * @param {Game} game * @param {Segment} rail diff --git a/frontend/static/js/api/matchmaking.js b/frontend/static/js/api/matchmaking.js index e55e48c..1178803 100644 --- a/frontend/static/js/api/matchmaking.js +++ b/frontend/static/js/api/matchmaking.js @@ -13,7 +13,14 @@ class MatchMaking this.client = client this.searching = false; } - + + /** + * + * @param {CallableFunction} receive_func + * @param {CallableFunction} disconnect_func + * @param {Number} mode The number of players in a game + * @returns {undefined} + */ async start(receive_func, disconnect_func, mode) { if (!await this.client.isAuthentificate()) @@ -36,6 +43,7 @@ class MatchMaking this._socket.onclose = this.onclose.bind(this); } + onclose(event) { this.stop(); @@ -44,8 +52,10 @@ class MatchMaking async stop() { + if (this._socket) + this._socket.close() + this._socket = undefined this.searching = false; - this._socket.close() } } diff --git a/frontend/static/js/api/profile.js b/frontend/static/js/api/profile.js index 29db822..39717b5 100644 --- a/frontend/static/js/api/profile.js +++ b/frontend/static/js/api/profile.js @@ -11,9 +11,25 @@ class Profile * @type {Client} client */ this.client = client; + + /** + * @type {String} + */ this.username = username; + + /** + * @type {Number} + */ this.id = id; + + /** + * @type {String} + */ this.avatar_url = avatar_url; + + /** + * @type {Boolean} + */ this.isBlocked = false; } diff --git a/frontend/static/js/api/profiles.js b/frontend/static/js/api/profiles.js index 70618db..52206ec 100644 --- a/frontend/static/js/api/profiles.js +++ b/frontend/static/js/api/profiles.js @@ -13,6 +13,10 @@ class Profiles this.client = client } + /** + * + * @returns {[Profile]} + */ async all() { let response = await this.client._get("/api/profiles/"); @@ -25,6 +29,11 @@ class Profiles return profiles; } + /** + * + * @param {String} username + * @returns {?Profile} + */ async getProfile(username) { let profile = new Profile(this.client, username); @@ -33,6 +42,11 @@ class Profiles return profile; } + /** + * Block a user + * @param {Number} user_id + * @returns + */ async block(user_id) { // blocker & blocked @@ -45,6 +59,11 @@ class Profiles } + /** + * Unblock a user + * @param {Number} user_id + * @returns + */ async deblock(user_id) { // blocker & blocked diff --git a/frontend/static/js/api/tournament/tournament.js b/frontend/static/js/api/tournament/tournament.js index 75816df..b0ac785 100644 --- a/frontend/static/js/api/tournament/tournament.js +++ b/frontend/static/js/api/tournament/tournament.js @@ -3,7 +3,17 @@ import { Client } from "../client.js"; class Tourmanent { /** - * @param {Client} client + * + * @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} level + * @param {Boolean} started + * @param {Boolean} finished + * @param {[]} levels + * @param {String} state */ constructor(client, id, name = undefined, nb_players = undefined, nb_players_by_game = undefined, level = undefined, started = undefined, finished = undefined, levels = undefined, state = undefined) { @@ -11,16 +21,55 @@ class Tourmanent * @type {Client} */ this.client = client; + + /** + * @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.nb_players_by_game = nb_players_by_game; + + /** + * @type {Number} + */ this.level = level; + + /** + * @type {Number} + */ this.started = started; + + /** + * @type {Number} + */ this.finished = finished; + + /** + * @type {[]} + */ this.levels = levels; + + /** + * @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 + */ this.connected = false; } diff --git a/frontend/static/js/api/tournament/tournaments.js b/frontend/static/js/api/tournament/tournaments.js index a560111..2ae420a 100644 --- a/frontend/static/js/api/tournament/tournaments.js +++ b/frontend/static/js/api/tournament/tournaments.js @@ -14,6 +14,11 @@ class Tourmanents this.client = client } + /** + * + * @param {Number} id + * @returns + */ async getTournament(id) { let tournament = new Tourmanent(this.client); @@ -22,6 +27,13 @@ class Tourmanents return tournament; } + /** + * + * @param {Number} nb_players + * @param {Number} nb_players_by_game + * @param {String} name + * @returns + */ async createTournament(nb_players, nb_players_by_game, name = "") { let response = await this.client._post("/api/tournaments/", {nb_players: nb_players, nb_players_by_game: nb_players_by_game, name: name}); @@ -34,15 +46,14 @@ class Tourmanents } /** - * @param {string} state must be "finished", or "started", or "waiting". Any other return all elements + * @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) + if (response.status === 403) { this.client._update_logged(false); return null;