summaryrefslogtreecommitdiff
path: root/public/main.js
blob: 01848b3f81bf3c2c0ca39343644374977bbdd308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
document.addEventListener("DOMContentLoaded", async () => { "use strict";

const $ = (s,c) => (c||document).querySelector(s);
function $$(x,y,z,a){a=(z||document).querySelectorAll(x);if(typeof y=="function")[].forEach.call(a,y);return a}
function m(a,b,c){c=document;b=c.createElement(b||"p");b.innerHTML=a.trim();for(a=c.createDocumentFragment();c=b.firstChild;)a.appendChild(c);return a.firstChild}

const board = [];
const boardEl = document.createElement("table");
$("#board").appendChild(boardEl);

for (let i=0;i<6;i++) {
  let o = "<tr>";
  for (let j=0;j<6;j++) o += `<td>&nbsp;</td>`;
  o = m(o, "tbody");
  boardEl.appendChild(o);
  board.push(o);
}

function setRow(r, s) {
  s = s.toUpperCase();
  $$("td", (e,i) => e.innerHTML = s.charAt(i)||"&nbsp;", r);
}

function setRowState(r, s) {
  $$("td", (e,i) => {
    e.classList.add({
      "=":"found",
      "-":"present",
      " ":"absent"
    }[s.charAt(i)]);
  }, r);
}

function updateKeyboard(keys) {
  $$("td[data-key]", e => {
    if (keys.hasOwnProperty(e.dataset.key)) {
      e.classList.remove("found","present","absent");
      e.classList.add(["absent","present","found"][keys[e.dataset.key]]);
    }
  });
}

let done = false;
let currentRow = 0;
let currentStr = "";
function processKey(key) {
  if (done || !/^([a-z]|enter|bspc)$/.test(key)) return;

  if (key === "enter") {
    return tryWord();

  } else if (key === "bspc") {
    currentStr = currentStr.slice(0,-1);

  } else {
    currentStr += key;
    currentStr = currentStr.slice(0,6);
  }

  setRow(board[currentRow], currentStr);
}

const guessedLetters = {};
async function tryWord() {
  if (currentStr.length !== 6) return alert("Guesses must use all six letters!");

  const r = await fetch("/guess/"+currentStr).then(r => r.json());
  if (r.result === false) return alert("Not in word list!");

  for (let i=0;i<6;i++) {
    const j = currentStr.charAt(i);
    guessedLetters[j] = Math.max(guessedLetters[j]||0,
      " -=".indexOf(r.result.charAt(i)));
  }

  updateKeyboard(guessedLetters);

  setRowState(board[currentRow], r.result);
  if (r.result === "======") {
    setTimeout(() => alert("Congratulations!"));
    done = true;
    return;
  }

  currentStr = "";
  currentRow++;

  if (currentRow === 6) {
    setTimeout(() => alert("Better luck next time!"));
    done = true;
  }
}

document.addEventListener("keydown", e =>
  e.ctrlKey || e.altKey || e.metaKey ||
    processKey(e.key.toLowerCase()
      .replace("backspace", "bspc")));

$$("td[data-key]", k => k.addEventListener("click",
  () => processKey(k.dataset.key)));

//if ("serviceWorker" in navigator) {
//  navigator.serviceWorker.register("sw.js")
//    .then(() => console.log("Service worker registered"));
//}

});