Completed
Push — master ( f35f55...9dde73 )
by Andres
25s
created

elements.js ➔ elements   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 76

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 0
Metric Value
cc 1
c 6
b 0
f 0
nc 1
dl 0
loc 76
rs 8.9667
nop 4

6 Functions

Rating   Name   Duplication   Size   Complexity  
A elements.js ➔ ... ➔ ct.elementClass 0 9 3
A elements.js ➔ ... ➔ ct.buyElement 0 19 4
A elements.js ➔ ... ➔ isElementAvailable 0 8 3
A elements.js ➔ ... ➔ ct.elementSecondaryClass 0 3 1
A elements.js ➔ ... ➔ ct.clearMessage 0 3 1
A elements.js ➔ ... ➔ ct.getChance 0 15 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
 elements
3
 Component that handles the periodic table tab.
4
 It includes the logic to purchase and display elements.
5
6
 @namespace Components
7
 */
8
'use strict';
9
10
angular.module('game').component('elements', {
11
  templateUrl: 'views/elements.html',
12
  controller: ['$timeout', 'state', 'data', 'util', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data, util) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.util = util;
21
  ct.outcome = {};
22
  ct.keys = Object.keys;
23
  ct.buyAmount = [1, 10, 25, 100, 1000];
24
25
  ct.getChance = function(element, player) {
26
    let bonus = 1;
27
    for(let resource of data.elements[element].includes){
28
      let allTime = player.statistics.all_time[resource];
29
      if(allTime){
30
         bonus *= allTime*data.constants.ELEMENT_CHANCE_BONUS+1;
31
       }
32
    }
33
34
    let singleChance = data.elements[element].abundance*bonus;
35
    let chance = 1 - Math.pow(Math.max(0, 1-singleChance),
36
                              Math.min(player.resources.dark_matter.number, ct.buyAmount[player.options.elementBuyIndex]));
37
38
    return Math.min(1, chance);
39
  };
40
41
  ct.buyElement = function (element, player) {
42
    if (player.elements[element]) {
43
      return;
44
    }
45
    if(Math.random() < ct.getChance(element, player)){
46
      player.elements[element] = true;
47
      player.exotic_upgrades[element] = {};
48
      for(let up in data.exotic_upgrades){
49
        player.exotic_upgrades[element][up] = false;
50
      }
51
      player.elements_unlocked++;
52
      ct.outcome[element] = 'Success';
53
    }else{
54
      ct.outcome[element] = 'Fail';
55
    }
56
    player.resources.dark_matter.number -= Math.min(player.resources.dark_matter.number, ct.buyAmount[player.options.elementBuyIndex]);
57
58
    util.delayedExec(performance.now(),performance.now(), 1000, () => ct.clearMessage(element));
0 ignored issues
show
Bug introduced by
The variable performance seems to be never declared. If this is a global, consider adding a /** global: performance */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
59
  };
60
61
  ct.clearMessage = function (element) {
62
    ct.outcome[element] = '';
63
  };
64
65
  /* This function returns the class that determines on which
66
  colour an element card */
67
  ct.elementClass = function (element, player) {
68
    if (player.elements[element]) {
69
      return 'element_purchased';
70
    }
71
    if (isElementAvailable(element, player)) {
72
      return 'element_available';
73
    }
74
    return 'element_unavailable';
75
  };
76
77
  function isElementAvailable(element, player) {
78
    for(let resource of data.elements[element].includes){
79
      if(player.resources[resource].unlocked){
80
        return true;
81
      }
82
    }
83
    return false;
84
  }
85
86
  /* This function returns the class that determines the secondary
87
  colour of an element card */
88
  ct.elementSecondaryClass = function (element, player) {
89
    return ct.elementClass(element, player) + '_dark';
90
  };
91
}
92