Skip to content

Commit a6f0c6f

Browse files
committed
Refactor loading logic
1 parent caf3f10 commit a6f0c6f

File tree

4 files changed

+191
-55
lines changed

4 files changed

+191
-55
lines changed

docs/notes.txt

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
http://localhost:3000
2+
3+
4+
12/7
5+
#Wordle536 3/6* Grade: B
6+
⬛⬛⬛⬛⬛ AMBER F
7+
🟨🟨⬛⬛⬛ STING D
8+
🟩🟩🟩🟩🟩 JOUST A+
9+
http://localhost:3000/#SLv0YX9thxeBvJKz
10+
11+
12+
12/6
13+
#Wordle535 6/6* Grade: F
14+
15+
⬛⬛⬛🟩⬛ WOKEN F
16+
⬛⬛🟨🟩⬛ STREP F
17+
⬛🟨⬛🟩⬛ CRUEL F
18+
⬛⬛⬛🟩🟩 HIDER F
19+
⬛🟨🟨🟩🟩 GAMER A
20+
🟩🟩🟩🟩🟩 AMBER A+
21+
http://localhost:3000/#aliBRW82gb5wbiv4fRX95pqbGZx73
22+
23+
24+
CRANE (Fail)
25+
#Wordle X/6 Grade: F
26+
27+
🟨🟨🟨⬛⬛ ||`RENTS`|| B-
28+
⬛🟩⬛⬛⬛ ||`PROUD`|| F
29+
🟨⬛⬛🟨⬛ ||`ADIEU`|| C
30+
⬛⬛⬛⬛⬛ ||`PLOTS`|| F
31+
⬛🟨⬛⬛⬛ ||`HELLO`|| F
32+
🟨⬛⬛⬛🟩 ||`APPLE`|| C
33+
34+
http://localhost:3000/#1l2K15JcbqhQ1YPBC7Sh9YVx836rq2o
35+
36+
37+
JOUST
38+
#Wordle 5/6* Grade: C
39+
40+
⬛⬛⬛⬛⬛ >!CRANE!< F
41+
⬛🟩⬛⬛🟨 >!FOLDS!< C
42+
🟨🟩⬛⬛⬛ >!SORRY!< F
43+
🟩🟩⬛🟩🟩 >!JOIST!< A+
44+
🟩🟩🟩🟩🟩 >!JOUST!< A+
45+
46+
http://localhost:3000/#fEUfOHgXn3lDGsviZjvXEv
47+
48+
49+
12/1 (Fail)
50+
#Wordle530 X/6 Grade: F
51+
52+
⬛⬛⬛⬛⬛ >!FARMS!< F
53+
🟨⬛⬛⬛🟨 >!CRANE!< C
54+
⬛⬛⬛⬛🟩 >!PLANT!< C
55+
⬛⬛⬛⬛🟩 >!BOOST!< F
56+
⬛⬛⬛⬛⬛ >!MOLDY!< F
57+
⬛⬛⬛⬛⬛ >!FAILS!< F
58+
59+
http://localhost:3000/#alipBEpkg48knhPGWs8KvzCs1aMee

