Skip to content

Commit

Permalink
add type information
Browse files Browse the repository at this point in the history
get the typescript compiler to mostly pass with noImplicitAny
  • Loading branch information
darthwalsh committed Jul 2, 2019
1 parent 337d761 commit f3ac293
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 20 deletions.
3 changes: 3 additions & 0 deletions cli/lib.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,9 @@ class lib {
});
}

/**
* @param {string} name
*/
async connect(name) {
if (this.open) {
throw new Error("Already open");
Expand Down
4 changes: 2 additions & 2 deletions server/cards.js
Original file line number Diff line number Diff line change
Expand Up @@ -921,7 +921,7 @@ function compareTo(other) {
* @type {Object.<string, Card>}
*/
const cards = Object.keys(kingdom).reduce((o, name) => {
const k = kingdom[name];
const k = /** @type {any} */ (kingdom[name]);
const card = typeof k === "function" ? {play: k} : k;

card.name = name;
Expand All @@ -939,7 +939,7 @@ const cards = Object.keys(kingdom).reduce((o, name) => {
card.text = tableRow.text;
card.set = tableRow.set;

card.ofKind = k => card.kind.includes(k);
card.ofKind = /** @param {string} k */ k => card.kind.includes(k);
card.color = colorMap[card.kind[0]];

o[name] = card;
Expand Down
10 changes: 6 additions & 4 deletions server/connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ class Connection {
this.sentChoices = null;

this.messageHandlers = {
choice: data => this.onChoice(data),
name: data => this.name = data,
chat: data => {},
choice: /** @param {string} data */ data => this.onChoice(data),
name: /** @param {string} data */ data => {
this.name = data;
},
chat: /** @param {string} data */ data => {},
};

this.initListeners();
Expand Down Expand Up @@ -86,7 +88,7 @@ class Connection {

this.sentChoices = JSON.stringify({choices});

this.onChoice = choice => {
this.onChoice = /** @param {string} choice */ choice => {
if (!choices.includes(choice)) {
this.send({message: `"${choice}" not a valid choice of "${choices}"!!!`});
this.resendChoices();
Expand Down
21 changes: 12 additions & 9 deletions server/game.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Game {
* @type {Object.<string, Player>}
*/
this.players = {};
this.trash = [];
this.trash = /** @type {Card[]} */ ([]);
this.started = false;
this.shuffle = shuffle || this.fisherYatesShuffle;

Expand Down Expand Up @@ -109,15 +109,13 @@ class Game {
*/
getEndGame(cards) {
cards = cards.slice().sort((a, b) => a.compareTo(b));
const isPoint = c => c.ofKind("victory") || c.ofKind("curse");
const isPoint = /** @param {Card} c */ c => c.ofKind("victory") || c.ofKind("curse");
const byVictory = [...cards.filter(isPoint), ...cards.filter(c => !isPoint(c))];

const map = new Map();
byVictory.forEach(c => map.set(c.name, 1 + (map.get(c.name) || 0)));

const text = [];
map.forEach((count, name) => text.push(`${name} (${count})`));
return text.join(", ");
return Array.from(map, ([name, count]) => `${name} (${count})`).join(", ");
}

/**
Expand Down Expand Up @@ -148,10 +146,12 @@ class Game {
this.allLog(player.name + ": " + data);
};

connection.messageHandlers.gameStart = data => {
this.store.init(data.included.map(n => cards[n]), this.allPlayers().length);
this.start(data.debugMode);
};
connection.messageHandlers["gameStart"] =
/** @param {{included: string[], debugMode: boolean}} data */
data => {
this.store.init(data.included.map(n => cards[n]), this.allPlayers().length);
this.start(data.debugMode);
};

connection.ws.addEventListener("close", () => {
this.allLog(player.name + " disconnected");
Expand Down Expand Up @@ -200,6 +200,9 @@ class Game {
}
}

/**
* @param {string} text
*/
allLog(text) {
for (const id in this.players) {
this.players[id].sendMessage(text);
Expand Down
20 changes: 19 additions & 1 deletion server/player.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ class Player {
this.sendHand();
}

/**
* @param {string} name
*/
fromHand(name) {
const hi = this.hand.map(c => c.name).indexOf(name);
if (hi === -1) {
Expand Down Expand Up @@ -239,6 +242,9 @@ class Player {
this.discardPile.push(...discard);
}

/**
* @param {string} name
*/
async playCard(name) {
const card = this.fromHand(name);
if (card === null) {
Expand Down Expand Up @@ -272,6 +278,9 @@ class Player {
this.redrawHand();
}

/**
* @param {string} [prefix]
*/
draw(n = 1, prefix) {
for (let i = 0; i < n; ++i) {
const card = this.fromDraw();
Expand All @@ -294,7 +303,7 @@ class Player {
this.game.shuffle(this.discardPile);

this.drawPile = this.discardPile;
this.discardPile = [];
this.discardPile = /** @type {Card[]} */ ([]);
}

/**
Expand All @@ -311,6 +320,9 @@ class Player {
return attack(this);
}

/**
* @return {number}
*/
getPoints() {
return this.victory + this.allCards().reduce((a, c) => a + (c.getPoints ? c.getPoints(this) : 0), 0);
}
Expand All @@ -323,10 +335,16 @@ class Player {
return all;
}

/**
* @param {object} o
*/
send(o) {
this.connection.send(o);
}

/**
* @param {string} msg
*/
sendMessage(msg) {
this.connection.send({message: msg});
}
Expand Down
8 changes: 4 additions & 4 deletions server/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ class Store {
const extraSize = playerCount > 4;

const kinds = {
victory: c => {
victory: /** @param {Card} c */ c => {
let count = playerCount <= 2 ? 8 : 12;
if (c.name === "Province" && extraSize) {
count += (playerCount - 4) * 3;
}
return count;
},
curse: _ => 10 * Math.max(playerCount - 1, 1),
treasure: c => {
curse: () => 10 * Math.max(playerCount - 1, 1),
treasure: /** @param {Card} c */ c => {
switch (c.name) {
case "Copper": return (extraSize ? 120 : 60) - 7 * playerCount;
case "Silver": return extraSize ? 80 : 40;
Expand All @@ -47,7 +47,7 @@ class Store {
default: return 10;
}
},
action: _ => 10,
action: () => 10,
};

/** @type {Object<string, number>} */
Expand Down

0 comments on commit f3ac293

Please sign in to comment.