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;
        }
Ejemplo n.º 2
0
        public void Changed(ChangeArgs args)
        {
            #region Substitution
            if (args is SubstitutionArgs)
            {
                var subArgs   = (SubstitutionArgs)args;
                var firstSub  = subArgs.FirstSub;
                var secondSub = subArgs.SecondSub;

                switch (args.Type)
                {
                case ChangeType.SubSelected:
                    if (_firstSubstitute == null)
                    {
                        _firstSubstitute = Team.Squad.Find(p => p.Id == firstSub.Id);
                    }
                    else if (firstSub.Id == _firstSubstitute.Id && secondSub == null)
                    {
                        return;
                    }

                    if (secondSub != null)
                    {
                        if (_secondSubstitute == null)
                        {
                            _secondSubstitute = Team.Squad.Find(p => p.Id == secondSub.Id);
                        }
                        else if (secondSub.Id == _secondSubstitute.Id)
                        {
                            return;
                        }
                    }
                    break;

                case ChangeType.SubDeselect:
                    if (firstSub != null)
                    {
                        _firstSubstitute = null;
                    }
                    else if (secondSub != null)
                    {
                        _secondSubstitute = null;
                    }
                    break;

                case ChangeType.SubConfirmed:
                    _firstSubstitute.RotationTeam.Value  = firstSub.Rotation;
                    _secondSubstitute.RotationTeam.Value = secondSub.Rotation;

                    var firstSubIndex  = Substitutions.Contains(_firstSubstitute) ? Substitutions.IndexOf(_firstSubstitute) : Reserves.IndexOf(_firstSubstitute);
                    var secondSubIndex = Substitutions.Contains(_secondSubstitute) ? Substitutions.IndexOf(_secondSubstitute) : Reserves.IndexOf(_secondSubstitute);

                    var firstSubX  = subArgs.secondSubX;
                    var firstSubY  = subArgs.secondSubY;
                    var secondSubX = subArgs.firstSubX;
                    var secondSubY = subArgs.firstSubY;

                    // sub inside 'lineup details'
                    if ((RotationTeam)_firstSubstitute.RotationTeam.Value != RotationTeam.Lineup && (RotationTeam)_secondSubstitute.RotationTeam.Value != RotationTeam.Lineup)
                    {
                        // sub inside same rotation team
                        if ((RotationTeam)_firstSubstitute.RotationTeam.Value == (RotationTeam)_secondSubstitute.RotationTeam.Value)
                        {
                            if (Substitutions.Contains(_firstSubstitute))
                            {
                                Substitutions.RemoveAt(firstSubIndex);
                                Substitutions.Insert(firstSubIndex, _secondSubstitute);
                                Substitutions.RemoveAt(secondSubIndex);
                                Substitutions.Insert(secondSubIndex, _firstSubstitute);
                            }
                            else if (Reserves.Contains(_firstSubstitute))
                            {
                                Reserves.RemoveAt(firstSubIndex);
                                Reserves.Insert(firstSubIndex, _secondSubstitute);
                                Reserves.RemoveAt(secondSubIndex);
                                Reserves.Insert(secondSubIndex, _firstSubstitute);
                            }
                        }
                        // sub between differant rotaion team
                        else if ((RotationTeam)_firstSubstitute.RotationTeam.Value != (RotationTeam)_secondSubstitute.RotationTeam.Value)
                        {
                            if (Substitutions.Contains(_firstSubstitute))
                            {
                                Substitutions.RemoveAt(firstSubIndex);
                                Substitutions.Insert(firstSubIndex, _secondSubstitute);

                                Reserves.RemoveAt(secondSubIndex);
                                Reserves.Insert(secondSubIndex, _firstSubstitute);
                            }
                            else if (Reserves.Contains(_firstSubstitute))
                            {
                                Reserves.RemoveAt(firstSubIndex);
                                Reserves.Insert(firstSubIndex, _secondSubstitute);

                                Substitutions.RemoveAt(secondSubIndex);
                                Substitutions.Insert(secondSubIndex, _firstSubstitute);
                            }
                        }
                    }
                    // sub first sub from 'field details' to second sub in 'lineup details'
                    else if ((RotationTeam)_firstSubstitute.RotationTeam.Value == RotationTeam.Lineup && (RotationTeam)_secondSubstitute.RotationTeam.Value != RotationTeam.Lineup)
                    {
                        if (Substitutions.Contains(_secondSubstitute))
                        {
                            Substitutions.Remove(_secondSubstitute);
                            _firstSubstitute.RotationTeam.Value = RotationTeam.Substitute;
                            Substitutions.Insert(secondSubIndex, _firstSubstitute);
                        }
                        else if (Reserves.Contains(_secondSubstitute))
                        {
                            Reserves.Remove(_secondSubstitute);
                            _firstSubstitute.RotationTeam.Value = RotationTeam.Reserves;
                            Reserves.Insert(secondSubIndex, _firstSubstitute);
                        }

                        _secondSubstitute.X = firstSubX;
                        _secondSubstitute.Y = firstSubY;
                        _firstSubstitute.X  = secondSubX;
                        _firstSubstitute.Y  = secondSubY;
                    }
                    // sub first sub from 'lineup details' to second sub in 'field details'
                    else if ((RotationTeam)_firstSubstitute.RotationTeam.Value != RotationTeam.Lineup && (RotationTeam)_secondSubstitute.RotationTeam.Value == RotationTeam.Lineup)
                    {
                        if (Substitutions.Contains(_firstSubstitute))
                        {
                            Substitutions.Remove(_firstSubstitute);
                            _secondSubstitute.RotationTeam.Value = RotationTeam.Substitute;
                            Substitutions.Insert(firstSubIndex, _secondSubstitute);
                        }
                        else if (Reserves.Contains(_firstSubstitute))
                        {
                            Reserves.Remove(_firstSubstitute);
                            _secondSubstitute.RotationTeam.Value = RotationTeam.Reserves;
                            Reserves.Insert(firstSubIndex, _secondSubstitute);
                        }

                        _firstSubstitute.X  = secondSubX;
                        _firstSubstitute.Y  = secondSubY;
                        _secondSubstitute.X = firstSubX;
                        _secondSubstitute.Y = firstSubY;
                    }

                    Substitutions.RemoveFirstNames();
                    Substitutions.ArrangePositionRoleAsec();
                    Substitutions = new SquadList <SoccerPlayerViewModel>(Substitutions);

                    Reserves.RemoveFirstNames();
                    Reserves.ArrangePositionRoleAsec();
                    Reserves = new SquadList <SoccerPlayerViewModel>(Reserves);

                    // reset subs VM
                    _firstSubstitute.SetIsSelectedBinding(false);
                    _secondSubstitute.SetIsSelectedBinding(false);
                    _firstSubstitute  = null;
                    _secondSubstitute = null;

                    // Also:
                    // recreate TeamDetails & PlayerDetails
                    break;
                }
            }
            #endregion

            #region EditFormation
            if (args is EditFormationArgs)
            {
                var editArgs = (EditFormationArgs)args;
                Substitutions.ForEach(p => p.IsEnabled = !editArgs.IsEditMode);
                Reserves.ForEach(p => p.IsEnabled      = !editArgs.IsEditMode);

                switch (editArgs.Type)
                {
                //case ChangeType.EditFormationModeEnabled:
                //    break;

                case ChangeType.EditFormationModeDisabled:
                    break;
                }
            }
            #endregion
        }