public void ShowAvailableGames() { connectDialog.SetActive(true); hostJoinButtons.SetActive(false); firebaseDatabaseReference.Child("games").Child("pending").GetValueAsync() .ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { // Get availableGames from snapshot DataSnapshot snapshot = task.Result; IEnumerable <DataSnapshot> snapChildren = snapshot.Children; foreach (DataSnapshot child in snapChildren) { MultiplayerInstance availableGame = JsonUtility.FromJson <MultiplayerInstance>(child.GetRawJsonValue()); // Display availableGame item GameObject availableGameItem = Instantiate(availableGamePrefab) as GameObject; availableGameItem.transform.SetParent(availableGamesContainer); availableGameItem.GetComponentInChildren <Text>().text = availableGame.gameKey; } } }); }
public void JoinGame(string joinGameID) { // Join MultiplayerInstance for firebase, set GameManager vars DatabaseReference newGameReference = firebaseDatabaseReference.Child("games").Child("pending").Child(joinGameID); newGameReference.GetValueAsync() .ContinueWith(task => { if (task.IsFaulted) { // Handle the error... } else if (task.IsCompleted) { // Get availableGames from snapshot DataSnapshot snapshot = task.Result; MultiplayerInstance joinGameGame = JsonUtility.FromJson <MultiplayerInstance>(snapshot.GetRawJsonValue()); joinGameGame.playersList.Add(PLAYER_ID); string joinGameJson = JsonUtility.ToJson(joinGameGame); newGameReference.SetRawJsonValueAsync(joinGameJson); // Listen for a Joiner newGameReference.ValueChanged += FirebaseReadyGameListener; GAME_ID = joinGameID; PLAYER_TEAM = 1; } }); }
void FirebaseReadyGameListener(object sender, ValueChangedEventArgs args) { if (args.DatabaseError != null) { Debug.LogError(args.DatabaseError.Message); return; } // Delete pending game and start game in client MultiplayerInstance joinedGame = JsonUtility.FromJson <MultiplayerInstance>(args.Snapshot.GetRawJsonValue()); if (joinedGame.isStarted == true) { // Set isStarted to true in pending game firebaseDatabaseReference.Child("games").Child("pending").Child(GAME_ID).RemoveValueAsync(); // Remove gameReady listener firebaseDatabaseReference.Child("games").Child("pending").Child(GAME_ID).ValueChanged -= FirebaseReadyGameListener; // GOTO game GoToGame(); } }
// Host / Join functions public void HostGame() { connectDialog.SetActive(true); hostJoinButtons.SetActive(false); // Create MultiplayerInstance for firebase, set GameManager vars DatabaseReference pendingGameReference = firebaseDatabaseReference.Child("games").Child("pending").Push(); string referenceKey = pendingGameReference.Key; GAME_ID = referenceKey; PLAYER_TEAM = 0; List <string> newPlayerList = new List <string>(); newPlayerList.Add(PLAYER_ID); MultiplayerInstance newGame = new MultiplayerInstance(false, referenceKey, newPlayerList); string newGameJson = JsonUtility.ToJson(newGame); pendingGameReference.SetRawJsonValueAsync(newGameJson); // Listen for a Joiner pendingGameReference.ValueChanged += FirebaseJoinListener; }
// Firebase listeners void FirebaseJoinListener(object sender, ValueChangedEventArgs args) { if (args.DatabaseError != null) { Debug.LogError(args.DatabaseError.Message); return; } // Set isStarted to true and start game instance in host client MultiplayerInstance joinedGame = JsonUtility.FromJson <MultiplayerInstance>(args.Snapshot.GetRawJsonValue()); if (joinedGame.playersList.Count >= 2) { // Set isStarted to true in pending game firebaseDatabaseReference.Child("games").Child("pending").Child(GAME_ID).Child("isStarted").SetValueAsync(true); // Remove Joiner listener firebaseDatabaseReference.Child("games").Child("pending").Child(GAME_ID).ValueChanged -= FirebaseJoinListener; // Create game in active node string joinedGameJson = JsonUtility.ToJson(joinedGame); firebaseDatabaseReference.Child("games").Child("active").Child(GAME_ID).SetRawJsonValueAsync(joinedGameJson); // GOTO game GoToGame(); } }