room can create a unique code
This commit is contained in:
parent
5a39339251
commit
449c0829ae
@ -2,7 +2,10 @@
|
|||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using Firebase;
|
using Firebase;
|
||||||
using Firebase.Database;
|
using Firebase.Database;
|
||||||
|
using Firebase.Extensions;
|
||||||
|
using TMPro;
|
||||||
using UnityEngine;
|
using UnityEngine;
|
||||||
|
using System;
|
||||||
|
|
||||||
public class RoomManager : MonoBehaviour
|
public class RoomManager : MonoBehaviour
|
||||||
{
|
{
|
||||||
@ -30,6 +33,11 @@ public class RoomManager : MonoBehaviour
|
|||||||
|
|
||||||
DatabaseReference realtimeDB;
|
DatabaseReference realtimeDB;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// TextMeshPro that show the value of the current rooom code
|
||||||
|
/// </summary>
|
||||||
|
public TextMeshProUGUI roomCodeLabel;
|
||||||
|
|
||||||
private void Awake()
|
private void Awake()
|
||||||
{
|
{
|
||||||
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
|
||||||
@ -41,8 +49,6 @@ private void Start()
|
|||||||
|
|
||||||
propositionCurrentTime = propositionTime;
|
propositionCurrentTime = propositionTime;
|
||||||
votingCurrentTime = votingTime;
|
votingCurrentTime = votingTime;
|
||||||
|
|
||||||
//we need to call CreateNewRoom. but not yet
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void OnApplicationQuit()
|
private void OnApplicationQuit()
|
||||||
@ -57,6 +63,33 @@ private void Initialize()
|
|||||||
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
|
||||||
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
|
||||||
Debug.Log("Realtime DB initialized");
|
Debug.Log("Realtime DB initialized");
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
List<int> alreadyUsedCodes = new List<int>();
|
||||||
|
realtimeDB.Child("rooms").GetValueAsync().ContinueWithOnMainThread(task =>
|
||||||
|
{
|
||||||
|
if (task.IsFaulted)
|
||||||
|
{
|
||||||
|
Debug.LogException(task.Exception);
|
||||||
|
}
|
||||||
|
else if (task.IsCompleted)
|
||||||
|
{
|
||||||
|
DataSnapshot snapshot = task.Result;
|
||||||
|
List<Room> onlineRooms = JsonUtility.FromJson<List<Room>>(snapshot.GetRawJsonValue());
|
||||||
|
foreach (Room r in onlineRooms)
|
||||||
|
{
|
||||||
|
alreadyUsedCodes.Add(int.Parse(r.code));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
callback_OnCodesChecked?.Invoke(alreadyUsedCodes);
|
||||||
|
});
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -65,11 +98,39 @@ private void Initialize()
|
|||||||
[ContextMenu("Create New Room")]
|
[ContextMenu("Create New Room")]
|
||||||
public void CreateNewRoom()
|
public void CreateNewRoom()
|
||||||
{
|
{
|
||||||
Room newRoom = new Room(Random.Range(0, 1000).ToString("D4"));
|
WhichCodesAreAlreadyUsed(codes =>
|
||||||
currentRoom = newRoom;
|
{
|
||||||
string JSON = JsonUtility.ToJson(newRoom);
|
Room newRoom = new Room(GenerateRandomAvailableCode(codes).ToString("D4"));
|
||||||
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON);
|
currentRoom = newRoom;
|
||||||
Debug.Log($"room {currentRoom.code} has been created on the server");
|
string JSON = JsonUtility.ToJson(newRoom);
|
||||||
|
realtimeDB.Child("rooms").Child(newRoom.code).SetRawJsonValueAsync(JSON).ContinueWithOnMainThread(task =>
|
||||||
|
{
|
||||||
|
Debug.Log($"room {currentRoom.code} has been created on the server");
|
||||||
|
//TODO MARINE : uncomment and reference the correct game object
|
||||||
|
//roomCodeLabel.text = currentRoom.code;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Generate a code between 0 and 1000 that is not in the list
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="_impossibleCodes">the list of code you don"t want to get</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
private int GenerateRandomAvailableCode(List<int> _impossibleCodes)
|
||||||
|
{
|
||||||
|
int random = UnityEngine.Random.Range(0, 1000);
|
||||||
|
while (_impossibleCodes.Contains(random))
|
||||||
|
{
|
||||||
|
Debug.Log($"{random} is already taken, choosing another room code", this);
|
||||||
|
random = UnityEngine.Random.Range(0, 1000);
|
||||||
|
}
|
||||||
|
return random;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DisplayRoomCode()
|
||||||
|
{
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void PlayerSendProposition(Proposition _proposition)
|
public void PlayerSendProposition(Proposition _proposition)
|
||||||
|
Loading…
Reference in New Issue
Block a user