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 {
|
|
|
|
static targets = ["row"];
|
|
|
|
|
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-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:
|
|
|
|
console.log("not a word!");
|
|
|
|
rowElem.replaceWith(this._buildPlayfieldRow(this._gameController.wordLength()));
|
|
|
|
this._activeLetter = 0;
|
|
|
|
break;
|
|
|
|
case GUESS_RESULT.MISS:
|
|
|
|
console.log("try again!");
|
2025-01-22 10:29:50 +00:00
|
|
|
|
2025-01-22 10:50:12 +00:00
|
|
|
this._colorizeRow(rowElem, results);
|
2025-01-22 10:29:50 +00:00
|
|
|
this._activeRowIndex += 1;
|
|
|
|
this._activeLetter = 0;
|
2025-01-22 10:50:12 +00:00
|
|
|
break;
|
|
|
|
case GUESS_RESULT.WIN:
|
2025-01-22 11:04:24 +00:00
|
|
|
console.log("CORRECT!");
|
|
|
|
|
|
|
|
if (this._gameController.nextWord()) {
|
|
|
|
this._buildPlayfield();
|
|
|
|
} else {
|
|
|
|
console.log("No more words");
|
|
|
|
this._activeRowIndex = -1;
|
|
|
|
this._colorizeRow(rowElem, results);
|
|
|
|
}
|
2025-01-22 10:50:12 +00:00
|
|
|
break;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
this.element.replaceChildren.apply(this.element, newRows);
|
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
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|