Skip to content

Commit

Permalink
Merge pull request #154 from hydralauncher/feat/format-playtime-in-hours
Browse files Browse the repository at this point in the history
Feat/format playtime in hours
  • Loading branch information
Hydra authored May 3, 2024
2 parents 473d487 + 2ae3f95 commit d7b92b4
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"copied_link_to_clipboard": "Link copied",
"hours": "hours",
"minutes": "minutes",
"amount_hours": "{{amount}} hours",
"amount_minutes": "{{amount}} minutes",
"accuracy": "{{accuracy}}% accuracy",
"add_to_library": "Add to library",
"remove_from_library": "Remove from library",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/es/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"copied_link_to_clipboard": "Enlace copiado",
"hours": "horas",
"minutes": "minutos",
"amount_hours": "{{amount}} horas",
"amount_minutes": "{{amount}} minutos",
"accuracy": "{{accuracy}}% precisión",
"add_to_library": "Agregar a la biblioteca",
"remove_from_library": "Eliminar de la biblioteca",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/fr/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
"copied_link_to_clipboard": "Lien copié",
"hours": "heures",
"minutes": "minutes",
"amount_hours": "{{amount}} heures",
"amount_minutes": "{{amount}} minutes",
"accuracy": "{{accuracy}}% précision",
"add_to_library": "Ajouter à la bibliothèque",
"remove_from_library": "Supprimer de la bibliothèque",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/hu/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@
"copied_link_to_clipboard": "Link másolva",
"hours": "óra",
"minutes": "perc",
"amount_hours": "{{amount}} óra",
"amount_minutes": "{{amount}} perc",
"accuracy": "{{accuracy}}% pontosság",
"add_to_library": "Hozzáadás a könyvtárhoz",
"remove_from_library": "Eltávolítás a könyvtárból",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/it/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
"copied_link_to_clipboard": "Link copiato",
"hours": "ore",
"minutes": "minuti",
"amount_hours": "{{amount}} ore",
"amount_minutes": "{{amount}} minuti",
"accuracy": "{{accuratezza}}% di accuratezza",
"add_to_library": "Aggiungi alla libreria",
"remove_from_library": "Rimuovi dalla libreria",
Expand Down
2 changes: 2 additions & 0 deletions src/locales/pt/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
"copied_link_to_clipboard": "Link copiado",
"hours": "horas",
"minutes": "minutos",
"amount_hours": "{{amount}} horas",
"amount_minutes": "{{amount}} minutos",
"accuracy": "{{accuracy}}% de precisão",
"add_to_library": "Adicionar à biblioteca",
"remove_from_library": "Remover da biblioteca",
Expand Down
8 changes: 4 additions & 4 deletions src/main/services/process-watcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,18 @@ import { WindowManager } from "./window-manager";
const sleep = (ms: number) => new Promise((resolve) => setTimeout(resolve, ms));

export const startProcessWatcher = async () => {
const sleepTime = 300;
const sleepTime = 500;
const gamesPlaytime = new Map<number, number>();

// eslint-disable-next-line no-constant-condition
while (true) {
await sleep(sleepTime);

const games = await gameRepository.find({
where: {
executablePath: Not(IsNull()),
},
});

if (games.length == 0) {
if (games.length === 0) {
continue;
}

Expand Down Expand Up @@ -71,5 +69,7 @@ export const startProcessWatcher = async () => {
}
}
}

await sleep(sleepTime);
}
};
51 changes: 30 additions & 21 deletions src/renderer/src/pages/game-details/hero/hero-panel.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { format } from "date-fns";
import { useCallback, useEffect, useMemo, useState } from "react";
import { useEffect, useMemo, useState } from "react";
import { useTranslation } from "react-i18next";

import { useDownload } from "@renderer/hooks";
Expand All @@ -22,6 +22,8 @@ export interface HeroPanelProps {
getGame: () => void;
}

const MAX_MINUTES_TO_SHOW_IN_PLAYTIME = 120;

export function HeroPanel({
game,
gameDetails,
Expand All @@ -30,7 +32,7 @@ export function HeroPanel({
getGame,
isGamePlaying,
}: HeroPanelProps) {
const { t } = useTranslation("game_details");
const { t, i18n } = useTranslation("game_details");

const [showBinaryNotFoundModal, setShowBinaryNotFoundModal] = useState(false);
const [lastTimePlayed, setLastTimePlayed] = useState("");
Expand All @@ -48,29 +50,36 @@ export function HeroPanel({
} = useDownload();
const isGameDownloading = isDownloading && gameDownloading?.id === game?.id;

const updateLastTimePlayed = useCallback(() => {
setLastTimePlayed(
formatDistance(game.lastTimePlayed, new Date(), {
addSuffix: true,
})
);
}, [game?.lastTimePlayed, formatDistance]);

useEffect(() => {
if (game?.lastTimePlayed) {
updateLastTimePlayed();

const interval = setInterval(() => {
updateLastTimePlayed();
}, 1000);
setLastTimePlayed(
formatDistance(game.lastTimePlayed, new Date(), {
addSuffix: true,
})
);
}
}, [game?.lastTimePlayed, formatDistance]);

return () => {
clearInterval(interval);
};
const numberFormatter = useMemo(() => {
return new Intl.NumberFormat(i18n.language, {
maximumFractionDigits: 1,
});
}, [i18n]);

const formatPlayTime = () => {
const milliseconds = game?.playTimeInMilliseconds || 0;
const seconds = milliseconds / 1000;
const minutes = seconds / 60;

if (minutes < MAX_MINUTES_TO_SHOW_IN_PLAYTIME) {
return t("amount_minutes", {
amount: minutes.toFixed(0),
});
}

return () => {};
}, [game?.lastTimePlayed, updateLastTimePlayed]);
const hours = minutes / 60;
return t("amount_hours", { amount: numberFormatter.format(hours) });
};

const finalDownloadSize = useMemo(() => {
if (!game) return "N/A";
Expand Down Expand Up @@ -139,7 +148,7 @@ export function HeroPanel({
<>
<p>
{t("play_time", {
amount: formatDistance(0, game.playTimeInMilliseconds),
amount: formatPlayTime(game.playTimeInMilliseconds),
})}
</p>

Expand Down

0 comments on commit d7b92b4

Please sign in to comment.