UNPKG

@leofavre/memory-game-component

Version:

A memory game web component.

168 lines (132 loc) 5.5 kB
"use strict"; 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); }); }; } Object.defineProperty(exports, "__esModule", { value: true }); exports.requestPlayAsync = exports.sendEvent = exports.disallowInteraction = exports.allowInteraction = exports.hideCards = exports.matchCards = exports.revealCard = exports.distributeCards = void 0; var _sequence = _interopRequireDefault(require("../helpers/sequence.js")); var _shuffle = _interopRequireDefault(require("../helpers/shuffle.js")); var _waitInPromise = _interopRequireDefault(require("../helpers/waitInPromise.js")); var _memoryGameSelectors = require("./memoryGameSelectors.js"); var _memoryGameConstants = require("./memoryGameConstants.js"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } var distributeCards = function distributeCards(cards, positions) { return { type: _memoryGameConstants.DISTRIBUTE_CARDS, cards: cards, positions: positions || (0, _shuffle.default)((0, _sequence.default)(cards.length * 2)) }; }; exports.distributeCards = distributeCards; var revealCard = function revealCard(position) { return { type: _memoryGameConstants.REVEAL_CARD, position: position }; }; exports.revealCard = revealCard; var matchCards = function matchCards() { return { type: _memoryGameConstants.MATCH_CARDS }; }; exports.matchCards = matchCards; var hideCards = function hideCards() { return { type: _memoryGameConstants.HIDE_CARDS }; }; exports.hideCards = hideCards; var allowInteraction = function allowInteraction() { return { type: _memoryGameConstants.ALLOW_INTERACTION }; }; exports.allowInteraction = allowInteraction; var disallowInteraction = function disallowInteraction() { return { type: _memoryGameConstants.DISALLOW_INTERACTION }; }; exports.disallowInteraction = disallowInteraction; var sendEvent = function sendEvent(name, detail) { return { type: _memoryGameConstants.SEND_EVENT, name: name, detail: detail }; }; exports.sendEvent = sendEvent; 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 ((0, _memoryGameSelectors.isCardRevealed)(position, state)) { dispatch(sendEvent('error', 'Cannot reveal an already revealed card.')); } else if ((0, _memoryGameSelectors.isCardMatched)(position, state)) { dispatch(sendEvent('error', 'Cannot reveal an already matched card.')); } else if ((0, _memoryGameSelectors.isPairOfCardsVisible)(state)) { dispatch(sendEvent('error', 'Cannot reveal more than two cards.')); } else { currentState = dispatch(revealCard(position)); dispatch(sendEvent('reveal', position)); } if (!(0, _memoryGameSelectors.isPairOfCardsVisible)(currentState)) { _context.next = 15; break; } dispatch(disallowInteraction); currentState = dispatch(matchCards); if (!(0, _memoryGameSelectors.isVisiblePairOfCardsAMatch)(currentState)) { _context.next = 12; break; } dispatch(sendEvent('match', currentState.revealed)); matchHappened = true; _context.next = 14; break; case 12: _context.next = 14; return (0, _waitInPromise.default)(2000)(); case 14: if ((0, _memoryGameSelectors.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); }; }() ); }; exports.requestPlayAsync = requestPlayAsync;