game: add: docstring

This commit is contained in:
starnakin 2024-01-22 13:21:51 +01:00
parent ab73826c11
commit 6fd11d0028
10 changed files with 231 additions and 12 deletions

View File

@ -1,13 +1,22 @@
import { Client } from "./client.js";
import { Profile } from "./profile.js"; import { Profile } from "./profile.js";
class MyProfile extends Profile class MyProfile extends Profile
{ {
/**
* @param {Client} client
*/
constructor (client) constructor (client)
{ {
super(client, "me") super(client, "me")
} }
/**
*
* @param {*} form_data
* @returns
*/
async change_avatar(form_data) async change_avatar(form_data)
{ {
let response = await this.client._patch_file(`/api/profiles/me`, form_data); let response = await this.client._patch_file(`/api/profiles/me`, form_data);

View File

@ -13,6 +13,11 @@ class Account
this.client = client; this.client = client;
} }
/**
* @param {String} username
* @param {String} password
* @returns
*/
async create(username, password) async create(username, password)
{ {
let response = await this.client._post("/api/accounts/register", {username: username, password: password}); let response = await this.client._post("/api/accounts/register", {username: username, password: password});
@ -26,6 +31,10 @@ class Account
return response_data return response_data
} }
/**
* @param {String} password
* @returns
*/
async delete(password) async delete(password)
{ {
let response = await this.client._delete("/api/accounts/delete", {password: password}); let response = await this.client._delete("/api/accounts/delete", {password: password});
@ -41,6 +50,10 @@ class Account
return response_data; return response_data;
} }
/**
* Get account data (username)
* @returns
*/
async get() async get()
{ {
let response = await this.client._get("/api/accounts/edit"); let response = await this.client._get("/api/accounts/edit");
@ -54,6 +67,12 @@ class Account
return response_data; return response_data;
} }
/**
*
* @param {*} data
* @param {Number} password
* @returns
*/
async update(data, password) async update(data, password)
{ {
data.current_password = password; data.current_password = password;
@ -65,6 +84,7 @@ class Account
this.client._update_logged(false); this.client._update_logged(false);
return null; return null;
} }
return response_data; return response_data;
} }
} }

View File

