all repos — honk @ a774192626249641eb7fad08ccfe075046f74396

my fork of honk

views/wonk.js (view raw)

 1export function addguesscontrols(elem, word, wordlist, xid) {
 2	var host = elem.parentElement
 3	elem.innerHTML = "loading..."
 4
 5	host.correctAnswer = word
 6	host.guesses = []
 7	host.xid = xid
 8	var xhr = new XMLHttpRequest()
 9        xhr.open("GET", "/bloat/wonkles?w=" + encodeURIComponent(wordlist))
10        xhr.responseType = "json"
11        xhr.onload = function() { 
12		var wordlist = xhr.response.wordlist
13		var validguesses = {}
14		console.log("valid " + wordlist.length)
15		for (var i = 0; i < wordlist.length; i++) {
16			validguesses[wordlist[i]] = true
17		}
18		host.validGuesses = validguesses
19		var div = document.createElement( 'div' );
20		div.innerHTML = "<p><input> <button>guess</button>"
21		div.querySelector('button').onclick = function() {
22			makeaguess(this)
23		}
24		host.append(div)
25		elem.remove()
26	}
27        xhr.send()
28}
29export function makeaguess(btn) {
30	var host = btn.parentElement.parentElement.parentElement
31	var correct = host.correctAnswer
32	var valid = host.validGuesses
33	var inp = btn.previousElementSibling
34	var g = inp.value.toLowerCase()
35	var res = ""
36	if (valid[g]) {
37		var letters = {}
38		var obfu = ""
39		for (var i = 0; i < correct.length; i++) {
40			var l = correct[i]
41			letters[l] = (letters[l] | 0) + 1
42		}
43		for (var i = 0; i < g.length && i < correct.length; i++) {
44			if (g[i] == correct[i]) {
45				letters[g[i]] = letters[g[i]] - 1
46			}
47		}
48		for (var i = 0; i < g.length; i++) {
49			if (i < correct.length && g[i] == correct[i]) {
50				res += g[i].toUpperCase()
51				obfu += "&#129001;"
52			} else if (letters[g[i]] > 0) {
53				res += g[i]
54				obfu += "&#129000;"
55				letters[g[i]] = letters[g[i]] - 1
56			} else {
57				obfu += "&#11035;"
58				res += "."
59			}
60		}
61
62		var div = document.createElement( 'div' );
63		div.innerHTML = "<p class='fontmonospace'>" + res
64		host.append(div)
65		host.guesses.push(obfu)
66	} else {
67		var div = document.createElement( 'div' );
68		div.innerHTML = "<p> invalid guess"
69		host.append(div)
70	}
71	var div = document.createElement( 'div' );
72	if (res == correct.toUpperCase()) {
73		var mess = "<p>you are very smart!"
74		mess += "<p>" + host.xid
75		for (var i = 0; i < host.guesses.length; i++) {
76			mess += "<p>" + host.guesses[i]
77		}
78		div.innerHTML = mess
79		if (typeof(csrftoken) != "undefined")
80			post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "wonk", "guesses": host.guesses.join("<p>"), "what": host.xid}))
81	} else {
82		div.innerHTML = "<p><input> <button>guess</button>"
83		div.querySelector('button').onclick = function() {
84			makeaguess(this)
85		}
86	}
87	host.append(div)
88	btn.parentElement.remove()
89}