Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Boards Optimizations #17

Open
Aster0 opened this issue Jun 12, 2022 · 0 comments
Open

Boards Optimizations #17

Aster0 opened this issue Jun 12, 2022 · 0 comments
Assignees
Labels
completed documentation Improvements or additions to documentation implementation New feature or request

Comments

@Aster0
Copy link
Owner

Aster0 commented Jun 12, 2022

For how sending chat works, please visit #21.

The boards are now optimized to only load the data when it's scanned by the player.

When the tracking is lost, the boards will turn off the asynchronous update to the database. Meaning, it won't receive anymore updates from the board's chat.

image

*When the board loses its tracking - *

    public void OnBoardHide()
    {
        if(listener != null)
            listener.Dispose(); // dispose the async listener
    }

*When the board being tracked, it opens an asynchronous listener to Firebase. - *

   public void CacheBoard()
    {
        FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
        DocumentReference docRef = db.Collection("boards").Document(boardName);
        listener = docRef.Listen(snapshot => {




            foreach (Transform child in transform)
            {
                Destroy(child.gameObject);
            }
            
            
            Debug.Log("Callback received document snapshot.");
            Debug.Log(String.Format("Document data for {0} document:", snapshot.Id));

       
            
            Dictionary<string, object> dict = snapshot.ToDictionary();


            List<object> chats = 
                dict["chats"] as List<object>;


            //_gameManager.currentChat = chats; // 10/04/2022

            _gameManager.currentBoard[boardName] = chats;

            if (chats.Count == 0)
            {
                return;
            }
            
            foreach (Dictionary<string, object> chat in chats)
            {


                GameObject gameObject = _gameManager.chatBoardPrefab;
                

                if (chat["contents"].ToString().StartsWith("STICKER//"))
                {
                    gameObject = _gameManager.chatBoardStickerPrefab;
                }
                
                
             
             
                GameObject chatBoardObject = Instantiate(gameObject, new Vector3(0,0),
                    Quaternion.identity);
            
                chatBoardObject.transform.SetParent(this.gameObject.transform, false);



                ChatBoard chatBoard = chatBoardObject.GetComponent<ChatBoard>();
                
                
                FirebaseFirestore db = FirebaseFirestore.DefaultInstance;
                DocumentReference docRef = db.Collection("users").Document(
                    chat["user"].ToString());

                docRef.GetSnapshotAsync().ContinueWithOnMainThread((task) =>
                {

                    DocumentSnapshot snapshot = task.Result;
                    Dictionary<string, object> userDictionary = snapshot.ToDictionary();


             
                    try 
                    {
                        // see if the player has an avatar first
                        Dictionary<string, object> userDetails = userDictionary["details"] as Dictionary<string, object>;
                        
                        if(userDetails != null) // null check
                            chatBoard.BuildChatBoard(userDictionary["username"].ToString(),
                                chat["contents"].ToString(), chat["user"].ToString(), userDetails["current_avatar"].ToString()); // load with player avatar
                    
                    }
                    catch (KeyNotFoundException avatarNotFound) // if the player has no avatar
                    {
                        chatBoard.BuildChatBoard(userDictionary["username"].ToString(),
                            chat["contents"].ToString(), chat["user"].ToString()); // load with no avatar as player do not have an avatar
                    
                    }

              
               
                   
                    // make sure the chat is finished building, then scroll to the bottom.
                    // this means that every new chat that is added (i.e. send), will bring the chat to the bottom too.
                    ScrollToBottom();
                    
                });
         

          
                
                chatBoardObject.transform.localScale = new Vector3(1, 1, 1);
                chatBoardObject.transform.localPosition = new Vector3(0, 0, -13);
                chatBoardObject.transform.localRotation = Quaternion.Euler(new Vector3(0,0));
                
               
            }
                
     



        });
        
        
    }
@Aster0 Aster0 self-assigned this Jun 12, 2022
@Aster0 Aster0 added documentation Improvements or additions to documentation implementation New feature or request completed labels Jun 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
completed documentation Improvements or additions to documentation implementation New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant