| Conditions | 1 |
| Paths | 2 |
| Total Lines | 62 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 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 | // We will process the reaction |
||
| 25 | for (let syn in player.reactions) { |
||
| 26 | let power = ct.reactionPower(player, syn); |
||
| 27 | if (power !== 0) { |
||
| 28 | reactionService.react(power, data.reactions[syn], player); |
||
| 29 | } |
||
| 30 | } |
||
| 31 | } |
||
| 32 | |||
| 33 | ct.reactionPower = function (player, reaction) { |
||
| 34 | let level = player.reactions[reaction].active; |
||
| 35 | return Math.ceil(Math.pow(level, data.constants.REACT_POWER_INCREASE)); |
||
| 36 | }; |
||
| 37 | |||
| 38 | ct.reactionPriceMultiplier = function (player, reaction) { |
||
| 39 | let level = player.reactions[reaction].number; |
||
| 40 | return Math.ceil(Math.pow(data.constants.REACT_PRICE_INCREASE, level)); |
||
| 41 | }; |
||
| 42 | |||
| 43 | function reactionPrice(player, reaction) { |
||
| 44 | let multiplier = ct.reactionPriceMultiplier(player, reaction); |
||
| 45 | let price = {}; |
||
| 46 | let reactant = data.reactions[reaction].reactant; |
||
| 47 | for (let resource in reactant) { |
||
| 48 | price[resource] = reactant[resource] * multiplier; |
||
| 49 | } |
||
| 50 | return price; |
||
| 51 | } |
||
| 52 | |||
| 53 | ct.isReactionCostMet = function (player, reaction) { |
||
| 54 | let price = reactionPrice(player, reaction); |
||
| 55 | for (let resource in price) { |
||
| 56 | if (player.resources[resource].number < price[resource]) { |
||
| 57 | return false; |
||
| 58 | } |
||
| 59 | } |
||
| 60 | return true; |
||
| 61 | }; |
||
| 62 | |||
| 63 | ct.buyReaction = function (player, reaction, number) { |
||
| 64 | let i = 0; |
||
| 65 | // we need a loop since we use the ceil operator |
||
| 66 | while (i < number && ct.isReactionCostMet(player, reaction)) { |
||
| 67 | let price = reactionPrice(player, reaction); |
||
| 68 | for (let resource in price) { |
||
| 69 | player.resources[resource].number -= price[resource]; |
||
| 70 | } |
||
| 71 | player.reactions[reaction].number += 1; |
||
| 72 | i++; |
||
| 73 | } |
||
| 74 | }; |
||
| 75 | |||
| 76 | state.registerUpdate('reactor', update); |
||
| 77 | }]); |
||
| 78 |