cakefoot/src/pre_js_collect.js

59 lines
1.9 KiB
JavaScript

/*~~~~.
|\~~~~: [ C A K E F O O T ] presented by dank.game
|\\~~~|
|#\\~~: source code and license at https://open.shampoo.ooo/shampoo/cakefoot
\\#\\~|
\\#\\: created using the SPACE🪐BOX engine https://open.shampoo.ooo/shampoo/spacebox
\\#\'
\\#|
`-'
@file pre_js_collect.js
Extend Emscripten's Module object to collect user play data in WASM builds.
*/
function collectData()
{
/* Open the database Emscripten's IDBFS library created. */
var databaseName = "/storage";
var open = indexedDB.open(databaseName);
var database;
open.onerror = (event) => {
console.error("Unable to open IndexedDB to read progress data.");
};
open.onsuccess = (event) => {
database = event.target.result;
/* Read the key created by Emscripten which corresponds to the file containing the progress data JSON. */
var path = databaseName + "/cakefoot_progress.json";
var read = database.transaction("FILE_DATA").objectStore("FILE_DATA").get(path);
read.onerror = (event) => {
console.error("Unable to read from", path);
};
read.onsuccess = (event) => {
/* Decode a string encoded as a UInt8Array of chars. The string is serialized JSON data. */
var contents = String.fromCharCode.apply(null, read.result.contents);
/* Pass play log to PHP script as JSON */
fetch("www/collect.php", {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: contents
}).then((response) => {
if (!response.ok)
{
console.log("Error collecting progress to play history");
}
else
{
return response.json();
}
});
}
};
}