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_Name:
m_EditorClassIdentifier:
currentPlayer:
name:
id:
explanationTime: 4
roomCodeField: {fileID: 2023851070}
roomError: {fileID: 991600093}
@ -3485,6 +3488,12 @@ MonoBehaviour:
VotePicture: {fileID: 531335861}
WaitingOtherPlayers: {fileID: 2095389711}
EndGame: {fileID: 1850164816}
myRoom:
code:
questions: []
players: []
currentQuestion: 0
creationDate: 0
--- !u!1 &1224049644
GameObject:
m_ObjectHideFlags: 0
@ -6173,6 +6182,7 @@ GameObject:
- component: {fileID: 2010012970}
- component: {fileID: 2010012969}
- component: {fileID: 2010012968}
- component: {fileID: 2010012971}
m_Layer: 5
m_Name: SubmitBtn
m_TagString: Untagged
@ -6243,7 +6253,19 @@ MonoBehaviour:
m_TargetGraphic: {fileID: 2010012969}
m_OnClick:
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
MonoBehaviour:
m_ObjectHideFlags: 0
@ -6282,6 +6304,20 @@ CanvasRenderer:
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 2010012966}
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
GameObject:
m_ObjectHideFlags: 0

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -15,7 +15,7 @@ public class GameManager : MonoBehaviour
{
private GameState currentState;
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")]
[SerializeField]
public GameObject HomeConnection;
[SerializeField]
public GameObject WaitingRoom;
[SerializeField]
public GameObject BeforeStart;
[SerializeField]
public GameObject TakePicture;
[SerializeField]
public GameObject VotePicture;
[SerializeField]
public GameObject WaitingOtherPlayers;
[SerializeField]
public GameObject EndGame;
private DatabaseReference realtimeDB;
private Room myRoom;
public Room myRoom;
private DatabaseReference myOnlineRoom;
private void Awake()

View File

@ -1,5 +1,3 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

View File

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

View File

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

View File

@ -1,9 +1,7 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Firebase.Extensions;
using Firebase.Firestore;
using System;
using Firebase.Extensions;
using UnityEngine;
/// <summary>
/// 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")]
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.Extensions;
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using System;
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: