Completed
Push — master ( 57baa2...9fe7d8 )
by Andres
37s
created

angular.controller(ꞌct_fusion_selectꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 1
dl 0
loc 3
rs 10
nop 0
1
/**
2
 fusion
3
 Component that handles the fusion of isotopes to create new ones.
4
5
 @namespace Components
6
 */
7
'use strict';
8
9
angular.module('game').component('fusionSelect', {
10
  templateUrl: 'views/fusion-select.html',
11
  controller:  'ct_fusion_select',
12
  controllerAs: 'ct',
13
  bindings: {
14
    source: '@',
15
    getCapacity: '&'
16
  }
17
});
18
19
angular.module('game').controller('ct_fusion_select', ['state', 'data','$scope',
20
  function (state, data, $scope) {
21
    let ct = this;
22
    ct.state = state;
23
    ct.data = data;
24
25
    ct.availableIsotopes = function(player){
26
      let result = [];
27
      for(let resource in data.resources){
28
        if(data.resources[resource].type.indexOf('isotope') !== -1 &&
29
           player.resources[resource].unlocked){
30
             result.push(resource);
31
           }
32
      }
33
      return result;
34
    };
35
36
    ct.setPercentage = function(percentage, player) {
37
      let fragment = state.player.fusion[0][ct.source];
38
      fragment.number = Math.floor(player.resources[fragment.name].number*(percentage/100));
39
      ct.fixNumber(player);
40
    };
41
42
    ct.fixNumber = function(player) {
43
      let fragment = state.player.fusion[0][ct.source];
44
      let resourceNumber = player.resources[fragment.name].number;
45
      let capacity = ct.getCapacity({resource: fragment.name, player:player});
46
      fragment.number = Math.max(0, Math.min(resourceNumber, fragment.number, capacity));
47
    };
48
49
    // we watch for changes in the area to adjust the numbers if it goes down
50
    $scope.$watch('ct.state.player.global_upgrades_current.fusion_area', function(){
51
      ct.fixNumber(state.player);
52
    });
53
  }
54
]);
55