public NewGameViewModel()
        {
            Players = new ObservableCollectionExtended <string>();

            var canStart = Players.ToObservableChangeSet().CountChanged().Select(_ => Players.Count >= 3);

            StartGame      = ReactiveCommand.Create(() => { }, canStart);
            RandomizeOrder = ReactiveCommand.Create(
                () =>
            {
                using (Players.SuspendNotifications())
                {
                    var r        = new Random();
                    var newOrder = Players.OrderBy(x => r.NextDouble()).ToList();
                    Players.Clear();
                    Players.AddRange(newOrder);
                }
            },
                canStart);

            RemovePlayer = ReactiveCommand.Create <string>(player => Players.Remove(player));
            var canAddPlayer = this.WhenAnyValue(
                x => x.Players.Count,
                x => x.NewPlayerName,
                (count, newPlayerName) => count < 7 && !string.IsNullOrWhiteSpace(newPlayerName) && !Players.Contains(newPlayerName));

            AddPlayer = ReactiveCommand.Create(
                () =>
            {
                Players.Add(NewPlayerName.Trim());
                NewPlayerName = string.Empty;
            },
                canAddPlayer);
        }
Ejemplo n.º 2
0
        public void AddPlayer(Func <bool> AddToCurrentRound, Action <string> ErrorAction)
        {
            if (NewPlayerName.Trim() == string.Empty)
            {
                ErrorAction("Enter a player's name");
                return;
            }
            else if (Players.PlayerExists(NewPlayerName))
            {
                ErrorAction("Player already exists");
                return;
            }

            var player = Players.NewPlayer(NewPlayerName, NewPlayerGender, NewPlayerSkill.Name);

            if (Tournament.Rounds.Count > 0 && AddToCurrentRound())
            {
                Tournament.AddPlayerToLastRound(player);
            }

            ResetNewPlayerSection();
        }