Exemple #1
0
 /// <summary>
 /// handles on complete events, capture image, upload image, init stats and get profile picture
 /// </summary>
 /// <param name="task">The task firestore returns</param>
 public void OnComplete(Task task)
 {
     if (task == taskCaptureImage || task == taskUploadImage && task.IsSuccessful)
     {
         UploadTask.TaskSnapshot taskSnapshot = (UploadTask.TaskSnapshot)task.Result;
         user.ProfilePicture_url = taskSnapshot.DownloadUrl.ToString();
         sp.SetData(Constants.PROFILE_PIC_URL, user.ProfilePicture_url);
         fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.PROFILE_PIC_URL, user.ProfilePicture_url);
         ImageService.Instance.LoadUrl(user.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(Constants.DOWNSAMPLE_SIZE * 2, Constants.DOWNSAMPLE_SIZE * 2).Into(ivProfilePic);
     }
     else if (task == taskEqualCollection && task.IsSuccessful)
     {
         DocumentSnapshot ds = (DocumentSnapshot)task.Result;
         if (ds.Get(Constants.PROFILE_PIC_URL) != null)
         {
             //if the user set a profile picture manually already, we add its url to the user's information in sp and input the picture to the imageview
             user.ProfilePicture_url = (string)ds.Get(Constants.PROFILE_PIC_URL);
             sp.SetData(Constants.PROFILE_PIC_URL, user.ProfilePicture_url);
             ImageService.Instance.LoadUrl(user.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(Constants.DOWNSAMPLE_SIZE * 2, Constants.DOWNSAMPLE_SIZE * 2).Into(ivProfilePic);
         }
     }
     else if (task == taskInitStats && task.IsSuccessful)
     {
         DocumentSnapshot ds = (DocumentSnapshot)task.Result;
         user.TieNum   = ds.Get(Constants.TIE_NUM) != null ? (int)ds.Get(Constants.TIE_NUM) : 0;
         user.WinNum   = ds.Get(Constants.WIN_NUM) != null ? (int)ds.Get(Constants.WIN_NUM) : 0;
         user.LossNum  = ds.Get(Constants.LOSS_NUM) != null ? (int)ds.Get(Constants.LOSS_NUM) : 0;
         tvGames.Text  = user.TotalGamesNum().ToString();
         tvTies.Text   = user.TieNum.ToString();
         tvWins.Text   = user.WinNum.ToString();
         tvLosses.Text = user.LossNum.ToString();
     }
 }
        public static Persons DocumentSnapshotToPersons(DocumentSnapshot documentSnapshot)
        {
            int    id   = int.Parse(documentSnapshot.Get("PersonId").ToString());
            string name = documentSnapshot.Get("Name").ToString();
            int    age  = int.Parse(documentSnapshot.Get("Age").ToString());

            return(new Persons(id, name, age));
        }
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (snapshot.Exists())
            {
                string fullname  = snapshot.Get("fullname").ToString();
                string email     = snapshot.Get("email").ToString();
                string image_url = snapshot.Get("image_id") != null?snapshot.Get("image_id").ToString() : "";

                Helper.SaveFullname(fullname);
                Helper.SaveImageUrl(image_url);
                Helper.SaveEmail(email);
            }
        }
        private void InstantiateUser(Java.Lang.Object result)
        {
            DocumentSnapshot snap = (DocumentSnapshot)result;

            if (snap.Exists())
            {
                thisUser = new User
                {
                    Image_Url = snap.Get("image_id") != null?snap.Get("image_id").ToString() : "",
                                    Fullname = snap.Get("fullname").ToString(),
                                    User_Id  = snap.Id.ToString(),
                                    Email    = snap.Get("email").ToString()
                };
                InvokeEvent();
            }
        }
