Beispiel #1
0
        public List <Player> HandleSearchAlgorithm(SearchCriteriaForPlayer request)
        {
            string        sqlSelectStatement = "";
            List <Player> playerList         = new List <Player>();

            //If no search criteria were selected, we select all player in DB and return them.
            if (request.Country == null && request.League == null && request.ContractStatus == null && request.MinimumAge == null &&
                request.MaximumAge == null && request.PrimaryPosition == null && request.SecondaryPosition == null &&
                request.InjuryStatus == null && request.HandPreference == null && request.MinimumHeight == null &&
                request.MaximumWeight == null && request.StrengthsList.Count == 0)
            {
                return((List <Player>)_playerRepos.GetAll());
            }

            //First we check if any League or PrimaryPosition is selected as search criteria. If true we selected after those 2 or 1 of them
            if (request.League != null || request.PrimaryPosition != null)
            {
                sqlSelectStatement += " p.isAvailable = 1";
                if (request.League != null)
                {
                    sqlSelectStatement += " and p.league = '" + request.League + "'";
                }
                if (request.PrimaryPosition != null)
                {
                    sqlSelectStatement += " and p.PrimaryPosition = '" + request.PrimaryPosition + "'";
                }
            }
            //If no league or primaryPosition was selected we try to selected after country if they specified any country in their search.
            else if (request.Country != null)
            {
                sqlSelectStatement = " p.isAvailable = 1 and p.country = '" + request.Country + "'";
            }
            // If no country, league or primary position was selected, we put togehter a selected statement for the rest of the search criteria
            // that are less important
            else if (request.ContractStatus != null || request.MinimumAge != null || request.MaximumAge != null ||
                     request.SecondaryPosition != null || request.InjuryStatus != null || request.HandPreference != null ||
                     request.MinimumHeight != null || request.MaximumWeight != null || request.StrengthsList.Count != 0)
            {
                if (request.ContractStatus != null)
                {
                    sqlSelectStatement = " p.contractstatus = '" + request.ContractStatus + "'" + " and p.isAvailable = 1";
                }
                if (request.MinimumAge != null)
                {
                    int timeNow = DateTime.Now.Year;
                    int?year    = timeNow - request.MinimumAge;
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.year <= " + year + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.year <= " + year + " and p.isAvailable = 1";
                    }
                }
                if (request.MaximumAge != null)
                {
                    int timeNow = DateTime.Now.Year;
                    int?year    = timeNow - request.MaximumAge;
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.year >= " + year + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.year >= " + year + " and p.isAvailable = 1";
                    }
                }
                if (request.SecondaryPosition != null)
                {
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.SecondaryPosition = '" + request.SecondaryPosition + "'" + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.SecondaryPosition = '" + request.SecondaryPosition + "'" + " and p.isAvailable = 1";
                    }
                }
                if (request.HandPreference != null)
                {
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.preferredhand = '" + request.HandPreference + "'" + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.preferredhand = '" + request.HandPreference + "'" + " and p.isAvailable = 1";
                    }
                }
                if (request.InjuryStatus != null)
                {
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.Injurystatus = '" + request.InjuryStatus + "'" + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.Injurystatus = '" + request.InjuryStatus + "'" + " and p.isAvailable = 1";
                    }
                }
                if (request.MinimumHeight != null)
                {
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.height >= " + request.MinimumHeight + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.height >= " + request.MinimumHeight + " and p.isAvailable = 1";
                    }
                }
                if (request.MaximumWeight != null)
                {
                    if (sqlSelectStatement == "")
                    {
                        sqlSelectStatement = " p.height >= " + request.MinimumHeight + " and p.isAvailable = 1";
                    }
                    else
                    {
                        sqlSelectStatement += " or p.weight <= " + request.MaximumWeight + " and p.isAvailable = 1";
                    }
                }
                if (request.StrengthsList.Count != 0)
                {
                    foreach (string item in request.StrengthsList)
                    {
                        if (sqlSelectStatement == "")
                        {
                            sqlSelectStatement += " s.name = '" + item + "'" + " and p.isAvailable = 1";
                        }
                        else
                        {
                            sqlSelectStatement += " or s.name = '" + item + "'" + " and p.isAvailable = 1";
                        }
                    }
                }
            }
            playerList = _playerRepos.GetBySearchCriteria(sqlSelectStatement).ToList();

            // Now we check for which values match the search criterias and calculate the percentage they match with of total search criteria
            // selected and order the list by the percentage and return the finished list
            playerList = CheckWhichCriteriaMatchAndCalculatePercentage(playerList, request);
            SortListByPercentage sort = new SortListByPercentage();

            playerList.Sort(sort.Compare);
            return(playerList);
        }
