Completed
Push — master ( 23c59f...e92c03 )
by Andres
38s
created

src/scripts/component/elements.js   A

Complexity

Total Complexity 19
Complexity/F 1.9

Size

Lines of Code 89
Function Count 10

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 19
c 2
b 0
f 0
nc 1
mnd 2
bc 20
fnc 10
dl 0
loc 89
rs 10
bpm 2
cpm 1.9
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B elements.js ➔ elements 0 81 1
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', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data) {
17
  let ct = this;
18
  ct.state = state;
19
  ct.data = data;
20
  ct.outcome = {};
21
  let buyAmount = [1, 10, 25, 100, 'max'];
22
23
  ct.getChance = function(element) {
24
    let bonus = 0;
25
    for(let isotope in data.elements[element].isotopes){
26
      bonus += state.player.resources[isotope].number*data.constants.ELEMENT_CHANCE_BONUS;
27
    }
28
    let singleChance = data.elements[element].abundance*(1+bonus);
29
    let chance = 1 - Math.pow(Math.max(0, 1-singleChance), Math.min(state.player.resources.dark_matter.number, ct.getbuyAmount(state.player)));
30
31
    return Math.min(1, chance);
32
  };
33
34
  ct.buyElement = function (element) {
35
    if (state.player.elements[element]) {
36
      return;
37
    }
38
    if(Math.random() < ct.getChance(element)){
39
      state.player.elements[element] = true;
40
      state.player.exotic_upgrades[element] = {};
41
      for(let up in data.exotic_upgrades){
42
        state.player.exotic_upgrades[element][up] = false;
43
      }
44
      state.player.elements_unlocked++;
45
      ct.outcome[element] = 'Success';
46
    }else{
47
      ct.outcome[element] = 'Fail';
48
    }
49
    state.player.resources.dark_matter.number -= Math.min(state.player.resources.dark_matter.number, ct.getbuyAmount(state.player));
50
51
    $timeout(function(){ct.clearMessage(element);}, 1000);
52
  };
53
54
  ct.clearMessage = function (element) {
55
    ct.outcome[element] = '';
56
  };
57
58
  /* This function returns the class that determines on which
59
  colour an element card */
60
  ct.elementClass = function (element) {
61
    if (state.player.elements[element]) {
62
      return 'element_purchased';
63
    }
64
    if (isElementAvailable(element)) {
65
      return 'element_available';
66
    }
67
    return 'element_unavailable';
68
  };
69
70
  function isElementAvailable(element) {
71
    for(let resource of data.elements[element].includes){
72
      if(state.player.resources[resource].unlocked){
73
        return true;
74
      }
75
    }
76
    return false;
77
  }
78
79
  /* This function returns the class that determines the secondary
80
  colour of an element card */
81
  ct.elementSecondaryClass = function (element) {
82
    return ct.elementClass(element) + '_dark';
83
  };
84
85
  ct.nextBuyAmount = function () {
86
    state.elementBuyIndex = (state.elementBuyIndex + 1) % buyAmount.length;
87
  };
88
89
  ct.getbuyAmount = function (player) {
90
    let result = buyAmount[state.elementBuyIndex];
91
    if(result === 'max'){
92
      result = player.resources.dark_matter.number;
93
    }
94
    return result;
95
  };
96
}
97