Completed
Push — master ( b61f0a...ad8f99 )
by Andres
25:26
created

angular.controller(ꞌct_reactorꞌ)   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
c 0
b 0
f 0
nc 3
dl 0
loc 12
rs 9.2
nop 3
1
'use strict';
2
3
angular.module('game').component('reactor', {
4
  templateUrl: 'views/reactor.html',
5
  controller:  'ct_reactor',
6
  controllerAs: 'ct'
7
});
8
9
angular.module('game').controller('ct_reactor', ['state', 'data', 'visibility', 'util', 'format', 'reaction',
10
function (state, data, visibility, util, format, reactionService) {
11
  let ct = this;
12
  ct.state = state;
13
  ct.data = data;
14
  ct.visibility = visibility;
15
  ct.util = util;
16
  ct.format = format;
17
18
  function update(player) {
19
    // We will process the reaction
20
    for (let syn in player.reactions) {
21
      let power = ct.reactionPower(player, syn);
22
      if (power !== 0) {
23
        reactionService.react(power, data.reactions[syn], player);
24
      }
25
    }
26
  }
27
28
  ct.reactionPower = function (player, reaction) {
29
    let level = player.reactions[reaction].active;
30
    return Math.ceil(Math.pow(level, data.constants.REACT_POWER_INCREASE));
31
  };
32
33
  ct.reactionMultiplier = function (player, reaction) {
34
    let level = player.reactions[reaction].number;
35
    return Math.ceil(Math.pow(data.constants.REACT_PRICE_INCREASE, level));
36
  };
37
38
  function reactionPrice(player, reaction) {
39
    let multiplier = ct.reactionMultiplier(player, reaction);
40
    let price = {};
41
    let reactant = data.reactions[reaction].reactant;
42
    for (let resource in reactant) {
43
      price[resource] = reactant[resource] * multiplier;
44
    }
45
    return price;
46
  }
47
48
  ct.isReactionCostMet = function (player, reaction) {
49
    let price = reactionPrice(player, reaction);
50
    for (let resource in price) {
51
      if (player.resources[resource].number < price[resource]) {
52
        return false;
53
      }
54
    }
55
    return true;
56
  };
57
58
  ct.buyReaction = function (player, reaction, number) {
59
    let i = 0;
60
    // we need a loop since we use the ceil operator
61
    while (i < number && ct.isReactionCostMet(player, reaction)) {
62
      let price = reactionPrice(player, reaction);
63
      for (let resource in price) {
64
        player.resources[resource].number -= price[resource];
65
      }
66
      player.reactions[reaction].number += 1;
67
      i++;
68
    }
69
  };
70
71
  state.registerUpdate('reactor', update);
72
}]);
73