2023-01-23 20:26:50 +00:00
|
|
|
<script lang="ts">
|
2023-01-24 21:12:19 +00:00
|
|
|
import type { PageData } from "./$types.js";
|
2023-01-26 21:49:40 +00:00
|
|
|
import { format as formatDate } from "date-fns";
|
|
|
|
|
import { onMount } from "svelte";
|
2023-01-27 23:12:06 +00:00
|
|
|
import { spring } from "svelte/motion";
|
|
|
|
|
import { fade } from "svelte/transition";
|
2023-01-24 21:12:19 +00:00
|
|
|
import { writable } from "svelte/store";
|
2023-01-26 21:49:40 +00:00
|
|
|
import Navbar from "$lib/components/Navbar.svelte";
|
|
|
|
|
|
2023-01-28 09:00:10 +00:00
|
|
|
import MetaTags from "./MetaTags.svelte";
|
2023-01-28 13:51:42 +00:00
|
|
|
import OptionsSection from "./OptionsSection.svelte";
|
|
|
|
|
import ScoreCardSection from "./ScoreCardSection.svelte";
|
2023-01-26 21:49:40 +00:00
|
|
|
import type { ISunriseSunsetGuessingHistory } from "./ISunriseSunsetGuessingHistory.js";
|
|
|
|
|
|
|
|
|
|
let hasGuessingHistoryBeenLoaded = false;
|
2023-01-28 13:51:42 +00:00
|
|
|
let debug = true;
|
2023-01-27 23:12:06 +00:00
|
|
|
let visibleNotification: "none" | "success" | "failure" = "none";
|
|
|
|
|
|
2023-01-26 21:49:40 +00:00
|
|
|
const guessingHistory = writable<ISunriseSunsetGuessingHistory>({
|
|
|
|
|
mostRecentGuessDate: undefined,
|
|
|
|
|
totalNumberOfGuesses: 0,
|
|
|
|
|
guesses: [],
|
|
|
|
|
correctDays: [],
|
|
|
|
|
incorrectDays: []
|
|
|
|
|
});
|
2023-01-24 21:12:19 +00:00
|
|
|
|
2023-01-27 23:12:06 +00:00
|
|
|
const notificationSpring = spring(20, {
|
|
|
|
|
stiffness: 0.1,
|
|
|
|
|
damping: 0.15,
|
|
|
|
|
precision: 0.01
|
|
|
|
|
});
|
|
|
|
|
|
2023-01-24 21:12:19 +00:00
|
|
|
export let data: PageData;
|
2023-01-26 21:49:40 +00:00
|
|
|
const now = new Date();
|
|
|
|
|
const todaysDateString = formatDate(now, "yyyy-MM-dd");
|
|
|
|
|
const localStorageKey = "sunrise-sunset-guessing-history";
|
2023-01-24 21:12:19 +00:00
|
|
|
|
|
|
|
|
$: picture = data.body.photo;
|
|
|
|
|
|
2023-01-27 23:12:06 +00:00
|
|
|
function debugRemoveLocalStorage() {
|
|
|
|
|
notificationSpring.set(20);
|
|
|
|
|
localStorage.removeItem(localStorageKey);
|
|
|
|
|
visibleNotification = "none";
|
|
|
|
|
guessingHistory.set({
|
|
|
|
|
mostRecentGuessDate: undefined,
|
|
|
|
|
totalNumberOfGuesses: 0,
|
|
|
|
|
guesses: [],
|
|
|
|
|
correctDays: [],
|
|
|
|
|
incorrectDays: []
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function revealNotification(wasCorrect: boolean) {
|
|
|
|
|
notificationSpring.set(0);
|
|
|
|
|
if (wasCorrect) {
|
|
|
|
|
visibleNotification = "success";
|
|
|
|
|
} else {
|
|
|
|
|
visibleNotification = "failure";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-28 13:51:42 +00:00
|
|
|
function onOptionSelected(event: { option: "sunrise" | "sunset" }) {
|
|
|
|
|
const { option } = event;
|
2023-01-26 21:49:40 +00:00
|
|
|
$guessingHistory.mostRecentGuessDate = todaysDateString;
|
|
|
|
|
$guessingHistory.totalNumberOfGuesses += 1;
|
|
|
|
|
$guessingHistory.guesses = [...$guessingHistory.guesses, option];
|
|
|
|
|
|
2023-01-24 21:12:19 +00:00
|
|
|
if (option === picture.sunrise_or_sunset) {
|
2023-01-27 23:12:06 +00:00
|
|
|
revealNotification(true);
|
2023-01-26 21:49:40 +00:00
|
|
|
$guessingHistory.correctDays = [
|
|
|
|
|
...$guessingHistory.correctDays,
|
|
|
|
|
todaysDateString
|
|
|
|
|
];
|
2023-01-24 21:12:19 +00:00
|
|
|
} else {
|
2023-01-27 23:12:06 +00:00
|
|
|
revealNotification(false);
|
2023-01-26 21:49:40 +00:00
|
|
|
$guessingHistory.incorrectDays = [
|
|
|
|
|
...$guessingHistory.incorrectDays,
|
|
|
|
|
todaysDateString
|
|
|
|
|
];
|
2023-01-24 21:12:19 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-01-26 21:49:40 +00:00
|
|
|
|
|
|
|
|
guessingHistory.subscribe((value) => {
|
|
|
|
|
if (!hasGuessingHistoryBeenLoaded) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
localStorage.setItem(localStorageKey, JSON.stringify(value));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
onMount(() => {
|
|
|
|
|
const storedGuessHistory = localStorage.getItem(localStorageKey);
|
|
|
|
|
if (storedGuessHistory) {
|
|
|
|
|
guessingHistory.set(JSON.parse(storedGuessHistory));
|
|
|
|
|
}
|
|
|
|
|
hasGuessingHistoryBeenLoaded = true;
|
|
|
|
|
});
|
2023-01-23 20:26:50 +00:00
|
|
|
</script>
|
|
|
|
|
|
2023-01-28 09:03:22 +00:00
|
|
|
<svelte:head>
|
|
|
|
|
<MetaTags />
|
|
|
|
|
</svelte:head>
|
2023-01-28 09:00:10 +00:00
|
|
|
|
2023-01-24 21:12:19 +00:00
|
|
|
<Navbar />
|
|
|
|
|
|
2023-01-26 21:49:40 +00:00
|
|
|
<div class="page">
|
|
|
|
|
<section class="header">
|
|
|
|
|
<h1 class="header__title">Sunrise, Sunset?</h1>
|
|
|
|
|
<p class="header__explanation">
|
2023-01-27 23:12:06 +00:00
|
|
|
It's a simple game. Is the picture below a sunrise, or a sunset?
|
2023-01-24 21:12:19 +00:00
|
|
|
</p>
|
2023-01-26 21:49:40 +00:00
|
|
|
</section>
|
|
|
|
|
|
2023-01-27 23:12:06 +00:00
|
|
|
{#if debug}
|
|
|
|
|
<button on:click={debugRemoveLocalStorage}>Remove Local Storage</button>
|
|
|
|
|
{/if}
|
|
|
|
|
|
2023-01-26 21:49:40 +00:00
|
|
|
<section class="picture">
|
|
|
|
|
<img src={picture.small_url} alt="Sunrise or Sunset?" />
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
{#if hasGuessingHistoryBeenLoaded}
|
2023-01-27 23:12:06 +00:00
|
|
|
<section class="notification">
|
|
|
|
|
{#if visibleNotification !== "none"}
|
|
|
|
|
<div
|
|
|
|
|
class="notification--success"
|
|
|
|
|
transition:fade={{ duration: 200 }}
|
|
|
|
|
style="transform: translateY({$notificationSpring}px);"
|
|
|
|
|
>
|
|
|
|
|
{visibleNotification === "success" ? "Correct 🎉" : "Incorrect 💔"}
|
|
|
|
|
</div>
|
|
|
|
|
{/if}
|
|
|
|
|
</section>
|
2023-01-26 21:49:40 +00:00
|
|
|
|
2023-01-28 13:51:42 +00:00
|
|
|
<OptionsSection
|
|
|
|
|
isDisabled={$guessingHistory.mostRecentGuessDate === todaysDateString}
|
|
|
|
|
hasAlreadyGuessedToday={$guessingHistory.mostRecentGuessDate ===
|
|
|
|
|
todaysDateString}
|
|
|
|
|
on:optionSelected={(event) => {
|
|
|
|
|
onOptionSelected(event.detail);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
|
|
|
|
|
<ScoreCardSection
|
|
|
|
|
doesUserHaveGuessingHistory={$guessingHistory.mostRecentGuessDate !==
|
|
|
|
|
undefined}
|
|
|
|
|
correctGuessCount={$guessingHistory.correctDays.length}
|
|
|
|
|
incorrectGuessCount={$guessingHistory.incorrectDays.length}
|
|
|
|
|
/>
|
2023-01-24 21:12:19 +00:00
|
|
|
{/if}
|
2023-01-26 21:49:40 +00:00
|
|
|
</div>
|
2023-01-23 20:26:50 +00:00
|
|
|
|
|
|
|
|
<style lang="scss" type="text/postcss">
|
2023-01-26 21:49:40 +00:00
|
|
|
:root {
|
|
|
|
|
--background-colour: hsl(40, 8%, 85%);
|
|
|
|
|
--colour-light-grey: hsl(28, 35%, 85%);
|
|
|
|
|
--colour-grey: hsl(20, 6%, 10%);
|
|
|
|
|
--colour-grey-lightened: hsl(20, 7%, 22%);
|
|
|
|
|
--colour-orage: hsl(19, 69%, 49%);
|
2023-01-27 23:12:06 +00:00
|
|
|
--colour-green: hsl(120, 69%, 49%);
|
2023-01-26 21:49:40 +00:00
|
|
|
--colour-orange-lightened: hsl(19, 75%, 55%);
|
|
|
|
|
--colour-dark-grey: #1b1918;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.page {
|
|
|
|
|
background-color: var(--background-colour);
|
|
|
|
|
display: grid;
|
|
|
|
|
min-height: calc(100vh - 64px);
|
|
|
|
|
grid-template-rows: max-content max-content max-content auto;
|
|
|
|
|
gap: 12px;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:12:19 +00:00
|
|
|
.header {
|
|
|
|
|
display: grid;
|
|
|
|
|
place-content: center;
|
2023-01-26 21:49:40 +00:00
|
|
|
text-align: center;
|
2023-01-24 21:12:19 +00:00
|
|
|
padding: 12px 0px;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-26 21:49:40 +00:00
|
|
|
.header__title {
|
|
|
|
|
font-size: 2rem;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.header__explanation {
|
|
|
|
|
font-size: 1.2rem;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-24 21:12:19 +00:00
|
|
|
.picture {
|
|
|
|
|
display: grid;
|
|
|
|
|
place-content: center;
|
|
|
|
|
padding: 12px 0px;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.picture img {
|
|
|
|
|
max-width: 100%;
|
|
|
|
|
max-height: 100%;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-27 23:12:06 +00:00
|
|
|
.notification {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
.notification--success {
|
|
|
|
|
color: var(--colour-dark-grey);
|
|
|
|
|
padding: 12px;
|
|
|
|
|
text-align: center;
|
|
|
|
|
font-size: 1.2rem;
|
|
|
|
|
}
|
2023-01-23 20:26:50 +00:00
|
|
|
</style>
|