const CLAIM_WARNING_MS = 15000; /* show timer for last 15 seconds */
const VOTE_TIMEOUT_MS = 15000; /* 15 seconds to vote */
const REVEAL_COUNTDOWN_MS = 1500; /* 1.5 seconds to reveal a tile */
+const MIN_CENTER_TILES = 7; /* auto-deal to maintain this many */
const MIN_WORD_LENGTH = 4;
class Anagrams extends Game {
* Letter management *
*****************************************************/
- /* Deal a letter from bag to center with a countdown reveal. */
- handle_deal_letter(request, response) {
- const session_id = request.session.id;
- if (!this.state.player_words[session_id]) {
- response.sendStatus(400);
- return;
- }
-
+ /* Deal a single letter from bag to center with a countdown reveal.
+ * Returns true if a letter was dealt, false otherwise. */
+ _deal_one() {
if (this.state.bag.length === 0 || this._revealing) {
- response.json({ ok: false });
- return;
+ return false;
}
const letter = this.state.bag.pop();
this._reveal_timer = setTimeout(() => {
this._revealing = false;
this._reveal_timer = null;
+ this._auto_deal();
}, REVEAL_COUNTDOWN_MS);
this.broadcast_event_object("letter-reveal", {
remaining: this.state.bag.length
});
- response.json({ ok: true });
+ return true;
+ }
+
+ /* Auto-deal if center has fewer than MIN_CENTER_TILES. */
+ _auto_deal() {
+ if (!this.state.started || this.state.finished) return;
+ if (this.state.center.length < MIN_CENTER_TILES) {
+ this._deal_one();
+ }
+ }
+
+ handle_deal_letter(request, response) {
+ const session_id = request.session.id;
+ if (!this.state.player_words[session_id]) {
+ response.sendStatus(400);
+ return;
+ }
+
+ response.json({ ok: this._deal_one() });
}
/*****************************************************
if (this.state.claim_queue.length > 0) {
this._activate_claimer();
}
+
+ /* Replenish center if needed. */
+ this._auto_deal();
}
_compute_score(session_id) {
}
this.broadcast_event_object("game-started", {});
+ this._auto_deal();
response.json({ ok: true });
}