Completed
Push — master ( 98e91a...5e1a95 )
by Andres
41s
created

angular.controller(ꞌct_reactorꞌ)   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 62

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
nc 2
dl 0
loc 62
rs 9.4743
nop 6

6 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 12 4
A ��) 0 9 2
A ��) 0 4 1
A ��) 0 9 3
A ��) 0 4 1
A ��) 0 9 3

How to fix   Long Method   

Long Method

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:

1
/**
2
 reactor
3
 Component that handles reactions and molecules.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('reactor', {
10
  templateUrl: 'views/reactor.html',
11
  controller:  'ct_reactor',
12
  controllerAs: 'ct'
13
});
14
15
angular.module('game').controller('ct_reactor', ['state', 'data', 'visibility', 'util', 'format', 'reaction',
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