Trigger modal
from js import document, console, setTimeout
from pyodide.ffi import create_proxy
from pyodide.http import pyfetch
from pyodide.ffi import JsException
import random
import re
import pyscript
wordlist = []
word = None
async def load_wordlist():
try:
response = await pyfetch(
url="https://raw.githubusercontent.com/moeb98/moedle/master/word_list.txt",
method="GET",
headers={"Content-Type": "text/plain"})
if response.ok:
data = await response.string()
console.log(data.split())
return data.split()
except JsException:
return None
async def pick_word():
global wordlist, word, accept_input
wordlist = await load_wordlist()
word = random.choice(wordlist)
console.log("word: ", " ".join(list(word)))
accept_input = True
setTimeout(create_proxy(pick_word), 0)
tiles = []
board = document.getElementById("board")
for row in range(6):
tiles.append([])
for col in range(5):
tile = document.createElement("div")
board.appendChild(tile)
tiles[-1].append(tile)
guess = ""
guess_no = 0
key_objects = {}
accept_input = False
game_no = 0
game_success_no = 0
pg = document.getElementById("played_games")
pg.innerHTML = game_no
wg = document.getElementById("won_games")
wg.innerHTML = game_success_no
wgp = document.getElementById("won_games_percentage")
wgp.innerHTML = game_no
mt = document.getElementById("modal_title")
mc = document.getElementById("modal_content")
def display_guess():
if guess_no > 5:
return
for i in range(5):
tiles[guess_no][i].textContent = ""
for pos, char in enumerate(list(guess)):
tiles[guess_no][pos].textContent = char
def evaluate_guess():
global word, guess, guess_no, accept_input, correct_counter, game_no, game_success_no
correct_counter = 0
for pos, char in enumerate(list(guess)):
if char == word[pos]:
tiles[guess_no][pos].classList.add("nailedit")
key_objects[char].classList.add("nailedit")
correct_counter += 1
elif char in word:
tiles[guess_no][pos].classList.add("somewhere")
key_objects[char].classList.add("somewhere")
else:
tiles[guess_no][pos].classList.add("nope")
key_objects[char].classList.add("nope")
if correct_counter >= 5:
game_no += 1
game_success_no += 1
pg.innerHTML = game_no
wg.innerHTML = game_success_no
wgp.innerHTML = "{:.0%}".format(game_success_no / game_no)
mt.innerHTML = "Correct!"
mc.innerHTML = f"„{guess}“ was the secret word."
mt.style = "font-weight: bold; color: darkgreen;"
document.getElementById("trigger_modal").click()
accept_input = False
elif guess_no >= 5:
game_no += 1
pg.innerHTML = game_no
wgp.innerHTML = "{:.0%}".format(game_success_no / game_no)
mt.innerHTML = "Game over!"
mc.innerHTML = f"„{word}“ was the secret word."
mt.style = "font-weight: bold; color: darkred;"
document.getElementById("trigger_modal").click()
accept_input = False
def check_enter():
global guess, guess_no, wordlist
if len(guess) != 5:
return
if guess not in wordlist:
mt.innerHTML = "I don't know that word!"
mc.innerHTML = f"„{guess}“ is not on my word list."
mt.style = "font-weight: bold; color: darkred;"
document.getElementById("trigger_modal").click()
return
evaluate_guess()
guess = ""
guess_no += 1
def key_clicked(e):
global accept_input, guess
if not accept_input:
return
if len(guess) >= 5 and len(e.target.textContent) == 1:
guess = guess[:5]
display_guess()
return
match len(e.target.textContent):
case 0:
guess = guess[:-1]
case 1:
guess += e.target.textContent
case _:
check_enter()
display_guess()
js_key_clicked = create_proxy(key_clicked)
for row in document.getElementById("keyboard").childNodes:
for key in row.childNodes:
key.addEventListener("click", js_key_clicked)
if len(key.textContent) == 1:
key_objects[key.textContent] = key
def key_down(e):
global accept_input, guess
if not accept_input:
return
if len(guess) >= 5 and len(e.key) == 1:
guess = guess[:5]
display_guess()
return
regex_matches = re.match(r'^(Enter|Backspace|[a-zA-Z0-9/-;\.öäüÖÄÜßẞ])$', e.key)
if not regex_matches:
display_guess()
return
match regex_matches.groups():
case ('Backspace',): guess = guess[:-1]
case ('Enter',): check_enter()
case ('ẞ',) | ('ß',): guess += "ß"
case _: guess += e.key.upper()
console.log(guess, e.key, e.key.upper())
display_guess()
document.onkeydown = create_proxy(key_down)
def new_game(event):
global word, guess, guess_no, accept_input
word = random.choice(wordlist)
guess = ""
guess_no = 0
console.log("word: ", " ".join(list(word)))
document.getElementById("newgame").blur();
for line in tiles:
for tile in line:
tile.classList.remove("nailedit", "somewhere", "nope")
tile.textContent = ""
for i in key_objects:
key_objects[i].classList.remove("nailedit", "somewhere", "nope")
accept_input = True
document.getElementById("newgame").addEventListener("click", create_proxy(new_game))