/// <summary> /// saving report to database /// </summary> /// <param name="report">saving report</param> /// <param name="callback">callback to notify caller when task is finished</param> /// <exception cref="System.ArgumentNullException">Thrown when callback not provided</exception> public void saveReport(jReport report, Action <bool, string> callback) { if (callback == null) { throw new ArgumentNullException("callback is not provided"); } //define the node we are going to save to as ROOT_REPORT var refReport = _db.RootReference.Child(ROOT_REPORT); //convert report that is passed in to json format string json = JsonConvert.SerializeObject(report); //create a key under ROOT_REPORT string key = refReport.Push().Key; //save the report to firebase refReport.Child(key).SetRawJsonValueAsync(json).ContinueWithOnMainThread((task) => { string message = readError(task); if (message != null) { Debug.Log("saveReport.e: " + message); callback(false, message); return; } message = "task is success"; Debug.Log("saveReport: " + message); callback(true, message); }); }
//method to save created report to database public void savejReport(jReport report) { //save generated report to the database FirebaseManager.getInstance().saveReport(report, (result, msg) => { Debug.Log(msg); }); }
//method to change to reportDetails page public void ChangeScene(jReport report) { Debug.Log("changescene 1, exists = " + exists); reportDetails rd = obj.GetComponent <reportDetails>(); //modify the text in reportDetails.unity with relevant information. rd.ModText(report); //change to reportDetails.unity page. SceneManager.LoadScene(22); //reset exists variable. exists = rd.ReturnExists(exists); Debug.Log("changescene 2, exists = " + exists); }
//modify the text in the page to details in the report retrieved. public void ModText(jReport report) { date.text = report.date; time.text = report.time; nss1.text = report.nss1.ToString(); nss2.text = report.nss2.ToString(); nss3.text = report.nss3.ToString(); nss4.text = report.nss4.ToString(); nss5.text = report.nss5.ToString(); nas1.text = report.nas1.ToString(); nas2.text = report.nas2.ToString(); nas3.text = report.nas3.ToString(); nas4.text = report.nas4.ToString(); nas5.text = report.nas5.ToString(); nacm.text = report.nacm.ToString(); }
/// <summary> /// retrive all saved report from database /// </summary> /// <param name="callback">callback to notify caller when task is finished</param> public void getAllReport(Action <List <jReport>, string> callback) { //define the node we are going to retrieve information from as ROOT_REPORT var refAllReport = _db.RootReference.Child(ROOT_REPORT); //retrieve all reports found under ROOT_REPORT refAllReport.GetValueAsync().ContinueWithOnMainThread(task => { string message = readError(task); if (message != null) { callback(null, message); return; } if (task.IsCompleted) { message = "task is success"; var snapshot = task.Result; var resultDict = JsonConvert.DeserializeObject <Dictionary <string, jReport> >(snapshot.GetRawJsonValue()); var list = new List <jReport>(); foreach (string key in resultDict.Keys) { jReport data = resultDict[key]; //if its not a init node if (data.date != "01/01/0001") { list.Add(data); } else { //if it is an init node, remove it from the database DatabaseReference rmvthisdata = _db.GetReference(ROOT_REPORT).Child(key); rmvthisdata.RemoveValueAsync(); } } callback(list, message); } }); }
void Start() { //create initial report to create root in database. will be removed when reports are retrieved. jReport tmp = new jReport(); tmp.date = init.ToString("dd/MM/yyyy"); savejReport(tmp); //get the date of the system now. date = System.DateTime.Now; Debug.Log("date is = " + date); //get all reports in the database if any. FirebaseManager.getInstance().getAllReport((result, msg) => { Debug.Log("getAllReport, operation : " + msg); if (result.Count >= 1) { //list is not empty. remove empty message. empty.SetActive(false); if (date.DayOfWeek == DayOfWeek.Sunday) { foreach (jReport t_report in result) { if ((t_report.date.Equals(date.ToString("dd/MM/yyyy")))) { //if sunday, check all reports in database. //if a report in database has the same date as today, set exists flag to true; exists = true; break; } else { exists = false; } } //if report does not exist in database, generate the report. Debug.Log("start, exists = " + exists); if (exists == false) { getData(); } //if report exists in database, just generate the report. else { generateReports(); } } else { //when its not a sunday, just generate a report generateReports(); } } else { //if returned list is empty... Debug.Log("result count : " + result.Count); if (date.DayOfWeek == DayOfWeek.Sunday) { getData(); empty.SetActive(false); } else { empty.SetActive(true); } } }); }
//method to create a report based on GameData retrieved public void createReport(List <AppData> AppDataList) { //after retrieving the data, need to get neccessary perimeters. Debug.Log("createReports, appDataList.Count = " + AppDataList.Count); if (AppDataList.Count >= 1) { foreach (AppData data in AppDataList) { switch (data.progress.curWorld) { case 1: nss1++; nas1 += data.progress.avgAttempts; Debug.Log("nss1 = " + nss1); Debug.Log("nas1 = " + nas1); break; case 2: nss2++; nas1++; nas2 += data.progress.avgAttempts; Debug.Log("nss2 = " + nss2); Debug.Log("nas2 = " + nas2); break; case 3: nss3++; nas2++; nas1++; nas3 += data.progress.avgAttempts; Debug.Log("nss3 = " + nss3); Debug.Log("nas3 = " + nas3); break; case 4: nss4++; nas3++; nas2++; nas1++; nas4 += data.progress.avgAttempts; Debug.Log("nss4 = " + nss4); Debug.Log("nas4 = " + nas4); break; case 5: nss5++; nas4++; nas3++; nas2++; nas1++; nas5 += data.progress.avgAttempts; Debug.Log("nss5 = " + nss5); Debug.Log("nas5 = " + nas5); break; } nacm += data.challengePoints; } nacm /= (10 * AppDataList.Count); nas1 /= AppDataList.Count; nas2 /= AppDataList.Count; nas3 /= AppDataList.Count; nas4 /= AppDataList.Count; nas5 /= AppDataList.Count; jReport jreport = new jReport(); jreport.date = date.ToString("dd/MM/yyyy"); jreport.time = date.ToString("h:mm:ss tt"); jreport.nss1 = nss1; jreport.nss2 = nss2; jreport.nss3 = nss3; jreport.nss4 = nss4; jreport.nss5 = nss5; jreport.nas1 = nas1; jreport.nas2 = nas2; jreport.nas3 = nas3; jreport.nas4 = nas4; jreport.nas5 = nas5; jreport.nacm = nacm; savejReport(jreport); generateReports(); } else { print("no data"); } }