Exemple #5
0
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (!snapshot.Exists())
            {
                return;
            }

            DocumentReference likeReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID);

            if (like)
            {
                likeReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true);
            }
            else
            {
                //check for null
                if (snapshot.Get("likes") == null)
                {
                    return;
                }

                var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null;

                if (data != null)
                {
                    var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister);

                    //retrieve our own id
                    string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid;

                    //check if our user ID is contained inside the dictionary
                    if (dictionaryFromHashMap.Contains(uid))
                    {
                        //remove our user ID to unlike the post
                        dictionaryFromHashMap.Remove(uid);

                        //update the hashmap withour our userid included
                        likeReference.Update("likes", dictionaryFromHashMap);
                    }
                }
            }
        }
Exemple #6
0
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (snapshot.Exists())
            {
                string fullname = snapshot.Get("fullname").ToString();
                AppDataHelper.SaveFullName(fullname);
            }
        }
Exemple #7
0
        public void OnEvent(Java.Lang.Object value, FirebaseFirestoreException error)
        {
            DocumentSnapshot ds           = (DocumentSnapshot)value;
            string           opponentName = string.Empty;

            if (isInGame)
            {
                Finish();
            }
            else if (ds != null)
            {
                if ((bool)ds.Get(Constants.ISLIVE_GAME))
                {
                    //stop loading and begin game
                    pd.Dismiss();
                    opponentName = (string)ds.Get(Constants.PLAYER_GAME);
                    StartGame((string)ds.Get(Constants.GAMENUM), opponentName, game.Subject);
                }
            }
        }
        public void OnSuccess(Java.Lang.Object result)
        {
            DocumentSnapshot snapshot = (DocumentSnapshot)result;

            if (!snapshot.Exists())
            {
                return;
            }

            DocumentReference likesReference = AppDataHelper.GetFirestore().Collection("posts").Document(postID);

            if (Like)
            {
                likesReference.Update("likes." + AppDataHelper.GetFirebaseAuth().CurrentUser.Uid, true);
            }
            else
            {
                if (snapshot.Get("likes") == null)
                {
                    return;
                }

                var data = snapshot.Get("likes") != null?snapshot.Get("likes") : null;

                if (data != null)
                {
                    var dictionaryFromHashMap = new Android.Runtime.JavaDictionary <string, string>(data.Handle, JniHandleOwnership.DoNotRegister);

                    string uid = AppDataHelper.GetFirebaseAuth().CurrentUser.Uid;

                    if (dictionaryFromHashMap.Contains(uid))
                    {
                        dictionaryFromHashMap.Remove(uid);
                        likesReference.Update("likes", dictionaryFromHashMap);
                    }
                }
            }
        }
