From e9ac36970272a321100eda9185799058f7057783 Mon Sep 17 00:00:00 2001 From: Fangh Date: Sun, 4 Feb 2024 13:57:13 +0100 Subject: [PATCH] fix: clearing room when new game --- Assets/Scripts/RoomManager.cs | 100 +++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index cee72ae..734f433 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -53,6 +53,11 @@ private void Awake() } private void Start() + { + InitializeFirstPage(); + } + + private void InitializeFirstPage() { explanationPage.SetActive(false); waitForPropositionsPage.gameObject.SetActive(false); @@ -98,29 +103,12 @@ private void ResetAllPlayerLabels() private bool deleteRoomFilesCompleted = false; private bool deleteRealtimeDBCompleted = false; - private IEnumerator OnApplicationQuit() + private void OnApplicationQuit() { - Debug.Log($"delete files of room {myRoom.code} from storage", this); - StorageManager.Instance.DeleteFileOfRoom(myRoom, () => + StartCoroutine(Coroutine_ClearCurrentRoom(() => { - Debug.Log($"cleaning photo files of room({myRoom.code})finish", this); - deleteRoomFilesCompleted = true; - }); - - Debug.Log($"delete realtimeDB room {myRoom.code}"); - realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync().ContinueWithOnMainThread(task => - { - Debug.Log($"room's({myRoom.code}) data has been deleted for database", this); - myRoom = null; - deleteRealtimeDBCompleted = true; - }); - - while (!deleteRoomFilesCompleted || !deleteRealtimeDBCompleted) - { - yield return null; // Yield until both tasks are completed - } - - Debug.Log("Everything is clean. You can go. Bye bye..."); + Debug.Log("You can go. Bye bye..."); + })); } private void Initialize() @@ -128,8 +116,51 @@ private void Initialize() FirebaseInitializer.Instance.onFirebaseReady -= Initialize; realtimeDB = FirebaseDatabase.DefaultInstance.RootReference; Debug.Log("Realtime DB initialized"); + ClearOldAndCreateNewRoom(); + } + + private void ClearOldAndCreateNewRoom() + { CleanOldRooms(); - CreateNewRoom(); + StartCoroutine(Coroutine_ClearCurrentRoom(() => + { + CreateNewRoom(); + })); + } + + private IEnumerator Coroutine_ClearCurrentRoom(Action callback_OnRoomClear = null) + { + if(myRoom == null || string.IsNullOrEmpty(myRoom.code)) + { + yield return null; + Debug.Log("There is no room to clean", this); + callback_OnRoomClear?.Invoke(); + } + else + { + Debug.Log($"delete files of room {myRoom.code} from storage", this); + StorageManager.Instance.DeleteFileOfRoom(myRoom, () => + { + Debug.Log($"cleaning photo files of room({myRoom.code})finish", this); + deleteRoomFilesCompleted = true; + }); + + Debug.Log($"delete realtimeDB room {myRoom.code}"); + realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync().ContinueWithOnMainThread(task => + { + Debug.Log($"room's({myRoom.code}) data has been deleted for database", this); + myRoom = null; + deleteRealtimeDBCompleted = true; + }); + + while (!deleteRoomFilesCompleted || !deleteRealtimeDBCompleted) + { + yield return null; // Yield until both tasks are completed + } + callback_OnRoomClear?.Invoke(); + + Debug.Log("Everything is clean."); + } } /// @@ -276,6 +307,10 @@ private void OnNewGameStateStarted() { switch (myRoom.currentState) { + case (int)GameState.EnteringName: //if game has been reset by a player on mobile + ClearOldAndCreateNewRoom(); + break; + case (int)GameState.WaitingForOtherPlayersToJoin: Debug.Log("New State : WaitingForOtherPlayersToJoin"); break; @@ -412,25 +447,4 @@ private void DeleteRoom(string _roomCode) Debug.Log($"Room {_roomCode} has been deleted"); }); } - - private async Task DoesPathExistAsync(StorageReference storageReference) - { - try - { - await storageReference.GetMetadataAsync(); - return true; // Path exists - } - catch (StorageException ex) - { - if (ex.ErrorCode == StorageException.ErrorObjectNotFound) - { - return false; // Path does not exist - } - else - { - Debug.LogException(ex); - return false; // Handle other errors as needed - } - } - } }