Skip to content

Commit

Permalink
Fixes for runaway errors and missing hit dice
Browse files Browse the repository at this point in the history
  • Loading branch information
gvorbeck committed Oct 6, 2024
1 parent b97b54e commit f146c36
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 57 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"bfrpgEdition": "4th",
"bfrpgRelease": "137",
"license": "CC BY-SA 4.0",
"version": "2.13.1",
"version": "2.13.3",
"private": true,
"type": "module",
"scripts": {
Expand Down
56 changes: 2 additions & 54 deletions src/components/PageNewCharacter/StepHitPoints/StepHitPoints.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import {
CharData,
CharDataAction,
ClassNames,
DiceTypes,
RaceNames,
} from "@/data/definitions";
import { CharData, CharDataAction, DiceTypes } from "@/data/definitions";
import {
Button,
Flex,
Expand All @@ -17,59 +11,13 @@ import {
import { rollDice } from "@/support/diceSupport";
import React from "react";
import { getClassType } from "@/support/classSupport";
import { races } from "@/data/races";
import { classes } from "@/data/classes";
import { getCharacterHitDiceFromClass } from "@/support/statSupport";

interface StepHitPointsProps {
character: CharData;
characterDispatch: React.Dispatch<CharDataAction>;
}

function getCharacterHitDiceFromClass(character: CharData) {
const diceArr = [
DiceTypes.D3,
DiceTypes.D4,
DiceTypes.D6,
DiceTypes.D8,
DiceTypes.D10,
DiceTypes.D12,
DiceTypes.D20,
];
const { race } = character;
const classType = getClassType(character.class);
// Some races require the character's hit dice to be incremented or decremented
const incrementChecker = (dice: DiceTypes) => {
// The index of the character's hit die in the diceArr
let diceIndex = diceArr.indexOf(dice);
// The max index a character's hit die is allowed to be in the diceArr
const diceMaxIndex = diceArr.indexOf(
races[race as RaceNames]?.maximumHitDice ?? DiceTypes.D20,
);
if (races[race as RaceNames]?.incrementHitDie) {
diceIndex++;
}
if (races[race as RaceNames]?.decrementHitDie) {
diceIndex--;
}
// If a character's hit die is greater than the max allowed hit die, set it to the max allowed hit die
if (diceIndex > diceMaxIndex) {
diceIndex = diceMaxIndex;
}
return diceArr[diceIndex];
};
if (classType[0] === "combination") {
if (character.class.includes(ClassNames.FIGHTER)) {
return incrementChecker(DiceTypes.D6);
}
if (character.class.includes(ClassNames.THIEF)) {
return incrementChecker(DiceTypes.D4);
}
} else if (classType[0] === "standard") {
return incrementChecker(classes[character.class[0] as ClassNames].hitDice);
}
return (character.hp.dice as DiceTypes) || undefined;
}

const StepHitPoints: React.FC<
StepHitPointsProps & React.ComponentPropsWithRef<"div">
> = ({ className, character, characterDispatch }) => {
Expand Down
4 changes: 3 additions & 1 deletion src/hooks/useCharacterData.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { User } from "firebase/auth";
import { characterReducer, emptyCharacter } from "@/support/characterSupport";

export function useCharacterData(user: User | null) {
const [disabled, setDisabled] = React.useState(false);
const { uid, id } = useParams();
const [character, characterDispatch] = React.useReducer(
characterReducer,
Expand All @@ -21,13 +22,14 @@ export function useCharacterData(user: User | null) {
}, [uid, id]);

React.useEffect(() => {
if (uid && id && character.race) {
if (uid && id && character.race && !disabled) {
updateDocument({
collection: "users",
docId: uid,
subCollection: "characters",
subDocId: id,
data: { ...character },
setDisabled,
});
}
}, [uid, id, character]);
Expand Down
4 changes: 3 additions & 1 deletion src/support/accountSupport.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,8 @@ export const updateDocument = async ({
subCollection,
subDocId,
data,
}: UpdatePayload) => {
setDisabled,
}: UpdatePayload & any) => {
if (!docId) {
console.error("Document ID is undefined");
return;
Expand All @@ -170,6 +171,7 @@ export const updateDocument = async ({
try {
await updateDoc(docRef, data);
} catch (error) {
setDisabled(true);
console.warn(data);
console.error("Error updating document: ", error);
}
Expand Down
49 changes: 49 additions & 0 deletions src/support/statSupport.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
CharData,
ClassNames,
DiceTypes,
EquipmentCategories,
EquipmentItem,
RaceNames,
Expand Down Expand Up @@ -268,6 +269,9 @@ export const getHitDice = (
character: CharData,
dice: string,
) => {
if (!dice) {
dice = getCharacterHitDiceFromClass(character) as string;
}
const dieType = dice.split("d")[1].split("+")[0];
const prefix = Math.min(level, 9);

Expand All @@ -280,6 +284,51 @@ export const getHitDice = (
return result;
};

export function getCharacterHitDiceFromClass(character: CharData) {
const diceArr = [
DiceTypes.D3,
DiceTypes.D4,
DiceTypes.D6,
DiceTypes.D8,
DiceTypes.D10,
DiceTypes.D12,
DiceTypes.D20,
];
const { race } = character;
const classType = getClassType(character.class);
// Some races require the character's hit dice to be incremented or decremented
const incrementChecker = (dice: DiceTypes) => {
// The index of the character's hit die in the diceArr
let diceIndex = diceArr.indexOf(dice);
// The max index a character's hit die is allowed to be in the diceArr
const diceMaxIndex = diceArr.indexOf(
races[race as RaceNames]?.maximumHitDice ?? DiceTypes.D20,
);
if (races[race as RaceNames]?.incrementHitDie) {
diceIndex++;
}
if (races[race as RaceNames]?.decrementHitDie) {
diceIndex--;
}
// If a character's hit die is greater than the max allowed hit die, set it to the max allowed hit die
if (diceIndex > diceMaxIndex) {
diceIndex = diceMaxIndex;
}
return diceArr[diceIndex];
};
if (classType[0] === "combination") {
if (character.class.includes(ClassNames.FIGHTER)) {
return incrementChecker(DiceTypes.D6);
}
if (character.class.includes(ClassNames.THIEF)) {
return incrementChecker(DiceTypes.D4);
}
} else if (classType[0] === "standard") {
return incrementChecker(classes[character.class[0] as ClassNames].hitDice);
}
return (character.hp.dice as DiceTypes) || undefined;
}

export const getSavingThrows = (className: string, level: number) =>
classes[className as ClassNames]?.savingThrows.find(
(savingThrow) => (savingThrow[0] as number) >= level,
Expand Down

0 comments on commit f146c36

Please sign in to comment.