fix: clearing room when new game

This commit is contained in:
Fangh 2024-02-04 13:57:13 +01:00
parent aa410f21e5
commit e9ac369702

View File

@ -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.");
}
}
/// <summary>
@ -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<bool> 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
}
}
}
}