Beispiel #2
0
        /**
         * Search for clubs based on the search criteria
         * Only gets neccessary club info based on the search criteria
         * Sorts the list based on Match Percentage and returns the list
         */
        public List <Club> HandleClubSearchAlgorithm(ClubSearchCriteria criterias, int id)
        {
            string      sql           = "";
            string      sqlPreference = "";
            string      sqlValue      = "";
            string      sqlSeason     = "";
            List <Club> clubs         = new List <Club>();

            // If no criteria is selected all clubs is returned with season match
            // Since no criteria is selected, all clubs match 100%
            if (criterias.Country == null && criterias.League == null &&
                criterias.Position == null && criterias.PreferencesList.Count == 0 &&
                criterias.ValuesList.Count == 0)
            {
                string seasonSql = "";
                if (criterias.Season == "Current year")
                {
                    seasonSql = "and jp.season = 'Current year'";
                }
                else if (criterias.Season == "Next year")
                {
                    seasonSql = "and jp.season = 'Next year'";
                }
                return((List <Club>)_clubRepos.GetAll(seasonSql));
            }

            // Country, league and position is a must-match when selected
            // Get all clubs with matching league, country and/or position
            if (criterias.Country != null || criterias.League != null || criterias.Position != null)
            {
                sql += GetSeasonSql(criterias);
                if (criterias.League != null)
                {
                    sql += " and c.league = '" + criterias.League + "' and isAvailable = 1 ";
                }
                if (criterias.Country != null)
                {
                    sql += " and c.country = '" + criterias.Country + "' and isAvailable = 1 ";
                }
                if (criterias.Position != null)
                {
                    sql += " and jp.position = '" + criterias.Position + "' and isAvailable = 1 ";
                }
                clubs = _clubRepos.GetBySearchCriteria(sql).ToList();
            }
            // If Country, League and Position is not selected as a criteria
            // We continue to match with the 'less important' criterias

            // If only preference, season and value is selected
            else if (criterias.PreferencesList.Count > 0 && criterias.ValuesList.Count > 0)
            {
                sqlPreference = GetPreferenceSql(criterias);
                sqlValue      = GetValueSql(criterias);
                sqlSeason     = GetSeasonSql(criterias);

                clubs = _clubRepos.GetBySearchCriteriaWithJobPositionPreferenceValue(sqlPreference, sqlValue, sqlSeason).ToList();
            }
            // If only season and value is selected
            else if (criterias.ValuesList.Count > 0)
            {
                sqlValue  = GetValueSql(criterias);
                sqlSeason = GetSeasonSql(criterias);

                clubs = _clubRepos.GetBySearchCriteriaWithJobPoisitionValue(sqlValue, sqlSeason).ToList();
            }
            // If only season and preference is selected
            else if (criterias.PreferencesList.Count > 0)
            {
                sqlPreference = GetPreferenceSql(criterias);
                sqlSeason     = GetSeasonSql(criterias);

                clubs = _clubRepos.GetBySearchCriteriaWithJobPoisitionPreference(sqlPreference, sqlSeason).ToList();
            }
            // If only preference and value is selected
            else if (criterias.PreferencesList.Count > 0 && criterias.ValuesList.Count > 0)
            {
                sqlPreference = GetPreferenceSql(criterias);
                sqlValue      = GetValueSql(criterias);
                sqlSeason     = GetSeasonSql(criterias);

                clubs = _clubRepos.GetBySearchCriteriaWithJobPositionPreferenceValue(sqlPreference, sqlValue, sqlSeason).ToList();
            }
            // When the clubs list is build it is ready to be sorted by match percentage
            // Since we match player with open job positions, we need to get the player first
            Player player = _playerRepos.GetById(id);

            // Calculate match percentage, sort by match percentage and return the list
            clubs = CalculateCriteriaMatchPercentage(clubs, criterias, player);
            SortListByPercentage sort = new SortListByPercentage();

            clubs.Sort(sort.CompareClub);
            return(clubs);
        }