Implementation der Datenbankschnittstelle - thm-mni-ii/SWT-P_SS20_Dixit GitHub Wiki

Zum Speichern unserer Fragen nutzen wir Cloud Firestore. Daten werden in folgender Struktur gespeichert:

  • swt-p-ss20-profcollector
    • QuestionSets
      • Docent: String
      • Module: String
      • Name: String
      • Questions: DocumentReference[]
    • Questions
      • Answer: String
      • Difficulty: int
      • Explanation: String
      • QuestionText: String

Es können also Fragen und Fragensammlungen angelegt werden.

Benutzung in Unity

Um Daten von der Datenbank anfragen zu können muss für das Projekt eine Standartinstanz also eine Referenz auf die genutzte Datenbank gesetzt werden.

private readonly Lazy<FirebaseFirestore> _db = new Lazy<FirebaseFirestore>(() =>
    {
        // Set up the Editor before calling the database.
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://swt-p-ss20-profcollector.firebaseio.com/");

        // Get the root reference location of the database.
        return FirebaseFirestore.DefaultInstance;
    });
public FirebaseFirestore DB => _db.Value;

Spielintern werden Fragensammlungen und Fragen durch die Klassen QuestionSet und Question repräsentiert. Fragensammlungen und Fragen werden wie folgt aus der Datenbank geholt:

Fragensammlungen:

var docRef = db.Collection("QuestionSets").Document(questionSetID);

return docRef.GetSnapshotAsync().ContinueWith((task) =>
{
    if (task.IsFaulted) throw task.Exception;

    var snapshot = task.Result;
    if (!snapshot.Exists)
    {
        Debug.Log(string.Format("QuestionSet document {0} does not exist!", snapshot.Id));
        return null;
    }
    return snapshot.ConvertTo<QuestionSet>();
});

Fragen:

// With a DocumentReference to a Queastion from a previously fetched QuestionSet
return reference.GetSnapshotAsync().ContinueWith<Question>((task) =>
{
    if (task.IsFaulted) throw task.Exception;

    var snapshot = task.Result;
    if (!snapshot.Exists)
    {
        Debug.Log(string.Format("Question document {0} does not exist!", snapshot.Id));
        return null;
    }
    return snapshot.ConvertTo<Question>();
});
⚠️ **GitHub.com Fallback** ⚠️