From aa282f8971c89c19dbb7be2c9f0608a14f4a5955 Mon Sep 17 00:00:00 2001 From: Balint Morasz Date: Wed, 26 Jul 2023 23:35:43 +0200 Subject: [PATCH] refactor a bit --- src/components/TulajdonsagInput.tsx | 44 +++++++ src/domain-models/faj.ts | 9 +- src/domain-models/kockak.ts | 8 ++ src/domain-models/tulajdonsag.ts | 104 +++++++++++++++ src/pages/CreateCharacter.tsx | 193 +++++++++------------------- 5 files changed, 215 insertions(+), 143 deletions(-) create mode 100644 src/components/TulajdonsagInput.tsx create mode 100644 src/domain-models/kockak.ts create mode 100644 src/domain-models/tulajdonsag.ts diff --git a/src/components/TulajdonsagInput.tsx b/src/components/TulajdonsagInput.tsx new file mode 100644 index 0000000..540cce4 --- /dev/null +++ b/src/components/TulajdonsagInput.tsx @@ -0,0 +1,44 @@ +import React from "react"; + +import {Faj, FajLabel} from "../domain-models/faj"; +import {Modifier} from "../domain-models/tulajdonsag"; + +export function SignedNumberToText(val: number) : string { + return val > 0 ? '+'+val : val.toString() +} + +function fajiModositoText(faj: Faj, szamolas: (f: Faj) => number) : string { + const mod = szamolas(faj) + return (mod !== 0) ? SignedNumberToText(mod) + ' (' + FajLabel(faj) + ')' : '' +} + +function TulajdonsagInput(props: { + tulajdonsag: string, + currentFaj : () => Faj, + fajiModosito: (faj: Faj) => number, + register: () => any, + getCurrentValue: () => number +}) { + const {tulajdonsag, register, currentFaj, getCurrentValue, fajiModosito} = props; + return (
+ +
+ +
+ + {fajiModositoText(currentFaj(), fajiModosito)} + + + Összesen: { (getCurrentValue() + (fajiModosito(currentFaj()))).toString() } + + + Módosító: { SignedNumberToText(Modifier(getCurrentValue() + fajiModosito(currentFaj()))) } + + {getCurrentValue() < 3 && ( + Túl gyenge vagy, nem bírtad felemelni a kezed a jelentkezéshez!)} + {getCurrentValue() > 18 && ( + Szét szakadtak az izmaid!)} +
) +} + +export default TulajdonsagInput; \ No newline at end of file diff --git a/src/domain-models/faj.ts b/src/domain-models/faj.ts index 4523634..380e25b 100644 --- a/src/domain-models/faj.ts +++ b/src/domain-models/faj.ts @@ -1,11 +1,4 @@ -export enum Tulajdonsag { - Ero = 't_ero', - Ugyesseg = 't_ugy', - Egeszseg = 't_egs', - Intelligencia = 't_int', - Bolcsesseg = 't_bol', - Karizma = 't_kar', -} + export enum Faj { Ember = 'f_ember', diff --git a/src/domain-models/kockak.ts b/src/domain-models/kockak.ts new file mode 100644 index 0000000..b008c32 --- /dev/null +++ b/src/domain-models/kockak.ts @@ -0,0 +1,8 @@ + +export function getRandomInt(max: number) { + return Math.floor(Math.random() * max) + 1; +} + +export function d6() { + return getRandomInt(6) +} diff --git a/src/domain-models/tulajdonsag.ts b/src/domain-models/tulajdonsag.ts new file mode 100644 index 0000000..16b5d2f --- /dev/null +++ b/src/domain-models/tulajdonsag.ts @@ -0,0 +1,104 @@ +import {d6} from "./kockak"; +import {Faj} from "./faj"; + +export enum Tulajdonsag { + Ero = 't_ero', + Ugyesseg = 't_ugy', + Egeszseg = 't_egs', + Intelligencia = 't_int', + Bolcsesseg = 't_bol', + Karizma = 't_kar', +} + + +function rollAbility() { + let min = 6; + let sum = 0; + let rolls: number[] = [] + for (let i = 0; i < 4; i++) { + const roll = d6(); + if (roll < min) { + min = roll; + } + sum += roll; + rolls = [...rolls, roll] + } + const result = sum - min; + console.log("Rolled: " + rolls + " got: " + result) + return result; +} + +export function RollAllAbilities(setValue: (ability: string, value: number) => void) { + setValue(Tulajdonsag.Ero, rollAbility()) + setValue(Tulajdonsag.Ugyesseg, rollAbility()) + setValue(Tulajdonsag.Egeszseg, rollAbility()) + setValue(Tulajdonsag.Intelligencia, rollAbility()) + setValue(Tulajdonsag.Bolcsesseg, rollAbility()) + setValue(Tulajdonsag.Karizma, rollAbility()) +} + +export const Modifier = (val : number) => Math.floor(val / 3) - 3 + +/* eslint-disable */ /* tslint:disable */ +function tulajdonsagModositokPerFaj(tul: Tulajdonsag, faj: Faj) : number { + switch (faj) { + case Faj.Birodalmi: switch (tul) { + case Tulajdonsag.Egeszseg: return -1; + case Tulajdonsag.Intelligencia: return +1; + default: return 0; + } + case Faj.Eszaki: switch (tul) { + case Tulajdonsag.Ero: return +1; + case Tulajdonsag.Bolcsesseg: return -1; + default: return 0; + } + case Faj.Etuniai: switch (tul) { + case Tulajdonsag.Egeszseg: return +1; + case Tulajdonsag.Bolcsesseg: return -1; + default: return 0 + } + case Faj.Osember: switch (tul) { + case Tulajdonsag.Ero: return +1; + case Tulajdonsag.Egeszseg: return +1; + case Tulajdonsag.Intelligencia: return -1; + case Tulajdonsag.Bolcsesseg: return -1; + default: return 0 + } + case Faj.Elf: switch (tul) { + case Tulajdonsag.Egeszseg: return -1; + case Tulajdonsag.Ugyesseg: return +1; + default: return 0; + } + case Faj.Felork: switch (tul) { + case Tulajdonsag.Ero: return +1; + case Tulajdonsag.Egeszseg: return +1; + case Tulajdonsag.Karizma: return -2; + default: return 0; + } + case Faj.Felszerzet: switch (tul) { + case Tulajdonsag.Ero: return -1; + case Tulajdonsag.Ugyesseg: return +1; + default: return 0; + } + case Faj.Gnom: switch (tul) { + case Tulajdonsag.Ero: return -1; + case Tulajdonsag.Intelligencia: return +1; + default: return 0; + } + case Faj.Torpe: switch (tul) { + case Tulajdonsag.Egeszseg: return +1; + case Tulajdonsag.Karizma: return -1; + default: return 0; + } + + default: return 0; + } +} + +export function TulajdonsagModositokFajokra(tul: Tulajdonsag) { + return (faj: Faj) => tulajdonsagModositokPerFaj(tul, faj); +} + +export function FajiTulajdonsagModositok(faj: Faj) { + return (tul: Tulajdonsag) => tulajdonsagModositokPerFaj(tul, faj) +} \ No newline at end of file diff --git a/src/pages/CreateCharacter.tsx b/src/pages/CreateCharacter.tsx index ef5c974..03c54c9 100644 --- a/src/pages/CreateCharacter.tsx +++ b/src/pages/CreateCharacter.tsx @@ -1,7 +1,8 @@ -import React, {ReactNode} from 'react'; +import React from 'react'; import './CreateCharacter.css' import {useForm} from "react-hook-form"; -import {Faj, FajDescription, FajLabel, FajSpecials, Tulajdonsag} from "../domain-models/faj"; +import {Faj, FajDescription, FajLabel, FajSpecials} from "../domain-models/faj"; +import {RollAllAbilities, Tulajdonsag, TulajdonsagModositokFajokra} from "../domain-models/tulajdonsag"; import { Osztaly, OsztalyDescription, @@ -9,62 +10,7 @@ import { OsztalyProperties, OsztalySpecialSkills } from "../domain-models/osztaly" - -function getRandomInt(max: number) { - return Math.floor(Math.random() * max) + 1; -} - -function rollAbility() { - let min = 6; - let sum = 0; - let rolls: number[] = [] - for (let i = 0; i < 4; i++) { - const roll = getRandomInt(6); - if (roll < min) { - min = roll; - } - sum += roll; - rolls = [...rolls, roll] - } - const result = sum - min; - console.log("Rolled: " + rolls + " got: " + result) - return result; -} - -const modifier = (val : number) => Math.floor(val / 3) - 3 - -const fajiEroModosito = (faj: Faj) => [Faj.Felork, Faj.Osember, Faj.Eszaki].includes(faj) ? +1 : [Faj.Gnom, Faj.Felszerzet].includes(faj) ? -1 : 0 - -const fajiUgyModosito = (faj: Faj) => [Faj.Elf, Faj.Felszerzet].includes(faj) ? +1 : 0 - -const fajiEgsModosito = (faj: Faj) => [Faj.Etuniai, Faj.Osember, Faj.Felork, Faj.Torpe].includes(faj) ? +1 : - [Faj.Birodalmi, Faj.Elf].includes(faj) ? -1 : 0 - -const fajiIntModosito = (faj: Faj) => [Faj.Gnom, Faj.Birodalmi].includes(faj) ? +1 : - [Faj.Osember].includes(faj) ? -1 : 0 - -const fajiBolModosito = (faj: Faj) => [Faj.Osember, Faj.Eszaki, Faj.Etuniai].includes(faj) ? -1 : 0 - -const fajiKarModosito = (faj: Faj) => [Faj.Torpe].includes(faj) ? -1 : - [Faj.Felork].includes(faj) ? -2 : 0 - -function signedNumberToText(val: number) : string { - return val > 0 ? '+'+val : val.toString() -} - -function fajiModositoText(faj: Faj, szamolas: (f: Faj) => number) : string { - const mod = szamolas(faj) - return (mod !== 0) ? signedNumberToText(mod) + ' (' + FajLabel(faj) + ')' : '' -} - -function rollAllAbilities(setValue: (ability: string, value: number) => void) { - setValue(Tulajdonsag.Ero, rollAbility()) - setValue(Tulajdonsag.Ugyesseg, rollAbility()) - setValue(Tulajdonsag.Egeszseg, rollAbility()) - setValue(Tulajdonsag.Intelligencia, rollAbility()) - setValue(Tulajdonsag.Bolcsesseg, rollAbility()) - setValue(Tulajdonsag.Karizma, rollAbility()) -} +import TulajdonsagInput from "../components/TulajdonsagInput"; const tulajdonsagDefaultOptions = { required: true, @@ -72,28 +18,6 @@ const tulajdonsagDefaultOptions = { max: 18, }; -function displayAbilityInput(tulajdonsag: string, currentFaj : () => Faj, fajiModosito: (faj: Faj) => number, register: () => any, getCurrentValue: () => number) : ReactNode { - return (
- -
- -
- - {fajiModositoText(currentFaj(), fajiModosito)} - - - Összesen: { (getCurrentValue() + (fajiModosito(currentFaj()))).toString() } - - - Módosító: { signedNumberToText(modifier(getCurrentValue() + fajiModosito(currentFaj()))) } - - {getCurrentValue() < 3 && ( - Túl gyenge vagy, nem bírtad felemelni a kezed a jelentkezéshez!)} - {getCurrentValue() > 18 && ( - Szét szakadtak az izmaid!)} -
) -} - function CreateCharacterPage() { const { register, @@ -162,52 +86,52 @@ function CreateCharacterPage() {
Tulajdonságok
- {displayAbilityInput( - 'Erő', - currentFaj, - fajiEroModosito, - () => register(Tulajdonsag.Ero, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Ero, 10)) - )} - {displayAbilityInput( - 'Ügyesség', - currentFaj, - fajiUgyModosito, - () => register(Tulajdonsag.Ugyesseg, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Ugyesseg, 10)) - )} - {displayAbilityInput( - 'Egészség', - currentFaj, - fajiEgsModosito, - () => register(Tulajdonsag.Egeszseg, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Egeszseg, 10)) - )} - {displayAbilityInput( - 'Intelligencia', - currentFaj, - fajiIntModosito, - () => register(Tulajdonsag.Intelligencia, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Intelligencia, 10)) - )} - {displayAbilityInput( - 'Bölcsesség', - currentFaj, - fajiBolModosito, - () => register(Tulajdonsag.Bolcsesseg, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Bolcsesseg, 10)) - )} - {displayAbilityInput( - 'Karizma', - currentFaj, - fajiKarModosito, - () => register(Tulajdonsag.Karizma, tulajdonsagDefaultOptions), - () => Number(watch(Tulajdonsag.Karizma, 10)) - )} + Number(watch(Tulajdonsag.Ero, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Ero)} + register={() => register(Tulajdonsag.Ero, tulajdonsagDefaultOptions)} + /> + Number(watch(Tulajdonsag.Ugyesseg, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Ugyesseg)} + register={() => register(Tulajdonsag.Ugyesseg, tulajdonsagDefaultOptions)} + /> + Number(watch(Tulajdonsag.Egeszseg, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Egeszseg)} + register={() => register(Tulajdonsag.Egeszseg, tulajdonsagDefaultOptions)} + /> + Number(watch(Tulajdonsag.Intelligencia, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Intelligencia)} + register={() => register(Tulajdonsag.Intelligencia, tulajdonsagDefaultOptions)} + /> + Number(watch(Tulajdonsag.Bolcsesseg, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Bolcsesseg)} + register={() => register(Tulajdonsag.Bolcsesseg, tulajdonsagDefaultOptions)} + /> + Number(watch(Tulajdonsag.Karizma, 10))} + currentFaj={currentFaj} + fajiModosito={TulajdonsagModositokFajokra(Tulajdonsag.Karizma)} + register={() => register(Tulajdonsag.Karizma, tulajdonsagDefaultOptions)} + />
Tanult
@@ -216,20 +140,19 @@ function CreateCharacterPage() { @@ -268,8 +191,8 @@ function CreateCharacterPage() {
-
- +
+