Make docstrings better. (resolves #77)
This commit is contained in:
parent
a3b3cfa71f
commit
3b797ca9d8
4 changed files with 82 additions and 75 deletions
|
|
@ -14,14 +14,14 @@ export class Deck<T> {
|
||||||
get size(): number { return this._deck.length; }
|
get size(): number { return this._deck.length; }
|
||||||
|
|
||||||
|
|
||||||
public draw(quantity: number): T[] {
|
|
||||||
/**
|
/**
|
||||||
* Draws X cards from the deck
|
* Draws X cards from the deck
|
||||||
*
|
*
|
||||||
* @param quantity -> The number of cards to draw
|
* @param quantity The number of cards to draw
|
||||||
* @throws Error -> If quantity is <= 0
|
* @throws Error If quantity is <= 0
|
||||||
* @throws Error -> If quantity > size
|
* @throws Error If quantity > size
|
||||||
*/
|
*/
|
||||||
|
public draw(quantity: number): T[] {
|
||||||
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> {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public discard(card: T) {
|
|
||||||
/**
|
/**
|
||||||
* Adds the specific card to the discard pile
|
* Adds the specific card to the discard pile
|
||||||
*
|
*
|
||||||
* @param card -> The card to add to the discard pile
|
* @param card The card to add to the discard pile
|
||||||
*/
|
*/
|
||||||
|
public discard(card: T) {
|
||||||
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> {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public toJSON(): datastoreDeck<T> {
|
|
||||||
/**
|
/**
|
||||||
* Converts this Deck into a JSON-compatible object
|
* Converts this Deck into a JSON-compatible object
|
||||||
*/
|
*/
|
||||||
|
public toJSON(): datastoreDeck<T> {
|
||||||
return {
|
return {
|
||||||
deck: this._deck,
|
deck: this._deck,
|
||||||
unknown: this._unknown,
|
unknown: this._unknown,
|
||||||
|
|
@ -76,10 +76,10 @@ export class Deck<T> {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
public static fromJSON<A>(data: datastoreDeck<A>): Deck<A> {
|
|
||||||
/**
|
/**
|
||||||
* Converts the JSON representation of a deck into a Deck
|
* Converts the JSON representation of a deck into a Deck
|
||||||
*/
|
*/
|
||||||
|
public static fromJSON<A>(data: datastoreDeck<A>): Deck<A> {
|
||||||
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;
|
||||||
|
|
|
||||||
|
|
@ -46,10 +46,10 @@ export class Game {
|
||||||
|
|
||||||
get questions() { return this._questions; };
|
get questions() { return this._questions; };
|
||||||
|
|
||||||
get objects() {
|
|
||||||
/**
|
/**
|
||||||
* Return the objects that the spirits can choose from for the game.
|
* Return the objects that the spirits can choose from for the game.
|
||||||
*/
|
*/
|
||||||
|
get objects() {
|
||||||
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 {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
private parseDeckCSV() {
|
|
||||||
/**
|
/**
|
||||||
* Parses out the CSV files and creates the decks for the game to run
|
* Parses out the CSV files and creates the decks for the game to run
|
||||||
* on.
|
* on.
|
||||||
*/
|
*/
|
||||||
|
private parseDeckCSV() {
|
||||||
|
|
||||||
// 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 {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
private parseDeckGoogleSheets() {
|
|
||||||
/**
|
/**
|
||||||
* Fetches and parses the CSV data from Google Sheets instead of local
|
* Fetches and parses the CSV data from Google Sheets instead of local
|
||||||
* CSV files.
|
* CSV files.
|
||||||
*/
|
*/
|
||||||
|
private parseDeckGoogleSheets() {
|
||||||
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 {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public resetObject() {
|
|
||||||
/**
|
/**
|
||||||
* Resets the objects card, for restarting the game
|
* Resets the objects card, for restarting the game
|
||||||
*/
|
*/
|
||||||
|
public resetObject() {
|
||||||
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 {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public toJSON(): datastoreGame {
|
|
||||||
/**
|
/**
|
||||||
* Returns a JSON representation of the game.
|
* Returns a JSON representation of the game.
|
||||||
*/
|
*/
|
||||||
|
public toJSON(): datastoreGame {
|
||||||
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 {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
public static fromJSON(host: Player, data: datastoreGame): Game {
|
|
||||||
/**
|
/**
|
||||||
* Converts a JSON representation into a Game object
|
* Converts a JSON representation into a Game object
|
||||||
*/
|
*/
|
||||||
|
public static fromJSON(host: Player, data: datastoreGame): Game {
|
||||||
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 {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public static generateID(length: number): string {
|
|
||||||
/**
|
/**
|
||||||
* Generates a game code with the given length
|
* Generates a game code with the given length
|
||||||
*
|
*
|
||||||
* @param length -> The length of the code we want to generate
|
* @param length The length of the code we want to generate
|
||||||
*/
|
*/
|
||||||
|
public static generateID(length: number): string {
|
||||||
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
|
||||||
|
|
|
||||||
|
|
@ -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;
|
||||||
|
|
|
||||||
|
|
@ -25,55 +25,54 @@ export class Team {
|
||||||
get questions(): string[] { return this._questions; };
|
get questions(): string[] { return this._questions; };
|
||||||
|
|
||||||
|
|
||||||
public addCardsToHand(questions: string[]): void {
|
|
||||||
/**
|
/**
|
||||||
* Adds the question(s) to the medium's hand
|
* Adds the question(s) to the medium's hand
|
||||||
*
|
*
|
||||||
* @param questions -> The array of question text to add the medium's
|
* @param questions The array of question text to add the medium's hand.
|
||||||
* hand.
|
|
||||||
*/
|
*/
|
||||||
|
public addCardsToHand(questions: string[]): void {
|
||||||
this._hand.push(...questions);
|
this._hand.push(...questions);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public reset(): void {
|
|
||||||
/**
|
/**
|
||||||
* Resets all the per-game data related to this team
|
* Resets all the per-game data related to this team
|
||||||
*/
|
*/
|
||||||
|
public reset(): void {
|
||||||
this._hand = [];
|
this._hand = [];
|
||||||
this._questions = [];
|
this._questions = [];
|
||||||
this._answers = new Array<string>(8).fill(``);
|
this._answers = new Array<string>(8).fill(``);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public removeCard(question: string) {
|
|
||||||
/**
|
/**
|
||||||
* Removes the given question from the medium's hand
|
* Removes the given question from the medium's hand
|
||||||
*
|
*
|
||||||
* @param question -> The card text to remove from the hand.
|
* @param question The card text to remove from the hand.
|
||||||
*/
|
*/
|
||||||
|
public removeCard(question: string) {
|
||||||
this._hand = this._hand.filter(x => x != question);
|
this._hand = this._hand.filter(x => x != question);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public selectQuestion(question: string) {
|
|
||||||
/**
|
/**
|
||||||
* Adds the given question to the history of the questions.
|
* Adds the given question to the history of the questions.
|
||||||
*
|
*
|
||||||
* @param question -> The question the spirit is answering
|
* @param question The question the spirit is answering
|
||||||
*/
|
*/
|
||||||
|
public selectQuestion(question: string) {
|
||||||
this._questions.push(question);
|
this._questions.push(question);
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public modifyAnswer(answerIndex: answer, answer: string) {
|
|
||||||
/**
|
/**
|
||||||
* Takes the value of an answer and modifies in the storage.
|
* Takes the value of an answer and modifies in the storage.
|
||||||
*
|
*
|
||||||
* @param answerIndex -> The value of the answer between 1 and 8 (inclusive)
|
* @param answerIndex The value of the answer between 1 and 8 (inclusive)
|
||||||
* @param answer -> The new answer for that index
|
* @param answer The new answer for that index
|
||||||
* @throws Error -> If the answerIndex is not in range
|
* @throws Error If the answerIndex is not in range
|
||||||
*/
|
*/
|
||||||
|
public modifyAnswer(answerIndex: answer, answer: string) {
|
||||||
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 {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
public toJSON(): datastoreTeam {
|
|
||||||
/**
|
/**
|
||||||
* Converts the given object into a JSON representation of the data
|
* Converts the given object into a JSON representation of the data
|
||||||
*/
|
*/
|
||||||
|
public toJSON(): datastoreTeam {
|
||||||
return {
|
return {
|
||||||
questions: this._questions,
|
questions: this._questions,
|
||||||
answers: this._answers,
|
answers: this._answers,
|
||||||
|
|
@ -93,10 +92,10 @@ export class Team {
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
public static fromJSON(data: datastoreTeam): Team {
|
|
||||||
/**
|
/**
|
||||||
* Converts a team JSON object back into a Team object.
|
* Converts a team JSON object back into a Team object.
|
||||||
*/
|
*/
|
||||||
|
public static fromJSON(data: datastoreTeam): Team {
|
||||||
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;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue