aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Chart/index.tsx
blob: 6aac91e4792b267d24992f13614216046dad3ffa (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import {
  Chart as ChartJS,
  CategoryScale,
  LinearScale,
  BarElement,
  Tooltip,
  Legend,
} from "chart.js";
import type { ChartOptions } from "chart.js";
import { Bar } from "react-chartjs-2";

ChartJS.register(
  CategoryScale,
  LinearScale,
  BarElement,
  Tooltip,
  Legend
);

interface Props {
  currentTry: number;
  didGuess: boolean;
  sessionDate: string;
}

interface HistoricalPlayData {
  sessionDates?: string[];
  guesses?: number[];
  didGuess?: boolean[];
}

type GuessCount = 1 | 2 | 3 | 4 | 5 | 6;
type Distribution = Record<GuessCount, number> & { NS: number };

function getHistoricalPlayData(): HistoricalPlayData {
  try {
    return JSON.parse(localStorage.getItem("historicalPlayData") || "{}");
  } catch {
    return {};
  }
}

function addResultToDistribution(
  distribution: Distribution,
  guessCount: number,
  didGuess: boolean
) {
  if (!didGuess) {
    distribution.NS += 1;
    return;
  }

  if (guessCount >= 1 && guessCount <= 6) {
    distribution[guessCount as GuessCount] += 1;
  }
}

export default function GuessDistributionChart({
  currentTry,
  didGuess,
  sessionDate,
}: Props) {
  const historicalPlayData = getHistoricalPlayData();

  const distribution: Distribution = {
    1: 0,
    2: 0,
    3: 0,
    4: 0,
    5: 0,
    6: 0,
    NS: 0,
  };

  if (historicalPlayData.guesses && historicalPlayData.didGuess) {
    historicalPlayData.guesses.forEach((guessCount, index) => {
      addResultToDistribution(
        distribution,
        guessCount,
        historicalPlayData.didGuess?.[index] ?? false
      );
    });
  }

  if (!historicalPlayData.sessionDates?.includes(sessionDate)) {
    addResultToDistribution(distribution, currentTry, didGuess);
  }

  const data = {
    labels: ["1", "2", "3", "4", "5", "6", "NS"],
    datasets: [
      {
        data: [
          distribution[1],
          distribution[2],
          distribution[3],
          distribution[4],
          distribution[5],
          distribution[6],
          distribution.NS,
        ],
        backgroundColor: "#6aaa64",
        borderRadius: 4,
      },
    ],
  };

  const options: ChartOptions<"bar"> = {
    responsive: true,
    plugins: {
      legend: {
        display: false,
      },
      tooltip: {
        enabled: true,
      },
    },
    scales: {
      x: {
        grid: {
          display: false,
        },
      },
      y: {
        beginAtZero: true,
        ticks: {
          precision: 0,
        },
      },
    },
  };

  return (
    <div style={{ maxWidth: 500, width: "100%", marginBottom: "20px" }}>
      <h3>Statistics</h3>
      <Bar data={data} options={options} />
    </div>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage