Compare commits

..

2 Commits

Author SHA1 Message Date
Fangh
81134390f7 Merge branch 'main' of https://git.crystalyx.net/GameJams/Snaparazzi 2024-01-28 17:34:21 +01:00
Fangh
93bd799a1b cleaner 2024-01-28 17:34:12 +01:00
2 changed files with 117 additions and 41 deletions

View File

@ -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

View File

@ -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();
}
/// <summary>
/// Check all the rooms in the server and give back the number already taken
/// </summary>
private void WhichCodesAreAlreadyUsed(Action<List<int>> callback_OnCodesChecked)
{
Debug.Log("Checking other rooms to get which codes are already used", this);
List<int> alreadyUsedCodes = new List<int>();
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<string, Room> onlineRooms = JsonConvert.DeserializeObject<Dictionary<string, Room>>(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<int> 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);
});
}
/// <summary>
@ -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<Player> _players)
waitingForPlayersLabels[i].text = orderedPlayers[i].name;
}
}
private void QueryRoomsInDatabase(Action<List<Room>> 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<Room>());
return;
}
DataSnapshot snapshot = task.Result;
if (snapshot.Value != null)
{
string JSON = snapshot.GetRawJsonValue();
Debug.Log($"Found some rooms:\n{JSON}", this);
Dictionary<string, Room> onlineRooms = JsonConvert.DeserializeObject<Dictionary<string, Room>>(JSON);
callback?.Invoke(onlineRooms.Values.ToList());
}
else
{
Debug.Log("No rooms found in the database", this);
callback?.Invoke(new List<Room>());
}
});
}
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<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
}
}
}
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");
}
}
}