UNPKG

@leofavre/memory-game-component

Version:

A memory game web component.

68 lines (55 loc) 1.5 kB
import { INITIAL_STATE, DISTRIBUTE_CARDS, REVEAL_CARD, MATCH_CARDS, HIDE_CARDS, ALLOW_INTERACTION, DISALLOW_INTERACTION, SEND_EVENT } from './memoryGameConstants.js'; export default (state = INITIAL_STATE, action = {}) => { switch (action.type) { case DISTRIBUTE_CARDS: const { cards, positions } = action; const nextCards = cards.map((card, index) => ({ ...card, positions: positions.slice(index * 2, index * 2 + 2) })); return { ...state, cards: nextCards }; case REVEAL_CARD: return { ...state, revealed: [...state.revealed, action.position] }; case MATCH_CARDS: const nextMatched = [ ...state.matched, ...state.cards.reduce((result, card) => [ ...result, ...card.positions.every(position => state.revealed.includes(position)) ? card.positions : [] ], []) ]; return { ...state, matched: nextMatched }; case HIDE_CARDS: return { ...state, revealed: [] }; case ALLOW_INTERACTION: return { ...state, isInteractive: true }; case DISALLOW_INTERACTION: return { ...state, isInteractive: false }; case SEND_EVENT: const event = (action.detail != null) ? { name: action.name, detail: action.detail } : { name: action.name }; return { ...state, events: [...state.events, event] }; default: return state; } };