private void SubstitutePlayers()
        {
            var firstSubX  = _firstSubstitute.X;
            var firstSubY  = _firstSubstitute.Y;
            var secondSubX = _secondSubstitute.X;
            var secondSubY = _secondSubstitute.Y;

            SubFrom?isInsideLineupSection = null;

            // VIEW MODEL
            // sub inside 'field details'
            if (SelectedFormation.Lineup.Contains(_firstSubstitute) && SelectedFormation.Lineup.Contains(_secondSubstitute))
            {
                _firstSubstitute.X  = _secondSubstitute.X;
                _firstSubstitute.Y  = _secondSubstitute.Y;
                _secondSubstitute.X = firstSubX;
                _secondSubstitute.Y = firstSubY;
            }
            // sub first sub from 'field details' to second sub in 'lineup details'
            else if (SelectedFormation.Lineup.Contains(_firstSubstitute) && !SelectedFormation.Lineup.Contains(_secondSubstitute))
            {
                SubstituteBetweenChangeables(SubFrom.FieldDetailsSection, firstSubX, firstSubY);
                SelectedFormation.Lineup.RemoveFirstNames();
            }
            // sub first sub from 'lineup details' to second sub in 'field details'
            else if (!SelectedFormation.Lineup.Contains(_firstSubstitute) && SelectedFormation.Lineup.Contains(_secondSubstitute))
            {
                SubstituteBetweenChangeables(SubFrom.LineupDetailsSection, secondSubX, secondSubY);
                SelectedFormation.Lineup.RemoveFirstNames();
            }
            // sub inside 'lineup details'
            else if (!SelectedFormation.Lineup.Contains(_firstSubstitute) && !SelectedFormation.Lineup.Contains(_secondSubstitute))
            {
                isInsideLineupSection = SubFrom.InsideLineupDetailsSection;
            }

            // MODEL
            SoccerPlayer firstSubModel     = _teamModel.Squad.FirstOrDefault(p => p.Id == _firstSubstitute.Id);
            SoccerPlayer secondSubModel    = _teamModel.Squad.FirstOrDefault(p => p.Id == _secondSubstitute.Id);
            var          originalFirstSub  = firstSubModel.RefferenceCopy();
            var          originalSecondSub = secondSubModel.RefferenceCopy();

            if (isInsideLineupSection.HasValue && isInsideLineupSection.Value == SubFrom.InsideLineupDetailsSection)
            {
                firstSubModel.Rotation  = (RotationTeam)_secondSubstitute.RotationTeam.Value;
                secondSubModel.Rotation = (RotationTeam)_firstSubstitute.RotationTeam.Value;
            }
            else
            {
                firstSubModel.Rotation  = (RotationTeam)_firstSubstitute.RotationTeam.Value;
                secondSubModel.Rotation = (RotationTeam)_secondSubstitute.RotationTeam.Value;
            }

            var formation         = _teamModel.Formations.Find(f => f.Id == SelectedFormation.Id);
            var originalFormation = formation.RefferenceCopy();

            if (originalFormation.LineupIds.Contains(_firstSubstitute.Id))
            {
                UpdateFormationModel(formation, _firstSubstitute.Id);
            }
            if (originalFormation.LineupIds.Contains(_secondSubstitute.Id))
            {
                UpdateFormationModel(formation, _secondSubstitute.Id);
            }

            // DB
            SquadRepository.UpdatePlayer(firstSubModel);
            SquadRepository.UpdatePlayer(secondSubModel);
            if (!isInsideLineupSection.HasValue)
            {
                SquadRepository.UpdateFormation(formation);
            }

            // CHANGE
            if (firstSubModel == null || secondSubModel == null)
            {
                throw new InvalidOperationException("Models were not found from the view model id's");
            }
            var subArgs = new SubstitutionArgs(ChangeType.SubConfirmed, originalFirstSub, originalSecondSub)
            {
                firstSubX  = _firstSubstitute.X,
                firstSubY  = _firstSubstitute.Y,
                secondSubX = _secondSubstitute.X,
                secondSubY = _secondSubstitute.Y,
            };

            _changeManager.Change(subArgs);

            // RESET SUB VM => should be in VIEW MODEL update??
            _firstSubstitute.SetIsSelectedBinding(false);
            _secondSubstitute.SetIsSelectedBinding(false);
            _firstSubstitute  = null;
            _secondSubstitute = null;
        }
Пример #2
0
        private void SoccerPlayerViewModel_IsSelectedPropertyChanged(object sender, IsSelectedEventArgs args)
        {
            var player = (SoccerPlayerViewModel)sender;

            if (args.IsSelected)
            {
                if (_firstSubstitute == null)
                {
                    _firstSubstitute = player;
                    var subArgs = new SubstitutionArgs(ChangeType.SubSelected, _teamModel.Squad.Find(p => p.Id == _firstSubstitute.Id).RefferenceCopy())
                    {
                        firstSubX  = _firstSubstitute.X,
                        firstSubY  = _firstSubstitute.Y,
                        secondSubX = 0,
                        secondSubY = 0,
                    };
                    _changeManager.Change(subArgs);
                }
                else if (_firstSubstitute != null && _secondSubstitute == null)
                {
                    _secondSubstitute = player;
                    var subArgs = new SubstitutionArgs(ChangeType.SubSelected, _teamModel.Squad.Find(p => p.Id == _firstSubstitute.Id).RefferenceCopy(), _teamModel.Squad.Find(p => p.Id == _secondSubstitute.Id).RefferenceCopy())
                    {
                        firstSubX  = _firstSubstitute.X,
                        firstSubY  = _firstSubstitute.Y,
                        secondSubX = _secondSubstitute.X,
                        secondSubY = _secondSubstitute.Y,
                    };
                    _changeManager.Change(subArgs);
                }
                else if (_firstSubstitute != null && _secondSubstitute != null)
                {
                    player.SetIsSelectedBinding(false);
                }
            }
            else
            {
                if (_firstSubstitute != null && _firstSubstitute.Id == player.Id)
                {
                    _firstSubstitute = null;
                    var subArgs = new SubstitutionArgs(ChangeType.SubDeselect, null, _secondSubstitute != null ? _teamModel.Squad.Find(p => p.Id == _secondSubstitute.Id).RefferenceCopy() : null)
                    {
                        firstSubX  = 0,
                        firstSubY  = 0,
                        secondSubX = _secondSubstitute != null ? _secondSubstitute.X : 0,
                        secondSubY = _secondSubstitute != null ? _secondSubstitute.Y : 0,
                    };
                    _changeManager.Change(subArgs);
                }
                else if (_secondSubstitute != null && _secondSubstitute.Id == player.Id)
                {
                    _secondSubstitute = null;
                    var subArgs = new SubstitutionArgs(ChangeType.SubDeselect, _firstSubstitute != null ? _teamModel.Squad.Find(p => p.Id == _firstSubstitute.Id).RefferenceCopy() : null, null)
                    {
                        firstSubX  = _firstSubstitute != null ? _firstSubstitute.X : 0,
                        firstSubY  = _firstSubstitute != null ? _firstSubstitute.Y : 0,
                        secondSubX = 0,
                        secondSubY = 0,
                    };
                    _changeManager.Change(subArgs);
                }
            }
        }