From ec10f0d2ea6a9fa770f2145038cb65ac5b346d23 Mon Sep 17 00:00:00 2001 From: Leon Mika Date: Sat, 25 Jan 2025 11:52:07 +1100 Subject: [PATCH] Last bit of tuning for the rules --- site/assets/scripts/models/gamecontroller.js | 47 ++++++++++++++++---- site/assets/scripts/models/words.js | 32 +++++++------ 2 files changed, 53 insertions(+), 26 deletions(-) diff --git a/site/assets/scripts/models/gamecontroller.js b/site/assets/scripts/models/gamecontroller.js index 98fbb00..aa74f77 100644 --- a/site/assets/scripts/models/gamecontroller.js +++ b/site/assets/scripts/models/gamecontroller.js @@ -10,14 +10,34 @@ export const MARKERS = { RIGHT_CHAR: 'y' }; +class ProgressionState { + getProgression() { + let prog = localStorage.getItem('progression'); + if (prog) { + return JSON.parse(prog); + } + + prog = {wordLength: 4, wordIndex: {"4": 0, "5": 0, "6": 0}}; + localStorage.setItem('progression', JSON.stringify(prog)); + return prog; + } + + saveProgression(prog) { + localStorage.setItem('progression', JSON.stringify(prog)); + } +} + export class GameController { constructor(wordSource) { this._wordSource = wordSource; + this._progressionState = new ProgressionState(); } async start() { - this._currentWord = await this._wordSource.getCurrentWord(); - this._guesses = 5; + let prog = this._progressionState.getProgression(); + + this._currentWord = await this._wordSource.getCurrentWord(prog); + this._guesses = Math.max(this._currentWord.length, 5) + 1; console.log("The current word: " + this._currentWord); } @@ -38,8 +58,15 @@ export class GameController { } async nextWord() { - this._currentWord = await this._wordSource.getCurrentWord(); - this._guesses = 5; + // Increment the progress + let prog = this._progressionState.getProgression(); + prog.wordIndex[prog.wordLength + ""] += 1; + prog.wordLength = (((Math.random() * 23) | 0) / 10 | 0) + 4; + + this._progressionState.saveProgression(prog); + + this._currentWord = await this._wordSource.getCurrentWord(prog); + this._guesses = Math.max(this._currentWord.length, 5) + 1; return true; } @@ -57,11 +84,13 @@ export class GameController { let markers = new Array(guess.length); let misses = {}; let hits = {}; - - if (!this._wordSource.isWord(guess)) { - return { - guessResult: GUESS_RESULT.FOUL, - }; + + if (this._currentWord.length <= 5) { + if (!this._wordSource.isWord(guess)) { + return { + guessResult: GUESS_RESULT.FOUL, + }; + } } for (let i = 0; i < guess.length; i++) { diff --git a/site/assets/scripts/models/words.js b/site/assets/scripts/models/words.js index 2800e5c..bf3da4c 100644 --- a/site/assets/scripts/models/words.js +++ b/site/assets/scripts/models/words.js @@ -21,7 +21,8 @@ export class WordSource { constructor() { this._wordData = null; this._pattern = null; - this._currentWord = null; + // this._currentWord = null; + // this._progressionState= } isWord(word) { @@ -33,27 +34,24 @@ export class WordSource { return binSearch(list, word); } - async getCurrentWord() { - if (this._currentWord) { - return this._currentWord; - } - + async getCurrentWord(prog) { let words = await this._fetchAllWordsIfNecessary(); - let idx = this._pattern.index["4"][7]; - this._currentWord = words.words["4"][idx]; + let wordLengthKey = prog.wordLength + ""; + let wordIndex = prog.wordIndex[wordLengthKey]; + let idx = this._pattern.index[wordLengthKey][wordIndex]; - return this._currentWord; + return words.words[wordLengthKey][idx]; } - async nextWord() { - if (this._currentWord >= this._words.length - 1) { - return false; - } - - this._currentWord += 1; - return true; - } + // async nextWord() { + // if (this._currentWord >= this._words.length - 1) { + // return false; + // } + // + // this._currentWord += 1; + // return true; + // } async _fetchAllWordsIfNecessary() { if (this._wordData) {