Exemple #9
0
        /// <summary>
        /// Handles the callbacks from firestore - responsible for pulling profile pictures of the player and opponent, presenting the questions
        /// , posting player answers to current game document and saving the players' results after the game is over.
        /// </summary>
        /// <param name="task"></param>
        public void OnComplete(Task task)
        {
            DocumentSnapshot ds = (DocumentSnapshot)task.Result;

            if (task == taskPullMyPic)
            {
                if (task.IsSuccessful && ds.Get(Constants.PROFILE_PIC_URL) != null)//if we did pull an image, we put it in the imageview (we are supposed to at this point)
                {
                    //if the user set a profile picture manually already, we add it to the user's information
                    user.ProfilePicture_url = (string)ds.Get(Constants.PROFILE_PIC_URL);
                    ImageService.Instance.LoadUrl(user.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(130, 100).Into(ivMePic);
                }
            }
            else if (task == taskPullOpponentPic)
            {
                if (task.IsSuccessful && ds.Get(Constants.PROFILE_PIC_URL) != null)//if we did pull an image, we put it in the imageview (we are supposed to at this point)
                {
                    //if the opponent set a profile picture manually already, we add it to the user's information
                    opponent.ProfilePicture_url = (string)ds.Get(Constants.PROFILE_PIC_URL);
                    ImageService.Instance.LoadUrl(opponent.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(130, 100).Into(ivOpponentPic);
                    InitGame();//we start the game only when we finish those tasks
                }
            }
            else if (task.IsSuccessful && task == taskPresentQuestion)
            {
                PresentQuestion(rand);
            }
            else if (task.IsSuccessful && task == taskAnsweredQue)
            {
                gameHashMap = SetHashMap(ds, gameHashMap);
                if (isHost)
                {
                    gameHashMap.Put(Constants.HOST_ANSWER, int.Parse(tvMyAnswer.Text));
                }
                else
                {
                    gameHashMap.Put(Constants.PLAYER_ANSWER, int.Parse(tvMyAnswer.Text));
                }
                timer.GameHM = gameHashMap;
                fd.AddDocumentToCollection(Constants.GAMES_COL, game.GameNum, gameHashMap);
            }
            else if (task.IsSuccessful && task == taskSavePlayerResult)
            {
                int currTieNum  = ds.Get(Constants.TIE_NUM) != null ? (int)ds.Get(Constants.TIE_NUM) : 0;
                int currWinNum  = ds.Get(Constants.WIN_NUM) != null ? (int)ds.Get(Constants.WIN_NUM) : 0;
                int currLossNum = ds.Get(Constants.LOSS_NUM) != null ? (int)ds.Get(Constants.LOSS_NUM) : 0;
                switch (gameStatus)//if tie = 0, if host wins = 1, if player wins = 2
                {
                case 0:
                    fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.TIE_NUM, ++currTieNum);
                    break;

                case 1:
                    if (isHost)
                    {
                        fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.WIN_NUM, ++currWinNum);
                    }
                    else
                    {
                        fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.LOSS_NUM, ++currLossNum);
                    }
                    break;

                case 2:
                    if (isHost)
                    {
                        fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.LOSS_NUM, ++currLossNum);
                    }
                    else
                    {
                        fd.UpdateDocument(Constants.FS_USERS_COL, user.UserName, Constants.WIN_NUM, ++currWinNum);
                    }
                    break;
                }
                sp.SetData(Constants.TIE_NUM, currTieNum);
                sp.SetData(Constants.WIN_NUM, currWinNum);
                sp.SetData(Constants.LOSS_NUM, currLossNum);
            }
        }
Exemple #10
0
 /// <summary>
 /// Sets and returns game hashmap according to info in document snapshot recieved
 /// </summary>
 /// <param name="ds">document of the current game</param>
 /// <param name="hm">the game hashmap</param>
 /// <returns></returns>
 public HashMap SetHashMap(DocumentSnapshot ds, HashMap GameHMap)
 {
     GameHMap.Put(Constants.GAMENUM, (string)ds.Get(Constants.GAMENUM));
     GameHMap.Put(Constants.HOST_GAME, (string)ds.Get(Constants.HOST_GAME));
     GameHMap.Put(Constants.PLAYER_GAME, (string)ds.Get(Constants.PLAYER_GAME));
     GameHMap.Put(Constants.CURRENT_Q_INDEX, (int)ds.Get(Constants.CURRENT_Q_INDEX));
     GameHMap.Put(Constants.ISLIVE_GAME, (bool)ds.Get(Constants.ISLIVE_GAME));
     GameHMap.Put(Constants.IS_RESTING_TIME, (bool)ds.Get(Constants.IS_RESTING_TIME));
     GameHMap.Put(Constants.IS_NEW_QUESTION, (bool)ds.Get(Constants.IS_NEW_QUESTION));
     GameHMap.Put(Constants.HOST_ANSWER, (int)ds.Get(Constants.HOST_ANSWER));
     GameHMap.Put(Constants.PLAYER_ANSWER, (int)ds.Get(Constants.PLAYER_ANSWER));
     GameHMap.Put(Constants.HOST_POINTS, (int)ds.Get(Constants.HOST_POINTS));
     GameHMap.Put(Constants.PLAYER_POINTS, (int)ds.Get(Constants.PLAYER_POINTS));
     return(GameHMap);
 }
