"use strict"; var CardTypes = [ { name: "carte 01", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_01.svg" }, { name: "carte 02", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_02.svg" }, { name: "carte 03", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_03.svg" }, { name: "carte 04", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_04.svg" }, { name: "carte 05", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_05.svg" }, { name: "carte 06", image: "https://mediathequechazellessurlyon.opac-x.com/sites/chazelleslyon/files/externe/gh_img_de_06.svg" }, //{ name: "jade", image: "http://jade-lang.com/style/jade-logo-header.svg" }, ]; //{ name: "jade", image: "http://jade-lang.com/style/jade-logo-header.svg" }, var shuffleCards = function shuffleCards() { var cards = [].concat(_.cloneDeep(CardTypes), _.cloneDeep(CardTypes)); return _.shuffle(cards); }; new Vue({ el: "#app", data: { showSplash: false, cards: [], started: false, startTime: 0, turns: 0, flipBackTimer: null, timer: null, time: "--:--", score: 0 }, methods: { resetGame: function resetGame() { this.showSplash = false; var cards = shuffleCards(); this.turns = 0; this.score = 0; this.started = false; this.startTime = 0; _.each(cards, function(card) { card.flipped = false; card.found = false; }); this.cards = cards; }, flippedCards: function flippedCards() { return _.filter(this.cards, function(card) { return card.flipped; }); }, sameFlippedCard: function sameFlippedCard() { var flippedCards = this.flippedCards(); if (flippedCards.length == 2) { if (flippedCards[0].name == flippedCards[1].name) return true; } }, setCardFounds: function setCardFounds() { _.each(this.cards, function(card) { if (card.flipped) card.found = true; }); }, checkAllFound: function checkAllFound() { var foundCards = _.filter(this.cards, function(card) { return card.found; }); if (foundCards.length == this.cards.length) return true; }, startGame: function startGame() { var _this = this; this.started = true; this.startTime = moment(); this.timer = setInterval(function() { _this.time = moment(moment().diff(_this.startTime)).format("mm:ss"); }, 1000); }, finishGame: function finishGame() { this.started = false; clearInterval(this.timer); var score = 1000 - (moment().diff(this.startTime, "seconds") - CardTypes.length * 5) * 3 - (this.turns - CardTypes.length) * 5; this.score = Math.max(score, 0); this.showSplash = true; }, flipCard: function flipCard(card) { var _this2 = this; if (card.found || card.flipped) return; if (!this.started) { this.startGame(); } var flipCount = this.flippedCards().length; if (flipCount == 0) { card.flipped = !card.flipped; } else if (flipCount == 1) { card.flipped = !card.flipped; this.turns += 1; if (this.sameFlippedCard()) { // Match! this.flipBackTimer = setTimeout(function() { _this2.clearFlipBackTimer(); _this2.setCardFounds(); _this2.clearFlips(); if (_this2.checkAllFound()) { _this2.finishGame(); } }, 200); } else { // Wrong match this.flipBackTimer = setTimeout(function() { _this2.clearFlipBackTimer(); _this2.clearFlips(); }, 1000); } } }, clearFlips: function clearFlips() { _.map(this.cards, function(card) { return (card.flipped = false); }); }, clearFlipBackTimer: function clearFlipBackTimer() { clearTimeout(this.flipBackTimer); this.flipBackTimer = null; } }, created: function created() { this.resetGame(); } });