From f4dc09e94190f9365c33b033a1e1d7acc5ad692a Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Mon, 30 May 2022 15:29:24 -0500 Subject: [PATCH] improved games - ability to check phonetics and 3 strikes --- package.json | 2 +- src/games/sub-cores/EquativeGame.tsx | 12 +++--------- src/games/sub-cores/UnisexNounGame.tsx | 8 ++------ src/lib/game-utils.ts | 15 +++++++++++++++ yarn.lock | 8 ++++---- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/package.json b/package.json index 797808b..1ae86d9 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,7 @@ "@formkit/auto-animate": "^1.0.0-beta.1", "@fortawesome/fontawesome-free": "^5.15.4", "@lingdocs/lingdocs-main": "^0.3.1", - "@lingdocs/pashto-inflector": "^2.6.7", + "@lingdocs/pashto-inflector": "^2.6.8", "@testing-library/jest-dom": "^5.11.4", "@testing-library/react": "^11.1.0", "@testing-library/user-event": "^12.1.10", diff --git a/src/games/sub-cores/EquativeGame.tsx b/src/games/sub-cores/EquativeGame.tsx index e9afd8d..88c11eb 100644 --- a/src/games/sub-cores/EquativeGame.tsx +++ b/src/games/sub-cores/EquativeGame.tsx @@ -1,5 +1,6 @@ import { useEffect, useState } from "react"; import { + comparePs, makeProgress, } from "../../lib/game-utils"; import GameCore from "../GameCore"; @@ -7,7 +8,6 @@ import { Types as T, Examples, defaultTextOptions as opts, - standardizePashto, typePredicates as tp, makeNounSelection, randFromArray, @@ -277,9 +277,8 @@ export default function EquativeGame({ id, link, level }: { id: string, link: st return; } e.preventDefault(); - const given = standardizePashto(answer.trim()); - const correct = checkAnswer(given, question.equative.ps) - && withBa === question.equative.hasBa; + const correct = comparePs(answer, question.equative.ps) + && (withBa === question.equative.hasBa); if (correct) { setAnswer(""); } @@ -467,11 +466,6 @@ function humanReadableTense(tense: T.EquativeTense | "allProduce"): string { : tense; } -function checkAnswer(given: string, answer: T.SingleOrLengthOpts): boolean { - const possible = flattenLengths(answer); - return possible.some(x => given === x.p); -} - function makeEPS(subject: T.NPSelection, predicate: T.AdjectiveEntry | T.LocativeAdverbEntry, tense: T.EquativeTense): T.EPSelectionComplete { return { blocks: [ diff --git a/src/games/sub-cores/UnisexNounGame.tsx b/src/games/sub-cores/UnisexNounGame.tsx index d377727..07adf79 100644 --- a/src/games/sub-cores/UnisexNounGame.tsx +++ b/src/games/sub-cores/UnisexNounGame.tsx @@ -1,7 +1,7 @@ import { useState } from "react"; import { makeProgress, - compareF, + comparePs, } from "../../lib/game-utils"; import genderColors from "../../lib/gender-colors"; import GameCore from "../GameCore"; @@ -10,7 +10,6 @@ import { Examples, defaultTextOptions as opts, inflectWord, - standardizePashto, firstVariation, typePredicates as tp, randFromArray, @@ -76,12 +75,9 @@ export default function UnisexNounGame({ id, link }: { id: string, link: string } const handleSubmit = (e: React.FormEvent) => { e.preventDefault(); - const given = standardizePashto(answer.trim()); // @ts-ignore const correctAnswer = inflections[flipGender(question.gender)][0]; - const correct = correctAnswer.some((ps: T.PsString) => ( - (given === ps.p) || compareF(given, ps.f) - )); + const correct = comparePs(answer, correctAnswer); if (correct) { setAnswer(""); } diff --git a/src/lib/game-utils.ts b/src/lib/game-utils.ts index f346c72..74529be 100644 --- a/src/lib/game-utils.ts +++ b/src/lib/game-utils.ts @@ -1,6 +1,10 @@ import { removeAccents, hasAccents, + Types as T, + standardizePashto, + standardizePhonetics, + flattenLengths, } from "@lingdocs/pashto-inflector"; export function makeRandomQs( @@ -40,4 +44,15 @@ export function makeProgress(i: number, total: number): Progress { */ export function compareF(input: string, answer: string): boolean { return input === (hasAccents(input) ? answer : removeAccents(answer)); +} + +export function comparePs(input: string, answer: T.SingleOrLengthOpts): boolean { + if ("long" in answer) { + return comparePs(input, flattenLengths(answer)) + } + if (Array.isArray(answer)) { + return answer.some(a => comparePs(input, a)); + } + const stand = standardizePhonetics(standardizePashto(input)).trim(); + return stand === answer.p || compareF(stand, answer.f); } \ No newline at end of file diff --git a/yarn.lock b/yarn.lock index 4f8d88e..964370e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1695,10 +1695,10 @@ rambda "^6.7.0" react-select "^5.2.2" -"@lingdocs/pashto-inflector@^2.6.7": - version "2.6.7" - resolved "https://npm.lingdocs.com/@lingdocs%2fpashto-inflector/-/pashto-inflector-2.6.7.tgz#0b40b96772ec0bee284af711b017b86e2175c9ef" - integrity sha512-wXRCkZc2+H+MkMx1gka2iABHWBxPuz96gcNMTtxPFF/cJSP6pIMmTNXgFgjbSXK3frHteJGvnBaISnAQ05J7wg== +"@lingdocs/pashto-inflector@^2.6.8": + version "2.6.8" + resolved "https://npm.lingdocs.com/@lingdocs%2fpashto-inflector/-/pashto-inflector-2.6.8.tgz#d104620a75a4f034d3dcd9d8a097eab40b77024d" + integrity sha512-z2RetX9mRgZxM0FIX8R3cgCF+Exof/OigMDZIlEHLhBAljSwaojA9ZdXnyKP1zW8EDtjLIqsr3rWQre6niU80w== dependencies: "@formkit/auto-animate" "^1.0.0-beta.1" classnames "^2.2.6"