Completed
Push — master ( 8a100d...66108c )
by Andres
33s
created

angular.controller(ꞌmain-loopꞌ)   A

Complexity

Conditions 1
Paths 2

Size

Total Lines 59

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 1
Metric Value
cc 1
nc 2
dl 0
loc 59
rs 9.597
c 1
b 1
f 1
nop 5

4 Functions

Rating   Name   Duplication   Size   Complexity  
A ��) 0 13 2
A ��) 0 4 1
A ��) 0 9 1
A ��) 0 21 4

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