| Conditions | 1 |
| Paths | 2 |
| Total Lines | 59 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 1 |
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:
If many parameters/temporary variables are present:
| 1 | 'use strict'; |
||
| 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 |