133 lines
3.1 KiB
JavaScript
133 lines
3.1 KiB
JavaScript
|
/**
|
||
|
* System takes care of updating all the entities (based on their component data)
|
||
|
* @param graphics
|
||
|
* @param apiSystem GameLib.API.System
|
||
|
* @param apiUrl
|
||
|
* @param token
|
||
|
* @constructor
|
||
|
*/
|
||
|
GameLib.System.Storage = function(
|
||
|
graphics,
|
||
|
apiSystem,
|
||
|
apiUrl,
|
||
|
token
|
||
|
) {
|
||
|
|
||
|
if (GameLib.Utils.UndefinedOrNull(apiUrl)) {
|
||
|
console.warn('Need an API URL for a storage system');
|
||
|
}
|
||
|
this.apiUrl = apiUrl;
|
||
|
|
||
|
GameLib.System.call(
|
||
|
this,
|
||
|
graphics,
|
||
|
apiSystem
|
||
|
);
|
||
|
|
||
|
if (GameLib.Utils.UndefinedOrNull(token)) {
|
||
|
token = null;
|
||
|
}
|
||
|
this.token = token;
|
||
|
|
||
|
this.subscribe(
|
||
|
GameLib.Event.LOGGED_IN,
|
||
|
function(data) {
|
||
|
this.token = data.token;
|
||
|
}
|
||
|
)
|
||
|
|
||
|
};
|
||
|
|
||
|
GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype);
|
||
|
GameLib.System.Storage.prototype.constructor = GameLib.System.Storage;
|
||
|
|
||
|
|
||
|
/**
|
||
|
* 'Saves' data to baseURL
|
||
|
*/
|
||
|
GameLib.System.Storage.prototype.save = function(data) {
|
||
|
|
||
|
if (typeof XMLHttpRequest === 'undefined') {
|
||
|
console.log('Implement server side save here');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var xhr = new XMLHttpRequest();
|
||
|
|
||
|
xhr.open(
|
||
|
'POST',
|
||
|
this.apiUrl + '/component/create'
|
||
|
);
|
||
|
|
||
|
xhr.setRequestHeader("Accept", "application/json");
|
||
|
xhr.setRequestHeader("Content-Type", "application/json");
|
||
|
|
||
|
xhr.onreadystatechange = function () {
|
||
|
if (this.readyState === 4) {
|
||
|
try {
|
||
|
var response = JSON.parse(this.responseText)
|
||
|
} catch (error) {
|
||
|
GameLib.Event.Emit(
|
||
|
GameLib.Event.SAVE_FAILURE,
|
||
|
{
|
||
|
message: this.responseText
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
|
||
|
if (response.result === 'success') {
|
||
|
GameLib.Event.Emit(
|
||
|
GameLib.Event.SAVE_SUCCESS,
|
||
|
{
|
||
|
message: response.message || 'Successfully saved the component'
|
||
|
}
|
||
|
)
|
||
|
} else {
|
||
|
GameLib.Event.Emit(
|
||
|
GameLib.Event.SAVE_FAILURE,
|
||
|
{
|
||
|
message: response.message || 'The server responded but failed to save the component'
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
|
||
|
xhr.send(JSON.stringify({
|
||
|
component : data,
|
||
|
session : this.token
|
||
|
}));
|
||
|
|
||
|
};
|
||
|
|
||
|
/**
|
||
|
* 'Loads' data from baseURL
|
||
|
*/
|
||
|
GameLib.System.Storage.prototype.load = function(data) {
|
||
|
if (typeof XMLHttpRequest === 'undefined') {
|
||
|
console.log('Implement server side load here');
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
var xhr = new XMLHttpRequest();
|
||
|
|
||
|
xhr.open(
|
||
|
'GET',
|
||
|
this.apiUrl + '/component/' + data.id
|
||
|
);
|
||
|
|
||
|
xhr.onreadystatechange = function (xhr) {
|
||
|
return function () {
|
||
|
if (xhr.readyState === 4) {
|
||
|
this.publish(
|
||
|
GameLib.Event.LOADED,
|
||
|
{
|
||
|
component: JSON.parse(xhr.responseText)
|
||
|
}
|
||
|
)
|
||
|
}
|
||
|
}
|
||
|
}(xhr).bind(this);
|
||
|
|
||
|
xhr.send();
|
||
|
};
|