/// <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(); } }
/// <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); } }