From a8bd1a337feed830a99430e115378b9606ed6720 Mon Sep 17 00:00:00 2001 From: lingdocs <71590811+lingdocs@users.noreply.github.com> Date: Tue, 15 Mar 2022 19:35:54 +0400 Subject: [PATCH] pronoun clash protection --- src/components/np-picker/NPPicker.tsx | 6 ++-- .../phrase-builder/ObjectDisplay.tsx | 4 +-- .../phrase-builder/PhraseBuilder.tsx | 35 +++++++++++++++++-- src/lib/np-tools.ts | 18 ++++++++-- 4 files changed, 55 insertions(+), 8 deletions(-) diff --git a/src/components/np-picker/NPPicker.tsx b/src/components/np-picker/NPPicker.tsx index 8bc28aa..eaf3758 100644 --- a/src/components/np-picker/NPPicker.tsx +++ b/src/components/np-picker/NPPicker.tsx @@ -10,7 +10,7 @@ import { nouns, verbs } from "../../words/words"; const npTypes: NPType[] = ["noun", "pronoun", "participle"]; -function NPPicker({ np, onChange }: { onChange: (nps: NPSelection | undefined) => void, np: NPSelection | undefined }) { +function NPPicker({ np, onChange, counterPart }: { onChange: (nps: NPSelection | undefined) => void, np: NPSelection | undefined, counterPart: NPSelection | VerbObject | undefined }) { // eslint-disable-next-line const [npType, setNpType] = useState(np ? np.type : undefined); function handleClear() { @@ -19,7 +19,9 @@ function NPPicker({ np, onChange }: { onChange: (nps: NPSelection | undefined) = } function handleNPTypeChange(ntp: NPType) { if (ntp === "pronoun") { - const person = randomPerson(); + const person = ((typeof counterPart === "object") && (counterPart.type === "pronoun")) + ? randomPerson({ counterPart: counterPart.person }) + : randomPerson(); const pronoun: PronounSelection = { type: "pronoun", person, diff --git a/src/components/phrase-builder/ObjectDisplay.tsx b/src/components/phrase-builder/ObjectDisplay.tsx index 8b1f9f3..c146464 100644 --- a/src/components/phrase-builder/ObjectDisplay.tsx +++ b/src/components/phrase-builder/ObjectDisplay.tsx @@ -1,9 +1,9 @@ import NPPicker from "../np-picker/NPPicker"; -function ObjectDisplay({ object, onChange }: { object: Exclude, onChange: (o: NPSelection | undefined) => void }) { +function ObjectDisplay({ object, onChange, counterPart }: { object: Exclude, onChange: (o: NPSelection | undefined) => void, counterPart: NPSelection | undefined }) { return (typeof object === "number") ?
Unspoken 3rd Pers. Masc. Plur.
- : ; + : ; } export default ObjectDisplay; diff --git a/src/components/phrase-builder/PhraseBuilder.tsx b/src/components/phrase-builder/PhraseBuilder.tsx index d9fa161..110f340 100644 --- a/src/components/phrase-builder/PhraseBuilder.tsx +++ b/src/components/phrase-builder/PhraseBuilder.tsx @@ -4,6 +4,9 @@ import VerbPicker from "../VerbPicker"; import VPDisplay from "./VPDisplay"; import ObjectDisplay from "./ObjectDisplay"; import { verbs } from "../../words/words"; +import { + isInvalidSubjObjCombo, +} from "../../lib/np-tools"; function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefined, verb: VerbSelection | undefined }): VPSelection | undefined { if (!subject) return undefined; @@ -20,9 +23,33 @@ function verbPhraseComplete({ subject, verb }: { subject: NPSelection | undefine export function PhraseBuilder() { const [subject, setSubject] = useState(undefined); const [verb, setVerb] = useState(undefined); + function handleSubjectChange(subject: NPSelection | undefined) { + const objPronoun = (typeof verb?.object === "object" && verb.object.type === "pronoun") + ? verb.object.person + : undefined; + if (subject?.type === "pronoun" && objPronoun && isInvalidSubjObjCombo(subject.person, objPronoun)) { + alert("That combination of pronouns is not allowed"); + return; + // let newP = 0; + // do { + // newP = randomPerson(); + // } while (isInvalidSubjObjCombo(newP, object.person)); + // return setSubject({ ...incoming, person: newP }); + } + setSubject(subject); + } function handleObjectChange(object: NPSelection | undefined) { if (!verb) return; if ((verb.object === "none") || (typeof verb.object === "number")) return; + if (object?.type === "pronoun" && subject?.type === "pronoun" && isInvalidSubjObjCombo(object.person, subject.person)) { + alert("That combination of pronouns is not allowed"); + return; + // let newP = 0; + // do { + // newP = randomPerson(); + // } while (isInvalidSubjObjCombo(newP, object.person)); + // return setSubject({ ...incoming, person: newP }); + } setVerb({ ...verb, object, @@ -33,11 +60,15 @@ export function PhraseBuilder() {
Subject
- +
{verb && (verb.object !== "none") &&
Object
- +
}
Verb
diff --git a/src/lib/np-tools.ts b/src/lib/np-tools.ts index e80a66a..b28ee84 100644 --- a/src/lib/np-tools.ts +++ b/src/lib/np-tools.ts @@ -15,11 +15,25 @@ function getRandPers(): T.Person { return Math.floor(Math.random() * 12); } -export function randomPerson(p?: T.Person) { +export function randomPerson(a?: { old?: T.Person, counterPart?: T.Person | VerbObject }) { let newP = 0; do { newP = getRandPers(); - } while (newP === p); + } while ( + a && + ( + ((a.old !== undefined) && newP === a.old) || + (a.counterPart && ( + ( + (typeof a.counterPart === "object") + && a.counterPart.type === "pronoun" + && isInvalidSubjObjCombo(a.counterPart.person, newP) + ) + || + ((typeof a?.counterPart === "number") && isInvalidSubjObjCombo(a.counterPart, newP)) + )) + ) + ); return newP; }