示例#1
0
        private void FinishTurnButton_OnClicked(object sender, EventArgs e)
        {
            Round.IsRoundOver = true;
            Round.IsCurrent   = false;
            var database = new ScoreTrackerDatabase();

            database.Save(Round);
            foreach (var turn in Round.Turns)
            {
                database.Save(turn);
            }
            var game = database.GetItem <Game>(Round.GameId);

            if (!game.Rounds.All(r => r.IsRoundOver))
            {
                var roundCount = Round.RoundCount + 1;
                var nextRound  = game.Rounds.FirstOrDefault(r => r.RoundCount == roundCount);
                nextRound.IsCurrent = true;
                var nextRoundX = new GameRoundsX {
                    BindingContext = nextRound
                };
                Navigation.PushModalAsync(nextRoundX);
            }
            else
            {
                var turns        = game.Rounds.SelectMany(r => r.Turns).ToList();
                var playerScores = new Dictionary <int, int>();

                foreach (var player in game.Players)
                {
                    if (!playerScores.ContainsKey(player.Id))
                    {
                        playerScores.Add(player.Id, 0);
                    }
                }

                foreach (var turn in turns)
                {
                    playerScores[turn.PlayerId] += turn.Score;
                }

                foreach (var player in game.Players)
                {
                    if (playerScores[player.Id] <= game.WinningScore)
                    {
                        continue;
                    }
                    game.WinningPlayer = player;
                    game.WinningScore  = playerScores[player.Id];
                }

                var winnerNewGame = new WinnerNewGame {
                    BindingContext = game
                };
                Navigation.PushModalAsync(winnerNewGame);
            }
        }
示例#2
0
        private void StartButton_OnClicked(object sender, EventArgs e)
        {
            var database = new ScoreTrackerDatabase();

            if (Game.Id <= 0)
            {
                database.Insert(Game);
            }
            if (!Game.Rounds.Any())
            {
                AddRoundsToGame(database);
            }
            database.Save(Game);
            var firstRound = Game.Rounds.FirstOrDefault(r => r.IsCurrent);
            var gameRounds = new GameRoundsX {
                BindingContext = firstRound
            };

            Navigation.PushModalAsync(gameRounds);
        }