all files / src/ Player.js

84.8% Statements 106/125
68.33% Branches 41/60
88.57% Functions 31/35
85.15% Lines 86/101
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 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321    1×       13×   1×   1×   1×   1×   1×   2×   26×   1× 1× 26×         26×   26× 26× 26× 26× 26×     26× 26× 26×   26× 26× 182×   182× 182×   182×           26× 26×       26×                           1×     19× 19×                       22×                         11× 11× 11× 11×                             19× 19× 19× 19×                           2× 2× 2×   2× 2× 9×   9×           2× 2×       2×           1×                       4× 3×                     4×                       3× 2×                     4×                       1×     1× 6×     1× 6×   1×                       17×   17× 17×           17×   17× 169×   17×                     3×   3× 21×   21×   18×                         2×   2× 34×     2× 34×       34×               34×             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 _PlayerActions = require('./PlayerActions');
 
var _PlayerActions2 = _interopRequireDefault(_PlayerActions);
 
var _Landmark = require('./cards/Landmark');
 
var _Landmark2 = _interopRequireDefault(_Landmark);
 
var _Card = require('./cards/Card');
 
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 Player = function () {
	function Player(_ref) {
		var _ref$landmarks = _ref.landmarks,
		    landmarks = _ref$landmarks === undefined ? require('../config/landmarks.json') : _ref$landmarks,
		    _ref$actions = _ref.actions,
		    actions = _ref$actions === undefined ? _PlayerActions2.default : _ref$actions;
 
		_classCallCheck(this, Player);
 
		this.cards = [];
		this.coins = 0;
		this.landmarks = {};
		this.actions = new actions();
		Iif (!(this.actions instanceof _PlayerActions2.default)) throw Error('Player must have callable actions');
 
		// make the landmarks
		var _iteratorNormalCompletion = true;
		var _didIteratorError = false;
		var _iteratorError = undefined;
 
		try {
			for (var _iterator = Object.keys(landmarks)[Symbol.iterator](), _step; !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) {
				var cardName = _step.value;
 
				var data = landmarks[cardName];
				data.name = cardName;
 
				this.landmarks[cardName] = new _Landmark2.default(data);
			}
		} catch (err) {
			_didIteratorError = true;
			_iteratorError = err;
		} finally {
			try {
				Iif (!_iteratorNormalCompletion && _iterator.return) {
					_iterator.return();
				}
			} finally {
				Iif (_didIteratorError) {
					throw _iteratorError;
				}
			}
		}
	}
 
	/**
  * Alias to Card's setOwner
  * @param {Card} card
  * @return {Player}
  */
 
 
	_createClass(Player, [{
		key: 'addCard',
		value: function addCard(card) {
			card.setOwner(this);
			return this;
		}
 
		/**
   * A part of a game
   * Set Game listeners here
   * @param game
   */
 
	}, {
		key: 'setGame',
		value: function setGame(game) {
			this.game = game;
			//this.game.on('turn')
		}
 
		/**
   * Buys a landmark if it is available
   * @param name
   * @return {boolean}
   */
 
	}, {
		key: 'buyLandmark',
		value: function buyLandmark(name) {
			Eif (this.landmarks[name] && this.coins >= this.landmarks[name].cost) {
				this.coins -= this.landmarks[name].cost;
				this.landmarks[name].activate();
				return true;
			}
 
			return false;
		}
 
		/**
   * Buys a card if possible
   * @param card
   * @return boolean
   */
 
	}, {
		key: 'buyCard',
		value: function buyCard(card) {
			Eif (this.coins >= card.cost) {
				this.coins -= card.cost;
				this.addCard(card);
				return true;
			}
 
			return false;
		}
 
		/**
   * Check if player is a winner
   * @return {boolean}
   */
 
	}, {
		key: 'won',
		value: function won() {
			var _iteratorNormalCompletion2 = true;
			var _didIteratorError2 = false;
			var _iteratorError2 = undefined;
 
			try {
				for (var _iterator2 = Object.keys(this.landmarks)[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) {
					var name = _step2.value;
 
					if (!this.landmarks[name].activated) return false;
				}
			} catch (err) {
				_didIteratorError2 = true;
				_iteratorError2 = err;
			} finally {
				try {
					Iif (!_iteratorNormalCompletion2 && _iterator2.return) {
						_iterator2.return();
					}
				} finally {
					Iif (_didIteratorError2) {
						throw _iteratorError2;
					}
				}
			}
 
			return true;
		}
 
		/**
   * Can increase roll by 2
   * @param {number[]} dice
   * @return {boolean}
   */
 
	}, {
		key: 'canAdd2ToRoll',
		value: function canAdd2ToRoll(dice) {
			if (this.landmarks['Harbor'].activated) Eif (dice.length >= 2 && dice[0] + dice[1] >= 10) return true;
			return false;
		}
 
		/**
   * Can re roll dice
   * @return {boolean}
   */
 
	}, {
		key: 'canRollAgain',
		value: function canRollAgain() {
			return this.landmarks['Radio Tower'].activated;
		}
 
		/**
   * Can take another turn
   * @param {number[]} dice
   * @return {boolean}
   */
 
	}, {
		key: 'canTurnAgain',
		value: function canTurnAgain(dice) {
			if (this.landmarks['Amusement Park'].activated) if (dice.length === 2 && dice[0] === dice[1]) return true;
			return false;
		}
 
		/**
   * Can roll 2 dice
   * @return {boolean}
   */
 
	}, {
		key: 'canRoll2Dice',
		value: function canRoll2Dice() {
			return this.landmarks['Train Station'].activated;
		}
 
		/**
   * Can buy the landmarks
   * @param {Landmark[]} landmarks
   * @return {boolean}
   */
 
	}, {
		key: 'canBuyLandmarks',
		value: function canBuyLandmarks(landmarks) {
			var possible = this.possibleLandmarks();
 
			// stop if landmark isn't possible
			Iif (!landmarks.every(function (lm) {
				return possible.indexOf(lm) !== -1;
			})) return false;
 
			var totalCost = landmarks.reduce(function (acc, lm) {
				return acc + lm.cost;
			}, 0);
			return totalCost <= this.coins;
		}
 
		/**
   * Can buy the cards
   * @param {Card[]} cards
   * @return {boolean}
   */
 
	}, {
		key: 'canBuyCards',
		value: function canBuyCards(cards) {
			var _this = this;
 
			var notOwn = this.cards.filter(function (card) {
				return card.color === _Card.Color.PURPLE;
			}).every(function (card) {
				return _this.cards.indexOf(card.name) === -1;
			});
 
			// owns a matching purple
			Iif (!notOwn) return false;
 
			var totalCost = cards.reduce(function (acc, card) {
				return acc + card.cost;
			}, 0);
			return totalCost <= this.coins;
		}
 
		/**
   * The possible landmarks player can buy
   * @return {Landmark[]}
   */
 
	}, {
		key: 'possibleLandmarks',
		value: function possibleLandmarks() {
			var _this2 = this;
 
			return Object.keys(this.landmarks).map(function (name) {
				return _this2.landmarks[name];
			}).filter(function (lm) {
				return !lm.activated;
			}).filter(function (lm) {
				return _this2.coins >= lm.cost;
			});
		}
 
		/**
   * The indies of the possible cards on the market player can buy
   * @param {Card[]} market
   * @return {number[]} Indexes of possible cards in the market
   */
 
	}, {
		key: 'possibleCards',
		value: function possibleCards(market) {
			var _this3 = this;
 
			var indexes = market.map(function (v, index) {
				return index;
			});
 
			return indexes.filter(function (i) {
				return _this3.coins >= market[i].cost;
			})
			// keep purples I don't own
			.filter(function (i) {
				return market[i].color !== _Card.Color.PURPLE || _this3.cards.filter(function (c) {
					return c.color === _Card.Color.PURPLE;
				}).map(function (c) {
					return c.name;
				}).indexOf(market[i].name) === -1;
			})
			// remove duplicate purples
			.filter(function (i) {
				return market[i].color !== _Card.Color.PURPLE || market.map(function (c) {
					return c.name;
				}).indexOf(market[i].name) === i;
			});
		}
	}]);
 
	return Player;
}();
 
exports.default = Player;
//# sourceMappingURL=Player.js.map