Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding duration time in stats data #54

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useEffect, useRef, useState } from "react";
import { intervalToDuration } from "date-fns";

import Board from "./Board";
import Keyboard from "./Keyboard";
Expand Down Expand Up @@ -48,6 +49,7 @@ export default function App(props: Props) {
return;
}

game.state.startedAt = game.state.startedAt ?? new Date().getTime();
game.setState({
...game.state,
answers: game.state.answers.map((answer, i) => {
Expand Down Expand Up @@ -214,6 +216,10 @@ export default function App(props: Props) {
},
currentStreak,
maxStreak: Math.max(stats.maxStreak, currentStreak),
duration: intervalToDuration({
start: new Date(game.state.startedAt),
end: new Date(game.state.lastCompletedDate || new Date().getTime()),
}),
});
} else if (game.state.attempt === 5) {
if (typeof game.submitAnswer === "function") {
Expand Down Expand Up @@ -241,6 +247,10 @@ export default function App(props: Props) {
},
currentStreak: 0,
maxStreak: stats.maxStreak,
duration: intervalToDuration({
start: new Date(game.state.startedAt),
end: new Date(game.state.lastCompletedDate || new Date().getTime()),
}),
});

const failureMessage = getFailureMessage(
Expand Down
42 changes: 36 additions & 6 deletions components/StatsModal.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import useSWR from "swr";
import { useEffect, useState } from "react";
import { useTheme } from "next-themes";
import { Duration } from "date-fns";

import Modal from "./Modal";

Expand Down Expand Up @@ -90,6 +91,9 @@ export default function StatsModal(props: Props) {
return text;
}

text += `\nDurasi: ${stats.duration.hours}:${pad0(
stats.duration.minutes
)}:${pad0(stats.duration.seconds)}`;
text += "\n" + window.location.href;
return text;
}
Expand Down Expand Up @@ -144,6 +148,13 @@ export default function StatsModal(props: Props) {
ctx.fillStyle = resolvedTheme === "dark" ? "#ffffff" : "#111827";
ctx.fillText(text, canvas.width / 2, 300);
ctx.font = "32px sans-serif";
ctx.fillText(
`Durasi: ${stats.duration.hours}:${pad0(stats.duration.minutes)}:${pad0(
stats.duration.seconds
)}`,
canvas.width / 2,
canvas.height - 300
);
ctx.fillText("katla.vercel.app", canvas.width / 2, canvas.height - 150);

answerStates.forEach((states, y) => {
Expand Down Expand Up @@ -278,7 +289,10 @@ export default function StatsModal(props: Props) {
<WordDefinition answer={answer} />
<div className="flex items-center justify-between w-3/4 m-auto my-8 space-x-2">
{props.remainingTime ? (
<TimeCounter time={props.remainingTime} />
<TimeCounter
time={props.remainingTime}
duration={props.stats.duration}
/>
) : (
<div />
)}
Expand Down Expand Up @@ -413,17 +427,33 @@ function WordDefinition({ answer }) {
);
}

function TimeCounter({ time }: { time: ReturnType<typeof useRemainingTime> }) {
function TimeCounter({
time,
duration,
}: {
time: ReturnType<typeof useRemainingTime>;
duration: Duration;
}) {
const remainingTime = `${time.hours}:${pad0(time.minutes)}:${pad0(
time.seconds
)}`;

const finishedTime = `${duration.hours}:${pad0(duration.minutes)}:${pad0(
duration.seconds
)}`;

return (
<div className="text-center flex flex-1 flex-col">
<div className="font-semibold uppercase text-xs md:text-md">
Katla berikutnya
<div>
<div className="text-center flex flex-1 flex-col mb-6">
<div className="font-semibold uppercase text-xs md:text-md">Durasi</div>
<div className="text-xl md:text-4xl">{finishedTime}</div>
</div>
<div className="text-center flex flex-1 flex-col">
<div className="font-semibold uppercase text-xs md:text-md">
Katla berikutnya
</div>
<div className="text-xl md:text-4xl">{remainingTime}</div>
</div>
<div className="text-xl md:text-4xl">{remainingTime}</div>
</div>
);
}
1 change: 1 addition & 0 deletions pages/arsip/[num].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const initialStats: GameStats = {
},
currentStreak: 0,
maxStreak: 0,
duration: null,
};

export default function Arsip(props: Props) {
Expand Down
1 change: 1 addition & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const initialStats: GameStats = {
},
currentStreak: 0,
maxStreak: 0,
duration: null,
};

const useStats = createStoredState<GameStats>(GAME_STATS_KEY);
Expand Down
1 change: 1 addition & 0 deletions pages/lawan.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ const initialStats: GameStats = {
},
currentStreak: 0,
maxStreak: 0,
duration: null,
};

interface MainProps {
Expand Down
1 change: 1 addition & 0 deletions utils/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export const initialState: GameState = {
enableFreeEdit: false,
enableLiarMode: false,
lieBoxes: [],
startedAt: null,
};

export const useGamePersistedState =
Expand Down
3 changes: 3 additions & 0 deletions utils/types.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Duration } from "date-fns";
import { Dispatch, SetStateAction } from "react";

export type AnswerState = "c" | "e" | "w";
Expand All @@ -12,6 +13,7 @@ export interface GameState {
enableFreeEdit: boolean;
enableLiarMode: boolean;
lieBoxes: ForcedResult[];
startedAt: number | null;
}

export interface GameStats {
Expand All @@ -26,6 +28,7 @@ export interface GameStats {
};
currentStreak: number;
maxStreak: number;
duration: Duration | null;
}

export interface Game<T = GameState> {
Expand Down