-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy pathapp.save.js
389 lines (336 loc) · 10 KB
/
app.save.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
(function() {
'use strict';
// For iteration
var i;
var l;
var ri;
var pi;
var pl;
var ci;
var row;
var tile;
var upgrade;
var SAVE_MANAGER = function() {
this.game;
this.active_saver;
this.init = function(game) {
this.game = game;
}
};
var save_manager = new SAVE_MANAGER();
window.save_manager = save_manager;
var LocalSaver = function() {
this.save = function(data, callback) {
save_manager.game.save_debug && console.log('LocalSaver.save');
window.localStorage.setItem('rks', data);
if ( callback ) {
callback();
}
}
this.enable = function() {
save_manager.game.save_debug && console.log('LocalSaver.enable');
localStorage.removeItem('google_drive_save');
save_manager.active_saver = this;
}
this.load = function(callback) {
save_manager.game.save_debug && console.log('LocalSaver.load');
var rks = window.localStorage.getItem('rks');
callback(rks);
}
};
save_manager.LocalSaver = LocalSaver
var google_loaded = false;
var google_auth_called = false;
window.set_google_loaded = function() {
save_manager.game.save_debug && console.log('set_google_loaded');
google_loaded = true;
if ( google_auth_called ) {
google_saver.checkAuth(null, true);
}
};
var GoogleSaver = function() {
var CLIENT_ID = '572695445092-svr182bgaass7vt97r5mnnk4phmmjh5u.apps.googleusercontent.com';
var SCOPES = ['https://www.googleapis.com/auth/drive.appfolder'];
var src = 'https://apis.google.com/js/client.js?onload=set_google_loaded';
var filename = 'save.txt'
var file_id = null;
var file_meta = null;
var tried_load = false;
var load_callback = null;
var self = this;
var enable_callback;
var access_token;
// TODO: check if `google_loaded` can be safely used to replace this
this.loadfailed = false;
this.authChecked = false;
this.enable = function(callback, event) {
save_manager.game.save_debug && console.log('GoogleSaver.enable');
enable_callback = callback;
if ( google_loaded && this.authChecked === true && file_id !== null ) {
if ( callback ) {
callback();
}
return;
} else if ( google_loaded ) {
// If this was from a button click, open the popup
self.checkAuth(null, event ? false : true);
} else {
// Make sure they can see the auth popup
//show_page.call($('#options'), null);
google_auth_called = true;
}
save_manager.active_saver = this;
};
this.save = function(data, callback) {
save_manager.game.save_debug && console.log('GoogleSaver.save');
local_saver.save(data);
if ( google_loaded === true && this.authChecked === true && file_id !== null ) {
update_file(data, callback);
}
};
this.load = function(callback) {
save_manager.game.save_debug && console.log('GoogleSaver.load');
if ( file_meta !== null ) {
download_file(file_meta, callback);
} else {
tried_load = true;
load_callback = callback;
}
};
var load_script = function() {
var el = document.createElement('script');
el.setAttribute('type', 'text/javascript');
el.setAttribute('src', src);
// don't actually disable the button since we want to give some kind of feedback if players click it
el.onerror = function(event) { self.loadfailed = true; $('#enable_google_drive_save').classList.add("button_disabled") }
document.getElementsByTagName('head')[0].appendChild(el);
};
/**
* Check if the current user has authorized the application.
*/
this.checkAuth = function(callback, immediate=false) {
save_manager.game.save_debug && console.log('GoogleSaver.checkAuth');
gapi.auth.authorize(
{
'client_id': CLIENT_ID,
'scope': SCOPES,
'immediate': immediate
},
function(authResult) {
save_manager.game.save_debug && console.log('gapi.auth.authorize CB', authResult);
if ( authResult && !authResult.error ) {
google_loaded = true;
self.authChecked = true;
access_token = authResult['access_token'];
// Access token has been successfully retrieved, requests can be sent to the API.
localStorage.setItem('google_drive_save', 1);
// We have a callback for refreshing the auth
if ( callback ) {
callback();
} else {
gapi.client.load('drive', 'v2', function(data) {
save_manager.game.save_debug && console.log('gapi.client.load CB', data);
get_file();
});
}
//show_page.call($('#show_reactor'), null);
} else if ( !immediate ) {
// No access token could be retrieved
local_saver.enable();
save_game = local_saver;
localStorage.removeItem('google_drive_save');
enable_local_save();
alert('Could not authorize. Switching to local save.')
} else {
self.checkAuth(callback, false);
}
}
);
};
var update_file = function(data='{}', callback) {
save_manager.game.save_debug && console.log('GoogleSaver update_file', data);
var boundary = '-------314159265358979323846';
var delimiter = "\r\n--" + boundary + "\r\n";
var close_delim = "\r\n--" + boundary + "--";
var contentType = 'text/plain';
var base64Data = btoa(data);
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(file_meta) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files/' + file_id,
'method': 'PUT',
'params': {
uploadType: 'multipart',
alt: 'json'
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
request.execute(function(data) {
save_manager.game.save_debug && console.log('gapi.client.request CB', data);
if ( !data || data.error ) {
if ( data.error.code === 404 ) {
alert('It looks like the game was taken over in a new window - to take the game back, please refresh');
} else {
self.authChecked = false;
// TODO: Use the refresh token instead
self.checkAuth(function() {
update_file(data, callback);
}, true);
}
} else {
if ( callback ) {
callback();
}
}
});
}
/**
* Permanently delete a file, skipping the trash.
*
* @param {String} fileId ID of the file to delete.
*/
var deleteFile = function(fileId, callback) {
save_manager.game.save_debug && console.log('GoogleSaver deleteFile');
var request = gapi.client.drive.files.delete({
'fileId': fileId
});
request.execute(function(resp) {
if ( callback ) callback();
});
}
var get_file = function() {
save_manager.game.save_debug && console.log('GoogleSaver get_file');
/**
* List all files contained in the Application Data folder.
*
* @param {Function} callback Function to call when the request is complete.
*/
function listFilesInApplicationDataFolder(callback) {
var retrievePageOfFiles = function(request, result) {
request.execute(function(resp) {
result = result.concat(resp.items);
var nextPageToken = resp.nextPageToken;
if (nextPageToken) {
request = gapi.client.drive.files.list({
'pageToken': nextPageToken
});
retrievePageOfFiles(request, result);
} else {
save_manager.game.save_debug && console.log('GoogleSaver retrievePageOfFiles CB', result);
callback(result);
}
});
}
var initialRequest = gapi.client.drive.files.list({
'q': '\'appfolder\' in parents'
});
retrievePageOfFiles(initialRequest, []);
}
listFilesInApplicationDataFolder(function(result) {
save_manager.game.save_debug && console.log('GoogleSaver listFilesInApplicationDataFolder CB', result);
for ( var i = 0, l = result.length; i < l; i++ ) {
var file = result[i];
// Found save file
if ( file.title === filename ) {
file_id = file.id;
file_meta = file;
if ( tried_load ) {
self.load(load_callback);
} else if ( enable_callback ) {
enable_callback();
enable_callback = null;
}
return;
}
}
// No save file found, make a new one
new_save_file(save_manager);
});
};
var new_save_file = function(callback) {
save_manager.game.save_debug && console.log('GoogleSaver new_save_file');
var boundary = '-------314159265358979323846264';
var delimiter = "\r\n--" + boundary + "\r\n";
var close_delim = "\r\n--" + boundary + "--";
var contentType = 'text/plain';
var metadata = {
'title': filename,
'mimeType': contentType,
'parents': [{'id': 'appfolder'}]
};
var base64Data = btoa(btoa(JSON.stringify({})));
var multipartRequestBody =
delimiter +
'Content-Type: application/json\r\n\r\n' +
JSON.stringify(metadata) +
delimiter +
'Content-Type: ' + contentType + '\r\n' +
'Content-Transfer-Encoding: base64\r\n' +
'\r\n' +
base64Data +
close_delim;
var request = gapi.client.request({
'path': '/upload/drive/v2/files',
'method': 'POST',
'params': {
uploadType: 'multipart'
},
'headers': {
'Content-Type': 'multipart/mixed; boundary="' + boundary + '"'
},
'body': multipartRequestBody
});
request.execute(function(arg) {
save_manager.game.save_debug && console.log('gapi.client.request CB', arg);
file_id = arg.id;
file_meta = arg;
if ( callback ) callback();
});
};
/**
* Download a file's content.
*
* @param {File} file Drive File instance.
* @param {Function} callback Function to call when the request is complete.
*/
var download_file = function(file, callback) {
save_manager.game.save_debug && console.log('GoogleSaver download_file');
if ( file.downloadUrl ) {
var accessToken = gapi.auth.getToken().access_token;
var xhr = new XMLHttpRequest();
xhr.open('GET', file.downloadUrl);
xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
xhr.onload = function() {
file_meta = null;
deleteFile(file_id, function() {
file_id = null;
new_save_file(function() {
callback(xhr.responseText);
// save game state
save_manager();
});
});
};
xhr.onerror = function() {
callback(null);
};
xhr.send();
} else {
callback(null);
}
}
load_script();
};
save_manager.GoogleSaver = GoogleSaver;
})();