Code coverage report for ./src/client/app/shop/shop.service.js

Statements: 25% (11 / 44)      Branches: 0% (0 / 12)      Functions: 8.33% (1 / 12)      Lines: 25% (11 / 44)      Ignored: none     

All files » ./src/client/app/shop/ » shop.service.js
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 841     1       1     1                                     1                     1                 1       1           1               1           1                    
(function() {
    'use strict';
 
    angular
        .module('app.shop')
        .service('shopService', shopService);
 
    shopService.$inject = ['playerService', 'inventoryService', 'resourcesService', 'messageService'];
 
    /* @ngInject */
    function shopService(playerService, inventoryService, resourcesService, messageService) {
    	var vm = this;
        vm.resources = resourcesService.resources;
        vm.itemDictionary = inventoryService.itemDictionary;
 
    	vm.initShop = function() {
    		createShopList();
    	};
 
        vm.initPurchase = function(item) {
            checkBalance(item);
        };
 
        vm.grabItemDictionary = function() {
            return vm.itemDictionary;
        };
 
        ////////////////
 
        function createShopList() {
            vm.shopList = [];
        	for (var i = 0; i < inventoryService.buyableItems.length; i++) {
        		var key = inventoryService.buyableItems[i];
        		var thisItem = inventoryService.itemDictionary[key][0][1];
        		if (thisItem.buyable) {
        			vm.shopList.push(thisItem);
        		}
        	}
 		}
 
        function checkBalance(item) {
        	if (item.price > vm.resources.money) {
        		messageService.updateMainMessage('Not enough money.', true);
        	} else {
        		subtractCost(item);
        		addToInventory(item);
        	}
        }
 
        function subtractCost(item) {
        	vm.resources.money = vm.resources.money - item.price;
        }
 
        function addItem(item) {
        	if (typeof item.unlock !== 'undefined') {
        		inventoryService.itemDictionary[item.unlock][0][1].buyable = true;
        	}
        }
 
		function removeItem(item) {
			if (item.removeAfterBuy) {
				item.buyable = false;
                displayNextItem(item);
                createShopList();
			}
        }
 
        function displayNextItem(item) {
            if (typeof item.unlock !== 'undefined') {
                inventoryService.itemDictionary[item.unlock][0][1].buyable = true;
            }
        }
 
        function addToInventory(item) {
            if (typeof item.special !== 'undefined') {
                item.special();
            }
            messageService.updateMainMessage(item.name + ' has been purchased.');
        	inventoryService.itemDictionary[item.slug][1][1] = inventoryService.itemDictionary[item.slug][1][1] + 1;
            removeItem(item);
        }
 
    }
})();