Example #1
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);
            }
        }
Example #2
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
                 }
             }
         }
     }
 }