Skip to content

Commit

Permalink
Use cached games
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-hu committed Jan 8, 2024
1 parent 4495cbc commit 5038a5f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 11 deletions.
2 changes: 1 addition & 1 deletion src/Connections/ConnectionsPlay.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ function ConnectionsPlay({ game, backTo, debug }: { game: ConnectionsGame, backT
// Save puzzleState to localStorage when categoriesState or guesses change
useEffect(() => {
if (id) {
setPuzzleState(id, guesses, categoriesState);
setPuzzleState(id, normalizeGame(game, true), guesses, categoriesState);
}
}, [categoriesState, guesses]);

Expand Down
7 changes: 6 additions & 1 deletion src/Connections/ConnectionsProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useEffect, useState, useMemo } from 'react';
import axios from 'axios';
import { Helmet } from 'react-helmet';
import { ConnectionsProviderProps, ConnectionsContext } from './ConnectionsContext';
import { ConnectionsGame } from './utils';
import { ConnectionsGame, getPuzzleState } from './utils';
import './connections.scss';

function ConnectionsProvider({ children }: ConnectionsProviderProps) {
Expand Down Expand Up @@ -30,6 +30,11 @@ function ConnectionsProvider({ children }: ConnectionsProviderProps) {
}, [loadedConnections]);

const getGame = async (id: number): Promise<ConnectionsGame> => {
const cachedPuzzle = getPuzzleState(id.toString());
if (cachedPuzzle?.game) {
return cachedPuzzle.game;
}

if (id <= nytConnections.length) {
return nytConnections[id - 1];
}
Expand Down
20 changes: 11 additions & 9 deletions src/Connections/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ export const BODIED_TEXTS = ['Damn bruh 💀', 'Down bad 😔', 'Try harder', 'S
const PUZZLE_STATES_KEY = 'puzzle_states';

type PuzzleState = {
game?: ConnectionsGame,
categoriesState?: CategoriesState,
guesses?: RecordedGuess[],
};
Expand All @@ -177,24 +178,25 @@ export const getPuzzleState = (id: string): PuzzleState => {
return {};
};

export const isSolved = (categoriesState?: CategoriesState): boolean => {
if (!categoriesState) {
return false;
}

return Object.values(categoriesState).every((category) => category.solved);
};

export const setPuzzleState = (id: string, guesses?: RecordedGuess[], categoriesState?: CategoriesState): void => {
export const setPuzzleState = (id: string, game: ConnectionsGame, guesses?: RecordedGuess[], categoriesState?: CategoriesState): void => {
const puzzleStates = getPuzzleStates();
const puzzleState = puzzleStates[id];
puzzleStates[id] = {
game,
guesses: guesses || puzzleState.guesses,
categoriesState: categoriesState || puzzleState.categoriesState,
};
window.localStorage.setItem(PUZZLE_STATES_KEY, JSON.stringify(puzzleStates));
};

export const isSolved = (categoriesState?: CategoriesState): boolean => {
if (!categoriesState) {
return false;
}

return Object.values(categoriesState).every((category) => category.solved);
};

const FIRST_DAY = new Date(2023, 5, 12);

export const daysBetween = (now: Date, then: Date): number => {
Expand Down

0 comments on commit 5038a5f

Please sign in to comment.