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

src/scripts/component/reactor.js   A

Complexity

Total Complexity 15
Complexity/F 2.14

Size

Lines of Code 71
Function Count 7

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 0
wmc 15
c 2
b 0
f 0
nc 2
mnd 2
bc 14
fnc 7
dl 0
loc 71
rs 10
bpm 2
cpm 2.1427
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
A angular.controller(ꞌct_reactorꞌ) 0 62 1
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