Exemple #11
0
        /// <summary>
        /// Checks the answers of both player (if didn't answer - 0) and updates the textviews and points in firestore accordingly
        /// </summary>
        /// <param name="ds"></param>
        private void CheckAnsAndUpdate(DocumentSnapshot ds)
        {
            int hostAns   = ds.Get(Constants.HOST_ANSWER) != null ? (int)ds.Get(Constants.HOST_ANSWER) : 0;
            int playerAns = ds.Get(Constants.PLAYER_ANSWER) != null ? (int)ds.Get(Constants.PLAYER_ANSWER) : 0;
            int result    = CheckAnswers(hostAns, playerAns, (int)ds.Get(Constants.CURRENT_Q_INDEX));              //if tie = 0, if host wins = 1, if player wins = 2
            //init points
            int hostPoints   = ds.Get(Constants.HOST_POINTS) != null ? (int)ds.Get(Constants.HOST_POINTS) : 0;     //if there are no points we init to 0
            int playerPoints = ds.Get(Constants.PLAYER_POINTS) != null ? (int)ds.Get(Constants.PLAYER_POINTS) : 0; //if there are no points we init to 0

            switch (result)                                                                                        //if tie = 0, if host wins = 1, if player wins = 2
            {
            case Constants.TIE:
                hostPoints++;
                playerPoints++;
                break;

            case Constants.HOST_WINS:
                hostPoints++;
                break;

            case Constants.PLAYER_WINS:
                playerPoints++;
                break;
            }

            gameHashMap.Put(Constants.HOST_POINTS, hostPoints);
            gameHashMap.Put(Constants.PLAYER_POINTS, playerPoints);

            fd.UpdateDocument(Constants.GAMES_COL, game.GameNum, Constants.HOST_POINTS, hostPoints);
            fd.UpdateDocument(Constants.GAMES_COL, game.GameNum, Constants.PLAYER_POINTS, playerPoints);

            //put host and player answers + points on both screens
            if (isHost)
            {
                user.Points = hostPoints; opponent.Points = playerPoints;
                //init answers
                tvMyAnswer.Text       = ds.Get(Constants.HOST_ANSWER) != null ? ((int)ds.Get(Constants.HOST_ANSWER)).ToString() : "0";
                tvOpponentAnswer.Text = ds.Get(Constants.PLAYER_ANSWER) != null ? ((int)ds.Get(Constants.PLAYER_ANSWER)).ToString() : "0";
                //init points
                tvMyPoints.Text       = hostPoints.ToString();
                tvOpponentPoints.Text = playerPoints.ToString();
            }
            else
            {
                user.Points = playerPoints; opponent.Points = hostPoints;
                //init answers
                tvMyAnswer.Text       = ds.Get(Constants.PLAYER_ANSWER) != null ? ((int)ds.Get(Constants.PLAYER_ANSWER)).ToString() : "0";
                tvOpponentAnswer.Text = ds.Get(Constants.HOST_ANSWER) != null ? ((int)ds.Get(Constants.HOST_ANSWER)).ToString() : "0";
                //init points
                tvMyPoints.Text       = playerPoints.ToString();
                tvOpponentPoints.Text = hostPoints.ToString();
            }
        }
