Snaparazzi/Assets/Scripts/PlayersEmulator.cs
2024-12-22 19:52:41 +01:00

61 lines
1.7 KiB
C#

using System;
using Firebase.Database;
using UnityEngine;
#if UNITY_EDITOR
/// <summary>
/// Class used only in editor in order to be able to test without real players
/// </summary>
public class PlayersEmulator : MonoBehaviour
{
private DatabaseReference realtimeDB;
private DatabaseReference myOnlineRoom;
private void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
private void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
realtimeDB = FirebaseDatabase.DefaultInstance.RootReference;
}
private void Update()
{
if(Input.GetKeyDown(KeyCode.N))
CreateNewFakePlayer();
if(Input.GetKeyDown(KeyCode.S))
StartGame();
}
/// <summary>
/// Use the context menu to create a new fake player in this game.
/// Use it after the room has been created
/// </summary>
[ContextMenu("Create New Fake Player")]
private void CreateNewFakePlayer()
{
Player fakeP = new Player("fakePlayer", Guid.NewGuid().ToString(), DateTime.Now.ToOADate());
string JSON = JsonUtility.ToJson(fakeP);
myOnlineRoom = realtimeDB.Child("rooms").Child(RoomManager.Instance.myRoom.code);
myOnlineRoom.Child("players").Child(fakeP.id).SetRawJsonValueAsync(JSON);
}
/// <summary>
/// Use the context menu to start the game from the Editor.
/// Use it after the room has been created & at least 3 players have joined
/// </summary>
[ContextMenu("Start Game")]
private void StartGame()
{
myOnlineRoom.Child("currentState").SetValueAsync((int)GameState.Explanation);
}
}
#endif