Completed
Push — master ( cb980c...09f078 )
by Andres
33s
created

angular.controller(ꞌmain-loopꞌ)   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

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 6
rs 9.4285
nop 1
1
'use strict';
2
3
angular
4
  .module('game')
5
  .controller('main-loop', ['$scope',
6
    '$interval',
7
    '$timeout',
8
    'savegame',
9
    'state',
10
    'data',
11
    function($scope, $interval, $timeout, savegame, state, data) {
12
      $scope.state = state;
13
14
      let self = this;
15
      let playerCopy = null;
16
17
      self.update = function() {
18
        // do the update in a copy
19
        playerCopy = angular.copy(state.player);
20
21
        state.update(playerCopy);
22
23
        // and update all at once
24
        state.player = playerCopy;
25
      };
26
27
      self.updateLoop = function() {
28
        self.update();
29
        $timeout(self.updateLoop, 1000);
30
      };
31
32
      self.processOffline = function() {
33
        let remaining = state.offlineCyclesTotal-state.offlineCyclesCurrent;
34
        let cycles = Math.min(32, remaining);
35
36
        for(let i = 0; i < cycles; i++){
37
          self.update();
38
          state.offlineCyclesCurrent++;
39
          remaining--;
40
        }
41
42
        if(remaining > 0 && !state.cancelOffline){
43
          $timeout(self.processOffline);
44
        }else{
45
          // we are done processing, turn off the screens
46
          state.processingOffline = false;
47
          state.loading = false;
48
          // trigger the game loop
49
          $timeout(self.updateLoop, 1000);
50
          $interval(savegame.save, 10000);
51
        }
52
      };
53
54
      self.startup = function() {
55
        savegame.load();
56
        let elapsed = Math.floor(Date.now()/1000)-state.player.last_login;
57
        // lets limit the offline elapsed time
58
        elapsed = Math.min(self.offlinePower(state.player), elapsed);
59
        state.offlineCyclesTotal = elapsed;
60
        state.offlineCyclesCurrent = 0;
61
        if(elapsed > 32){
62
          state.loading = false;
63
          state.processingOffline = true;
64
        }
65
        $timeout(self.processOffline);
66
      };
67
68
      /* Calculates the redox power based on the redox upgrades */
69
      self.offlinePower = function(player) {
70
        let level = player.global_upgrades.offline_time;
71
        let upgrade = data.global_upgrades.offline_time;
72
        let basePower = upgrade.power;
73
        return basePower * level;
74
      };
75
76
      $timeout(self.startup);
77
    }
78
  ]);
79