feat: StorageManager

This commit is contained in:
Michel Roux 2024-01-27 19:58:02 +01:00
parent 14f57134d2
commit fa81bb5895
15 changed files with 148 additions and 74 deletions

View File

@ -3470,6 +3470,9 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: 27a435387a7644784a6fc6ae538b68e5, type: 3} m_Script: {fileID: 11500000, guid: 27a435387a7644784a6fc6ae538b68e5, type: 3}
m_Name: m_Name:
m_EditorClassIdentifier: m_EditorClassIdentifier:
currentPlayer:
name:
id:
explanationTime: 4 explanationTime: 4
roomCodeField: {fileID: 2023851070} roomCodeField: {fileID: 2023851070}
roomError: {fileID: 991600093} roomError: {fileID: 991600093}
@ -3485,6 +3488,12 @@ MonoBehaviour:
VotePicture: {fileID: 531335861} VotePicture: {fileID: 531335861}
WaitingOtherPlayers: {fileID: 2095389711} WaitingOtherPlayers: {fileID: 2095389711}
EndGame: {fileID: 1850164816} EndGame: {fileID: 1850164816}
myRoom:
code:
questions: []
players: []
currentQuestion: 0
creationDate: 0
--- !u!1 &1224049644 --- !u!1 &1224049644
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -6173,6 +6182,7 @@ GameObject:
- component: {fileID: 2010012970} - component: {fileID: 2010012970}
- component: {fileID: 2010012969} - component: {fileID: 2010012969}
- component: {fileID: 2010012968} - component: {fileID: 2010012968}
- component: {fileID: 2010012971}
m_Layer: 5 m_Layer: 5
m_Name: SubmitBtn m_Name: SubmitBtn
m_TagString: Untagged m_TagString: Untagged
@ -6243,7 +6253,19 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 2010012969} m_TargetGraphic: {fileID: 2010012969}
m_OnClick: m_OnClick:
m_PersistentCalls: m_PersistentCalls:
m_Calls: [] m_Calls:
- m_Target: {fileID: 2010012971}
m_TargetAssemblyTypeName: StorageManager, Assembly-CSharp
m_MethodName: SaveFile
m_Mode: 1
m_Arguments:
m_ObjectArgument: {fileID: 0}
m_ObjectArgumentAssemblyTypeName: UnityEngine.Object, UnityEngine
m_IntArgument: 0
m_FloatArgument: 0
m_StringArgument:
m_BoolArgument: 0
m_CallState: 2
--- !u!114 &2010012969 --- !u!114 &2010012969
MonoBehaviour: MonoBehaviour:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0
@ -6282,6 +6304,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0} m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2010012966} m_GameObject: {fileID: 2010012966}
m_CullTransparentMesh: 1 m_CullTransparentMesh: 1
--- !u!114 &2010012971
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2010012966}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: 3b41deef4ebd372d5a18eabdb00cfbb4, type: 3}
m_Name:
m_EditorClassIdentifier:
PicturePlayer: {fileID: 1316036595}
Canvas: {fileID: 1202782726}
--- !u!1 &2023851068 --- !u!1 &2023851068
GameObject: GameObject:
m_ObjectHideFlags: 0 m_ObjectHideFlags: 0

View File

