Skip to content
Permalink
Browse files

Initial Commit

  • Loading branch information...
Fritsbie committed Jul 12, 2019
1 parent 2804c1f commit f7992a0624490861ade4dd78fad443f9d3776d31
Showing with 350 additions and 0 deletions.
  1. +350 −0 fireweb.js
@@ -1 +1,351 @@
// Your web app's Firebase configuration,

//Start deleting from here
var firebaseConfig = {
apiKey: ".......................................",
authDomain: ".......................................",
databaseURL: ".......................................",
projectId: ".......................................",
storageBucket: ".......................................",
messagingSenderId: ".......................................",
appId: "......................................."
};
//To here
//And put your config

var app = firebase.initializeApp(firebaseConfig);
db = firebase.firestore(app);


//Constants
const codeError = -1;
const codeFailed = 0;
const codeSuccess = 1;
const codeChangeServer = 1;
const codeChangeLocal = 0;

//Firestore Database
var db;
//Result String holder of a document
var resultString = "";

//Query Builder
var queryRef = null;
var dataListener = {};

//Network Online/Offline
var networkStatus = 'default';


//Some Global Variables
var debugMode = false;


//Debug Mode
function firestore_web_debug_mode(deb) {
if (deb=="DEBUGIT") {
debugMode = true;
} else {
debugMode = false;
}
}

function firestore_web_disable_network() {

networkStatus = 'cache';
debugLog("Network/Cache","Network operations are forced to use LOCAL CACHE.");
}

function firestore_web_enable_network() {
networkStatus = 'server';
debugLog("Network/Cache","Network operations are forced to use SERVER for data operations.");

}

function firestore_web_default_network() {
networkStatus = 'default';
debugLog("Network/Cache","Network operations are forced to use DEFAULT behaviour.");

}



function firestore_web_document_json() {
return resultString;
}

///>>>

function firestore_web_document_get(documentLocation, callbackFunction) {

var getOptions = {
source: networkStatus
};

debugLog("Document/Get","Getting the document : " + documentLocation + ". It'll be retrieved to function > " + callbackFunction)
db.doc(documentLocation).get(getOptions).then(function(doc) {
if (doc.exists) {
debugLog("Document/Get","Document " + documentLocation + " retrieved.");
transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);

} else {
// doc.data() will be undefined in this case
debugLog("Document/Get","Document " + documentLocation + " does not exist!");
goCallback(callbackFunction, codeFailed);
}
}).catch(function(error) {
debugLog("Document/Get","Error fetching the document. Error code : " + error);
goCallback(callbackFunction, codeError);
});
}




function firestore_web_document_listen(documentLocation, callbackFunction, dataListenerID) {

var listener = db.doc(documentLocation)
.onSnapshot(function(doc) {

var source = "";
if (doc.metadata.hasPendingWrites=="Local") {
source =="Local DB";
goCallback(callbackFunction,codeChangeLocal);
} else {
source =="Server DB";
goCallback(callbackFunction,codeChangeServer);
}
var source = doc.metadata.hasPendingWrites ? "Local" : "Server";
debugLog("Document/Listen","A "+source+" based change has been detected in " + documentLocation);

}).catch(function(error) {
debugLog("Document/Get","Error listening to the document. Error code : " + error);
goCallback(callbackFunction, codeError);
});

dataListenerAdd(dataListenerID,listener);
}

function firestore_web_listener_detach(listenerID) {

dataListenerRemove(listenerID);
}

///Set a Document
function firestore_web_document_set(documentLocation, jsonStr, callbackFunction) {

let jsonObject = JSON.parse(jsonStr);

db.doc(documentLocation).set(jsonObject)
.then(function() {
goCallback(callbackFunction,codeSuccess);
debugLog("Document/Set","Successfully SET the document to " + documentLocation);
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error SETting the document. Error code : " + error);
});
}

///Add a Document
function firestore_web_document_add(collectionLocation, jsonStr, callbackFunction) {

let jsonObject = JSON.parse(jsonStr);

db.collection(collectionLocation).add(jsonObject)
.then(function(docRef) {
goCallback(callbackFunction,codeSuccess);
debugLog("Document/Set","Successfully ADDed the document to "+ collectionLocation +". Document ID : " + docRef.id);
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error ADDing the document. Error code : " + error);
});
}

///Update a Document
function firestore_web_document_update(documentLocation, jsonStr, callbackFunction) {

let jsonObject = JSON.parse(jsonStr);

db.doc(collectionLocation).update(jsonObject)
.then(function(docRef) {
goCallback(callbackFunction,codeSuccess);
debugLog("Document/Set","Successfully written the document to "+ collectionLocation +". Document ID : " + docRef.id);
console.log("Document successfully written!");
})
.catch(function(error) {
goCallback(callbackFunction,codeError);
debugLog("Document/Get","Error SETting the document. Error code : " + error);
});
}


