-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #119 from emiljohansson/feature/spider-undo-states
Feature/spider undo states
- Loading branch information
Showing
8 changed files
with
184 additions
and
91 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import type { Card } from 'src/types/card-games' | ||
import { classNames } from 'lib/utils/string' | ||
|
||
export const Image = ({ | ||
card, | ||
cardImage, | ||
}: { | ||
card: Card | ||
cardImage: string | ||
}) => ( | ||
<img | ||
src={`/images/cards/${cardImage}.png`} | ||
alt={card?.combined ?? 'blank card'} | ||
className={classNames( | ||
` | ||
border-4 border-transparent border-solid rounded-lg | ||
relative top-0 left-0 | ||
mx-auto | ||
w-[calc(100%-0px)] | ||
`, | ||
{ | ||
'bg-primary': card?.selected, | ||
}, | ||
)} | ||
/> | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,66 @@ | ||
'use client' | ||
|
||
import shuffle from 'just-shuffle' | ||
import { Game } from './Game' | ||
import { createBaseDeck } from './createBaseDeck' | ||
import { useEffect, useState } from 'react' | ||
import { isDefined } from 'lib/utils/lang' | ||
import { Deck, Piles } from '@/types/card-games' | ||
import { restoreGameFromHash } from './state' | ||
|
||
export const metadata = { | ||
title: 'Spider Solitaire', | ||
description: 'Spider Solitaire', | ||
} | ||
// export const metadata = { | ||
// title: 'Spider Solitaire', | ||
// description: 'Spider Solitaire', | ||
// } | ||
|
||
export default function Page() { | ||
const deck = shuffle( | ||
[ | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
].flat(), | ||
) | ||
const [deck, setDeck] = useState<Deck>() | ||
const [piles, setPiles] = useState<Piles>() | ||
|
||
const initPiles = [ | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
] | ||
initPiles.forEach((pile) => { | ||
pile.slice(0, pile.length - 1).forEach((card) => { | ||
card.hidden = true | ||
useEffect(() => { | ||
const restored = restoreGameFromHash() | ||
if (isDefined(restored)) { | ||
setDeck(restored.deck) | ||
setPiles(restored.piles) | ||
return | ||
} | ||
const deck = shuffle( | ||
[ | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
createBaseDeck(), | ||
].flat(), | ||
) | ||
|
||
const initPiles = [ | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 6), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
deck.splice(0, 5), | ||
] | ||
initPiles.forEach((pile) => { | ||
pile.slice(0, pile.length - 1).forEach((card) => { | ||
card.hidden = true | ||
}) | ||
}) | ||
}) | ||
setDeck(deck) | ||
setPiles(initPiles) | ||
}, []) | ||
|
||
return <Game remainingCards={deck} initPiles={initPiles} /> | ||
return ( | ||
<> | ||
{!deck || !piles ? ( | ||
<></> | ||
) : ( | ||
<Game remainingCards={deck} initPiles={piles} /> | ||
)} | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Deck, Piles } from '@/types/card-games' | ||
import { | ||
compress, | ||
compressToBase64, | ||
decompressFromBase64, | ||
decompress, | ||
} from 'lz-string' | ||
|
||
export function saveGameToHash(deck: Deck, piles: Piles) { | ||
const data = JSON.stringify({ deck, piles }) | ||
const compressed = compress(data) | ||
const encoded = compressToBase64(compressed) | ||
window.location.hash = encoded | ||
} | ||
|
||
export function restoreGameFromHash() { | ||
const hash = window.location.hash | ||
if (hash === '') return | ||
const encoded = hash.substring(1) | ||
const decoded = decompressFromBase64(encoded) | ||
const decompressed = decompress(decoded) | ||
const { deck, piles } = JSON.parse(decompressed) | ||
return { deck, piles } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
7fc7ea7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
games – ./apps/games
games-git-main-emiljohansson.vercel.app
games-emiljohansson.vercel.app
games.emiljohansson.dev
games-ivory.vercel.app
7fc7ea7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
emiljohansson.dev – ./apps/next
emiljohanssondev-emiljohansson.vercel.app
emiljohanssondev-git-main-emiljohansson.vercel.app
emiljohansson.dev