/**
* AngularJS Service that determines the 'state' of the app.
*
* @module appStateService
* @see https://docs.angularjs.org/api/ng/type/angular.Module#service
*/
angular.module('tgaApp').service('appStateService', ['$log', 'gameConfig',
function appStateService($log, gameConfig) {
/**
* Stored value for current active state name
*
* @private
* @member {string} currentState
*/
let currentState;
/**
* Stored pause state
*
* @private
* @member {boolean} paused
*/
let paused = false;
/**
* Stored set of all possible states and which is active.
*
* @private
* @member {Object.<string, boolean>} states
*/
let states = {
splash: false,
info: false,
register: false,
tutorial: false,
game: false,
gameover: false,
unauthorized: false,
};
/**
* Initialize appStateService with set of possible states from {@link gameConfig}
*/
this.init = () => {
const s = gameConfig.states;
if (s) {
const newStates = {};
for (let i = 0; i < s.length; i += 1) {
newStates[s[i]] = false;
}
states = newStates;
}
};
/**
* Get the current active state name.
*
* @returns {string}
*/
this.get = () => currentState;
/**
* Set a state to active
*
* @param {string} key - The name of the state to set active.
*/
this.set = (key) => {
if (currentState === key) {
$log.warn(`change state: ${key} is already set`);
return;
}
if (angular.isUndefined(states[key])) {
$log.error(`change state: ${key} is not a state`);
return;
}
Object.keys(states).forEach((value) => { states[value] = false; });
// set the passed in one to true
states[key] = true;
currentState = key;
$log.debug(`set state: ${key}`);
};
/**
* Set paused status.
*
* @param {boolean} status
*/
this.pause = (status) => {
paused = status;
};
/**
* Check paused status.
*
* @returns {boolean}
*/
this.isPaused = () => paused;
},
]);