aboutsummaryrefslogtreecommitdiffstats
path: root/src/components/Game/index.tsx
blob: 657b2af11f021a3d80738e0975b6064fb254c7a2 (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
import React from "react";

import { GuessType } from "../../types/guess";
import { Song } from "../../types/song";
import { playTimes } from "../../constants";

import { Button, Guess, YTPlayer, Search, Result, Player } from "../";

import * as Styled from "./index.styled";

interface Props {
  guesses: GuessType[];
  todaysSolution: Song;
  dailyDate?: string;
  currentTry: number;
  didGuess: boolean;
  setSelectedSong: React.Dispatch<React.SetStateAction<Song | undefined>>;
  skip: () => void;
  guess: () => void;
  mode?: "daily" | "unlimited";
  onPlayAgain?: () => void;
}

function getUtcDate() {
  return new Date().toISOString().split("T")[0];
}

export function Game({
  guesses,
  todaysSolution,
  dailyDate,
  currentTry,
  didGuess,
  setSelectedSong,
  skip,
  guess,
  mode = "daily",
  onPlayAgain,
}: Props) {
  const recentFinishedPlay = localStorage.getItem("recentFinishedPlay");
  const hasFinishedCurrentRound = didGuess || currentTry >= guesses.length;
  const hasFinishedResponseDaily =
    mode === "daily" && !!dailyDate && recentFinishedPlay === dailyDate;
  const isGameOver = hasFinishedCurrentRound || hasFinishedResponseDaily;
  const isBlocked =
    mode === "daily" &&
    !!dailyDate &&
    !hasFinishedResponseDaily &&
    new Date(getUtcDate()) > new Date(dailyDate);
  const sessionDate = dailyDate ?? getUtcDate();

  React.useEffect(() => {
    if (mode !== "daily") return;
    if (!hasFinishedCurrentRound) return;
    localStorage.setItem("recentFinishedPlay", sessionDate);

    const historicalPlayData = localStorage.getItem("historicalPlayData");
    if (historicalPlayData === null) {
      localStorage.setItem(
        "historicalPlayData",
        JSON.stringify({
          sessionDates: [sessionDate],
          guesses: [currentTry],
          didGuess: [didGuess],
        })
      );
    } else {
      const parsedData = JSON.parse(historicalPlayData);
      if (parsedData.sessionDates.includes(sessionDate)) return;
      localStorage.setItem(
        "historicalPlayData",
        JSON.stringify({
          sessionDates: [...parsedData.sessionDates, sessionDate],
          guesses: [...parsedData.guesses, currentTry],
          didGuess: [...parsedData.didGuess, didGuess],
        })
      );
    }
  }, [mode, hasFinishedCurrentRound, sessionDate, currentTry, didGuess]);

  if (isBlocked) {
    return <h1>Daily MIXX is not available yet. Check back soon!</h1>;
  }

  if (isGameOver) {
    return (
      <Result
        didGuess={didGuess}
        currentTry={currentTry}
        todaysSolution={todaysSolution}
        guesses={guesses}
        mode={mode}
        sessionDate={sessionDate}
        onPlayAgain={onPlayAgain}
      />
    );
  }

  return (
    <>
      {guesses.map((guess: GuessType, index) => (
        <Guess key={index} guess={guess} active={index === currentTry} />
      ))}
      {mode === "unlimited" ? (
        <YTPlayer id={todaysSolution.youtubeId} currentTry={currentTry} />
      ) : (
        <Player currentTry={currentTry} />
      )}

      <Search currentTry={currentTry} setSelectedSong={setSelectedSong} />
      <Styled.Buttons>
        <Button onClick={skip}>
          {currentTry === 5
            ? "Give Up"
            : `Skip +${playTimes[currentTry] / 1000}s`}
        </Button>
        <Button variant="green" onClick={guess}>
          Submit
        </Button>
      </Styled.Buttons>
    </>
  );
}
send patches to the email below
yukais@pinapelz.com
include the subject [PATCH repo_name]
pinapelz.com
homepage