Example #1
0
        private PlaceCollection GetPlaceCollection(T each)
        {
            PlaceCollection places = null;

            if (string.IsNullOrEmpty(PlaceNamespace))
            {
                places = each.Places;
            }
            else
            {
                places = each.Places.NS(PlaceNamespace);
            }
            return(places);
        }
Example #2
0
        public RatingScope <T> GetTopPercentage(string scoreName, int top)
        {
            RatingScope <T> result           = new RatingScope <T>(Name);
            RatingScope <T> radixStuds       = new RatingScope <T>(Name);
            decimal         filterPercentage = top;

            //把有那項排名的學生獨立出來。
            foreach (T each in _students.Values)
            {
                PlaceCollection places = GetPlaceCollection(each);

                if (places.Contains(scoreName))
                {
                    radixStuds.Add(each);
                }
            }
            //計算要取的 Percentage。
            decimal extractPlace = Math.Ceiling((filterPercentage / 100m) * radixStuds.Count);

            foreach (T each in radixStuds)
            {
                PlaceCollection places = GetPlaceCollection(each);

                if (places[scoreName].Level <= extractPlace)
                {
                    result.Add(each);
                }
            }

            //之前的取百分比名次做法。
            //foreach (RatingStudent each in students)
            //{
            //    if (each.Places.Contains(ratingName))
            //    {
            //        Place p = each.Places[ratingName];
            //        decimal percentage = (100m * ((decimal)p.Level / (decimal)p.Radix));
            //        if (percentage <= 0) percentage = 1;
            //        if (percentage <= filterPercentage)
            //        {
            //            result.Add(each);
            //        }
            //    }
            //}

            return(result);
        }
Example #3
0
        /// <summary>
        /// 取得前 n 名次的學生。
        /// </summary>
        /// <param name="ratingName">排名的名稱,例如:國文。(與IScoreParser 實作之實體 Name 屬性值相同)</param>
        /// <param name="top">要取得名次。</param>
        /// <param name="placeNamespace">名次存放的 Namespace。</param>
        public RatingScope <T> GetTopPlaces(string ratingName, int top)
        {
            RatingScope <T> result = new RatingScope <T>(Name);

            foreach (T each in _students.Values)
            {
                PlaceCollection places = GetPlaceCollection(each);

                if (places.Contains(ratingName))
                {
                    if (places[ratingName].Level <= top)
                    {
                        result.Add(each);
                    }
                }
            }
            return(result);
        }
Example #4
0
        public RatingScope <T> GetLastPercentage(string scoreName, int last)
        {
            RatingScope <T> result = new RatingScope <T>(Name);
            //RatingScope<T> radixStuds = new RatingScope<T>(Name);
            decimal  filterPercentage = last;
            List <T> students         = new List <T>();

            //把有那項排名的學生獨立出來。
            foreach (T each in _students.Values)
            {
                PlaceCollection places = GetPlaceCollection(each);

                if (places.Contains(scoreName))
                {
                    students.Add(each);
                }
            }
            //計算要取的 Percentage。
            decimal extractPlace = Math.Ceiling((filterPercentage / 100m) * students.Count);

            students.Sort((x, y) =>
            {
                PlaceCollection placesX = GetPlaceCollection(x);
                PlaceCollection placesY = GetPlaceCollection(y);

                return(placesY[scoreName].Level.CompareTo(placesX[scoreName].Level));
            });

            if (students.Count <= 0)
            {
                return(result);
            }

            //取得最後一名的名次百分比。
            decimal lastLevel = students[0].Places[scoreName].Level - extractPlace;

            foreach (T student in students.Where(x => x.Places[scoreName].Level > lastLevel))
            {
                result.Add(student);
            }

            return(result);
        }
Example #5
0
        /// <summary>
        /// 取得後 n 名次的學生。
        /// </summary>
        /// <param name="ratingName">排名的名稱,例如:國文。(與IScoreParser 實作之實體 Name 屬性值相同)</param>
        /// <param name="last">要取得名次。</param>
        public RatingScope <T> GetLastPlaces(string scoreName, int last)
        {
            RatingScope <T> result = new RatingScope <T>(Name);

            List <T> students = new List <T>();

            foreach (T each in _students.Values)
            {
                PlaceCollection places = GetPlaceCollection(each);

                if (places.Contains(scoreName))
                {
                    students.Add(each);
                }
            }

            students.Sort((x, y) =>
            {
                PlaceCollection placesX = GetPlaceCollection(x);
                PlaceCollection placesY = GetPlaceCollection(y);

                return(placesY[scoreName].Level.CompareTo(placesX[scoreName].Level));
            });

            if (students.Count <= 0)
            {
                return(result);
            }

            //取得最後一名的名次。
            int lastLevel = students[0].Places[scoreName].Level - last;

            foreach (T student in students.Where(x => x.Places[scoreName].Level > lastLevel))
            {
                result.Add(student);
            }

            return(result);
        }
Example #6
0
        /// <summary>
        /// 進行排名。
        /// </summary>
        /// <param name="scoreParser">要被排名的成績計算邏輯。</param>
        /// <param name="option">排名選項,接序 or 不接序排名。</param>
        /// <param name="provider">當相同名次時決定先後成績資料,所有排名範圍內的學生都應該供相同順序與數量的成績資料,否則會產生無法預期的結果。</param>
        public void Rank(IScoreParser <T> scoreParser, PlaceOptions option)
        {
            //取得有成績的學生。
            List <ScoredStudent> students = GetScoredStudents(scoreParser);

            //沒有成績就不用排名了。
            if (students.Count <= 0)
            {
                return;
            }

            //排序名次。
            students.Sort(delegate(ScoredStudent x, ScoredStudent y)
            {
                if (scoreParser is IScoresParser <T> )
                {
                    IScoresParser <T> p2 = scoreParser as IScoresParser <T>;

                    if (x.SecondScores.Count <= 0)
                    {
                        x.SecondScores = p2.GetSecondScores(x.Source);
                    }
                    if (y.SecondScores.Count <= 0)
                    {
                        y.SecondScores = p2.GetSecondScores(y.Source);
                    }
                }
                return(y.CompareTo(x));
            });

            int    radix     = students.Count;   //基數。
            string scorename = scoreParser.Name; //排名的名稱。

            //先把之前的排名結果清掉。
            foreach (T each in this)
            {
                PlaceCollection places = GetPlaceCollection(each);
                if (places.Contains(scorename))
                {
                    places.Remove(scorename);
                }
            }

            //決定排序。
            IPlaceAlgorithm placeAlg = null;

            if (option == PlaceOptions.Sequence)
            {
                placeAlg = new Sequence();
            }
            else
            {
                placeAlg = new Unsequence();
            }

            StandardPercentage percenAlg = new StandardPercentage();

            percenAlg.Initital(students.Count);

            foreach (ScoredStudent each in students)
            {
                int level  = placeAlg.NextLevel(each);
                int percen = percenAlg.NextPercentage(level);

                T student = _students[each.Id];

                PlaceCollection places = GetPlaceCollection(student);

                if (places.Contains(scorename))
                {
                    places[scorename] = new Place(level, percen, each.Score, radix, this.Count);
                }
                else
                {
                    places.Add(scorename, new Place(level, percen, each.Score, radix, this.Count));
                }
            }
        }