0
0
Fork 0

Make docstrings better. (resolves #77)

This commit is contained in:
Oliver-Akins 2021-02-28 18:23:26 -07:00
parent a3b3cfa71f
commit 3b797ca9d8
4 changed files with 82 additions and 75 deletions

View file

@ -14,14 +14,14 @@ export class Deck<T> {
get size(): number { return this._deck.length; } 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[] { 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) { if (quantity <= 0) {
throw new Error(`Cannot get ${quantity} cards.`); throw new Error(`Cannot get ${quantity} cards.`);
} else if (quantity > this.size) { } else if (quantity > this.size) {
@ -47,12 +47,12 @@ export class Deck<T> {
}; };
/**
* Adds the specific card to the discard pile
*
* @param card The card to add to the discard pile
*/
public discard(card: T) { 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._unknown = this._unknown.filter(x => x != card);
this._discard.push(card); this._discard.push(card);
}; };
@ -65,10 +65,10 @@ export class Deck<T> {
}; };
/**
* Converts this Deck into a JSON-compatible object
*/
public toJSON(): datastoreDeck<T> { public toJSON(): datastoreDeck<T> {
/**
* Converts this Deck into a JSON-compatible object
*/
return { return {
deck: this._deck, deck: this._deck,
unknown: this._unknown, unknown: this._unknown,
@ -76,10 +76,10 @@ export class Deck<T> {
}; };
}; };
/**
* Converts the JSON representation of a deck into a Deck
*/
public static fromJSON<A>(data: datastoreDeck<A>): Deck<A> { public static fromJSON<A>(data: datastoreDeck<A>): Deck<A> {
/**
* Converts the JSON representation of a deck into a Deck
*/
let d = new Deck(data.deck); let d = new Deck(data.deck);
d._discard = data.discard; d._discard = data.discard;
d._unknown = data.unknown; d._unknown = data.unknown;

View file

@ -46,10 +46,10 @@ export class Game {
get questions() { return this._questions; }; get questions() { return this._questions; };
/**
* Return the objects that the spirits can choose from for the game.
*/
get objects() { get objects() {
/**
* Return the objects that the spirits can choose from for the game.
*/
if (!this._objectCard) { if (!this._objectCard) {
this._objectCard = this._objects.draw(1)[0]; 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() { private parseDeckCSV() {
/**
* Parses out the CSV files and creates the decks for the game to run
* on.
*/
// parse the questions from the CSV // parse the questions from the CSV
readFile(conf.game.cards.questions.fingerprint, `utf-8`, (err, filebuffer) => { 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() { 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 key = conf.game.cards.key as string;
let questions_id = conf.game.cards.questions.fingerprint; let questions_id = conf.game.cards.questions.fingerprint;
let objects_id = conf.game.cards.objects.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() { public resetObject() {
/**
* Resets the objects card, for restarting the game
*/
if (this._objectCard) { if (this._objectCard) {
this._objects.discard(this._objectCard); this._objects.discard(this._objectCard);
this._objectCard = null; this._objectCard = null;
@ -172,10 +172,10 @@ export class Game {
}; };
/**
* Returns a JSON representation of the game.
*/
public toJSON(): datastoreGame { public toJSON(): datastoreGame {
/**
* Returns a JSON representation of the game.
*/
return { return {
players: this.players.map(p => p.toJSON()), players: this.players.map(p => p.toJSON()),
teams: this.teams.map(t => t.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 { public static fromJSON(host: Player, data: datastoreGame): Game {
/**
* Converts a JSON representation into a Game object
*/
let game = new this(host, { id: data.id }); let game = new this(host, { id: data.id });
// Re-create the deck objects // 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 { 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; let code: string;
// Generate a code until we don't have a collision // Generate a code until we don't have a collision

View file

@ -13,6 +13,9 @@ export class Player {
this.isHost = isHost; this.isHost = isHost;
}; };
/**
* Converts the Player into a JSON-compatible representation of the player
*/
public toJSON(): datastorePlayer { public toJSON(): datastorePlayer {
return { return {
name: this.name, 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 { public static fromJSON(data: datastorePlayer): Player {
let player = new this(data.name, null, data.host); let player = new this(data.name, null, data.host);
player.role = data.role; player.role = data.role;

View file

@ -25,55 +25,54 @@ export class Team {
get questions(): string[] { return this._questions; }; 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 { 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); this._hand.push(...questions);
}; };
/**
* Resets all the per-game data related to this team
*/
public reset(): void { public reset(): void {
/**
* Resets all the per-game data related to this team
*/
this._hand = []; this._hand = [];
this._questions = []; this._questions = [];
this._answers = new Array<string>(8).fill(``); this._answers = new Array<string>(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) { 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); 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) { 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); 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) { 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) { if (answerIndex > this._answers.length || answerIndex <= 0) {
throw new Error(`Cannot set answer at index ${answerIndex}.`) 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 { public toJSON(): datastoreTeam {
/**
* Converts the given object into a JSON representation of the data
*/
return { return {
questions: this._questions, questions: this._questions,
answers: this._answers, 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 { public static fromJSON(data: datastoreTeam): Team {
/**
* Converts a team JSON object back into a Team object.
*/
let t = new Team(data.id); let t = new Team(data.id);
t._questions = data.questions; t._questions = data.questions;
t._answers = data.answers; t._answers = data.answers;