Completed
Push — master ( 1e2918...37de3f )
by Andres
36s
created

nova.js ➔ nova   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 87

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
c 2
b 0
f 0
nc 2
dl 0
loc 87
rs 8.6296
nop 4

7 Functions

Rating   Name   Duplication   Size   Complexity  
A nova.js ➔ ... ➔ ct.canBuyGlobalUpgrade 0 11 3
C nova.js ➔ ... ➔ ct.buyAll 0 27 7
A nova.js ➔ ... ➔ createVisibleFunction 0 5 1
A nova.js ➔ ... ➔ ct.priceMultiplier 0 4 1
A nova.js ➔ ... ➔ ct.buyUpgrade 0 10 1
A nova.js ➔ ... ➔ ct.visibleUpgrades 0 3 1
A nova.js ➔ ... ➔ ct.buyGlobalUpgrade 0 13 3

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/**
2
 nova
3
 Component that handles upgrades.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('nova', {
10
  templateUrl: 'views/nova.html',
11
  controller: ['state', 'visibility', 'upgrade', 'data', nova],
12
  controllerAs: 'ct'
13
});
14
15
function nova(state, visibility, upgrade, data) {
16
  let ct = this;
17
  ct.state = state;
18
  ct.data = data;
19
20
  // tries to buy all the upgrades it can, starting from the cheapest
21
  ct.buyAll = function (element) {
22
    let currency = data.elements[element].main;
23
    let cheapest;
24
    let cheapestPrice;
25
    do{
26
      cheapest = null;
27
      cheapestPrice = Infinity;
0 ignored issues
show
Comprehensibility Best Practice introduced by
You seem to be aliasing the built-in name Infinity as cheapestPrice. This makes your code very difficult to follow, consider using the built-in name directly.
Loading history...
28
      for(let up of ct.visibleUpgrades(element, data.upgrades)){
29
        let price = data.upgrades[up].price;
30
        if(!state.player.elements[element].upgrades[up] &&
31
          price <= state.player.resources[currency].number){
32
          if(price < cheapestPrice){
33
            cheapest = up;
34
            cheapestPrice = price;
35
          }
36
        }
37
      }
38
      if(cheapest){
39
        let upgrades = state.player.elements[element].upgrades;
40
        upgrade.buyUpgrade(state.player,
41
          upgrades,
42
          cheapest,
43
          cheapestPrice,
44
          currency);
45
      }
46
    }while(cheapest);
47
  };
48
49
  ct.buyUpgrade = function (name, element) {
50
    let upgrades = state.player.elements[element].upgrades;
51
    let price = data.upgrades[name].price;
52
    let currency = data.elements[element].main;
53
    upgrade.buyUpgrade(state.player,
54
      upgrades,
55
      name,
56
      price,
57
      currency);
58
  };
59
60
  /* Global upgrades are non-resource specific, repeatable upgrades */
61
  ct.buyGlobalUpgrade = function (name) {
62
    if (!ct.canBuyGlobalUpgrade(name)) {
63
      return;
64
    }
65
66
    let up = data.global_upgrades[name];
67
    for (let currency in up.price) {
68
      let value = up.price[currency] * ct.priceMultiplier(name);
69
      state.player.resources[currency].number -= value;
70
    }
71
72
    state.player.global_upgrades[name]++;
73
  };
74
75
  ct.canBuyGlobalUpgrade = function (name) {
76
    let up = data.global_upgrades[name];
77
    for (let currency in up.price) {
78
      let value = up.price[currency] * ct.priceMultiplier(name);
79
      if (state.player.resources[currency].number < value) {
80
        return false;
81
      }
82
    }
83
84
    return true;
85
  };
86
87
  ct.priceMultiplier = function (name) {
88
    let level = state.player.global_upgrades[name];
89
    return Math.ceil(Math.pow(data.global_upgrades[name].price_exp, level));
90
  };
91
92
  ct.visibleUpgrades = function(currentElement, source) {
93
    return visibility.visible(source, createVisibleFunction(source), currentElement);
94
  };
95
96
  function createVisibleFunction(source){
97
    return function isBasicUpgradeVisible(name, currentElement) {
98
      return visibility.isUpgradeVisible(name, currentElement, source[name]);
99
    };
100
  }
101
}
102