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 | let sortFunc = [ |
||
25 | (a,b) => data.elements[a].number - data.elements[b].number, |
||
26 | (a,b) => ct.getChance(b, state.player) - ct.getChance(a, state.player) |
||
27 | ] |
||
28 | |||
29 | ct.getChance = function(element, player) { |
||
30 | let bonus = 1; |
||
31 | for (let resource of data.elements[element].includes) { |
||
32 | let allTime = player.statistics.all_time[resource]; |
||
33 | if (allTime) { |
||
34 | bonus *= allTime * data.constants.ELEMENT_CHANCE_BONUS + 1; |
||
35 | } |
||
36 | } |
||
37 | |||
38 | let singleChance = data.elements[element].abundance * bonus; |
||
39 | let chance = 1 - Math.pow(Math.max(0, 1 - singleChance), |
||
40 | Math.min(player.resources.dark_matter, ct.buyAmount[player.options.elementBuyIndex])); |
||
41 | |||
42 | return Math.min(1, chance); |
||
43 | }; |
||
44 | |||
45 | ct.buyElement = function(element, player) { |
||
46 | if (player.elements[element]) { |
||
47 | return; |
||
48 | } |
||
49 | if (Math.random() < ct.getChance(element, player)) { |
||
50 | player.elements[element] = true; |
||
51 | player.exotic_upgrades[element] = {}; |
||
52 | for (let up in data.exotic_upgrades) { |
||
53 | player.exotic_upgrades[element][up] = false; |
||
54 | } |
||
55 | player.elements_unlocked++; |
||
56 | ct.outcome[element] = 'Success'; |
||
57 | } else { |
||
58 | ct.outcome[element] = 'Fail'; |
||
59 | } |
||
60 | player.resources.dark_matter -= Math.min(player.resources.dark_matter, ct.buyAmount[player.options.elementBuyIndex]); |
||
61 | |||
62 | util.delayedExec(performance.now(), performance.now(), 1000, () => ct.clearMessage(element)); |
||
0 ignored issues
–
show
|
|||
63 | }; |
||
64 | |||
65 | ct.clearMessage = function(element) { |
||
66 | ct.outcome[element] = ''; |
||
67 | }; |
||
68 | |||
69 | /* This function returns the class that determines on which |
||
70 | colour an element card */ |
||
71 | ct.elementClass = function(element, player) { |
||
72 | if (player.elements[element]) { |
||
73 | return 'element_purchased'; |
||
74 | } |
||
75 | if (isElementAvailable(element, player)) { |
||
76 | return 'element_available'; |
||
77 | } |
||
78 | return 'element_unavailable'; |
||
79 | }; |
||
80 | |||
81 | function isElementAvailable(element, player) { |
||
82 | for (let resource of data.elements[element].includes) { |
||
83 | if (player.resources[resource] !== null) { |
||
84 | return true; |
||
85 | } |
||
86 | } |
||
87 | return false; |
||
88 | } |
||
89 | |||
90 | /* This function returns the class that determines the secondary |
||
91 | colour of an element card */ |
||
92 | ct.elementSecondaryClass = function(element, player) { |
||
93 | return ct.elementClass(element, player) + '_dark'; |
||
94 | }; |
||
95 | |||
96 | ct.visibleTableElements = function(player) { |
||
97 | return visibility.visible(data.elements, isTableElementVisible, null, sortFunc[player.options.elementSortIndex], player); |
||
98 | }; |
||
99 | |||
100 | function isTableElementVisible(element, _, player) { |
||
101 | switch (ct.elementClass(element, player)) { |
||
102 | case 'element_purchased': |
||
103 | return !player.options.hideElementsPurchased; |
||
104 | case 'element_available': |
||
105 | return !player.options.hideElementsAvailable; |
||
106 | case 'element_unavailable': |
||
107 | return !player.options.hideElementsUnavailable; |
||
108 | default: |
||
109 | return true; |
||
110 | } |
||
111 | } |
||
112 | } |
||
113 |
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.