all repos — honk @ 31fba5f708c51dd3c5f930fb887d480689cc2347

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=" + escape(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 onclick='return makeaguess(this)'>guess</button>"
21		host.append(div)
22		elem.remove()
23	}
24        xhr.send()
25}
26export function makeaguess(btn) {
27	var host = btn.parentElement.parentElement.parentElement
28	var correct = host.correctAnswer
29	var valid = host.validGuesses
30	var inp = btn.previousElementSibling
31	var g = inp.value.toLowerCase()
32	var res = ""
33	if (valid[g]) {
34		var letters = {}
35		var obfu = ""
36		for (var i = 0; i < correct.length; i++) {
37			var l = correct[i]
38			letters[l] = (letters[l] | 0) + 1
39		}
40		for (var i = 0; i < g.length && i < correct.length; i++) {
41			if (g[i] == correct[i]) {
42				letters[g[i]] = letters[g[i]] - 1
43			}
44		}
45		for (var i = 0; i < g.length; i++) {
46			if (i < correct.length && g[i] == correct[i]) {
47				res += g[i].toUpperCase()
48				obfu += "&#129001;"
49			} else if (letters[g[i]] > 0) {
50				res += g[i]
51				obfu += "&#129000;"
52				letters[g[i]] = letters[g[i]] - 1
53			} else {
54				obfu += "&#11035;"
55				res += "."
56			}
57		}
58
59		var div = document.createElement( 'div' );
60		div.innerHTML = "<p style='font-family: monospace'>" + res
61		host.append(div)
62		host.guesses.push(obfu)
63	} else {
64		var div = document.createElement( 'div' );
65		div.innerHTML = "<p> invalid guess"
66		host.append(div)
67	}
68	var div = document.createElement( 'div' );
69	if (res == correct.toUpperCase()) {
70		var mess = "<p>you are very smart!"
71		mess += "<p>" + host.xid
72		for (var i = 0; i < host.guesses.length; i++) {
73			mess += "<p>" + host.guesses[i]
74		}
75		div.innerHTML = mess
76		if (typeof(csrftoken) != "undefined")
77			post("/zonkit", encode({"CSRF": csrftoken, "wherefore": "wonk", "guesses": host.guesses.join("<p>"), "what": host.xid}))
78	} else {
79		div.innerHTML = "<p><input> <button onclick='return makeaguess(this)'>guess</button>"
80	}
81	host.append(div)
82	btn.parentElement.remove()
83}