@ -6,6 +6,7 @@ import { MyProfile } from "./MyProfile.js";
import { navigateTo } from "../index.js" import { navigateTo } from "../index.js"
import { Tourmanents } from "./tournament/tournaments.js"; import { Tourmanents } from "./tournament/tournaments.js";
import {Notice} from "./chat/notice.js" import {Notice} from "./chat/notice.js"
import { Channel } from "./chat/channel.js";
function getCookie(name) function getCookie(name)
{ {
@ -19,21 +20,62 @@ function getCookie(name)
class Client class Client
{ {
/**
*
* @param {String} url
*/
constructor(url) constructor(url)
{ {
/**
* @type {String}
*/
this._url = url; this._url = url;
/**
* @type {Account}
*/
this.account = new Account(this); this.account = new Account(this);
/**
* @type {Profiles}
*/
this.profiles = new Profiles(this); this.profiles = new Profiles(this);
/**
* @type {MatchMaking}
*/
this.matchmaking = new MatchMaking(this); this.matchmaking = new MatchMaking(this);
/**
* @type {Tourmanents}
*/
this.tournaments = new Tourmanents(this); 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; this._logged = undefined;
/**
* @type {Channels}
*/
this.channels = new Channels(this); this.channels = new Channels(this);
/**
* @type {Channel}
*/
this.channel = undefined; this.channel = undefined;
/**
* @type {Notice}
*/
this.notice = new Notice(this); this.notice = new Notice(this);
} }
/**
* The only right way to determine is the user is logged
* @returns {Boolean}
*/
async isAuthentificate() async isAuthentificate()
{ {
if (this._logged == undefined) if (this._logged == undefined)
@ -41,6 +83,12 @@ class Client
return this._logged; return this._logged;
} }
/**
* Send a GET request to %uri%
* @param {String} uri
* @param {*} data
* @returns {Response}
*/
async _get(uri, data) async _get(uri, data)
{ {
let response = await fetch(this._url + uri, { let response = await fetch(this._url + uri, {
@ -50,6 +98,12 @@ class Client
return response; return response;
} }
/**
* Send a POST request
* @param {String} uri
* @param {*} data
* @returns {Response}
*/
async _post(uri, data) async _post(uri, data)
{ {
let response = await fetch(this._url + uri, { let response = await fetch(this._url + uri, {
@ -63,6 +117,12 @@ class Client
return response; return response;
} }
/**
* Send a DELETE request
* @param {String} uri
* @param {String} data
* @returns {Response}
*/
async _delete(uri, data) async _delete(uri, data)
{ {
let response = await fetch(this._url + uri, { let response = await fetch(this._url + uri, {
@ -76,6 +136,12 @@ class Client
return response; return response;
} }
/**
* Send a PATCH request with json
* @param {String} uri
* @param {*} data
* @returns {Response}
*/
async _patch_json(uri, data) async _patch_json(uri, data)
{ {
let response = await fetch(this._url + uri, { let response = await fetch(this._url + uri, {
@ -89,6 +155,12 @@ class Client
return response; return response;
} }
/**
* Send a PATCH request with file
* @param {String} uri
* @param {*} file
* @returns {Response}
*/
async _patch_file(uri, file) async _patch_file(uri, file)
{ {
let response = await fetch(this._url + uri, { let response = await fetch(this._url + uri, {
@ -101,6 +173,11 @@ class Client
return response; return response;
} }
/**
* Change logged state. Use It if you recv an 403 error
* @param {Boolean} state
* @returns
*/
async _update_logged(state) async _update_logged(state)
{ {
if (this._logged == state) if (this._logged == state)
@ -126,6 +203,12 @@ class Client
this._logged = state; this._logged = state;
} }
/**
* Loggin the user
* @param {String} username
* @param {String} password
* @returns
*/
async login(username, password) async login(username, password)
{ {
let response = await this._post("/api/accounts/login", {username: username, password: password}) let response = await this._post("/api/accounts/login", {username: username, password: password})
@ -135,12 +218,19 @@ class Client
return response; return response;
} }
/**
* Logout the user
*/
async logout() async logout()
{ {
await this._get("/api/accounts/logout"); await this._get("/api/accounts/logout");
await this._update_logged(false); await this._update_logged(false);
} }
/**
* Determine if the user is logged. NEVER USE IT, USE isAuthentificated()
* @returns {Boolean}
*/
async _test_logged() async _test_logged()
{ {
let response = await this._get("/api/accounts/logged"); let response = await this._get("/api/accounts/logged");

View File

@ -6,7 +6,6 @@ import { Segment } from "./Segment.js";
class MyPlayer extends Player class MyPlayer extends Player
{ {
/** /**
*
* @param {Client} client * @param {Client} client
* @param {Game} game * @param {Game} game
* @param {Segment} rail * @param {Segment} rail
@ -23,9 +22,7 @@ class MyPlayer extends Player
} }
/** /**
*
* @param {[string]} keys_pressed * @param {[string]} keys_pressed
* @returns
*/ */
update_paddle(keys_pressed) update_paddle(keys_pressed)
{ {
@ -47,7 +44,6 @@ class MyPlayer extends Player
} }
/** /**
*
* @param {Number} new_position * @param {Number} new_position
* @param {Number} time * @param {Number} time
*/ */

View File

@ -5,7 +5,6 @@ import { Segment } from "./Segment.js";
class Player class Player
{ {
/** /**
*
* @param {Number} id * @param {Number} id
* @param {Game} game * @param {Game} game
* @param {Segment} rail * @param {Segment} rail

View File

@ -14,6 +14,13 @@ class MatchMaking
this.searching = false; 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) async start(receive_func, disconnect_func, mode)
{ {
if (!await this.client.isAuthentificate()) if (!await this.client.isAuthentificate())
@ -36,6 +43,7 @@ class MatchMaking
this._socket.onclose = this.onclose.bind(this); this._socket.onclose = this.onclose.bind(this);
} }
onclose(event) onclose(event)
{ {
this.stop(); this.stop();
@ -44,8 +52,10 @@ class MatchMaking
async stop() async stop()
{ {
this.searching = false; if (this._socket)
this._socket.close() this._socket.close()
this._socket = undefined
this.searching = false;
} }
} }

View File

@ -11,9 +11,25 @@ class Profile
* @type {Client} client * @type {Client} client
*/ */
this.client = client; this.client = client;
/**
* @type {String}
*/
this.username = username; this.username = username;
/**
* @type {Number}
*/
this.id = id; this.id = id;
/**
* @type {String}
*/
this.avatar_url = avatar_url; this.avatar_url = avatar_url;
/**
* @type {Boolean}
*/
this.isBlocked = false; this.isBlocked = false;
} }

View File

@ -13,6 +13,10 @@ class Profiles
this.client = client this.client = client
} }
/**
*
* @returns {[Profile]}
*/
async all() async all()
{ {
let response = await this.client._get("/api/profiles/"); let response = await this.client._get("/api/profiles/");
@ -25,6 +29,11 @@ class Profiles
return profiles; return profiles;
} }
/**
*
* @param {String} username
* @returns {?Profile}
*/
async getProfile(username) async getProfile(username)
{ {
let profile = new Profile(this.client, username); let profile = new Profile(this.client, username);
@ -33,6 +42,11 @@ class Profiles
return profile; return profile;
} }
/**
* Block a user
* @param {Number} user_id
* @returns
*/
async block(user_id) { async block(user_id) {
// blocker & blocked // blocker & blocked
@ -45,6 +59,11 @@ class Profiles
} }
/**
* Unblock a user
* @param {Number} user_id
* @returns
*/
async deblock(user_id) { async deblock(user_id) {
// blocker & blocked // blocker & blocked

View File

@ -3,7 +3,17 @@ import { Client } from "../client.js";
class Tourmanent 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) 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} * @type {Client}
*/ */
this.client = client; this.client = client;
/**
* @type {String} the name of the tournament
*/
this.name = name || `${nb_players_by_game}x1, ${nb_players} players`; this.name = name || `${nb_players_by_game}x1, ${nb_players} players`;
/**
* @type {Number}
*/
this.nb_players = nb_players; this.nb_players = nb_players;
/**
* @type {Number}
*/
this.nb_players_by_game = nb_players_by_game; this.nb_players_by_game = nb_players_by_game;
/**
* @type {Number}
*/
this.level = level; this.level = level;
/**
* @type {Number}
*/
this.started = started; this.started = started;
/**
* @type {Number}
*/
this.finished = finished; this.finished = finished;
/**
* @type {[]}
*/
this.levels = levels; this.levels = levels;
/**
* @type {String} must be "finished", or "started", or "waiting". Any other return all elements
*/
this.state = state; this.state = state;
/**
* @type {Number}
*/
this.id = id this.id = id
/**
* @type {Boolean} if a websocket connection is enabled
*/
this.connected = false; this.connected = false;
} }

View File

@ -14,6 +14,11 @@ class Tourmanents
this.client = client this.client = client
} }
/**
*
* @param {Number} id
* @returns
*/
async getTournament(id) async getTournament(id)
{ {
let tournament = new Tourmanent(this.client); let tournament = new Tourmanent(this.client);
@ -22,6 +27,13 @@ class Tourmanents
return tournament; 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 = "") 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}); 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) async search(state)
{ {
let response = await this.client._get(`/api/tournaments/search/${state}`); let response = await this.client._get(`/api/tournaments/search/${state}`);
let response_data = await response.json() let response_data = await response.json()
if (response.status === 404) if (response.status === 403)
{ {
this.client._update_logged(false); this.client._update_logged(false);
return null; return null;