This commit is contained in:
slicer2k
2014-07-14 23:07:17 +02:00
parent aca23d6043
commit faf5ec9353
+87 -33
View File
@@ -1,14 +1,26 @@
(function (Engine, Dropbox) { (function (Engine, Events, Dropbox, $) {
if (!Engine) return false; // Game Engine not available /**
if (!Dropbox) return false; // Dropbox Connector not available * Module that enables a save of the gamestate to the dropbox datastore
* @see https://www.dropbox.com/developers/datastore
*
* The dropbox datastore (dbds) connector lets you save your data to your own dropbox datastore
* without jamming files to it.
*
* This connector uses the game engines own base64 encoder.
*/
var DropboxConnector = window.Engine.Dropbox = { 'use strict';
if (!Engine) { return false; } // Game Engine not available
if (!Dropbox) { return false; } // Dropbox Connector not available
var DropboxConnector = {
options: { options: {
log: false, log: false,
key: 'YOUR_DROPBOX_APP_KEY', // get one at https://www.dropbox.com/developers/apps key: 'YOUR_DROPBOX_APP_KEY', // get one at https://www.dropbox.com/developers/apps
table: 'adarkroom' // table: 'adarkroom' // use whatever you like
}, },
client: false, client: false,
@@ -19,8 +31,8 @@
init: function (options) { init: function (options) {
this.options = $.extend( this.options = $.extend(
this.options, this.options,
options options
); );
this._log = this.options.log; this._log = this.options.log;
@@ -31,7 +43,6 @@
return this; return this;
}, },
startDropbox: function () { startDropbox: function () {
if (!DropboxConnector.client || !DropboxConnector.table) { if (!DropboxConnector.client || !DropboxConnector.table) {
DropboxConnector.startDropboxConnectEvent(); DropboxConnector.startDropboxConnectEvent();
@@ -40,6 +51,12 @@
} }
}, },
/**
* ******
* Events
* ******
*/
startDropboxConnectEvent: function () { startDropboxConnectEvent: function () {
Events.startEvent({ Events.startEvent({
title: _('Dropbox connection'), title: _('Dropbox connection'),
@@ -51,7 +68,7 @@
text: _('connect'), text: _('connect'),
nextScene: 'end', nextScene: 'end',
onChoose: function () { onChoose: function () {
DropboxConnector.connectToDropbox(DropboxConnector.startDropboxImportEvent) DropboxConnector.connectToDropbox(DropboxConnector.startDropboxImportEvent);
} }
}, },
'cancel': { 'cancel': {
@@ -94,7 +111,7 @@
}, },
saveToSlot: { saveToSlot: {
text: [_('choose one slot to save to')], text: [_('choose one slot to save to')],
buttons: function () { buttons: (function () {
var buttons = {}; var buttons = {};
$.each(DropboxConnector.savegames, function (n, savegame) { $.each(DropboxConnector.savegames, function (n, savegame) {
@@ -106,23 +123,23 @@
// timeout prevents error due to fade out animation of the previous event // timeout prevents error due to fade out animation of the previous event
window.setTimeout(function () { window.setTimeout(function () {
DropboxConnector.log('Save to slot ' + n); DropboxConnector.log('Save to slot ' + n);
DropboxConnector.saveGameToDropbox(n, DropboxConnector.savedtoDropboxEvent) DropboxConnector.saveGameToDropbox(n, DropboxConnector.savedtoDropboxEvent);
}, 1000); }, 1000);
} }
}; };
}); });
buttons['cancel'] = { buttons.cancel = {
text: _('cancel'), text: _('cancel'),
nextScene: 'end' nextScene: 'end'
}; };
return buttons; return buttons;
}() }())
}, },
loadFromSlot: { loadFromSlot: {
text: [_('choose one slot to load from')], text: [_('choose one slot to load from')],
buttons: function () { buttons: (function () {
var buttons = {}; var buttons = {};
$.each(DropboxConnector.savegames, function (n, savegame) { $.each(DropboxConnector.savegames, function (n, savegame) {
@@ -135,20 +152,20 @@
// timeout prevents error due to fade out animation of the previous event // timeout prevents error due to fade out animation of the previous event
window.setTimeout(function () { window.setTimeout(function () {
DropboxConnector.log('Load from slot ' + n); DropboxConnector.log('Load from slot ' + n);
DropboxConnector.loadGameFromDropbox(n) DropboxConnector.loadGameFromDropbox(n);
}, 1000); }, 1000);
} }
}; };
} }
}); });
buttons['cancel'] = { buttons.cancel = {
text: _('cancel'), text: _('cancel'),
nextScene: 'end' nextScene: 'end'
}; };
return buttons; return buttons;
}() }())
} }
} }
}); });
@@ -172,6 +189,18 @@
}); });
}, },
/**
* ***************
* functional code
* ***************
*/
/**
* Initiate dropbox connection
*
* @param interactive
* @param callback
*/
connectToDropbox: function (interactive, callback) { connectToDropbox: function (interactive, callback) {
DropboxConnector.log('start dropbox'); DropboxConnector.log('start dropbox');
@@ -204,7 +233,7 @@
DropboxConnector.log("Got savegames", DropboxConnector.savegames); DropboxConnector.log("Got savegames", DropboxConnector.savegames);
if (typeof callback == "function") { if (typeof callback === "function") {
callback.call(DropboxConnector.table); callback.call(DropboxConnector.table);
} }
} }
@@ -214,8 +243,12 @@
} }
}, },
/**
* Requests your savegames fom dbds
*
* @returns {*}
*/
loadGamesFromDropbox: function () { loadGamesFromDropbox: function () {
var savegames = DropboxConnector.savegames; var savegames = DropboxConnector.savegames;
$.each(savegames, function (n) { $.each(savegames, function (n) {
@@ -226,11 +259,15 @@
return savegames; return savegames;
}, },
/**
* Imports a gamestate of a given slotnumber to your game
*
* @param slotnumber
*/
loadGameFromDropbox: function (slotnumber) { loadGameFromDropbox: function (slotnumber) {
var table = DropboxConnector.table; var table = DropboxConnector.table;
var id = DropboxConnector.prepareSavegameID(slotnumber); var id = DropboxConnector.prepareSavegameID(slotnumber);
var results = table.query({savegameId: id}); var results = table.query({savegameId: id});
var record = results[0]; var record = results[0];
@@ -239,6 +276,12 @@
} }
}, },
/**
* Saves a gamestate to a given slot in dbds
*
* @param slotnumber
* @param callback
*/
saveGameToDropbox: function (slotnumber, callback) { saveGameToDropbox: function (slotnumber, callback) {
var table = DropboxConnector.table; var table = DropboxConnector.table;
@@ -251,7 +294,7 @@
timestamp: new Date().getTime() timestamp: new Date().getTime()
}; };
if (DropboxConnector.savegames[slotnumber]) { // slot aleady user -> overwrite if (DropboxConnector.savegames[slotnumber]) { // slot aleady used -> overwrite
record = DropboxConnector.savegames[slotnumber]; record = DropboxConnector.savegames[slotnumber];
try { try {
record.update(saveGame); record.update(saveGame);
@@ -271,20 +314,14 @@
success = false; success = false;
} }
} }
if (typeof callback == "function") { if (typeof callback === "function") {
callback(success); callback(success);
} }
}, },
prepareSavegameID: function (slotnumber) { /**
return 'adarkroom_savegame_' + slotnumber; * Terminates the connection to your db account
}, */
prepareSaveDate: function (timestamp) {
var date = new Date(timestamp);
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
},
signout: function () { signout: function () {
DropboxConnector.client.signOut({}, function (error) { DropboxConnector.client.signOut({}, function (error) {
if (error) { if (error) {
@@ -298,11 +335,28 @@
}); });
}, },
/**
* **************
* Helper methods
* **************
*/
prepareSavegameID: function (slotnumber) {
return 'adarkroom_savegame_' + slotnumber;
},
prepareSaveDate: function (timestamp) {
var date = new Date(timestamp);
return date.toLocaleDateString() + ' ' + date.toLocaleTimeString();
},
log: function () { log: function () {
if (this._log) { if (this._log) {
console.log(arguments); console.log(arguments);
} }
} }
} };
})(window.Engine, window.Dropbox); Engine.Dropbox = DropboxConnector;
})(Engine, Events, Dropbox, jQuery);