| Conditions | 1 |
| Paths | 1 |
| Total Lines | 70 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 4 | ||
| Bugs | 0 | Features | 0 |
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:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 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.buyAmount = [1, 10, 25, 100, 1000]; |
||
| 23 | |||
| 24 | ct.getChance = function(element) { |
||
| 25 | let bonus = 0; |
||
| 26 | for(let resource of data.elements[element].includes){ |
||
| 27 | bonus += state.player.resources[resource].number*data.constants.ELEMENT_CHANCE_BONUS; |
||
| 28 | } |
||
| 29 | let singleChance = data.elements[element].abundance*(1+bonus); |
||
| 30 | let chance = 1 - Math.pow(Math.max(0, 1-singleChance), Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex])); |
||
| 31 | |||
| 32 | return Math.min(1, chance); |
||
| 33 | }; |
||
| 34 | |||
| 35 | ct.buyElement = function (element) { |
||
| 36 | if (state.player.elements[element]) { |
||
| 37 | return; |
||
| 38 | } |
||
| 39 | if(Math.random() < ct.getChance(element)){ |
||
| 40 | state.player.elements[element] = true; |
||
| 41 | state.player.exotic_upgrades[element] = {}; |
||
| 42 | for(let up in data.exotic_upgrades){ |
||
| 43 | state.player.exotic_upgrades[element][up] = false; |
||
| 44 | } |
||
| 45 | state.player.elements_unlocked++; |
||
| 46 | ct.outcome[element] = 'Success'; |
||
| 47 | }else{ |
||
| 48 | ct.outcome[element] = 'Fail'; |
||
| 49 | } |
||
| 50 | state.player.resources.dark_matter.number -= Math.min(state.player.resources.dark_matter.number, ct.buyAmount[state.player.options.elementBuyIndex]); |
||
| 51 | |||
| 52 | $timeout(function(){ct.clearMessage(element);}, 1000); |
||
| 53 | }; |
||
| 54 | |||
| 55 | ct.clearMessage = function (element) { |
||
| 56 | ct.outcome[element] = ''; |
||
| 57 | }; |
||
| 58 | |||
| 59 | /* This function returns the class that determines on which |
||
| 60 | colour an element card */ |
||
| 61 | ct.elementClass = function (element) { |
||
| 62 | if (state.player.elements[element]) { |
||
| 63 | return 'element_purchased'; |
||
| 64 | } |
||
| 65 | if (isElementAvailable(element)) { |
||
| 66 | return 'element_available'; |
||
| 67 | } |
||
| 68 | return 'element_unavailable'; |
||
| 69 | }; |
||
| 70 | |||
| 71 | function isElementAvailable(element) { |
||
| 72 | for(let resource of data.elements[element].includes){ |
||
| 73 | if(state.player.resources[resource].unlocked){ |
||
| 74 | return true; |
||
| 75 | } |
||
| 76 | } |
||
| 77 | return false; |
||
| 78 | } |
||
| 79 | |||
| 80 | /* This function returns the class that determines the secondary |
||
| 81 | colour of an element card */ |
||
| 82 | ct.elementSecondaryClass = function (element) { |
||
| 83 | return ct.elementClass(element) + '_dark'; |
||
| 84 | }; |
||
| 85 | } |
||
| 86 |