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