From 93bd799a1b7187cde65dc4e8c7222615b1d56858 Mon Sep 17 00:00:00 2001 From: Fangh Date: Sun, 28 Jan 2024 17:34:12 +0100 Subject: [PATCH] cleaner --- Assets/Scenes/ComputerView.unity | 4 +- Assets/Scripts/RoomManager.cs | 154 +++++++++++++++++++++++-------- 2 files changed, 117 insertions(+), 41 deletions(-) diff --git a/Assets/Scenes/ComputerView.unity b/Assets/Scenes/ComputerView.unity index b653cbd..2d10afc 100644 --- a/Assets/Scenes/ComputerView.unity +++ b/Assets/Scenes/ComputerView.unity @@ -38,7 +38,7 @@ RenderSettings: m_ReflectionIntensity: 1 m_CustomReflection: {fileID: 0} m_Sun: {fileID: 705507994} - m_IndirectSpecularColor: {r: 0.18028378, g: 0.22571412, b: 0.30692285, a: 1} + m_IndirectSpecularColor: {r: 0.18018535, g: 0.22559482, b: 0.30677685, a: 1} m_UseRadianceAmbientProbe: 0 --- !u!157 &3 LightmapSettings: @@ -10050,7 +10050,7 @@ MonoBehaviour: m_OnCullStateChanged: m_PersistentCalls: m_Calls: [] - m_Sprite: {fileID: 21300000, guid: 40956548d757c3c44b9ffe9e7317cc56, type: 3} + m_Sprite: {fileID: 21300000, guid: c61e5b2ecf71bd44fa029b05c6f7d0b2, type: 3} m_Type: 0 m_PreserveAspect: 0 m_FillCenter: 1 diff --git a/Assets/Scripts/RoomManager.cs b/Assets/Scripts/RoomManager.cs index ec727d8..d592699 100644 --- a/Assets/Scripts/RoomManager.cs +++ b/Assets/Scripts/RoomManager.cs @@ -7,6 +7,9 @@ using Newtonsoft.Json; using System.Linq; using Google.MiniJSON; +using Firebase.Storage; +using System.Security.Policy; +using System.Threading.Tasks; public class RoomManager : MonoBehaviour { @@ -139,12 +142,14 @@ private void ResetAllPlayerLabels() private void OnApplicationQuit() { - Debug.Log($"delete room {myRoom.code}"); + Debug.Log($"delete realtimeDB room {myRoom.code}"); realtimeDB.Child("rooms").Child(myRoom.code).RemoveValueAsync().ContinueWithOnMainThread(task => { Debug.Log($"room {myRoom.code} has been deleted"); myRoom = null; }); + Debug.Log($"delete files of room {myRoom.code}"); + DeleteRoomFiles(); } private void Initialize() @@ -152,52 +157,22 @@ private void Initialize() FirebaseInitializer.Instance.onFirebaseReady -= Initialize; realtimeDB = FirebaseDatabase.DefaultInstance.RootReference; Debug.Log("Realtime DB initialized"); + CleanOldRooms(); CreateNewRoom(); } /// /// Check all the rooms in the server and give back the number already taken /// + private void WhichCodesAreAlreadyUsed(Action> callback_OnCodesChecked) { - Debug.Log("Checking other rooms to get which codes are already used", this); - List alreadyUsedCodes = new List(); - try + QueryRoomsInDatabase(onlineRooms => { - realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => - { - Debug.Log("looking into the online rooms", this); - if (task.IsFaulted) - { - Debug.LogException(task.Exception); - } - else if (task.IsCompleted) - { - DataSnapshot snapshot = task.Result; - if (snapshot.Value != null) - { - string JSON = snapshot.GetRawJsonValue(); - Debug.Log($"found some rooms :\n{JSON}", this); - Dictionary onlineRooms = JsonConvert.DeserializeObject>(JSON); - foreach (Room r in onlineRooms.Values) - { - Debug.Log($"Code {r.code} is already used by another party", this); - alreadyUsedCodes.Add(int.Parse(r.code)); - } - } - else - { - Debug.Log($"Your party is the first one!", this); - } - } - callback_OnCodesChecked?.Invoke(alreadyUsedCodes); - }); - } - catch (Exception ex) - { - Debug.LogException(ex); - } - + List alreadyUsedCodes = onlineRooms.Select(room => int.Parse(room.code)).ToList(); + Debug.Log($"Codes {string.Join(", ", alreadyUsedCodes)} are already used by other parties", this); + callback_OnCodesChecked?.Invoke(alreadyUsedCodes); + }); } /// @@ -254,7 +229,6 @@ public void HostHasStartedGame() waitingForPlayersPage.SetActive(false); endOfPropositionDate = DateTime.Now.AddSeconds(propositionTime); - propositionLabelsByID.Clear(); //display only correct numbers of labels @@ -453,4 +427,106 @@ private void UpdateConnectedPlayerList(List _players) waitingForPlayersLabels[i].text = orderedPlayers[i].name; } } + + private void QueryRoomsInDatabase(Action> callback) + { + Debug.Log("Querying rooms from the database", this); + realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task => + { + if (task.IsFaulted) + { + Debug.LogException(task.Exception); + callback?.Invoke(new List()); + return; + } + + DataSnapshot snapshot = task.Result; + if (snapshot.Value != null) + { + string JSON = snapshot.GetRawJsonValue(); + Debug.Log($"Found some rooms:\n{JSON}", this); + Dictionary onlineRooms = JsonConvert.DeserializeObject>(JSON); + callback?.Invoke(onlineRooms.Values.ToList()); + } + else + { + Debug.Log("No rooms found in the database", this); + callback?.Invoke(new List()); + } + }); + } + + + private void CleanOldRooms() + { + QueryRoomsInDatabase(onlineRooms => + { + DateTime oneHourLater = DateTime.Now.AddHours(1); + + foreach (Room r in onlineRooms) + { + if (DateTime.FromOADate(r.creationDate) < oneHourLater) + { + Debug.Log($"Room {r.code} has been created at {DateTime.FromOADate(r.creationDate)} and it's more than one hour old. Deleting it"); + realtimeDB.Child("rooms").Child(r.code).RemoveValueAsync().ContinueWithOnMainThread(task => + { + if (task.IsFaulted) + { + Debug.LogException(task.Exception); + return; + } + Debug.Log($"Room {r.code} 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 + } + } + } + + private async void DeleteRoomFiles() + { + string roomCode = myRoom.code; //save it in case room has been deleted during you check files + + StorageReference roomFolder = FirebaseStorage.DefaultInstance.RootReference.Child(roomCode); + + bool pathExists = await DoesPathExistAsync(roomFolder); + + if (pathExists) + { + _ = roomFolder.DeleteAsync().ContinueWithOnMainThread(task => + { + if (task.IsCanceled || task.IsFaulted) + { + Debug.LogException(task.Exception); + } + else + { + Debug.Log($"Files of Room {roomCode} have been deleted"); + } + }); + } + else + { + Debug.Log($"Path for Room {roomCode} does not exist, nothing to delete"); + } + } }