let fs = require('fs');
let _ = require('lodash');

class CardManager {
  constructor() {
    this.cards = [];
    this.used = [];
    this.available = [];
    this.index = 0;

    let id = 0;
    let files = fs.readdirSync('public/cards');
    for (let f of files) {
      if (/^card-.+\.(jpg|jpeg|png)$/.test(f)) {
        this.cards.push({id: ++id, url: `cards/${f}`});
      }
    }

    this.available = _.shuffle(this.cards.slice());
  }

  getDeck() {
    if (this.index + 6 >= this.available.length) {
      this.index = 0;
      this.available = _.shuffle(this.available);
    }

    let deck = this.available.splice(this.index, 6);
    Array.prototype.push.apply(this.used, deck);
    this.index += 6;

    return deck;
  }

  exchangeCard(usedCard) {
    if (this.index + 1 >= this.available.length) {
      this.index = 0;
      this.available = _.shuffle(this.available);
    }

    let newCard = this.available.splice(this.index++, 1)[0];
    this.used.push(newCard);

    let index = _.findIndex(this.used, {'id': usedCard});
    usedCard = this.used.splice(index, 1)[0];
    this.available.push(usedCard);

    return newCard;
  }

  getCard(cardId) {
    return this.cards[cardId - 1];
  }

  reset() {
    this.used = [];
    this.available = _.shuffle(this.cards.slice());
  }
}

module.exports = CardManager;

@@ -104,36 +104,16 @@
 s = %5B%5D;%0A
-    this.used = %5B%5D;%0A
     this
@@ -125,24 +125,24 @@
 lable = %5B%5D;%0A
+
     this.ind
@@ -619,57 +619,8 @@
 6);%0A
-    Array.prototype.push.apply(this.used, deck);%0A
     
@@ -890,142 +890,8 @@
 his.
-used.push(newCard);%0A%0A    let index = _.findIndex(this.used, %7B'id': usedCard%7D);%0A    usedCard = this.used.splice(index, 1)%5B0%5D;%0A    this.
 avai
@@ -900,16 +900,29 @@
 ble.push
+(this.getCard
 (usedCar
@@ -923,16 +923,17 @@
 sedCard)
+)
 ;%0A%0A    r
@@ -1012,16 +1012,16 @@
 %5D;%0A  %7D%0A%0A
+
   reset(
@@ -1028,28 +1028,8 @@
 ) %7B%0A
-    this.used = %5B%5D;%0A
     

Simplify card-manager