Completed
Push — master ( 412ccc...814126 )
by Andres
55s
created

elements.js ➔ ... ➔ ct.visibleTableElements   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 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', 'util', 'visibility', elements],
13
  controllerAs: 'ct'
14
});
15
16
function elements($timeout, state, data, util, visibility) {
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
  ct.visibleTableElements = function(player) {
93
    return visibility.visible(data.elements, isTableElementVisible, null, null, player);
94
  };
95
96
  function isTableElementVisible(element, _, player) {
97
    switch (ct.elementClass(element, player)) {
98
      case 'element_purchased':
99
        return !player.options.hideElementsPurchased;
100
      case 'element_available':
101
        return !player.options.hideElementsAvailable;
102
      case 'element_unavailable':
103
        return !player.options.hideElementsUnavailable;
104
      default:
105
        return true;
106
    }
107
  }
108
}
109