@leofavre/memory-game-component
Version:
A memory game web component.
127 lines (114 loc) • 4.57 kB
JavaScript
function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { Promise.resolve(value).then(_next, _throw); } }
function _asyncToGenerator(fn) { return function () { var self = this, args = arguments; return new Promise(function (resolve, reject) { var gen = fn.apply(self, args); function _next(value) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "next", value); } function _throw(err) { asyncGeneratorStep(gen, resolve, reject, _next, _throw, "throw", err); } _next(undefined); }); }; }
import sequence from '../helpers/sequence.js';
import shuffle from '../helpers/shuffle.js';
import waitInPromise from '../helpers/waitInPromise.js';
import { isCardRevealed, isCardMatched, isGameOver, isPairOfCardsVisible, isVisiblePairOfCardsAMatch } from './memoryGameSelectors.js';
import { DISTRIBUTE_CARDS, REVEAL_CARD, MATCH_CARDS, HIDE_CARDS, ALLOW_INTERACTION, DISALLOW_INTERACTION, SEND_EVENT } from './memoryGameConstants.js';
export var distributeCards = function distributeCards(cards, positions) {
return {
type: DISTRIBUTE_CARDS,
cards: cards,
positions: positions || shuffle(sequence(cards.length * 2))
};
};
export var revealCard = function revealCard(position) {
return {
type: REVEAL_CARD,
position: position
};
};
export var matchCards = function matchCards() {
return {
type: MATCH_CARDS
};
};
export var hideCards = function hideCards() {
return {
type: HIDE_CARDS
};
};
export var allowInteraction = function allowInteraction() {
return {
type: ALLOW_INTERACTION
};
};
export var disallowInteraction = function disallowInteraction() {
return {
type: DISALLOW_INTERACTION
};
};
export var sendEvent = function sendEvent(name, detail) {
return {
type: SEND_EVENT,
name: name,
detail: detail
};
};
export var requestPlayAsync = function requestPlayAsync(position) {
return (
/*#__PURE__*/
function () {
var _ref = _asyncToGenerator(
/*#__PURE__*/
regeneratorRuntime.mark(function _callee(dispatch, getState) {
var state, currentState, matchHappened, previouslyRevealed;
return regeneratorRuntime.wrap(function _callee$(_context) {
while (1) {
switch (_context.prev = _context.next) {
case 0:
state = getState();
currentState = state;
matchHappened = false;
if (isCardRevealed(position, state)) {
dispatch(sendEvent('error', 'Cannot reveal an already revealed card.'));
} else if (isCardMatched(position, state)) {
dispatch(sendEvent('error', 'Cannot reveal an already matched card.'));
} else if (isPairOfCardsVisible(state)) {
dispatch(sendEvent('error', 'Cannot reveal more than two cards.'));
} else {
currentState = dispatch(revealCard(position));
dispatch(sendEvent('reveal', position));
}
if (!isPairOfCardsVisible(currentState)) {
_context.next = 15;
break;
}
dispatch(disallowInteraction);
currentState = dispatch(matchCards);
if (!isVisiblePairOfCardsAMatch(currentState)) {
_context.next = 12;
break;
}
dispatch(sendEvent('match', currentState.revealed));
matchHappened = true;
_context.next = 14;
break;
case 12:
_context.next = 14;
return waitInPromise(2000)();
case 14:
if (isGameOver(currentState)) {
dispatch(disallowInteraction);
dispatch(sendEvent('finish'));
} else {
previouslyRevealed = currentState.revealed;
currentState = dispatch(hideCards);
if (!matchHappened) {
dispatch(sendEvent('hide', previouslyRevealed));
}
dispatch(allowInteraction);
}
case 15:
case "end":
return _context.stop();
}
}
}, _callee, this);
}));
return function (_x, _x2) {
return _ref.apply(this, arguments);
};
}()
);
};