///###CREATE QUERY
function firestore_web_query_create(collectionLocation) {

queryRef = db.collection(collectionLocation);
debugLog("Query/Create","Query created for " + collectionLocation);

}

function firestore_web_query_equal_to(key, value, dataType) {

if (dataType=="fire_data_string") {
queryRef = queryRef.where(key, "==", value);
}
else {
queryRef = queryRef.where(key, "==", parseFloat(value));
}
debugLog("Query/Build","Checks if " + key + "==" + value);

}

function firestore_web_query_greater_than_or_equal_to(key, value) {

queryRef = queryRef.where(key, ">=", value);
debugLog("Query/Build","Checks if " + key + ">=" + parseFloat(value));
}

function firestore_web_query_less_than_or_equal_to(key, value) {

queryRef = queryRef.where(key, "<=", value);
debugLog("Query/Build","Checks if " + key + "<=" + parseFloat(value));
}

function firestore_web_query_greater_than(key, value) {

queryRef = queryRef.where(key, ">", value);
debugLog("Query/Build","Checks if " + key + ">" + parseFloat(value));

}

function firestore_web_query_less_than(key, value) {

queryRef = queryRef.where(key, "<", value);
debugLog("Query/Build","Checks if " + key + "<" + parseFloat(value));

}

function firestore_web_query_limit(value) {

queryRef = queryRef.limit(value);
debugLog("Query/Build","Limits the query size to " + parseFloat(value));

}

function firestore_web_query_orderby(key) {

queryRef = queryRef.orderBy(key);
debugLog("Query/Build","Orders the data by the key : " + key);

}

function firestore_web_query_orderby_ext(key, order) {

if (order=="ASC"){
queryRef = queryRef.orderBy(key,'asc');
debugLog("Query/Build","Orders the data by the key : " + key + " in ASCENDING order.");
} else {
queryRef = queryRef.orderBy(key,'desc');
debugLog("Query/Build","Orders the data by the key : " + key + " in DESCENDING order.");
}

}

function firestore_web_query_execute_get(callbackFunction) {

queryRef.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {

debugLog("Query/Execute/Get","Document received from a query, being redirected to " + callbackFunction);

transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);

});
});

}


function firestore_web_query_execute_listen_all(callbackFunction, datalistenerID) {

var listener = queryRef.onSnapshot(function(querySnapshot) {

querySnapshot.forEach(function(doc) {
debugLog("Query/Execute/Listen","Document received from a query listener, being redirected to " + callbackFunction);

transferDataToJson(doc.data());
goCallback(callbackFunction, codeSuccess);

});
});
dataListenerAdd(dataListenerID,listener);
}


function firestore_web_query_execute_listen_changes(addedCallbackFunction, modifiedCallbackFunction, removedCallbackFunction, dataListenerID) {

var listener = queryRef.onSnapshot(function(snapshot) {
snapshot.docChanges().forEach(function(change) {
if (change.type === "added") {
debugLog("Query/Execute/Listen/Changes","A document is added to the Query scope, being redirected to " + addedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(addedCallbackFunction, codeSuccess);
}
if (change.type === "modified") {
debugLog("Query/Execute/Listen/Changes","A Document is modified within the Query scope, being redirected to " + modifiedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(modifiedCallbackFunction, codeSuccess);

}
if (change.type === "removed") {
debugLog("Query/Execute/Listen/Changes","A Document is removed from the Query scope, being redirected to " + removedCallbackFunction);
transferDataToJson(change.doc.data());
goCallback(removedCallbackFunction, codeSuccess);

}
});
});
dataListenerAdd(dataListenerID,listener);
}



///### INTERNAL FUNCTIONS. NON RELEVANT WITH GM
function dataListenerAdd(key,value) {
dataListener[key] = value;
debugLog("Listener/Remove","Started listening to the area marked with " + key + " key.");
}

function dataListenerRemove(key) {
var listener = dataListener[key];
listener();
delete dataListener[key];
debugLog("Listener/Remove","Stopped listening to the area marked with " + key + " key.");
}

function transferDataToJson(jsonData) {

resultString = JSON.stringify(jsonData);
}

function debugLog (category, message) {
if (debugMode==true) {
console.log("Game Maker Firebase Web ["+category+"] : " + message);
}
}


function goCallback(func, status) {
if (func!="")
{
eval("gml_Script_"+func+"(null,null,'"+status+"')");
}
}

function goCallbackExt(func, status, extra) {
eval("gml_Script_"+func+"(null,null,'"+status+"','+"+ extra +")");
}

0 comments on commit f7992a0

Please sign in to comment.
You can’t perform that action at this time.