@ -1,8 +1,7 @@
using System.Collections; using System;
using System.Collections.Generic;
using System.Text.RegularExpressions; using System.Text.RegularExpressions;
[System.Serializable] [Serializable]
public class Player public class Player
{ {
public string name; public string name;
@ -10,7 +9,7 @@ public class Player
public Player(string _name) public Player(string _name)
{ {
id = System.Guid.NewGuid().ToString(); id = Guid.NewGuid().ToString();
name = _name; name = _name;
} }
@ -30,7 +29,7 @@ private string SanitizeString(string input)
// Limitez la longueur à 16 caractères maximum // Limitez la longueur à 16 caractères maximum
if (sanitized.Length > 16) if (sanitized.Length > 16)
{ {
sanitized = sanitized.Substring(0, 16); sanitized = sanitized[..16];
} }
return sanitized; return sanitized;

View File

@ -1,9 +1,8 @@
using System.Collections;
using System.Collections.Generic;
using Firebase.Firestore; using Firebase.Firestore;
using System;
using UnityEngine; using UnityEngine;
[System.Serializable, FirestoreData] [Serializable, FirestoreData]
public class Prompt public class Prompt
{ {
[field: SerializeField] [field: SerializeField]

View File

@ -1,8 +1,6 @@
using System.Collections; using System;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable] [Serializable]
public class Proposition public class Proposition
{ {
public string photoUrl; public string photoUrl;

View File

@ -1,9 +1,6 @@
using System; using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[System.Serializable] [Serializable]
public class Question public class Question
{ {
public string promptId; public string promptId;

View File

@ -1,7 +1,7 @@
using System;
using System.Collections.Generic; using System.Collections.Generic;
using UnityEngine;
[System.Serializable] [Serializable]
public class Room public class Room
{ {
public string code; public string code;
@ -13,10 +13,10 @@ public class Room
public Room(string _code) public Room(string _code)
{ {
this.code = _code; code = _code;
this.creationDate = System.DateTime.Now.ToOADate(); creationDate = DateTime.Now.ToOADate();
this.players = new List<Player>(); players = new List<Player>();
this.questions = new List<Question>(); questions = new List<Question>();
this.currentQuestion = 0; currentQuestion = 0;
} }
} }

View File

@ -1,7 +1,7 @@
using Firebase; using Firebase;
using System.Collections; using Firebase.Auth;
using UnityEngine;
using System; using System;
using UnityEngine;
using UnityEngine.Android; using UnityEngine.Android;
public class FirebaseInitializer : MonoBehaviour public class FirebaseInitializer : MonoBehaviour
@ -41,7 +41,7 @@ void Start()
// Update is called once per frame // Update is called once per frame
void Authenticate() void Authenticate()
{ {
Firebase.Auth.FirebaseAuth auth = Firebase.Auth.FirebaseAuth.DefaultInstance; FirebaseAuth auth = FirebaseAuth.DefaultInstance;
auth.SignInAnonymouslyAsync().ContinueWith(task => auth.SignInAnonymouslyAsync().ContinueWith(task =>
{ {
@ -56,7 +56,7 @@ void Authenticate()
return; return;
} }
Firebase.Auth.AuthResult result = task.Result; AuthResult result = task.Result;
Debug.Log($"User signed in successfully: {result.User.UserId}"); Debug.Log($"User signed in successfully: {result.User.UserId}");
onFirebaseReady?.Invoke(); onFirebaseReady?.Invoke();

View File

@ -15,7 +15,7 @@ public class GameManager : MonoBehaviour
{ {
private GameState currentState; private GameState currentState;
private List<Player> players = new List<Player>(); private List<Player> players = new List<Player>();
private Player currentPlayer= null; public Player currentPlayer = null;
@ -37,25 +37,18 @@ public class GameManager : MonoBehaviour
[Header("Pages")] [Header("Pages")]
[SerializeField]
public GameObject HomeConnection; public GameObject HomeConnection;
[SerializeField]
public GameObject WaitingRoom; public GameObject WaitingRoom;
[SerializeField]
public GameObject BeforeStart; public GameObject BeforeStart;
[SerializeField]
public GameObject TakePicture; public GameObject TakePicture;
[SerializeField]
public GameObject VotePicture; public GameObject VotePicture;
[SerializeField]
public GameObject WaitingOtherPlayers; public GameObject WaitingOtherPlayers;
[SerializeField]
public GameObject EndGame; public GameObject EndGame;
private DatabaseReference realtimeDB; private DatabaseReference realtimeDB;
private Room myRoom; public Room myRoom;
private DatabaseReference myOnlineRoom; private DatabaseReference myOnlineRoom;
private void Awake() private void Awake()

View File

@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine; using UnityEngine;
using UnityEngine.UI; using UnityEngine.UI;
@ -14,6 +12,6 @@ void Start()
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
} }
} }

View File

@ -1,25 +1,13 @@
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine; using UnityEngine;
public class PhoneLoop : MonoBehaviour public class PhoneLoop : MonoBehaviour
{ {
[SerializeField]
public GameObject homeConnection; public GameObject homeConnection;
[SerializeField]
public GameObject WaitingRoom; public GameObject WaitingRoom;
[SerializeField]
public GameObject BeforeStart; public GameObject BeforeStart;
[SerializeField]
public GameObject TakePicture; public GameObject TakePicture;
[SerializeField]
public GameObject VotePicture; public GameObject VotePicture;
[SerializeField]
public GameObject WaitingOtherPlayers; public GameObject WaitingOtherPlayers;
[SerializeField]
public GameObject EndGame; public GameObject EndGame;
// Start is called before the first frame update // Start is called before the first frame update
@ -31,7 +19,7 @@ void Start()
// Update is called once per frame // Update is called once per frame
void Update() void Update()
{ {
} }
void ShowWaititngRoom() void ShowWaititngRoom()
@ -82,20 +70,14 @@ void ShowVotePicture()
void ShowEndGame() void ShowEndGame()
{ {
WaitingOtherPlayers.SetActive(false) ; WaitingOtherPlayers.SetActive(false);
EndGame.SetActive(true); EndGame.SetActive(true);
} }
[ContextMenu("Fake Player Connection")] [ContextMenu("Fake Player Connection")]
private void FakePlayerConnection() private void FakePlayerConnection()
{ {
} }
} }

View File

@ -5,8 +5,7 @@
[CreateAssetMenu(fileName = "PromptList", menuName = "ScriptableObjects/Prompt List", order = 1)] [CreateAssetMenu(fileName = "PromptList", menuName = "ScriptableObjects/Prompt List", order = 1)]
public class PromptList : ScriptableObject public class PromptList : ScriptableObject
{ {
public List<Prompt> prompts = new List<Prompt>(); public List<Prompt> prompts = new();
/// <summary> /// <summary>
/// Create a new entry with a unique ID /// Create a new entry with a unique ID
@ -14,8 +13,10 @@ public class PromptList : ScriptableObject
[ContextMenu("Create Entry")] [ContextMenu("Create Entry")]
private void CreateEntry() private void CreateEntry()
{ {
Prompt temp = new Prompt(); Prompt temp = new()
temp.id = System.Guid.NewGuid().ToString(); {
id = Guid.NewGuid().ToString()
};
prompts.Add(temp); prompts.Add(temp);
} }
} }

View File

@ -1,9 +1,7 @@
using System.Collections; using Firebase.Extensions;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Firestore; using Firebase.Firestore;
using System; using System;
using Firebase.Extensions; using UnityEngine;
/// <summary> /// <summary>
/// This class will download or upload the prompts from/to the Firestore /// This class will download or upload the prompts from/to the Firestore
@ -145,7 +143,7 @@ private void AddPromptToFirestore(Prompt _prompt = null)
[ContextMenu("Generate Random Id")] [ContextMenu("Generate Random Id")]
private void GenerateRandomId() private void GenerateRandomId()
{ {
addPrompt.id = System.Guid.NewGuid().ToString(); addPrompt.id = Guid.NewGuid().ToString();
} }
} }

View File

@ -1,11 +1,9 @@
using System.Collections;
using System.Collections.Generic;
using Firebase;
using Firebase.Database; using Firebase.Database;
using Firebase.Extensions; using Firebase.Extensions;
using System;
using System.Collections.Generic;
using TMPro; using TMPro;
using UnityEngine; using UnityEngine;
using System;
public class RoomManager : MonoBehaviour public class RoomManager : MonoBehaviour
{ {

View File

@ -0,0 +1,64 @@
using Firebase.Storage;
using System;
using System.Threading.Tasks;
using UnityEngine;
public class StorageManager : MonoBehaviour
{
public GameObject PicturePlayer;
public GameObject Canvas;
private FirebaseStorage storage;
void Awake()
{
FirebaseInitializer.Instance.onFirebaseReady += Initialize;
}
void Initialize()
{
FirebaseInitializer.Instance.onFirebaseReady -= Initialize;
storage = FirebaseStorage.DefaultInstance;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void UploadPhoto()
{
Texture2D photo = PicturePlayer.GetComponent<CameraManager>().GetPhoto();
byte[] photoBytes = ImageConversion.EncodeToJPG(photo);
GameManager game = Canvas.GetComponent<GameManager>();
string imageUuid = Guid.NewGuid().ToString();
StorageReference storageRef = storage.GetReferenceFromUrl("gs://ggj2024-5cb41.appspot.com");
StorageReference imageRef = storageRef.Child(game.myRoom.code).Child(game.currentPlayer.id).Child($"{imageUuid}.jpg");
imageRef.PutBytesAsync(photoBytes).ContinueWith((Task<StorageMetadata> task) =>
{
if (task.IsFaulted || task.IsCanceled)
{
Debug.Log(task.Exception.ToString());
// Uh-oh, an error occurred!
}
else
{
// Metadata contains file metadata such as size, content-type, and md5hash.
StorageMetadata metadata = task.Result;
string md5Hash = metadata.Md5Hash;
Debug.Log("Finished uploading...");
Debug.Log("md5 hash = " + md5Hash);
}
});
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: 3b41deef4ebd372d5a18eabdb00cfbb4
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: