Have got win and failures working

This commit is contained in:
Leon Mika 2025-01-25 11:30:04 +11:00
parent d9fa154a01
commit a8e42da6fd
10 changed files with 255 additions and 28 deletions

View file

@ -32,6 +32,16 @@ export default class extends Controller {
let key = ev.target.dataset["key"];
this.playfieldOutlet.tappedKey(key);
}
tapEnter(ev) {
ev.preventDefault();
this.playfieldOutlet.enterGuess();
}
tapBackspace(ev) {
ev.preventDefault();
this.playfieldOutlet.tappedBackspace();
}
resetKeyColors(ev) {
for (let keyElement of this.keyTargets) {

View file

@ -0,0 +1,18 @@
import { Controller } from "https://unpkg.com/@hotwired/stimulus@v3.2.2/dist/stimulus.js"
export default class extends Controller {
static targets = ["message"];
showMessage(msg) {
this.messageTarget.innerText = msg;
this.element.classList.add("show");
if (this._waitTimer) {
clearTimeout(this._waitTimer);
}
this._waitTimer = setTimeout(() => {
this.element.classList.remove("show");
}, 3000);
}
}

View file

@ -5,7 +5,8 @@ import { WordSource } from "../models/words.js";
export default class extends Controller {
static targets = ["row"];
static targets = ["row", "playfield", "topMessage", "nextPuzzleButton"];
static outlets = ["overlay"];
async connect() {
this._wordSource = new WordSource();
@ -64,20 +65,30 @@ export default class extends Controller {
switch (results.guessResult) {
case GUESS_RESULT.FOUL:
console.log("not a word!");
this.overlayOutlet.showMessage("Not a valid word.");
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
this._activeLetter = 0;
break;
case GUESS_RESULT.MISS:
console.log("try again!");
case GUESS_RESULT.MISS:
this._colorizeRow(rowElem, results);
this._activeRowIndex += 1;
this._activeLetter = 0;
if (this._activeRowIndex >= this._gameController.guesses()) {
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
this.nextPuzzleButtonTarget.classList.remove("hide");
} else {
this._activeLetter = 0;
}
break;
case GUESS_RESULT.WIN:
console.log("CORRECT!");
this._colorizeRow(rowElem, results);
this.topMessageTarget.innerText = "Hooray! You did it.";
this.nextPuzzleButtonTarget.classList.remove("hide");
/*
if (this._gameController.nextWord()) {
this._buildPlayfield();
} else {
@ -85,9 +96,19 @@ export default class extends Controller {
this._activeRowIndex = -1;
this._colorizeRow(rowElem, results);
}
*/
break;
}
}
async loadNextPuzzle(ev) {
ev.preventDefault();
if (await this._gameController.nextWord()) {
this._buildPlayfield();
} else {
this.overlayOutlet.showMessage("No more words available.");
}
}
_buildPlayfield() {
let rows = this._gameController.guesses();
@ -101,7 +122,10 @@ export default class extends Controller {
newRows.push(this._buildPlayfieldRow(wordLength));
}
this.element.replaceChildren.apply(this.element, newRows);
this.playfieldTarget.replaceChildren.apply(this.playfieldTarget, newRows);
this.topMessageTarget.innerHTML = " "
this.nextPuzzleButtonTarget.classList.add("hide");
window.dispatchEvent(new CustomEvent("resetKeyColors"));
}

View file

@ -2,9 +2,11 @@ import { Application, Controller } from "https://unpkg.com/@hotwired/stimulus@v3
import PlayfieldController from "./controllers/playfield.js";
import KeyboardController from "./controllers/keyboard.js";
import OverlayController from "./controllers/overlay.js";
window.Stimulus = Application.start();
Stimulus.register("playfield", PlayfieldController);
Stimulus.register("keyboard", KeyboardController);
Stimulus.register("keyboard", KeyboardController);
Stimulus.register("overlay", OverlayController);

View file

@ -31,6 +31,11 @@ export class GameController {
this._checkHasStarted();
return this._guesses;
}
currentWord() {
this._checkHasStarted();
return this._currentWord;
}
async nextWord() {
this._currentWord = await this._wordSource.getCurrentWord();

View file

@ -20,6 +20,7 @@ function binSearch(list, word) {
export class WordSource {
constructor() {
this._wordData = null;
this._pattern = null;
this._currentWord = null;
}
@ -38,7 +39,9 @@ export class WordSource {
}
let words = await this._fetchAllWordsIfNecessary();
this._currentWord = words.words["4"][7];
let idx = this._pattern.index["4"][7];
this._currentWord = words.words["4"][idx];
return this._currentWord;
}
@ -57,8 +60,8 @@ export class WordSource {
return this._wordData;
}
let res = await fetch("/assets/data/words.json");
this._wordData = await res.json();
this._wordData = await (await fetch("/assets/data/words.json")).json();
this._pattern = await (await fetch("/assets/data/shuffle_pattern.json")).json();
return this._wordData;
}
}