Exemple #12
0
        /// <summary>
        /// Handles callbacks from firestore, when certain values are changed, responsible for most of the game logic (when to show question/answer and when to change ect)
        /// </summary>
        /// <param name="value">value from firestore</param>
        /// <param name="error">exception</param>
        public void OnEvent(Java.Lang.Object value, FirebaseFirestoreException error)
        {
            DocumentSnapshot ds = (DocumentSnapshot)value;

            if (!isHost && ds.Get(Constants.CURRENT_Q_INDEX) != null && lastIndex != (int)ds.Get(Constants.CURRENT_Q_INDEX) && !isGameOver)
            {
                isInRest    = true;
                gameHashMap = SetHashMap(ds, gameHashMap);
                //reset the views before next question
                tvMyAnswer.Text       = 0.ToString();
                tvOpponentAnswer.Text = 0.ToString();
                tvAnswer.Text         = "תשובה";
                gameHashMap           = SetHashMap(ds, gameHashMap);
                int index = (int)ds.Get(Constants.CURRENT_Q_INDEX);
                lastIndex = index;
                PresentQuestion(index);
            }
            if (ds.Get(Constants.IS_RESTING_TIME) != null && (bool)ds.Get(Constants.IS_RESTING_TIME) && isInRest && !isGameOver)// if the question is done, we go to rest
            {
                gameHashMap = SetHashMap(ds, gameHashMap);
                isInRest    = false;
                //check answers
                CheckAnsAndUpdate(ds);
                gameStatus    = HasWon();            //if host wins = 1, if player wins = 2, if tie = 0, if not won at all = -1
                tvAnswer.Text = questions[(int)ds.Get(Constants.CURRENT_Q_INDEX)].Ans.ToString();
                if (gameStatus != Constants.NOT_WON) //if the game is over, we show the ending dialog
                {
                    isGameOver = true;
                    ShowGameEndDialog(gameStatus);
                }
                else if (!isGameOver)//else, we keep the game going
                {
                    if (isHost)
                    {
                        isNewQuestion = true;
                        gameHashMap.Put(Constants.IS_NEW_QUESTION, false);
                        gameHashMap.Put(Constants.HOST_ANSWER, 0);
                        gameHashMap.Put(Constants.PLAYER_ANSWER, 0);
                        fd.AddDocumentToCollection(Constants.GAMES_COL, game.GameNum, gameHashMap);
                    }
                    StartTimer(Constants.RESTING_TIME);//resting time after the answer is presented
                    //put the rest that is down there here after game is finished!
                }
                //we reset the answers of both players to 0, so we don't have them set for next round
            }
            if (isHost && ds.Get(Constants.IS_NEW_QUESTION) != null && (bool)ds.Get(Constants.IS_NEW_QUESTION) && isNewQuestion && !isGameOver)//host picks new question when resting is over
            {
                gameHashMap   = SetHashMap(ds, gameHashMap);
                isInRest      = true;
                isNewQuestion = false;//to not go in this condition many times at once...(bug)
                //reset the views before next question
                tvMyAnswer.Text       = 0.ToString();
                tvOpponentAnswer.Text = 0.ToString();
                tvAnswer.Text         = "תשובה";
                gameHashMap.Put(Constants.CURRENT_Q_INDEX, RandomNumber());
                gameHashMap.Put(Constants.IS_NEW_QUESTION, false);
                fd.AddDocumentToCollection(Constants.GAMES_COL, game.GameNum, gameHashMap);
                fd.AddSnapShotListenerToDocument(Constants.GAMES_COL, game.GameNum, this);
                PresentQuestion(rand);
            }
        }
Exemple #13
0
 public T Get <T>(string field)
 {
     return((T)_documentSnapshot.Get(field).ToFieldValue(new DocumentFieldInfo <T>()));
 }
