| Conditions | 1 |
| Paths | 1 |
| Total Lines | 105 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| 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 (state, data, visibility, util, format, reactionService) { |
||
| 17 | let ct = this; |
||
| 18 | ct.state = state; |
||
| 19 | ct.data = data; |
||
| 20 | ct.util = util; |
||
| 21 | ct.format = format; |
||
| 22 | |||
| 23 | function update(player) { |
||
| 24 | for (let reaction of player.reactions) { |
||
| 25 | if (!reaction.active) { |
||
| 26 | continue; |
||
| 27 | } |
||
| 28 | reactionService.react(numberToReact(player, reaction.reaction), reaction.reaction, player); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | |||
| 32 | function numberToReact(player, reaction) { |
||
| 33 | let power = ct.reactionPower(player); |
||
| 34 | let number = power; |
||
| 35 | for(let resource in reaction.reactants){ |
||
| 36 | number = Math.min(number, player.resources[resource].number); |
||
| 37 | } |
||
| 38 | return number; |
||
| 39 | } |
||
| 40 | |||
| 41 | /* Calculates the redox power based on the redox upgrades */ |
||
| 42 | ct.reactionPower = function(player) { |
||
| 43 | let level = player.global_upgrades.reaction_bandwidth; |
||
| 44 | let upgrade = data.global_upgrades.reaction_bandwidth; |
||
| 45 | let basePower = upgrade.power; |
||
| 46 | let polynomial = upgrade.power_poly; |
||
| 47 | return basePower * Math.floor(Math.pow(level, polynomial)); |
||
| 48 | }; |
||
| 49 | |||
| 50 | /* Calculates the number of redox slots based on the redox upgrades */ |
||
| 51 | ct.reactionSlots = function (player) { |
||
| 52 | let level = player.global_upgrades.reaction_slots; |
||
| 53 | let upgrade = data.global_upgrades.reaction_slots; |
||
| 54 | let basePower = upgrade.power; |
||
| 55 | let multiplier = upgrade.power_mult; |
||
| 56 | return basePower * Math.floor(multiplier * level); |
||
| 57 | }; |
||
| 58 | |||
| 59 | ct.reactionSize = function (player) { |
||
| 60 | return player.reactions.length; |
||
| 61 | }; |
||
| 62 | |||
| 63 | /* Adds a new reaction to the player list */ |
||
| 64 | ct.addReaction = function (player, key) { |
||
| 65 | if(ct.reactionSize(player) >= ct.reactionSlots(player)){ |
||
| 66 | return; |
||
| 67 | } |
||
| 68 | let reaction = data.reactions[key]; |
||
| 69 | player.reactions.push({ |
||
| 70 | active: false, |
||
| 71 | reaction: angular.copy(reaction) |
||
| 72 | }); |
||
| 73 | }; |
||
| 74 | |||
| 75 | ct.removeReaction = function (player, item) { |
||
| 76 | for(let i = 0; i < player.reactions.length; i++){ |
||
| 77 | if(player.reactions[i] === item){ |
||
| 78 | player.reactions.splice(i, 1); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | }; |
||
| 82 | |||
| 83 | ct.visibleReactions = function(currentElement) { |
||
| 84 | return visibility.visible(state.player.reactions, isReactionVisible, currentElement); |
||
| 85 | }; |
||
| 86 | |||
| 87 | function isReactionVisible(entry, currentElement) { |
||
| 88 | let reaction = entry.reaction; |
||
| 89 | for(let resource in reaction.reactant){ |
||
| 90 | let elements = data.resources[resource].elements; |
||
| 91 | if(Object.keys(elements).length === 0 && currentElement === ''){ |
||
| 92 | return true; |
||
| 93 | } |
||
| 94 | for(let element in elements){ |
||
| 95 | if(element === currentElement){ |
||
| 96 | return true; |
||
| 97 | } |
||
| 98 | } |
||
| 99 | } |
||
| 100 | return false; |
||
| 101 | } |
||
| 102 | |||
| 103 | ct.availableReactions = function(currentElement) { |
||
| 104 | return visibility.visible(data.reactions, isReactionAvailable, currentElement); |
||
| 105 | }; |
||
| 106 | |||
| 107 | function isReactionAvailable(entry, currentElement) { |
||
| 108 | let available = true; |
||
| 109 | let reaction = data.reactions[entry]; |
||
| 110 | for(let resource in reaction.reactant){ |
||
| 111 | available = available && state.player.resources[resource].unlocked; |
||
| 112 | } |
||
| 113 | // Workaround to reuse the visibility function. It expects an object with the |
||
| 114 | // reaction inside |
||
| 115 | let reactionObject = {reaction:reaction}; |
||
| 116 | return available && isReactionVisible(reactionObject, currentElement); |
||
| 117 | } |
||
| 118 | |||
| 119 | state.registerUpdate('reactions', update); |
||
| 120 | }]); |
||
| 121 |