all files / src/ Deck.js

84.31% Statements 86/102
64% Branches 32/50
100% Functions 10/10
84.62% Lines 66/78
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168    1×       2×   1×   1×   1×   15×   1×         1× 15×   15×   15× 15× 15× 15× 15×   15× 15× 15×   15× 15× 41×   41×   41× 226×             15× 15×       15×           15×                 1×     4× 46× 318× 559× 107×       46×     46×     45×     4×                         2× 2× 2×   2× 2× 2×   2× 2× 19×   19×           2× 2×       2×           2× 2× 2×   2× 2× 19×   19× 19× 19×           2× 2×       2×           2×       1×     1×  
'use strict';
 
Object.defineProperty(exports, "__esModule", {
	value: true
});
 
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; Eif ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { Eif (protoProps) defineProperties(Constructor.prototype, protoProps); Iif (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
 
var _shuffleArray = require('shuffle-array');
 
var _shuffleArray2 = _interopRequireDefault(_shuffleArray);
 
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
 
function _classCallCheck(instance, Constructor) { Iif (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
 
var Deck = function () {
	/**
  * @param {Object<string, number>} quantity
  * @param {number} marketSize
  */
	function Deck(quantity) {
		var marketSize = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 10;
 
		_classCallCheck(this, Deck);
 
		var CardTypes = {};
		this.cards = [];
		this.ownedCards = [];
		this.market = [];
		this.marketSize = marketSize;
 
		var _iteratorNormalCompletion = true;
		var _didIteratorError = false;
		var _iteratorError = undefined;
 
		try {
			for (var _iterator = Object.keys(quantity)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
				var cardName = _step.value;
 
				CardTypes[cardName] = require('./cards/' + cardName.replace(/[^\w]/g, '')).default;
 
				for (var i = 0; i < quantity[cardName]; i++) {
					this.cards.push(new CardTypes[cardName]());
				}
			}
		} catch (err) {
			_didIteratorError = true;
			_iteratorError = err;
		} finally {
			try {
				Iif (!_iteratorNormalCompletion && _iterator.return) {
					_iterator.return();
				}
			} finally {
				Iif (_didIteratorError) {
					throw _iteratorError;
				}
			}
		}
 
		(0, _shuffleArray2.default)(this.cards);
	}
 
	/**
  * Fills the market with cards
  * @returns {Card[]} the market generated
  */
 
 
	_createClass(Deck, [{
		key: 'updateMarket',
		value: function updateMarket() {
			Eif (this.market.length < this.marketSize) while (this.cards.length) {
				var unique = this.market.filter(function (value, index, array) {
					for (var i = 0; i < index; i++) {
						if (array[i].name === value.name) return false;
					}return true;
				});
 
				// push and don't look
				if (unique.length < this.marketSize) this.market.push(this.cards.shift());
 
				// stop
				if (unique.length === this.marketSize) break;
 
				// went too far
				Iif (unique.length > this.marketSize) this.cards.unshift(this.market.pop());
			}
 
			return this.market;
		}
 
		/**
   * Buys the Nth card on the market
   * @param {number|number[]} index
   * @param {Player} player
   * @return {Card[]}
   */
 
	}, {
		key: 'buy',
		value: function buy(index, player) {
			var ret = [];
			var retIndex = [];
			Iif (typeof index === 'number') index = [index];
 
			var _iteratorNormalCompletion2 = true;
			var _didIteratorError2 = false;
			var _iteratorError2 = undefined;
 
			try {
				for (var _iterator2 = index[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
					var i = _step2.value;
 
					Eif (player.buyCard(this.market[i])) retIndex.push(i);
				} // save card as owned in reverse order to keep key importance
			} catch (err) {
				_didIteratorError2 = true;
				_iteratorError2 = err;
			} finally {
				try {
					Iif (!_iteratorNormalCompletion2 && _iterator2.return) {
						_iterator2.return();
					}
				} finally {
					Iif (_didIteratorError2) {
						throw _iteratorError2;
					}
				}
			}
 
			var _iteratorNormalCompletion3 = true;
			var _didIteratorError3 = false;
			var _iteratorError3 = undefined;
 
			try {
				for (var _iterator3 = retIndex.sort().reverse()[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) {
					var _i = _step3.value;
 
					var card = this.market.splice(_i, 1);
					this.ownedCards.push(card[0]);
					ret.push(card[0]);
				}
			} catch (err) {
				_didIteratorError3 = true;
				_iteratorError3 = err;
			} finally {
				try {
					Iif (!_iteratorNormalCompletion3 && _iterator3.return) {
						_iterator3.return();
					}
				} finally {
					Iif (_didIteratorError3) {
						throw _iteratorError3;
					}
				}
			}
 
			return ret;
		}
	}]);
 
	return Deck;
}();
 
exports.default = Deck;
//# sourceMappingURL=Deck.js.map