2017-06-13 14:09:18 +02:00
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
2017-06-19 15:54:02 +02:00
|
|
|
this.loginSubscription = null;
|
|
|
|
this.saveSubscription = null;
|
|
|
|
this.loadSubscription = null;
|
2017-06-13 14:09:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.System.Storage.prototype = Object.create(GameLib.System.prototype);
|
|
|
|
GameLib.System.Storage.prototype.constructor = GameLib.System.Storage;
|
|
|
|
|
2017-06-19 15:54:02 +02:00
|
|
|
GameLib.System.Storage.prototype.start = function() {
|
|
|
|
|
|
|
|
this.loginSubscription = this.subscribe(
|
|
|
|
GameLib.Event.LOGGED_IN,
|
|
|
|
function(data) {
|
|
|
|
this.token = data.token;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
this.saveSubscription = this.subscribe(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.SAVE_COMPONENT,
|
2017-06-19 15:54:02 +02:00
|
|
|
this.save
|
|
|
|
);
|
|
|
|
|
|
|
|
this.loadSubscription = this.subscribe(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.LOAD_COMPONENT,
|
2017-06-19 15:54:02 +02:00
|
|
|
this.load
|
|
|
|
)
|
|
|
|
};
|
2017-06-13 14:09:18 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* '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(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.SAVE_COMPONENT_ERROR,
|
2017-06-13 14:09:18 +02:00
|
|
|
{
|
|
|
|
message: this.responseText
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (response.result === 'success') {
|
|
|
|
GameLib.Event.Emit(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.COMPONENT_SAVED,
|
2017-06-13 14:09:18 +02:00
|
|
|
{
|
|
|
|
message: response.message || 'Successfully saved the component'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
GameLib.Event.Emit(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.SAVE_COMPONENT_ERROR,
|
2017-06-13 14:09:18 +02:00
|
|
|
{
|
|
|
|
message: response.message || 'The server responded but failed to save the component'
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.send(JSON.stringify({
|
|
|
|
component : data,
|
|
|
|
session : this.token
|
|
|
|
}));
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2017-06-19 21:35:51 +02:00
|
|
|
* 'Loads' data from a url
|
2017-06-13 14:09:18 +02:00
|
|
|
*/
|
|
|
|
GameLib.System.Storage.prototype.load = function(data) {
|
2017-06-13 16:35:19 +02:00
|
|
|
|
2017-06-13 14:09:18 +02:00
|
|
|
if (typeof XMLHttpRequest === 'undefined') {
|
|
|
|
console.log('Implement server side load here');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var xhr = new XMLHttpRequest();
|
|
|
|
|
|
|
|
xhr.open(
|
|
|
|
'GET',
|
2017-06-19 21:35:51 +02:00
|
|
|
data.url
|
2017-06-13 14:09:18 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
xhr.onreadystatechange = function (xhr) {
|
|
|
|
return function () {
|
|
|
|
if (xhr.readyState === 4) {
|
2017-06-19 21:35:51 +02:00
|
|
|
|
|
|
|
try {
|
|
|
|
var object = JSON.parse(xhr.responseText);
|
|
|
|
} catch (error) {
|
|
|
|
this.publish(
|
|
|
|
GameLib.Event.LOAD_COMPONENT_ERROR,
|
|
|
|
{
|
|
|
|
error : error
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (object.result !== 'success') {
|
|
|
|
this.publish(
|
|
|
|
GameLib.Event.LOAD_COMPONENT_ERROR,
|
|
|
|
{
|
|
|
|
error : object
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-13 14:09:18 +02:00
|
|
|
this.publish(
|
2017-06-19 21:35:51 +02:00
|
|
|
GameLib.Event.COMPONENT_LOADED,
|
2017-06-13 14:09:18 +02:00
|
|
|
{
|
2017-06-19 21:35:51 +02:00
|
|
|
response : object
|
2017-06-13 14:09:18 +02:00
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(xhr).bind(this);
|
|
|
|
|
|
|
|
xhr.send();
|
2017-06-19 15:54:02 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
GameLib.System.Storage.prototype.stop = function() {
|
|
|
|
this.loginSubscription.remove();
|
|
|
|
this.loadSubscription.remove();
|
|
|
|
this.saveSubscription.remove();
|
|
|
|
};
|
|
|
|
|