refactor to clean up game creation and dont have the study button on the in chapter appearance

This commit is contained in:
lingdocs 2022-09-03 15:40:28 +04:00
parent 1b29b2d22d
commit 45f901fb62
9 changed files with 193 additions and 160 deletions

View File

@ -26,7 +26,8 @@ const errorVibration = 200;
const maxStrikes = 2; const maxStrikes = 2;
function GameCore<T>({ questions, Display, timeLimit, Instructions, studyLink, id }:{ function GameCore<T>({ inChapter, questions, Display, timeLimit, Instructions, studyLink, id }:{
inChapter: boolean,
id: string, id: string,
studyLink: string, studyLink: string,
Instructions: (props: { opts?: Types.TextOptions }) => JSX.Element, Instructions: (props: { opts?: Types.TextOptions }) => JSX.Element,
@ -200,9 +201,9 @@ function GameCore<T>({ questions, Display, timeLimit, Instructions, studyLink, i
<Instructions /> <Instructions />
</div> </div>
<div> <div>
<Link to={studyLink}> {!inChapter && <Link to={studyLink}>
<button className="btn btn-danger mt-4 mx-3">Study</button> <button className="btn btn-danger mt-4 mx-3">Study</button>
</Link> </Link>}
<button className="btn btn-warning mt-4 mx-3" onClick={() => handleRestart("practice")}>Practice</button> <button className="btn btn-warning mt-4 mx-3" onClick={() => handleRestart("practice")}>Practice</button>
<button className="btn btn-success mt-4 mx-3" onClick={() => handleRestart("test")}>Test</button> <button className="btn btn-success mt-4 mx-3" onClick={() => handleRestart("test")}>Test</button>
</div> </div>

View File

@ -15,7 +15,7 @@ function GameDisplay({ record: { title, Game, id } }: { record: GameRecord }) {
<h4>{completed ? "✅" : ""}</h4> <h4>{completed ? "✅" : ""}</h4>
</div> </div>
</div> </div>
<Game /> <Game inChapter />
</div>; </div>;
} }

View File

@ -48,7 +48,7 @@ function ChapterDisplay({ chapter, user, handleClick, expanded }: {
</div> </div>
</div> </div>
<SmoothCollapse expanded={expanded}> <SmoothCollapse expanded={expanded}>
{chapter.items.map(({ id, title, Game, studyLink }) => { {chapter.items.map(({ id, title, Game }) => {
const done = user && user.tests.some(t => t.id === id); const done = user && user.tests.some(t => t.id === id);
const open = opened === id; const open = opened === id;
return <div key={id}> return <div key={id}>
@ -64,7 +64,7 @@ function ChapterDisplay({ chapter, user, handleClick, expanded }: {
</div> </div>
</div> </div>
<SmoothCollapse expanded={open}> <SmoothCollapse expanded={open}>
<Game /> <Game inChapter={false} />
</SmoothCollapse> </SmoothCollapse>
</div> </div>
})} })}

View File

@ -3,152 +3,151 @@ import VerbGame from "./sub-cores/VerbGame";
import GenderGame from "./sub-cores/GenderGame"; import GenderGame from "./sub-cores/GenderGame";
import UnisexNounGame from "./sub-cores/UnisexNounGame"; import UnisexNounGame from "./sub-cores/UnisexNounGame";
function makeGameRecord( // NOUNS
title: string, export const nounGenderGame1 = makeGameRecord({
id: string, title: "Identify Noun Genders - Level 1",
studyLink: string, id: "gender-nouns-1",
game: (id: string, link: string) => () => JSX.Element, link: "/nouns/nouns-gender#gender-by-ending",
): GameRecord { level: 1,
return { SubCore: GenderGame,
title, });
studyLink, export const nounGenderGame2 = makeGameRecord({
id, title: "Identify Noun Genders - Level 2",
Game: game(id, studyLink), id: "gender-nouns-2",
} link: "/nouns/nouns-gender#exceptions",
} level: 2,
SubCore: GenderGame,
});
export const unisexNounGame = makeGameRecord({
title: "Changing genders on unisex nouns",
id: "unisex-nouns-1",
link: "/nouns/nouns-unisex/",
level: undefined,
SubCore: UnisexNounGame,
});
export const presentVerbGame = makeGameRecord( // EQUATIVES
"Write the present verb", export const equativeGamePresent = makeGameRecord({
"present-verbs-write", title: "Write the present equative",
"/verbs/present-verbs/", id: "equative-present",
(id, link) => () => <VerbGame id={id} level="presentVerb" link={link} /> link: "/equatives/present-equative/",
); level: "present",
export const subjunctiveVerbGame = makeGameRecord( SubCore: EquativeGame,
"Write the subjunctive verb", });
"subjunctive-verbs-write", export const equativeGameHabitual = makeGameRecord({
"/verbs/subjunctive-verbs/", title: "Write the habitual equative",
(id, link) => () => <VerbGame id={id} level="subjunctiveVerb" link={link} /> id: "equative-habitual",
); link: "/equatives/habitual-equative/",
export const futureVerbGame = makeGameRecord( level: "habitual",
"Write the future verb", SubCore: EquativeGame,
"future-verbs-write", });
"/verbs/future-verbs/", export const equativeGameSubjunctive = makeGameRecord({
(id, link) => () => <VerbGame id={id} level="futureVerb" link={link} /> title: "Write the subjunctive equative",
); id: "equative-subjunctive",
export const imperativeVerbGame = makeGameRecord( link: "/equatives/other-equatives/#subjunctive-equative",
"Write the imperative verb", level: "subjunctive",
"imperative-verbs-write", SubCore: EquativeGame,
"/verbs/imperative-verbs/", });
(id, link) => () => <VerbGame id={id} level="imperative" link={link} /> export const equativeGameFuture = makeGameRecord({
); title: "Write the future equative",
export const intransitivePerfectivePastVerbGame = makeGameRecord( id: "equative-future",
"Write the intransitive simple past verb", link: "/equatives/other-equatives/#future-equative",
"intransitive-perfective-past-verbs-write", level: "future",
"/verbs/past-verbs/#past-tense-with-transitive-verbs-", SubCore: EquativeGame,
(id, link) => () => <VerbGame id={id} level="intransitivePerfectivePast" link={link} /> });
); export const equativeGamePast = makeGameRecord({
export const intransitiveImperfectivePastVerbGame = makeGameRecord( title: "Write the past equative",
"Write the intransitive continuous past verb", id: "equative-past",
"transitive-imperfective-past-verbs-write", link: "/equatives/other-equatives/#past-equative",
"/verbs/past-verbs/#past-tense-with-transitive-verbs-", level: "past",
(id, link) => () => <VerbGame id={id} level="intransitiveImperfectivePast" link={link} /> SubCore: EquativeGame,
); });
export const equativeGameWouldBe = makeGameRecord({
title: 'Write the "would be" equative',
id: "equative-would-be",
link: "/equatives/other-equatives/#would-be-equative",
level: "wouldBe",
SubCore: EquativeGame,
});
export const equativeGamePastSubjunctive = makeGameRecord({
title: 'Write the past subjunctive equative',
id: "equative-past-subjunctive",
link: "/equatives/other-equatives/#past-subjunctive",
level: "pastSubjunctive",
SubCore: EquativeGame,
});
export const equativeGameWouldHaveBeen = makeGameRecord({
title: 'Write the "would have been" equative',
id: "equative-would-have-been",
link: "/equatives/other-equatives/#wold-have-been-equative",
level: "wouldHaveBeen",
SubCore: EquativeGame,
});
export const equativeGameAllIdentify = makeGameRecord({
title: "Identify the equative (all tenses)",
id: "equative-past-summary-identify",
link: "/equatives/other-equatives",
level: "allIdentify",
SubCore: EquativeGame,
});
export const equativeGameSituations = makeGameRecord({
title: "Choose the right equative for the situation",
id: "equative-past-situations",
link: "/equatives/other-equatives",
level: "situations",
SubCore: EquativeGame,
});
export const equativeGameAllProduce = makeGameRecord({
title: "Write the equative (all tenses)",
id: "equative-past-summary-produce",
link: "/equatives/other-equatives",
level: "allProduce",
SubCore: EquativeGame,
});
export const nounGenderGame1 = makeGameRecord( // VERBS
"Identify Noun Genders - Level 1", export const presentVerbGame = makeGameRecord({
"gender-nouns-1", title: "Write the present verb",
"/nouns/nouns-gender#gender-by-ending", id: "present-verbs-write",
(id, link) => () => <GenderGame id={id} level={1} link={link} />, link: "/verbs/present-verbs/",
); level: "presentVerb",
export const nounGenderGame2 = makeGameRecord( SubCore: VerbGame,
"Identify Noun Genders - Level 2", });
"gender-nouns-2", export const subjunctiveVerbGame = makeGameRecord({
"/nouns/nouns-gender#exceptions", title: "Write the subjunctive verb",
(id, link) => () => <GenderGame id={id} level={2} link={link} />, id: "subjunctive-verbs-write",
); link: "/verbs/subjunctive-verbs/",
export const unisexNounGame = makeGameRecord( level: "subjunctiveVerb",
"Changing genders on unisex nouns", SubCore: VerbGame,
"unisex-nouns-1", });
"/nouns/nouns-unisex/", export const futureVerbGame = makeGameRecord({
(id, link) => () => <UnisexNounGame id={id} link={link} />, title: "Write the future verb",
); id: "future-verbs-write",
link: "/verbs/future-verbs/",
export const equativeGamePresent = makeGameRecord( level: "futureVerb",
"Write the present equative", SubCore: VerbGame,
"equative-present", });
"/equatives/present-equative/", export const imperativeVerbGame = makeGameRecord({
(id, link) => () => <EquativeGame id={id} link={link} level="present" />, title: "Write the imperative verb",
); id: "imperative-verbs-write",
link: "/verbs/imperative-verbs/",
export const equativeGameHabitual = makeGameRecord( level: "imperative",
"Write the habitual equative", SubCore: VerbGame,
"equative-habitual", });
"/equatives/habitual-equative/", export const intransitivePerfectivePastVerbGame = makeGameRecord({
(id, link) => () => <EquativeGame id={id} link={link} level="habitual" />, title: "Write the intransitive simple past verb",
); id: "intransitive-perfective-past-verbs-write",
link: "/verbs/past-verbs/#past-tense-with-transitive-verbs-",
export const equativeGameSubjunctive = makeGameRecord( level: "intransitivePerfectivePast",
"Write the subjunctive equative", SubCore: VerbGame,
"equative-subjunctive", });
"/equatives/other-equatives/#subjunctive-equative", export const intransitiveImperfectivePastVerbGame = makeGameRecord({
(id, link) => () => <EquativeGame id={id} link={link} level="subjunctive" />, title: "Write the intransitive continuous past verb",
); id: "transitive-imperfective-past-verbs-write",
link: "/verbs/past-verbs/#past-tense-with-transitive-verbs-",
export const equativeGameFuture = makeGameRecord( level: "intransitiveImperfectivePast",
"Write the future equative", SubCore: VerbGame,
"equative-future", });
"/equatives/other-equatives/#future-equative",
(id, link) => () => <EquativeGame id={id} link={link} level="future" />,
);
export const equativeGamePast = makeGameRecord(
"Write the past equative",
"equative-past",
"/equatives/other-equatives/#past-equative",
(id, link) => () => <EquativeGame id={id} link={link} level="past" />,
);
export const equativeGameWouldBe = makeGameRecord(
'Write the "would be" equative',
"equative-would-be",
"/equatives/other-equatives/#would-be-equative",
(id, link) => () => <EquativeGame id={id} link={link} level="wouldBe" />,
);
export const equativeGamePastSubjunctive = makeGameRecord(
'Write the past subjunctive equative',
"equative-past-subjunctive",
"/equatives/other-equatives/#past-subjunctive",
(id, link) => () => <EquativeGame id={id} link={link} level="pastSubjunctive" />,
);
export const equativeGameWouldHaveBeen = makeGameRecord(
'Write the "would have been" equative',
"equative-would-have-been",
"/equatives/other-equatives/#wold-have-been-equative",
(id, link) => () => <EquativeGame id={id} link={link} level="wouldHaveBeen" />,
);
export const equativeGameAllIdentify = makeGameRecord(
"Identify the equative (all tenses)",
"equative-past-summary-identify",
"/equatives/other-equatives",
(id, link) => () => <EquativeGame id={id} link={link} level="allIdentify" />,
);
export const equativeGameSituations = makeGameRecord(
"Choose the right equative for the situation",
"equative-past-situations",
"/equatives/other-equatives",
(id, link) => () => <EquativeGame id={id} link={link} level="situations" />,
);
export const equativeGameAllProduce = makeGameRecord(
"Write the equative (all tenses)",
"equative-past-summary-produce",
"/equatives/other-equatives",
(id, link) => () => <EquativeGame id={id} link={link} level="allProduce" />,
);
const games: { chapter: string, items: GameRecord[] }[] = [ const games: { chapter: string, items: GameRecord[] }[] = [
{ {
@ -189,3 +188,19 @@ const games: { chapter: string, items: GameRecord[] }[] = [
]; ];
export default games; export default games;
function makeGameRecord<T>({ title, id, link, level, SubCore }:{
title: string,
id: string,
link: string,
level: T,
SubCore: GameSubCore<T>,
}): GameRecord {
return {
title,
id,
Game: ({ inChapter }: { inChapter: boolean }) => (
<SubCore inChapter={inChapter} id={id} level={level} link={link} />
),
}
}

View File

@ -189,7 +189,7 @@ const pronounTypes = [
[T.Person.ThirdPlurMale, T.Person.ThirdPlurFemale], [T.Person.ThirdPlurMale, T.Person.ThirdPlurFemale],
]; ];
export default function EquativeGame({ id, link, level }: { id: string, link: string, level: T.EquativeTense | "allProduce" | "allIdentify" | "situations" }) { export default function EquativeGame({ inChapter, id, link, level }: { inChapter: boolean, id: string, link: string, level: T.EquativeTense | "allProduce" | "allIdentify" | "situations" }) {
function* questions (): Generator<Current<Question>> { function* questions (): Generator<Current<Question>> {
let pool = [...pronounTypes]; let pool = [...pronounTypes];
let situationPool = [...situations]; let situationPool = [...situations];
@ -414,6 +414,7 @@ export default function EquativeGame({ id, link, level }: { id: string, link: st
} }
return <GameCore return <GameCore
inChapter={inChapter}
studyLink={link} studyLink={link}
questions={questions} questions={questions}
id={id} id={id}

View File

@ -85,9 +85,14 @@ const exceptions: Record<string, CategorySet> = {
}; };
const amount = 35; const amount = 35;
type Question = T.DictionaryEntry;
export default function GenderGame({level, id, link }: { level: 1 | 2, id: string, link: string }) { export default function GenderGame({level, id, link, inChapter }: {
function* questions () { inChapter: boolean,
level: 1 | 2, id: string,
link: string,
}) {
function* questions (): Generator<Current<Question>> {
const wordPool = {...types}; const wordPool = {...types};
const exceptionsPool = {...exceptions}; const exceptionsPool = {...exceptions};
console.log(exceptionsPool); console.log(exceptionsPool);
@ -108,8 +113,7 @@ export default function GenderGame({level, id, link }: { level: 1 | 2, id: strin
}; };
} }
} }
function Display({ question, callback }: QuestionDisplayProps<Question>) {
function Display({ question, callback }: QuestionDisplayProps<T.DictionaryEntry>) {
function check(gender: T.Gender) { function check(gender: T.Gender) {
const nounGender: T.Gender = nounNotIn(mascNouns)(question) ? "fem" : "masc"; const nounGender: T.Gender = nounNotIn(mascNouns)(question) ? "fem" : "masc";
const correct = gender === nounGender; const correct = gender === nounGender;
@ -146,6 +150,7 @@ export default function GenderGame({level, id, link }: { level: 1 | 2, id: strin
} }
return <GameCore return <GameCore
inChapter={inChapter}
studyLink={link} studyLink={link}
questions={questions} questions={questions}
id={id} id={id}

View File

@ -27,7 +27,7 @@ const amount = 20;
type Question = { entry: T.DictionaryEntry, gender: T.Gender }; type Question = { entry: T.DictionaryEntry, gender: T.Gender };
export default function UnisexNounGame({ id, link }: { id: string, link: string }) { export default function UnisexNounGame({ id, link, inChapter }: { inChapter: boolean, id: string, link: string }) {
function* questions (): Generator<Current<Question>> { function* questions (): Generator<Current<Question>> {
let pool = { ...types }; let pool = { ...types };
for (let i = 0; i < amount; i++) { for (let i = 0; i < amount; i++) {
@ -129,6 +129,7 @@ export default function UnisexNounGame({ id, link }: { id: string, link: string
} }
return <GameCore return <GameCore
inChapter={inChapter}
studyLink={link} studyLink={link}
questions={questions} questions={questions}
id={id} id={id}

View File

@ -93,11 +93,12 @@ const secondPersons = [
type VerbGameLevel = "presentVerb" | "subjunctiveVerb" type VerbGameLevel = "presentVerb" | "subjunctiveVerb"
| "futureVerb" | "imperative" | "intransitivePerfectivePast" | "intransitiveImperfectivePast"; | "futureVerb" | "imperative" | "intransitivePerfectivePast" | "intransitiveImperfectivePast";
export default function VerbGame({ id, link, level }: { const VerbGame: GameSubCore<VerbGameLevel> = ({ id, link, level, inChapter }: {
inChapter: boolean,
id: string, id: string,
link: string, link: string,
level: VerbGameLevel, level: VerbGameLevel,
}) { }) => {
function* questions (): Generator<Current<Question>> { function* questions (): Generator<Current<Question>> {
const personPool = makePool(level === "imperative" const personPool = makePool(level === "imperative"
? secondPersons ? secondPersons
@ -248,6 +249,7 @@ export default function VerbGame({ id, link, level }: {
} }
return <GameCore return <GameCore
inChapter={inChapter}
studyLink={link} studyLink={link}
questions={questions} questions={questions}
id={id} id={id}
@ -257,6 +259,8 @@ export default function VerbGame({ id, link, level }: {
/> />
}; };
export default VerbGame;
function QuestionDisplay({ question, userAnswer }: { function QuestionDisplay({ question, userAnswer }: {
question: Question, question: Question,
userAnswer: { withBa: boolean, answer: string }, userAnswer: { withBa: boolean, answer: string },

View File

@ -8,6 +8,13 @@ type Current<T> = {
question: T, question: T,
}; };
type GameSubCore<T> = (props: {
inChapter: boolean,
id: string,
level: T,
link: string;
}) => JSX.Element;
type QuestionGenerator<T> = Generator<Current<T>, void, unknown>; type QuestionGenerator<T> = Generator<Current<T>, void, unknown>;
type QuestionDisplayProps<T> = { type QuestionDisplayProps<T> = {
@ -18,6 +25,5 @@ type QuestionDisplayProps<T> = {
type GameRecord = { type GameRecord = {
title: string, title: string,
id: string, id: string,
studyLink: string, Game: (props: { inChapter: boolean }) => JSX.Element,
Game: () => JSX.Element,
}; };