Ejemplo n.º 1
0
        public string GetCompetitionsForListView(Int16 carid, int offset)
        {
            try {
                DBController dbc = new DBController();
                Car car = dbc.GetCarByCarId(carid);
                int countrank = 1;

                List<CompetingIn> CompetingIns = new List<CompetingIn>();
                List<Competition> competitions = dbc.GetAllCompetitionsWithOffset(offset);
                List<Int16> CarCompetitions = dbc.GetCompetitionIdByCarId(carid);
                dbc.Close();

                if (!car.Username.ToLower().StartsWith("lb")) {
                    competitions.Remove(competitions.Single(p => p.CompetitionId == 1));
                }
                List<CompetitionView> competitionsForListView = new List<CompetitionView>();

                foreach (Competition com in competitions) {
                    CompetitionView comView = new CompetitionView(com);

                    if (CarCompetitions.Contains(com.CompetitionId)) {
                        comView.IsParticipating = true;
                    }

                    CompetingIns = dbc.GetCompetitionInByCompetitionId(com.CompetitionId);

                    List<CompetingIn> scored = CompetingIns.Where(o => o.Attempts > 0).ToList();
                    List<CompetingIn> notScored = CompetingIns.Where(o => o.Attempts == 0).ToList();

                    List<CompetingIn> templist = scored.OrderBy(o => o.Score).ToList();
                    templist.AddRange(notScored);

                    comView.ParticipantCount = templist.Count;

                    foreach (CompetingIn compin in templist) {
                        if (compin.CarId == carid) {
                            comView.AttemptCount = compin.Attempts;
                            comView.Score = compin.Score;
                            comView.Rank = countrank;
                        } else { countrank++; }
                    }

                    countrank = 1;
                    competitionsForListView.Add(comView);
                }

                return JsonConvert.SerializeObject(competitionsForListView);

            } catch (Exception e) {
                Console.WriteLine(e.ToString());
                DBController dbc = new DBController();
                dbc.AddLog("GetCompetitionsForListView?carid={carid}&offset={offset}", carid, null, null, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), offset.ToString());
                dbc.Close();
            }

            return "";
        }