2025-01-22 10:29:50 +00:00
|
|
|
import { Controller } from "https://unpkg.com/@hotwired/stimulus@v3.2.2/dist/stimulus.js"
|
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
import { GUESS_RESULT, MARKERS, GameController } from "../models/gamecontroller.js";
|
|
|
|
import { WordSource } from "../models/words.js";
|
|
|
|
|
2025-01-22 10:29:50 +00:00
|
|
|
|
|
|
|
export default class extends Controller {
|
2025-01-25 23:19:06 +00:00
|
|
|
static targets = ["row", "playfield", "topMessage", "nextPuzzleButtons"];
|
2025-01-25 00:30:04 +00:00
|
|
|
static outlets = ["overlay"];
|
2025-01-22 10:29:50 +00:00
|
|
|
|
2025-01-24 22:45:55 +00:00
|
|
|
async connect() {
|
2025-01-22 10:50:12 +00:00
|
|
|
this._wordSource = new WordSource();
|
|
|
|
this._gameController = new GameController(this._wordSource);
|
2025-01-24 22:45:55 +00:00
|
|
|
|
|
|
|
await this._gameController.start();
|
|
|
|
|
2025-01-22 11:04:24 +00:00
|
|
|
this._buildPlayfield();
|
2025-01-22 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
tappedKey(key) {
|
|
|
|
console.log(`Key ${key} was tapped via outliet`);
|
|
|
|
|
|
|
|
this._addLetter(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
_addLetter(letter) {
|
2025-01-22 11:04:24 +00:00
|
|
|
if (this._activeRowIndex < 0) {
|
|
|
|
return;
|
2025-01-23 10:54:33 +00:00
|
|
|
} else if (this._activeLetter >= this._gameController.wordLength()) {
|
|
|
|
return;
|
2025-01-22 11:04:24 +00:00
|
|
|
}
|
|
|
|
|
2025-01-22 10:29:50 +00:00
|
|
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
|
|
|
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
|
|
|
|
|
|
|
colElem.innerText = letter.toUpperCase();
|
|
|
|
|
|
|
|
this._activeLetter += 1;
|
2025-01-23 10:54:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
enterGuess() {
|
2025-01-22 10:29:50 +00:00
|
|
|
if (this._activeLetter >= this._gameController.wordLength()) {
|
2025-01-23 10:54:33 +00:00
|
|
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
2025-01-22 11:04:24 +00:00
|
|
|
this._verifyGuess(rowElem);
|
2025-01-22 10:50:12 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-25 23:19:06 +00:00
|
|
|
|
|
|
|
loadDef(ev) {
|
|
|
|
ev.preventDefault()
|
|
|
|
|
|
|
|
let word = this._gameController.currentWord();
|
|
|
|
window.open(`https://www.ecosia.org/search?q=define+${word}`, "_blank");
|
|
|
|
}
|
2025-01-22 10:50:12 +00:00
|
|
|
|
2025-01-23 10:54:33 +00:00
|
|
|
tappedBackspace() {
|
|
|
|
if (this._activeLetter == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._activeLetter -= 1;
|
|
|
|
let rowElem = this.rowTargets[this._activeRowIndex];
|
|
|
|
let colElem = rowElem.querySelectorAll("span")[this._activeLetter];
|
|
|
|
|
|
|
|
colElem.innerText = "";
|
|
|
|
}
|
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
_verifyGuess(rowElem) {
|
|
|
|
let guessedWord = Array.from(rowElem.querySelectorAll("span")).map((x) => x.innerText).join("");
|
|
|
|
console.log("The guessed word is: " + guessedWord);
|
|
|
|
|
|
|
|
let results = this._gameController.checkGuess(guessedWord);
|
|
|
|
|
|
|
|
switch (results.guessResult) {
|
|
|
|
case GUESS_RESULT.FOUL:
|
2025-01-25 23:19:06 +00:00
|
|
|
this.overlayOutlet.showMessage("Not a valid word");
|
2025-01-25 00:30:04 +00:00
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
|
|
|
|
this._activeLetter = 0;
|
2025-01-25 23:19:06 +00:00
|
|
|
|
|
|
|
window.dispatchEvent(new CustomEvent("guessResults", {
|
|
|
|
detail: results
|
|
|
|
}));
|
2025-01-22 10:50:12 +00:00
|
|
|
break;
|
2025-01-25 00:30:04 +00:00
|
|
|
case GUESS_RESULT.MISS:
|
2025-01-22 10:50:12 +00:00
|
|
|
this._colorizeRow(rowElem, results);
|
2025-01-25 00:30:04 +00:00
|
|
|
|
2025-01-22 10:29:50 +00:00
|
|
|
this._activeRowIndex += 1;
|
2025-01-25 00:30:04 +00:00
|
|
|
if (this._activeRowIndex >= this._gameController.guesses()) {
|
|
|
|
this.topMessageTarget.innerText = this._gameController.currentWord().toUpperCase();
|
2025-01-25 23:19:06 +00:00
|
|
|
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
2025-01-25 00:30:04 +00:00
|
|
|
} else {
|
|
|
|
this._activeLetter = 0;
|
|
|
|
}
|
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
break;
|
|
|
|
case GUESS_RESULT.WIN:
|
2025-01-25 00:30:04 +00:00
|
|
|
this._colorizeRow(rowElem, results);
|
|
|
|
|
|
|
|
this.topMessageTarget.innerText = "Hooray! You did it.";
|
2025-01-25 23:19:06 +00:00
|
|
|
this.nextPuzzleButtonsTarget.classList.remove("hide");
|
2025-01-22 10:50:12 +00:00
|
|
|
break;
|
2025-01-22 10:29:50 +00:00
|
|
|
}
|
|
|
|
}
|
2025-01-25 00:30:04 +00:00
|
|
|
|
|
|
|
async loadNextPuzzle(ev) {
|
|
|
|
ev.preventDefault();
|
|
|
|
if (await this._gameController.nextWord()) {
|
|
|
|
this._buildPlayfield();
|
|
|
|
} else {
|
|
|
|
this.overlayOutlet.showMessage("No more words available.");
|
|
|
|
}
|
|
|
|
}
|
2025-01-22 10:29:50 +00:00
|
|
|
|
2025-01-22 11:04:24 +00:00
|
|
|
_buildPlayfield() {
|
|
|
|
let rows = this._gameController.guesses();
|
|
|
|
let wordLength = this._gameController.wordLength();
|
|
|
|
|
|
|
|
this._activeRowIndex = 0;
|
|
|
|
this._activeLetter = 0;
|
|
|
|
|
2025-01-22 10:29:50 +00:00
|
|
|
let newRows = [];
|
2025-01-22 10:50:12 +00:00
|
|
|
for (let r = 0; r < rows; r++) {
|
|
|
|
newRows.push(this._buildPlayfieldRow(wordLength));
|
2025-01-22 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
2025-01-25 00:30:04 +00:00
|
|
|
this.playfieldTarget.replaceChildren.apply(this.playfieldTarget, newRows);
|
|
|
|
|
|
|
|
this.topMessageTarget.innerHTML = " "
|
2025-01-25 23:19:06 +00:00
|
|
|
this.nextPuzzleButtonsTarget.classList.add("hide");
|
2025-01-22 11:04:24 +00:00
|
|
|
|
|
|
|
window.dispatchEvent(new CustomEvent("resetKeyColors"));
|
2025-01-22 10:29:50 +00:00
|
|
|
}
|
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
_buildPlayfieldRow(wordLength) {
|
|
|
|
let divElem = document.createElement("div");
|
|
|
|
divElem.classList.add("row");
|
|
|
|
divElem.setAttribute("data-playfield-target", "row");
|
2025-01-22 10:29:50 +00:00
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
for (let c = 0; c < wordLength; c++) {
|
|
|
|
let letterSpan = document.createElement("span");
|
|
|
|
divElem.appendChild(letterSpan);
|
|
|
|
}
|
|
|
|
return divElem;
|
|
|
|
}
|
|
|
|
|
|
|
|
_colorizeRow(row, results) {
|
2025-01-22 10:29:50 +00:00
|
|
|
let markers = results.markers;
|
|
|
|
|
|
|
|
for (let i = 0; i < this._gameController.wordLength(); i++) {
|
|
|
|
switch (markers[i]) {
|
|
|
|
case MARKERS.RIGHT_POS:
|
|
|
|
row.children[i].classList.add("right-pos");
|
|
|
|
break;
|
|
|
|
case MARKERS.RIGHT_CHAR:
|
|
|
|
row.children[i].classList.add("right-char");
|
|
|
|
break;
|
|
|
|
case MARKERS.MISS:
|
|
|
|
row.children[i].classList.add("miss");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
window.dispatchEvent(new CustomEvent("guessResults", {
|
|
|
|
detail: results
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|