示例#1
0
    private void RegisterDevice()
    {
        // Set these values before calling into the realtime database.
        FirebaseApp.DefaultInstance.SetEditorDatabaseUrl("https://fintech-expo.firebaseio.com/");
        //FirebaseApp.DefaultInstance.SetEditorP12FileName("YOUR-FIREBASE-APP-P12.p12");
        //FirebaseApp.DefaultInstance.SetEditorServiceAccountEmail("*****@*****.**");
        //FirebaseApp.DefaultInstance.SetEditorP12Password("notasecret");

        string           deviceId = SystemInfo.deviceUniqueIdentifier;
        FirebaseDatabase db       = FirebaseDatabase.DefaultInstance;

        Debug.Log("Registering device...");

        db.GetReference("TreasureHunt").OrderByChild("deviceId")
        .EqualTo(deviceId)
        .LimitToFirst(1)
        .GetValueAsync().ContinueWith
        (
            task =>
        {
            DataSnapshot snapshot = task.Result;
            Debug.Log("Records found: " + snapshot.ChildrenCount);
            if (snapshot.ChildrenCount == 0)
            {
                Debug.Log("Writing new record.");

                TreasureHuntRecord record = new TreasureHuntRecord(deviceId, new List <object>());
                Dictionary <string, object> recordValues = record.ToDictionary();
                Dictionary <string, object> childUpdates = new Dictionary <string, object>();
                childUpdates["/" + deviceId]             = recordValues;

                Debug.Log("Writing to: " + "/" + deviceId);
                db.GetReference("TreasureHunt").UpdateChildrenAsync(childUpdates);
            }
            else
            {
                //List<string> value= (List<string>)snapshot.Value;
                //Debug.Log("Found record with Key:" + value.Key);
            }
        }
        );
    }
示例#2
0
    private void RegisterCoinFound()
    {
        string deviceId  = SystemInfo.deviceUniqueIdentifier;
        string foundCoin = mTrackableBehaviour.TrackableName;

        Debug.Log("Device ID:" + deviceId);
        Debug.Log("Found Coin: " + foundCoin);

        //Pull the user's record.
        FirebaseDatabase db = FirebaseDatabase.DefaultInstance;

        db.GetReference("TreasureHunt").OrderByChild("deviceId")
        .EqualTo(deviceId)
        .LimitToFirst(1)
        .GetValueAsync().ContinueWith
        (
            task =>
        {
            DataSnapshot snapshot = task.Result;
            Debug.Log("Records found: " + snapshot.ChildrenCount);
            if (snapshot.ChildrenCount > 0)
            {
                foreach (var childSnapshot in snapshot.Children)
                {
                    Debug.Log("Found Record.");
                    Debug.Log(childSnapshot.GetRawJsonValue());
                    string _deviceId = childSnapshot.Child("deviceId").Value.ToString();

                    List <object> foundCoins = new List <object>();
                    if (childSnapshot.Child("foundCoins").Exists)
                    {
                        foundCoins = (List <object>)childSnapshot.Child("foundCoins").GetValue(false);
                    }

                    TreasureHuntRecord record = new TreasureHuntRecord(_deviceId, foundCoins);
                    if (!record.HasCoin(foundCoin))
                    {
                        Debug.Log("Adding Coin: " + foundCoin);
                        record.AddCoin(foundCoin);
                        Dictionary <string, object> recordValues = record.ToDictionary();
                        Dictionary <string, object> childUpdates = new Dictionary <string, object>();
                        childUpdates["/" + deviceId]             = recordValues;

                        Debug.Log("Writing to: " + "/" + deviceId);
                        db.GetReference("TreasureHunt").UpdateChildrenAsync(childUpdates);
                    }
                    //Only process 1 record (should ONLY have 1 record anyway!)
                    break;
                }
            }
            else
            {
                Debug.LogError("Unable to find record with Device ID: " + deviceId);
            }
        }
        );

        FirebaseDatabase.DefaultInstance
        .GetReference("TreasureHunt")
        .RunTransaction(FoundCoinTransaction)
        .ContinueWith(task => {
            if (task.Exception != null)
            {
            }
            else if (task.IsCompleted)
            {
            }
        });

        /*
         * FirebaseDatabase.DefaultInstance
         * //.GetReference("TreasureHunt")
         * .GetReference("users")
         * .GetValueAsync().ContinueWith(user => {
         *    //user.Result.Child
         *    DataSnapshot snapshot = user.Result;
         *
         *    Debug.Log(snapshot.ChildrenCount);
         *
         *    foreach (var childSnapshot in snapshot.Children)
         *    {
         *        Debug.Log("email:" + childSnapshot.Child("email").Value.ToString());
         *    }
         *
         * });
         */
    }