Ejemplo n.º 1
0
    //Method to create a village for the player, this is attached a button that is only available if the player does not have a village
    //Once village is created (and database updated) the edit village scene is loaded
    public void CreateVillage()
    {
        if (PlayerCheck.isCollidingNow())
        {
            //Show Toast
            WorldSceneToastMaker.showToast("You cannot build here as you are in an enemy zone", 2);
            return;
        }
        String id = vilRef.Push().Key;

        //Debug.Log(reference);
        //Debug.Log(reference.Child("villages"));
        //Debug.Log(id);
        String[] loc = location.text.Split(',');//lng then lat
        double   lat = Convert.ToDouble(loc[1]);
        double   lng = Convert.ToDouble(loc[0]);
        Villages vil = new Villages(user.Email, lat, lng, id);
        //reference.Child("villages").Child(key).Child("User").SetValueAsync(email);
        //vilRef.Child(id).Child("User").SetValueAsync(email);

        /*
         *  string json = JsonUtility.ToJson(user);
         *
         * mDatabaseRef.Child("users").Child(userId).SetRawJsonValueAsync(json);
         */
        String json = JsonUtility.ToJson(vil);

        vilRef.Child(id).SetRawJsonValueAsync(json);


        FirebaseDatabase.DefaultInstance.GetReference("users").GetValueAsync().ContinueWith(task => {
            if (task.IsFaulted)
            {
                // Handle the error...
            }
            else if (task.IsCompleted)
            {
                DataSnapshot dataSnapshot = task.Result;
                // Do something with snapshot...
                foreach (DataSnapshot s in dataSnapshot.Children)
                {
                    if (user.Email.Equals(s.Child("email").Value.ToString()))
                    {
                        //Debug.Log("Here: " + s.Child("email").Value + " " + s.Child("id").Value + " " + s.Child("villageID").Value + " " + s.Child("balance").Value);
                        //Villages vil = new Villages(s.Child("email").Value.ToString(), s.Child("lat").Value.ToString(), s.Child("lng").Value.ToString());
                        Accounts acc = new Accounts(s.Child("email").Value.ToString(), s.Child("id").Value.ToString(), int.Parse(s.Child("balance").Value.ToString()), id);
                        json         = JsonUtility.ToJson(acc);
                        reference.Child("users").Child(s.Child("id").Value.ToString()).SetRawJsonValueAsync(json);
                    }
                }
            }
        });

        //Change scene
        //SceneManager.LoadSceneAsync(2);
        Scenes.Load("BuildScene - Copy", "villageID", id);
    }