<commit_before>import React, {
  useState,
  useCallback,
  useRef,
  useEffect,
  useContext,
} from "react";
import { StateContext } from "../../../App";
import {
  MathService as MS,
  DiceRollService,
  StatsService,
} from "../../../services";
import { Row, Col } from "../../layout";
import {
  Card,
  ButtonGroup,
  Button,
  DatumGroup,
  FieldGroup,
  SelectField,
} from "../../ui";
import { makeStatMatrix, StatMatrixArray } from "./makeStatMatrix";

export const StatsWidget = () => {
  const { actions, dispatch, forms } = useContext(StateContext);
  const values = useRef<number[]>([]);
  const [rolls, setRolls] = useState<StatMatrixArray>();
  const [isRolled, setIsRolled] = useState<boolean>(false);
  const [isLocked, setIsLocked] = useState<boolean>(false);
  const { statRollForm } = forms;

  useEffect(() => {
    setRolls(undefined);
    setIsRolled(false);
  }, [statRollForm.statRollType]);

  const rollStats = useCallback(() => {
    var rollMatrix = makeStatMatrix([6, 6]);
    values.current = [];
    setRolls(undefined);
    setIsRolled(false);

    switch (statRollForm.statRollType) {
      case "keepHighest":
        rollMatrix = makeStatMatrix([6, 6, 6]);
        break;

      case "keepNth":
        rollMatrix = makeStatMatrix([6, 6, 6, 6]);
        break;

      default:
        rollMatrix = makeStatMatrix([6, 6]);
        break;
    }
    setRolls(rollMatrix);
    setIsRolled(true);
  }, [statRollForm.statRollType]);

  const saveStats = useCallback(() => {
    setIsLocked(true);
    dispatch(actions.updateCharacterStats(values.current));
    dispatch(
      actions.setBackgroundSkillCount(
        StatsService.getModifier(values.current[4]) + 3
      )
    );
  }, [actions, dispatch, values]);

  const isStatRolled = isRolled;

  return (
    <Card
      title="Characteristics"
      body={
        <Row>
          <Col xs={12} sm={6}>
            <Card
              title="Current Characteristics"
              isSecondary
              body={
                <Row>
                  {isStatRolled && rolls ? (
                    rolls.map((obj) => {
                      let curRoll: number;
                      switch (statRollForm.statRollType) {
                        case "keepHighest":
                          curRoll = MS.sum(
                            DiceRollService.rollDiceKeepHighest(2, obj.rolls)
                          );
                          break;

                        case "keepNth":
                          curRoll = MS.sum(
                            DiceRollService.rollDiceKeepNth([0, 2], obj.rolls)
                          );
                          break;

                        default:
                          curRoll = DiceRollService.roll(obj.rolls);
                          break;
                      }
                      values.current.push(curRoll);
                      return (
                        <Col xs={12} md={6} key={"roll-" + obj.name}>
                          <DatumGroup
                            title={obj.name}
                            value={curRoll}
                            isCountable<commit_msg>text update<commit_after>import React, {
  useState,
  useCallback,
  useRef,
  useEffect,
  useContext,
} from "react";
import { StateContext } from "../../../App";
import {
  MathService as MS,
  DiceRollService,
  StatsService,
} from "../../../services";
import { Row, Col } from "../../layout";
import {
  Card,
  ButtonGroup,
  Button,
  DatumGroup,
  FieldGroup,
  SelectField,
} from "../../ui";
import { makeStatMatrix, StatMatrixArray } from "./makeStatMatrix";

export const StatsWidget = () => {
  const { actions, dispatch, forms } = useContext(StateContext);
  const values = useRef<number[]>([]);
  const [rolls, setRolls] = useState<StatMatrixArray>();
  const [isRolled, setIsRolled] = useState<boolean>(false);
  const [isLocked, setIsLocked] = useState<boolean>(false);
  const { statRollForm } = forms;

  useEffect(() => {
    setRolls(undefined);
    setIsRolled(false);
  }, [statRollForm.statRollType]);

  const rollStats = useCallback(() => {
    var rollMatrix = makeStatMatrix([6, 6]);
    values.current = [];
    setRolls(undefined);
    setIsRolled(false);

    switch (statRollForm.statRollType) {
      case "keepHighest":
        rollMatrix = makeStatMatrix([6, 6, 6]);
        break;

      case "keepNth":
        rollMatrix = makeStatMatrix([6, 6, 6, 6]);
        break;

      default:
        rollMatrix = makeStatMatrix([6, 6]);
        break;
    }
    setRolls(rollMatrix);
    setIsRolled(true);
  }, [statRollForm.statRollType]);

  const saveStats = useCallback(() => {
    setIsLocked(true);
    dispatch(actions.updateCharacterStats(values.current));
    dispatch(
      actions.setBackgroundSkillCount(
        StatsService.getModifier(values.current[4]) + 3
      )
    );
  }, [actions, dispatch, values]);

  const isStatRolled = isRolled;

  return (
    <Card
      title="Characteristics"
      body={
        <Row>
          <Col xs={12} sm={6}>
            <Card
              title={`${isLocked ? "Saved" : "Current"} Characteristics`}
              isSecondary
              body={
                <Row>
                  {isStatRolled && rolls ? (
                    rolls.map((obj) => {
                      let curRoll: number;
                      switch (statRollForm.statRollType) {
                        case "keepHighest":
                          curRoll = MS.sum(
                            DiceRollService.rollDiceKeepHighest(2, obj.rolls)
                          );
                          break;

                        case "keepNth":
                          curRoll = MS.sum(
                            DiceRollService.rollDiceKeepNth([0, 2], obj.rolls)
                          );
                          break;

                        default:
                          curRoll = DiceRollService.roll(obj.rolls);
                          break;
                      }
                      values.current.push(curRoll);
                      return (
                        <Col xs={12} md={6} key={"roll-" + obj.name}>
                          <DatumGroup
                            title={obj.name}
                            value={curRoll}
                            isCountable
{'fraction_non_alphanumeric': 0.12350157728706625, 'fraction_numerical': 0.00694006309148265, 'mean_word_length': 1.5881632653061224, 'pattern_counts': {'":': 8, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '257937', 'n_tokens_mistral': 1809, 'n_tokens_neox': 1745, 'n_words': 346}