Ejemplo n.º 1
0
        private async void Update(int typeId)
        {
            Words.Items.Clear();
            try
            {
                CurrentWords = await Application.Words.Connection.Table <DbWord>().Where(word => word.TypeId == typeId &&
                                                                                         word.Language == LocalizationManager.Instance.CurrentCulture.Name).ToArrayAsync();

                if (CurrentWords.Count() > 0)
                {
                    foreach (var word in CurrentWords.OrderBy(w => w.Value.ToLower()))
                    {
                        Words.Items.Add(word.Value);
                    }
                    Words.SelectedIndex = 0;
                }
                else
                {
                    CurrentWords = null;
                }
            }
            catch { CurrentWords = null; }
        }
        public async Task NextRoundButton()
        {
            foreach (Word w in CurrentWords.Where(w => w.CurrentGameCorrect))
            {
                w.NumberOfTimesCorrect++;
            }

            int WordsCorrect = CurrentWords.Count(w => w.CurrentGameCorrect);

            CurrentPlayer.QuestionsCorrect         += WordsCorrect;
            CurrentPlayer.PointsThisGame           += WordsCorrect == 5 ? WordsCorrect + 1 : WordsCorrect;
            CurrentPlayer.QuestionsCorrectThisGame += WordsCorrect;

            CurrentTeam.Points += WordsCorrect == 5 ? WordsCorrect + 1 : WordsCorrect;

            if (CheckWinCondition())
            {
                Team WinningTeam = CurrentGame.Teams.OrderByDescending(t => t.Points).First();

                Task FinishGameTask = Task.Run(() =>
                {
                    CurrentGame.Finished = true;
                    Task t4 = Task.Run(() =>
                    {
                        GameHandler.instance.SaveGame(CurrentGame);
                    });

                    foreach (Player p in WinningTeam.Players)
                    {
                        p.GamesWon++;
                    }

                    PlayerHandler.instance.SavePlayers(WinningTeam.Players.ToArray());
                });

                //Current game does not exist in db anymore -> prepare data in viewmodels
                EndGamePageViewModel.instance.CurrentGame          = CurrentGame;
                VictoryAnimationPageViewModel.instance.WinningTeam = WinningTeam;
                await Navigate(typeof(VictoryAnimationPage));
            }
            else if (WordsCorrect == 0)
            {
                await Navigate(typeof(ZeroStarAnimationPage));
            }
            else if (WordsCorrect == 5)
            {
                FiveStarAnimationPageViewModel.instance.PlayedWords = CurrentWords;
                await Navigate(typeof(FiveStarAnimationPage));
            }
            else
            {
                await Navigate(typeof(NextPlayerPage));
            }

            await ClearBackStack(0);


            Task SaveTask = Task.Run(async() =>
            {
                await Task.Delay(750);
                WordHandler.instance.SaveWords(CurrentWords);
                PlayerHandler.instance.SavePlayer(CurrentPlayer);
                TeamHandler.instance.SaveTeam(CurrentTeam);
            });
        }