src/components/AnswerPicker.vue

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const props = defineProps<{
1212
}>();
1313
1414
const emit = defineEmits<{
15-
(e: 'update:date', value: Date | undefined): void,
16-
(e: 'update:answer', value: string): void,
15+
(e: 'updated', answer: string, date: Date | undefined): void,
1716
}>();
1817
1918
// Toggle between setting answer by date or manually inputting
@@ -32,6 +31,7 @@ function setManualInput() {
3231
3332
// Set initial type and values
3433
onMounted(() => {
34+
// console.log("AnswerPicker onMounted", props.date, props.answer);
3535
if (props.date) {
3636
inputType.value = "date";
3737
dateInput.value = isoDateString(props.date);
@@ -40,9 +40,6 @@ onMounted(() => {
4040
manualInput.value = props.answer;
4141
// Set the date input's value to today so it doesn't throw "invalid date" error
4242
dateInput.value = isoDateString(new Date());
43-
} else {
44-
// Set today's date as the default value
45-
dateInput.value = isoDateString(new Date());
4643
}
4744
});
4845
@@ -86,12 +83,26 @@ async function maxDateTimer() {
8683
}
8784
8885
function updateMaxDate() {
89-
const newDate = maxDateWithAnswer();
86+
let max;
87+
88+
// Set the maxDate to today unless our answer list hasn't been updated
89+
const today = new Date();
90+
const maxAnswerDate = maxDateWithAnswer();
91+
92+
if (maxAnswerDate >= today) {
93+
max = today;
94+
} else {
95+
max = maxAnswerDate;
96+
}
97+
98+
// If there is a dateMax in localStorage, which is the number of days to offset the maxDate, then adjust
9099
const dateMax = localStorage.getItem('dateMax');
91100
if (dateMax !== null) {
92-
newDate.setDate(newDate.getDate() + Number(dateMax))
101+
max.setDate(max.getDate() + Number(dateMax))
93102
}
94-
maxDate.value = newDate;
103+
104+
// Update the maxDate
105+
maxDate.value = max;
95106
}
96107
97108
onMounted(async () => {
@@ -148,13 +159,11 @@ const inputError = computed(() => {
148159
}
149160
});
150161
151-
watch(pickedAnswer, (answer) => {
152-
emit("update:answer", answer);
162+
watch([pickedAnswer, pickedDate], ([newPickedAnswer, newPickedDate]) => {
163+
// console.log("AnswerPicker emit updated", newPickedAnswer, newPickedDate)
164+
emit("updated", newPickedAnswer, newPickedDate);
153165
});
154166
155-
watch(pickedDate, (date) => {
156-
emit("update:date", date);
157-
});
158167
159168
watch(() => props.answer, (newAnswer, oldAnswer) => {
160169
if (inputType.value == "manual" && newAnswer) {

src/components/MainInterface.vue

Lines changed: 108 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<script setup lang="ts">
2-
import { ref, reactive, onBeforeMount, onMounted, onUnmounted, computed, watch } from 'vue';
2+
import { ref, reactive, onBeforeMount, onMounted, onUnmounted, computed, watch, nextTick } from 'vue';
33
import { Guess } from '@/guess';
44
import { encodeShareCode, decodeShareCode } from '@/encoding';
55
import type { ShareData } from '@/encoding';
@@ -246,15 +246,20 @@ function guessClicked(guess?: Guess) {
246246
}
247247
248248
// Load answer, date, guesses in share-code
249-
function loadFromShareCode(shareCode: string) {
249+
function loadFromShareCode(shareCode: string): boolean {
250+
loading = true;
251+
nextTick(() => { loading = false; });
252+
250253
let shareData: ShareData | undefined;
251254
try {
252255
shareData = decodeShareCode(shareCode);
253256
} catch(e) {
254257
console.error("Invalid shareCode", shareCode, e);
255-
return;
258+
return false;
256259
}
257260
261+
console.log("Decoded share code", shareCode, shareData);
262+
258263
// Remove existing guesses
259264
resetGuesses();
260265
@@ -274,68 +279,108 @@ function loadFromShareCode(shareCode: string) {
274279
for (let word of shareData.words) {
275280
appendWord(word);
276281
}
282+
283+
return true;
277284
}
278285
279-
function loadFromWindowLocation() {
286+
function loadFromWindowLocation(): boolean {
280287
if (window.location.hash != "") {
281288
let shareCode = window.location.hash;
282289
if (shareCode.slice(0, 1) == "#") {
283290
shareCode = shareCode.slice(1);
284291
}
292+
console.log("Found share code in hash", shareCode);
285293
286-
loadFromShareCode(shareCode);
294+
return loadFromShareCode(shareCode);
287295
}
296+
297+
return false;
288298
}
289299
290-
// If there is a hash in the URL, try to load it as a share code
300+
function loadPuzzleFromStorage(): boolean {
301+
loading = true;
302+
nextTick(() => { loading = false; });
303+
304+
if (puzzleDate.value) {
305+
const puzzleState = loadPuzzleByDate(puzzleDate.value);
306+
if (puzzleState) {
307+
console.log("Loading share code", puzzleState.shareCode, "for date", puzzleDate.value);
308+
return loadFromShareCode(puzzleState.shareCode);
309+
} else {
310+
resetGuesses();
311+
console.log("No share code found for date", puzzleDate.value, dateIndex(puzzleDate.value));
312+
return false;
313+
}
314+
} else if (puzzleAnswer.value) {
315+
const puzzleState = loadPuzzleByAnswer(puzzleAnswer.value);
316+
if (puzzleState) {
317+
console.log("Loading share code", puzzleState.shareCode, "for answer", puzzleAnswer.value);
318+
return loadFromShareCode(puzzleState.shareCode);
319+
} else {
320+
resetGuesses();
321+
console.log("No share code found for answer", puzzleAnswer.value);
322+
return false;
323+
}
324+
} else {
325+
console.log("No puzzle answer or date set, resetting guesses");
326+
resetGuesses();
327+
return false;
328+
}
329+
}
330+
331+
// If there is a hash in the URL, try to load it as a share code, otherwise try to load from localStorage
291332
onBeforeMount(() => {
292-
loadFromWindowLocation();
293-
loading = false;
333+
let loaded = false;
334+
335+
// Try loading from a hash in the URL
336+
loaded = loadFromWindowLocation();
337+
338+
// If that didn't work, set the puzzleDate to today and try loading from localStorage
339+
// NOTE: We don't set the puzzleAnswer, as that will be done by AnswerPicker
340+
if (!loaded) {
341+
puzzleDate.value = new Date();
342+
loaded = loadPuzzleFromStorage();
343+
}
344+
345+
// If that didn't work, reset the guesses
346+
if (!loaded) {
347+
resetGuesses();
348+
}
294349
});
295350
296351
// Update the URL bar when the shareURL changes
297352
watch(shareURL, (newShareURL, oldShareURL) => {
298-
if (loading) return;
299-
// console.log(`replaceState ${oldShareURL} -> ${newShareURL}`);
300-
history.replaceState(null, "", newShareURL);
353+
// if (loading) return;
354+
if (newShareURL !== window.location.toString()) {
355+
console.log(`replaceState ${window.location} -> ${newShareURL}`);
356+
history.replaceState(null, "", newShareURL);
357+
}
301358
})
302359
303360
// Detect if the URL hash changes and attempt to load it as a share code
304361
window.addEventListener("popstate", (e) => {
305-
loading = true;
362+
console.log("popstate", window.location);
306363
loadFromWindowLocation();
307-
loading = false;
308364
});
309365
310-
// If the answer changes attempt to load the list of guesses from local storage, or else just reset the list
311-
watch(puzzleAnswer, () => {
366+
// When the user interacts with the AnswerPicker, attempt to load the list of guesses from local storage,
367+
// or else just reset the list
368+
function answerPickerUpdated(answer: string, date?: Date) {
369+
console.log("AnswerPicker updated", answer, date)
370+
puzzleAnswer.value = answer;
371+
puzzleDate.value = date;
312372
if (!loading) {
313-
loading = true;
314-
if (puzzleDate.value) {
315-
const puzzleState = loadPuzzleByDate(puzzleDate.value);
316-
if (puzzleState) {
317-
loadFromShareCode(puzzleState.shareCode);
318-
console.log("Loaded share code", puzzleState.shareCode, "for date", puzzleDate.value);
319-
} else {
320-
resetGuesses();
321-
console.log("No share code found for date", puzzleDate.value);
322-
}
323-
} else if (puzzleAnswer.value) {
324-
const puzzleState = loadPuzzleByAnswer(puzzleAnswer.value);
325-
if (puzzleState) {
326-
loadFromShareCode(puzzleState.shareCode);
327-
console.log("Loaded share code", puzzleState.shareCode, "for answer", puzzleAnswer.value);
328-
} else {
329-
resetGuesses();
330-
console.log("No share code found for answer", puzzleAnswer.value);
331-
}
332-
} else {
333-
console.log("No puzzle answer or date set, resetting guesses");
334-
resetGuesses();
335-
}
336-
loading = false;
373+
loadPuzzleFromStorage();
337374
}
338-
});
375+
}
376+
377+
// watch(puzzleDate, (newPuzzleDate, oldPuzzleDate) => {
378+
// console.log(`puzzleDate changed ${oldPuzzleDate} -> ${newPuzzleDate} puzzleAnswer: ${puzzleAnswer.value}`);
379+
// });
380+
381+
// watch(puzzleAnswer, (newPuzzleAnswer, oldPuzzleAnswer) => {
382+
// console.log(`puzzleAnswer changed ${oldPuzzleAnswer} -> ${newPuzzleAnswer} puzzleDate: ${puzzleDate.value}`);
383+
// });
339384
340385
// Change title when the puzzleDate or puzzleAnswer change
341386
const title = computed(() => {
@@ -352,6 +397,17 @@ watch(title, (newTitle) => {
352397
document.title = newTitle;
353398
});
354399
400+
// Debug stuff
401+
const puzzleDebug = ref(false);
402+
403+
onMounted(() => {
404+
window.addEventListener('keyup', (e) => {
405+
if (e.key == "?") {
406+
puzzleDebug.value = !puzzleDebug.value;
407+
}
408+
})
409+
});
410+
355411
</script>
356412

357413
<template>
@@ -367,12 +423,17 @@ watch(title, (newTitle) => {
367423
<div class="page input-page" :class="{ show: isInputPage }"
368424
:style="{ transform: inputPageTransform, transition: dragTransition }">
369425
<div class="tools">
370-
<AnswerPicker v-model:date="puzzleDate" v-model:answer="puzzleAnswer" />
426+
<AnswerPicker :date="puzzleDate" :answer="puzzleAnswer" @updated="answerPickerUpdated" />
371427
<button class="button icon px-3" @click.stop="resetGuesses" title="Reset">
372428
<IconTrashCanUndo />
373429
</button>
374430
</div>
375431

432+
<div v-if="puzzleDebug" class="puzzle-debug">
433+
puzzleAnswer: {{ puzzleAnswer }}<br />
434+
puzzleDate: {{ puzzleDate }}<br />
435+
</div>
436+
376437
<PuzzleInput v-if="puzzleAnswer" :guesses="guesses" :selectedGuess="selectedGuess" :puzzleAnswer="puzzleAnswer || ''"
377438
:puzzleDate="puzzleDate" @appendGuess="appendGuess" @removeGuess="removeGuess"
378439
@guessClicked="guessClicked" :shareCode="shareCode" :shareURL="shareURL" />
@@ -398,6 +459,12 @@ watch(title, (newTitle) => {
398459
background: var(--color-background);
399460
}
400461
462+
.puzzle-debug {
463+
background: var(--gray-5);
464+
padding: .5em;
465+
margin-bottom: 1em;
466+
}
467+
401468
.tools {
402469
display: flex;
403470
flex-direction: row;

src/game.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export function dateIndex(d: Date = new Date()): number {
1010
export function maxDateWithAnswer(): Date {
1111
return new Date(startDate.valueOf() + ((answers.list.length - 1) * 24 * 60 * 60 * 1000));
1212
}
13-
// (window as any).maxDate = maxDate;
13+
// (window as any).maxDateWithAnswer = maxDateWithAnswer;
1414

1515
export function wordIndex(d: Date = new Date()): number {
1616
const di = dateIndex(d);
@@ -48,6 +48,7 @@ export function getWordByDayOffset(offset: number, today: Date = new Date()): st
4848
const d = getDateByDayOffset(offset, today);
4949
return getWord(d);
5050
}
51+
// (window as any).getWordByDayOffset = getWordByDayOffset
5152

5253
export function letterGrade(grade: number): string {
5354
if (grade >= 0.97) {

0 commit comments

Comments
 (0)