Exemple #14
0
 public void OnComplete(Task task)
 {
     if (task == taskEqualCollection)
     {
         if (task.IsSuccessful)//if we did pull an image url, we put it in the imageview
         {
             DocumentSnapshot ds = (DocumentSnapshot)task.Result;
             //if the user set a profile picture manually already, we add its url to the user's information and input the picture to the imageview
             if (ds.Exists() && ds.Get(Constants.PROFILE_PIC_URL) != null)
             {
                 user.SetUserData(ds);
                 user.TieNum  = ds.Get(Constants.TIE_NUM) != null ? (int)ds.Get(Constants.TIE_NUM) : 0;
                 user.WinNum  = ds.Get(Constants.WIN_NUM) != null ? (int)ds.Get(Constants.WIN_NUM) : 0;
                 user.LossNum = ds.Get(Constants.LOSS_NUM) != null ? (int)ds.Get(Constants.LOSS_NUM) : 0;
                 InitStats();
                 user.ProfilePicture_url = (string)ds.Get(Constants.PROFILE_PIC_URL);
                 sp.SetData(Constants.PROFILE_PIC_URL, user.ProfilePicture_url);
                 sp.SetData(Constants.USERNAME, (string)ds.Get(Constants.USERNAME));
                 sp.SetData(Constants.EMAIL, (string)ds.Get(Constants.EMAIL));
                 sp.SetData(Constants.PASSWORD, (string)ds.Get(Constants.PASSWORD));
                 if (ds.Get(Constants.TIE_NUM) != null)
                 {
                     sp.SetData(Constants.TIE_NUM, (int)ds.Get(Constants.TIE_NUM));
                 }
                 if (ds.Get(Constants.WIN_NUM) != null)
                 {
                     sp.SetData(Constants.WIN_NUM, (int)ds.Get(Constants.WIN_NUM));
                 }
                 if (ds.Get(Constants.LOSS_NUM) != null)
                 {
                     sp.SetData(Constants.LOSS_NUM, (int)ds.Get(Constants.LOSS_NUM));
                 }
                 ImageService.Instance.LoadUrl(user.ProfilePicture_url).Retry(3, 200).FadeAnimation(true).DownSample(Constants.DOWNSAMPLE_SIZE, Constants.DOWNSAMPLE_SIZE).Into(ivMePic);
             }
             else//incase its a new user
             {
                 HashMap hm = user.SetUserData();
                 fd.AddDocumentToCollection(Constants.FS_USERS_COL, user.UserName, hm); //Init user's information to firebase, document name is user's username
             }
         }
     }
     else if (task == taskFindGame && task.IsSuccessful)
     {
         QuerySnapshot qs           = (QuerySnapshot)task.Result;
         string        gameNum      = "";
         bool          doStart      = false;
         string        opponentName = string.Empty;
         foreach (DocumentSnapshot ds in qs.Documents)//we check to see if someone is looking to start a game (if not we start a game)
         {
             bool isLive = (bool)ds.Get(Constants.ISLIVE_GAME);
             if (!isLive)//we found a game that's not started yet (1 or less players connected)
             {
                 gameNum = (string)ds.Get(Constants.GAMENUM);
                 HashMap hm = new HashMap();
                 if (ds.Get(Constants.HOST_GAME) != null)//check if there is already a player in the game, if yes we join him
                 {
                     opponentName = (string)ds.Get(Constants.HOST_GAME);
                     hm.Put(Constants.GAMENUM, gameNum);
                     hm.Put(Constants.HOST_GAME, opponentName);
                     hm.Put(Constants.PLAYER_GAME, user.UserName);
                     hm.Put(Constants.ISLIVE_GAME, true);                          //we put true to say a game is running
                     fd.AddDocumentToCollection(Constants.GAMES_COL, gameNum, hm); //put the information according to the game number
                     doStart = true;
                     isHost  = false;
                     break;
                 }
             }
         }
         if (doStart)
         {
             StartGame(gameNum, opponentName, game.Subject);
         }
         else
         {
             foreach (DocumentSnapshot ds in qs.Documents)//we know a game hasn't started yet because we looped all the available games
             {
                 bool isLive = (bool)ds.Get(Constants.ISLIVE_GAME);
                 if (!isLive)
                 {
                     gameNum = (string)ds.Get(Constants.GAMENUM);
                     HashMap hm = new HashMap();
                     hm.Put(Constants.GAMENUM, gameNum);
                     hm.Put(Constants.ISLIVE_GAME, false);
                     hm.Put(Constants.HOST_GAME, user.UserName);
                     fd.AddDocumentToCollection(Constants.GAMES_COL, gameNum, hm);         //put the information according to the game number
                     fd.AddSnapShotListenerToDocument(Constants.GAMES_COL, gameNum, this); //add event listener on current game
                     //put loading screen until 2nd joins game
                     ShowProgressDlg();
                     isHost = true;
                     break;//stop checking for other games
                 }
             }
         }
     }
 }