@leofavre/memory-game-component
Version:
A memory game web component.
66 lines (55 loc) • 1.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _memoryGameConstants = require("./memoryGameConstants.js");
var _default = (state = _memoryGameConstants.INITIAL_STATE, action = {}) => {
switch (action.type) {
case _memoryGameConstants.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 _memoryGameConstants.REVEAL_CARD:
return { ...state,
revealed: [...state.revealed, action.position]
};
case _memoryGameConstants.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 _memoryGameConstants.HIDE_CARDS:
return { ...state,
revealed: []
};
case _memoryGameConstants.ALLOW_INTERACTION:
return { ...state,
isInteractive: true
};
case _memoryGameConstants.DISALLOW_INTERACTION:
return { ...state,
isInteractive: false
};
case _memoryGameConstants.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;
}
};
exports.default = _default;