-
Notifications
You must be signed in to change notification settings - Fork 1
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 #12 from JollyGrin/feat/hand-actions
Feat/hand actions
- Loading branch information
Showing
4 changed files
with
172 additions
and
24 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,62 @@ | ||
import { GRIDS } from "@/components/organisms/GameBoard/constants"; | ||
import { GameState } from "@/types/card"; | ||
|
||
/** | ||
* Return a card from hand to the top of the deck | ||
* */ | ||
export function actHandToTopDeck( | ||
state: GameState, | ||
deckType: "deck" | "atlas", | ||
handCardIndex: number, | ||
) { | ||
// Create a shallow copy of the previous grid items array | ||
const newState = [...state]; | ||
|
||
// choose between deck or atlas deck | ||
const GRIDS_DECK_TYPE = deckType === "deck" ? GRIDS.DECK : GRIDS.ATLAS_DECK; | ||
|
||
// Make a copy of the deck and hand arrays, preserving their positions | ||
const newDeck = [...newState[GRIDS_DECK_TYPE]]; | ||
const newHand = [...newState[GRIDS.HAND]]; | ||
|
||
// Pop a card from the hand and push it to the deck | ||
const [card] = newHand.splice(handCardIndex, 1); | ||
if (card) newDeck.push(card); | ||
|
||
// Update the newState array | ||
newState[GRIDS_DECK_TYPE] = newDeck; | ||
newState[GRIDS.HAND] = newHand; | ||
|
||
// Return the updated newState array to trigger a re-render | ||
return newState; | ||
} | ||
|
||
/** | ||
* Return a card from hand to the top of the deck | ||
* */ | ||
export function actHandToBottomDeck( | ||
state: GameState, | ||
deckType: "deck" | "atlas", | ||
handCardIndex: number, | ||
) { | ||
// Create a shallow copy of the previous grid items array | ||
const newState = [...state]; | ||
|
||
// choose between deck or atlas deck | ||
const GRIDS_DECK_TYPE = deckType === "deck" ? GRIDS.DECK : GRIDS.ATLAS_DECK; | ||
|
||
// Make a copy of the deck and hand arrays, preserving their positions | ||
const newDeck = [...newState[GRIDS_DECK_TYPE]]; | ||
const newHand = [...newState[GRIDS.HAND]]; | ||
|
||
// Pop a card from the hand and push it to the deck | ||
const [card] = newHand.splice(handCardIndex, 1); | ||
if (card) newDeck.unshift(card); | ||
|
||
// Update the newState array | ||
newState[GRIDS_DECK_TYPE] = newDeck; | ||
newState[GRIDS.HAND] = newHand; | ||
|
||
// Return the updated newState array to trigger a re-render | ||
return newState; | ||
} |