Exemplo n.º 1
0
    //public void WriteNewMessage(string messageID, long mesageDateTime, string sentBy, string message)
    //{

    //    AbilityMessage msg = new AbilityMessage(messageID, mesageDateTime, sentBy, message);
    //    string json = JsonUtility.ToJson(msg);

    //    dbReference.Child("Dummy").Child(messageID).SetRawJsonValueAsync(json);

    //}

    public async System.Threading.Tasks.Task ConnectToRoomAsync(string roomID, UnityAction <bool, string> callback)
    {
        var initialGetResult = await dbReference.Child("Rooms").Child(roomID).GetValueAsync();

        if (initialGetResult.Exists)
        {
            DataSnapshot snapshot = initialGetResult;

            if (snapshot.Value == null)
            {
                callback(false, "Room with this id doesn't exist.");
            }
            else
            {
                foreach (var child in snapshot.Children)
                {
                    if (child.Key == "PlayerOneID")
                    {
                        TheOtherPlayerID = child.Value.ToString();
                    }
                    if (child.Key == "PlayerTwoID")
                    {
                        if (child.Value.ToString() == "")
                        {
                            await dbReference.Child("Rooms").Child(roomID).Child("PlayerTwoID").SetValueAsync(PlayerID);

                            RoomID = roomID;
                            callback(true, "Connected!");
                            networkMessenger.Init(dbReference, RoomID, PlayerID, TheOtherPlayerID);
                            break;
                        }
                        else
                        {
                            callback(false, "Room is full.");
                            break;
                        }
                    }
                }
            }
        }
    }