It's playable

This commit is contained in:
Lynn
2022-01-14 00:16:27 +01:00
parent 4a08467237
commit 687f2acc22
6 changed files with 69 additions and 23 deletions

View File

@@ -44,6 +44,10 @@ body {
user-select: none; user-select: none;
} }
table.Game-rows {
margin: auto;
}
.Game-keyboard { .Game-keyboard {
display: flex; display: flex;
flex-direction: column; flex-direction: column;
@@ -171,7 +175,8 @@ a:active {
font-variant-numeric: tabular-nums; font-variant-numeric: tabular-nums;
} }
.Game-sr-feedback { .Game-sr-feedback,
.sr-only {
position: absolute; position: absolute;
left: -10000px; left: -10000px;
top: auto; top: auto;

View File

@@ -1,10 +1,10 @@
import { useEffect, useState } from "react"; import { useEffect, useState } from "react";
import { Row, RowState } from "./Row"; import { Row, RowState } from "./Row";
import dictionary from "./dictionary.json"; import dictionary from "./dictionary.json";
import { Clue, clue } from "./clue"; import { Clue, clue, describeClue } from "./clue";
import { Keyboard } from "./Keyboard"; import { Keyboard } from "./Keyboard";
import targetList from "./targets.json"; import targetList from "./targets.json";
import { dictionarySet, pick, resetRng, seed } from "./util"; import { dictionarySet, pick, resetRng, seed, speak } from "./util";
enum GameState { enum GameState {
Playing, Playing,
@@ -57,6 +57,7 @@ function Game(props: GameProps) {
if (/^[a-z]$/.test(key)) { if (/^[a-z]$/.test(key)) {
setCurrentGuess((guess) => (guess + key).slice(0, wordLength)); setCurrentGuess((guess) => (guess + key).slice(0, wordLength));
setHint(""); setHint("");
setSrStatus("");
} else if (key === "Backspace") { } else if (key === "Backspace") {
setCurrentGuess((guess) => guess.slice(0, -1)); setCurrentGuess((guess) => guess.slice(0, -1));
setHint(""); setHint("");
@@ -81,7 +82,7 @@ function Game(props: GameProps) {
setGameState(GameState.Lost); setGameState(GameState.Lost);
} else { } else {
setHint(""); setHint("");
setSrStatus("Feedback goes here"); speak(describeClue(clue(currentGuess, target)));
} }
} }
}; };
@@ -99,7 +100,7 @@ function Game(props: GameProps) {
}, [currentGuess, gameState]); }, [currentGuess, gameState]);
let letterInfo = new Map<string, Clue>(); let letterInfo = new Map<string, Clue>();
const rowDivs = Array(props.maxGuesses) const tableRows = Array(props.maxGuesses)
.fill(undefined) .fill(undefined)
.map((_, i) => { .map((_, i) => {
const guess = [...guesses, currentGuess][i] ?? ""; const guess = [...guesses, currentGuess][i] ?? "";
@@ -156,7 +157,7 @@ function Game(props: GameProps) {
}} }}
></input> ></input>
<button <button
style={{ flex: "0" }} style={{ flex: "0 0 auto" }}
disabled={gameState !== GameState.Playing || guesses.length === 0} disabled={gameState !== GameState.Playing || guesses.length === 0}
onClick={() => { onClick={() => {
setHint( setHint(
@@ -169,11 +170,11 @@ function Game(props: GameProps) {
Give up Give up
</button> </button>
</div> </div>
<div className="Game-rows">{rowDivs}</div> <table className="Game-rows">{tableRows}</table>
<p role="status">{hint || `\u00a0`}</p> <p role="alert">{hint || `\u00a0`}</p>
<p role="status" className="Game-sr-feedback"> {/* <p role="alert" className="Game-sr-feedback">
{srStatus} {srStatus}
</p> </p> */}
<Keyboard letterInfo={letterInfo} onKey={onKey} /> <Keyboard letterInfo={letterInfo} onKey={onKey} />
{seed ? ( {seed ? (
<div className="Game-seed-info"> <div className="Game-seed-info">

View File

@@ -13,7 +13,7 @@ export function Keyboard(props: KeyboardProps) {
]; ];
return ( return (
<div className="Game-keyboard"> <div className="Game-keyboard" aria-hidden="true">
{keyboard.map((row, i) => ( {keyboard.map((row, i) => (
<div key={i} className="Game-keyboard-row"> <div key={i} className="Game-keyboard-row">
{row.map((label, j) => { {row.map((label, j) => {
@@ -29,6 +29,7 @@ export function Keyboard(props: KeyboardProps) {
<div <div
tabIndex={-1} tabIndex={-1}
key={j} key={j}
role="button"
className={className} className={className}
onClick={() => { onClick={() => {
props.onKey(label); props.onKey(label);

View File

@@ -1,4 +1,4 @@
import { Clue, clueClass, CluedLetter } from "./clue"; import { Clue, clueClass, CluedLetter, clueWord } from "./clue";
export enum RowState { export enum RowState {
LockedIn, LockedIn,
@@ -24,20 +24,22 @@ export function Row(props: RowProps) {
letterClass += " " + clueClass(clue); letterClass += " " + clueClass(clue);
} }
return ( return (
<div key={i} className={letterClass}> <td
key={i}
className={letterClass}
aria-live="polite"
aria-label={
isLockedIn
? letter.toUpperCase() +
(clue === undefined ? "" : ": " + clueWord(clue))
: ""
}
>
{letter} {letter}
</div> </td>
); );
}); });
let rowClass = "Row"; let rowClass = "Row";
if (isLockedIn) rowClass += " Row-locked-in"; if (isLockedIn) rowClass += " Row-locked-in";
return ( return <tr className={rowClass}>{letterDivs}</tr>;
<div
className={rowClass}
role={isEditing ? "input" : "row"}
tabIndex={isEditing ? 0 : undefined}
>
{letterDivs}
</div>
);
} }

View File

@@ -39,3 +39,19 @@ export function clueClass(clue: Clue): string {
return "letter-correct"; return "letter-correct";
} }
} }
export function clueWord(clue: Clue): string {
if (clue === Clue.Absent) {
return "no";
} else if (clue === Clue.Elsewhere) {
return "yellow";
} else {
return "correct";
}
}
export function describeClue(clue: CluedLetter[]): string {
return clue
.map(({ letter, clue }) => letter.toUpperCase() + " " + clueWord(clue!))
.join(", ");
}

View File

@@ -24,3 +24,24 @@ export function resetRng(): void {
export function pick<T>(array: Array<T>): T { export function pick<T>(array: Array<T>): T {
return array[Math.floor(array.length * random())]; return array[Math.floor(array.length * random())];
} }
// https://a11y-guidelines.orange.com/en/web/components-examples/make-a-screen-reader-talk/
export function speak(
text: string,
priority: "polite" | "assertive" = "assertive"
) {
var el = document.createElement("div");
var id = "speak-" + Date.now();
el.setAttribute("id", id);
el.setAttribute("aria-live", priority || "polite");
el.classList.add("sr-only");
document.body.appendChild(el);
window.setTimeout(function () {
document.getElementById(id)!.innerHTML = text;
}, 100);
window.setTimeout(function () {
document.body.removeChild(document.getElementById(id)!);
}, 1000);
}