From 3b797ca9d870702c5f588361e57c73601e2e0329 Mon Sep 17 00:00:00 2001 From: Oliver-Akins Date: Sun, 28 Feb 2021 18:23:26 -0700 Subject: [PATCH] Make docstrings better. (resolves #77) --- server/src/objects/Deck.ts | 36 ++++++++++----------- server/src/objects/Game.ts | 50 ++++++++++++++-------------- server/src/objects/Player.ts | 8 +++++ server/src/objects/Team.ts | 63 ++++++++++++++++++------------------ 4 files changed, 82 insertions(+), 75 deletions(-) diff --git a/server/src/objects/Deck.ts b/server/src/objects/Deck.ts index a65e249..cecb6dc 100644 --- a/server/src/objects/Deck.ts +++ b/server/src/objects/Deck.ts @@ -14,14 +14,14 @@ export class Deck { get size(): number { return this._deck.length; } + /** + * Draws X cards from the deck + * + * @param quantity The number of cards to draw + * @throws Error If quantity is <= 0 + * @throws Error If quantity > size + */ public draw(quantity: number): T[] { - /** - * Draws X cards from the deck - * - * @param quantity -> The number of cards to draw - * @throws Error -> If quantity is <= 0 - * @throws Error -> If quantity > size - */ if (quantity <= 0) { throw new Error(`Cannot get ${quantity} cards.`); } else if (quantity > this.size) { @@ -47,12 +47,12 @@ export class Deck { }; + /** + * Adds the specific card to the discard pile + * + * @param card The card to add to the discard pile + */ public discard(card: T) { - /** - * Adds the specific card to the discard pile - * - * @param card -> The card to add to the discard pile - */ this._unknown = this._unknown.filter(x => x != card); this._discard.push(card); }; @@ -65,10 +65,10 @@ export class Deck { }; + /** + * Converts this Deck into a JSON-compatible object + */ public toJSON(): datastoreDeck { - /** - * Converts this Deck into a JSON-compatible object - */ return { deck: this._deck, unknown: this._unknown, @@ -76,10 +76,10 @@ export class Deck { }; }; + /** + * Converts the JSON representation of a deck into a Deck + */ public static fromJSON(data: datastoreDeck): Deck { - /** - * Converts the JSON representation of a deck into a Deck - */ let d = new Deck(data.deck); d._discard = data.discard; d._unknown = data.unknown; diff --git a/server/src/objects/Game.ts b/server/src/objects/Game.ts index f02db0e..0c4953a 100644 --- a/server/src/objects/Game.ts +++ b/server/src/objects/Game.ts @@ -46,10 +46,10 @@ export class Game { get questions() { return this._questions; }; + /** + * Return the objects that the spirits can choose from for the game. + */ get objects() { - /** - * Return the objects that the spirits can choose from for the game. - */ if (!this._objectCard) { this._objectCard = this._objects.draw(1)[0]; }; @@ -69,11 +69,11 @@ export class Game { } + /** + * Parses out the CSV files and creates the decks for the game to run + * on. + */ private parseDeckCSV() { - /** - * Parses out the CSV files and creates the decks for the game to run - * on. - */ // parse the questions from the CSV readFile(conf.game.cards.questions.fingerprint, `utf-8`, (err, filebuffer) => { @@ -102,11 +102,11 @@ export class Game { }); }; + /** + * Fetches and parses the CSV data from Google Sheets instead of local + * CSV files. + */ private parseDeckGoogleSheets() { - /** - * Fetches and parses the CSV data from Google Sheets instead of local - * CSV files. - */ let key = conf.game.cards.key as string; let questions_id = conf.game.cards.questions.fingerprint; let objects_id = conf.game.cards.objects.fingerprint; @@ -160,10 +160,10 @@ export class Game { }; + /** + * Resets the objects card, for restarting the game + */ public resetObject() { - /** - * Resets the objects card, for restarting the game - */ if (this._objectCard) { this._objects.discard(this._objectCard); this._objectCard = null; @@ -172,10 +172,10 @@ export class Game { }; + /** + * Returns a JSON representation of the game. + */ public toJSON(): datastoreGame { - /** - * Returns a JSON representation of the game. - */ return { players: this.players.map(p => p.toJSON()), teams: this.teams.map(t => t.toJSON()), @@ -190,10 +190,10 @@ export class Game { }; }; + /** + * Converts a JSON representation into a Game object + */ public static fromJSON(host: Player, data: datastoreGame): Game { - /** - * Converts a JSON representation into a Game object - */ let game = new this(host, { id: data.id }); // Re-create the deck objects @@ -217,12 +217,12 @@ export class Game { }; + /** + * Generates a game code with the given length + * + * @param length The length of the code we want to generate + */ public static generateID(length: number): string { - /** - * Generates a game code with the given length - * - * @param length -> The length of the code we want to generate - */ let code: string; // Generate a code until we don't have a collision diff --git a/server/src/objects/Player.ts b/server/src/objects/Player.ts index 2d279ba..0d9c31f 100644 --- a/server/src/objects/Player.ts +++ b/server/src/objects/Player.ts @@ -13,6 +13,9 @@ export class Player { this.isHost = isHost; }; + /** + * Converts the Player into a JSON-compatible representation of the player + */ public toJSON(): datastorePlayer { return { name: this.name, @@ -22,6 +25,11 @@ export class Player { }; }; + /** + * Converts JSON-compatible player data into a Player object. + * + * @param data The player data to convert + */ public static fromJSON(data: datastorePlayer): Player { let player = new this(data.name, null, data.host); player.role = data.role; diff --git a/server/src/objects/Team.ts b/server/src/objects/Team.ts index da5f1f3..5254361 100644 --- a/server/src/objects/Team.ts +++ b/server/src/objects/Team.ts @@ -25,55 +25,54 @@ export class Team { get questions(): string[] { return this._questions; }; + /** + * Adds the question(s) to the medium's hand + * + * @param questions The array of question text to add the medium's hand. + */ public addCardsToHand(questions: string[]): void { - /** - * Adds the question(s) to the medium's hand - * - * @param questions -> The array of question text to add the medium's - * hand. - */ this._hand.push(...questions); }; + /** + * Resets all the per-game data related to this team + */ public reset(): void { - /** - * Resets all the per-game data related to this team - */ this._hand = []; this._questions = []; this._answers = new Array(8).fill(``); } + /** + * Removes the given question from the medium's hand + * + * @param question The card text to remove from the hand. + */ public removeCard(question: string) { - /** - * Removes the given question from the medium's hand - * - * @param question -> The card text to remove from the hand. - */ this._hand = this._hand.filter(x => x != question); }; + /** + * Adds the given question to the history of the questions. + * + * @param question The question the spirit is answering + */ public selectQuestion(question: string) { - /** - * Adds the given question to the history of the questions. - * - * @param question -> The question the spirit is answering - */ this._questions.push(question); }; + /** + * Takes the value of an answer and modifies in the storage. + * + * @param answerIndex The value of the answer between 1 and 8 (inclusive) + * @param answer The new answer for that index + * @throws Error If the answerIndex is not in range + */ public modifyAnswer(answerIndex: answer, answer: string) { - /** - * Takes the value of an answer and modifies in the storage. - * - * @param answerIndex -> The value of the answer between 1 and 8 (inclusive) - * @param answer -> The new answer for that index - * @throws Error -> If the answerIndex is not in range - */ if (answerIndex > this._answers.length || answerIndex <= 0) { throw new Error(`Cannot set answer at index ${answerIndex}.`) }; @@ -81,10 +80,10 @@ export class Team { }; + /** + * Converts the given object into a JSON representation of the data + */ public toJSON(): datastoreTeam { - /** - * Converts the given object into a JSON representation of the data - */ return { questions: this._questions, answers: this._answers, @@ -93,10 +92,10 @@ export class Team { }; }; + /** + * Converts a team JSON object back into a Team object. + */ public static fromJSON(data: datastoreTeam): Team { - /** - * Converts a team JSON object back into a Team object. - */ let t = new Team(data.id); t._questions = data.questions; t._answers = data.answers;