11var gameState = function ( $q , notificationService , $filter , UPGRADES , GainCalculator , localStorage , AnimatedFlyTip ) {
2- var upgrades = { } ;
3- var units = 0 ;
4- var start = Date . now ( ) ;
5- var lastSave = Date . now ( ) ;
6- var currencyName = 'Unit' ;
7- var ads = true ;
2+
3+ var getNewState = function ( ) {
4+ return {
5+ upgrades : { } ,
6+ units : 0 ,
7+ start : Date . now ( ) ,
8+ lastSave : Date . now ( ) ,
9+ currencyName : 'Unit' ,
10+ ads : true
11+ } ;
12+ } ;
13+
14+ var currentState = getNewState ( ) ;
815
916 var upgradeDefer = $q . defer ( ) ;
1017 var unitDefer = $q . defer ( ) ;
1118
1219 var buildSaveObject = function ( ) {
13- return {
14- units : units ,
15- upgrades : upgrades ,
16- start : start ,
17- lastSave : lastSave ,
18- currencyName : currencyName ,
19- ads : ads
20- } ;
20+ return currentState ;
2121 } ;
2222
2323 var save = function ( ) {
24- lastSave = Date . now ( ) ;
24+ currentState . lastSave = Date . now ( ) ;
2525 localStorage . set ( 'game' , buildSaveObject ( ) ) ;
2626 } ;
2727
2828 var hardReset = function ( ) {
29- start = Date . now ( ) ;
30- units = 0 ;
31- upgrades = { } ;
32- currencyName = 'Unit' ;
33- ads = true ;
29+ currentState = getNewState ( ) ;
3430 save ( ) ;
3531 } ;
3632
@@ -39,93 +35,76 @@ var gameState = function($q, notificationService, $filter, UPGRADES, GainCalcula
3935
4036 if ( ! state ) { return ; }
4137
42- if ( state . units ) {
43- units = state . units ;
44- }
45-
46- if ( state . upgrades ) {
47- upgrades = state . upgrades ;
48- }
38+ _ . assign ( currentState , state ) ;
4939
50- if ( state . start ) {
51- start = state . start ;
40+ if ( ! upgrade . has ( 'Offline Progress' ) ) {
41+ return ;
5242 }
5343
54- if ( state . currencyName ) {
55- currencyName = state . currencyName ;
56- }
44+ var diff = Date . now ( ) - state . lastSave ;
45+ var multiplier = 0.25 + ( 0.25 * upgrade . getKey ( 'Offline Progress' ) ) ;
46+ var timersElapsed = Math . floor ( diff / GainCalculator . timer ( upgrade ) ) ;
47+ var gain = timersElapsed * multiplier * GainCalculator . all ( upgrade ) * GainCalculator . timerBoost ( upgrade ) ;
5748
58- if ( ! _ . isUndefined ( state . ads ) ) {
59- ads = state . ads ;
49+ if ( gain <= 0 ) {
50+ return ;
6051 }
6152
62- if ( state . lastSave ) {
63- lastSave = state . lastSave ;
64-
65- if ( upgrade . has ( 'Offline Progress' ) ) {
66- var diff = Date . now ( ) - state . lastSave ;
67- var multiplier = 0.25 + ( 0.25 * upgrade . getKey ( 'Offline Progress' ) ) ;
68- var timersElapsed = Math . floor ( diff / GainCalculator . timer ( upgrade ) ) ;
69- var gain = timersElapsed * multiplier * GainCalculator . all ( upgrade ) * GainCalculator . timerBoost ( upgrade ) ;
70- if ( gain > 0 ) {
71- unit . inc ( gain , false ) ;
72- save ( ) ;
53+ unit . inc ( gain , false ) ;
54+ save ( ) ;
7355
74- if ( upgrade . has ( 'Notifications' ) ) {
75- var numString = gain ;
76- if ( upgrade . has ( 'Number Formatting' ) ) {
77- numString = $filter ( 'number' ) ( numString , 0 ) ;
78- }
79-
80- notificationService . notifyWithDefaults ( {
81- type : 'success' ,
82- title : 'Offline Progression' ,
83- text : `You gained ${ numString } ${ currencyName } s while offline. Welcome back!`
84- } ) ;
85- }
86- }
56+ if ( upgrade . has ( 'Notifications' ) ) {
57+ var numString = gain ;
58+ if ( upgrade . has ( 'Number Formatting' ) ) {
59+ numString = $filter ( 'number' ) ( numString , 0 ) ;
8760 }
61+
62+ notificationService . notifyWithDefaults ( {
63+ type : 'success' ,
64+ title : 'Offline Progression' ,
65+ text : `You gained ${ numString } ${ currentState . currencyName } s while offline. Welcome back!`
66+ } ) ;
8867 }
8968 } ;
9069
9170 var currencySet = {
92- set : function ( newName ) { currencyName = newName ; save ( ) ; } ,
93- get : function ( ) { return currencyName ; }
71+ set : function ( newName ) { currentState . currencyName = newName ; save ( ) ; } ,
72+ get : function ( ) { return currentState . currencyName ; }
9473 } ;
9574
9675 var adSet = {
97- set : function ( isSet ) { ads = isSet ; } ,
98- get : function ( ) { return ads ; }
76+ set : function ( isSet ) { currentState . ads = isSet ; } ,
77+ get : function ( ) { return currentState . ads ; }
9978 } ;
10079
10180 var upgrade = {
102- has : function ( key , level = 0 ) { return upgrades [ key ] > level ; } ,
103- get : function ( ) { return upgrades ; } ,
104- getKey : function ( key ) { return upgrades [ key ] ; } ,
81+ has : function ( key , level = 0 ) { return currentState . upgrades [ key ] > level ; } ,
82+ get : function ( ) { return currentState . upgrades ; } ,
83+ getKey : function ( key ) { return currentState . upgrades [ key ] ; } ,
10584 inc : function ( key ) {
10685
107- var nextLevel = upgrades [ key ] || 0 ;
86+ var nextLevel = currentState . upgrades [ key ] || 0 ;
10887
10988 var cost = _ . isFunction ( UPGRADES [ key ] . levels ) ?
11089 UPGRADES [ key ] . levels ( nextLevel ) . cost :
11190 UPGRADES [ key ] . levels [ nextLevel ] . cost ;
112- if ( units < cost ) { return ; }
91+ if ( currentState . units < cost ) { return ; }
11392
114- if ( ! upgrades [ key ] ) { upgrades [ key ] = 0 ; }
115- upgrades [ key ] ++ ;
93+ if ( ! currentState . upgrades [ key ] ) { currentState . upgrades [ key ] = 0 ; }
94+ currentState . upgrades [ key ] ++ ;
11695 unit . inc ( - cost ) ;
117- upgradeDefer . notify ( { key : key , level : upgrades [ key ] , all : upgrades } ) ;
96+ upgradeDefer . notify ( { key : key , level : currentState . upgrades [ key ] , all : currentState . upgrades } ) ;
11897 } ,
11998 watch : function ( ) { return upgradeDefer . promise ; }
12099 } ;
121100
122101 var tick = 0 ;
123102
124103 var unit = {
125- has : function ( amt ) { return units > amt ; } ,
104+ has : function ( amt ) { return currentState . units > amt ; } ,
126105 inc : function ( amt , display = true ) {
127- units += amt ;
128- unitDefer . notify ( units ) ;
106+ currentState . units += amt ;
107+ unitDefer . notify ( currentState . units ) ;
129108
130109 if ( upgrade . has ( 'Basic Animation' ) && display ) {
131110 AnimatedFlyTip . fly ( amt , upgrade . has ( 'Number Formatting' ) ) ;
@@ -143,7 +122,7 @@ var gameState = function($q, notificationService, $filter, UPGRADES, GainCalcula
143122 }
144123
145124 } ,
146- get : function ( ) { return units ; } ,
125+ get : function ( ) { return currentState . units ; } ,
147126 watch : function ( ) { return unitDefer . promise ; }
148127 } ;
149128
0 commit comments