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 "";
        }
Ejemplo n.º 2
0
        public string AddFacts(Stream facts)
        {
            int carIdLog = -1;
            Int64 tripIdLog = -1;

            try {
                //Reading the stream of data sent from client
                Console.WriteLine("Message Received");
                StreamReader sr = new StreamReader(facts);
                string text = sr.ReadToEnd();
                Console.WriteLine("It was: " + text);

                //Parsing the nested JSON objects to a JArray
                JArray allfacts = JArray.Parse(text) as JArray;
                dynamic allthefacts = allfacts;

                //Assigning a tripid
                DBController dbc = new DBController();
                Int16 carId = (Int16)allthefacts[0].carid;
                Int64 assignedTripId = dbc.AddTripInformation(carId);
                dbc.Close();

                carIdLog = carId;
                tripIdLog = assignedTripId;

                Console.WriteLine("Assigned TripId: " + assignedTripId);

                //Converting each JSON object to a Fact object
                List<Fact> factObjs = new List<Fact>();

                foreach (dynamic fact in allthefacts) {
                    fact.tripid = assignedTripId;
                    factObjs.Add(new Fact(fact));
                }

                //Add Facts to DB
                dbc = new DBController();
                foreach (Fact f in factObjs) {
                    dbc.AddRawFact(f);
                }
                dbc.Close();

                Mapmatch.MatchTrip(carId, assignedTripId);
                Console.WriteLine("Trip " + assignedTripId + " was map-matched for CarId " + carId + ".");

                Console.WriteLine("Updating Facts for trip " + assignedTripId + ".");
                GPSFactUpdater.UpdateRawGPS(carId, assignedTripId);
                Console.WriteLine("Number of facts: " + factObjs.Count);

                Console.WriteLine("Updating trip " + assignedTripId + ".");
                TripFactUpdater.UpdateTrip(carId, assignedTripId);
                Console.WriteLine("Update on trip " + assignedTripId + " completed.");

                Car car = dbc.GetCarByCarId(carId);
                if (car.Username.ToLower().StartsWith("lb")) {
                    Console.WriteLine("User is a member of the LB competition");
                    Console.WriteLine("Updating this tripscore in the competition");
                    AddTripToCompetition(1, carId, assignedTripId);
                    Console.WriteLine("Update complete.");
                }

            } catch (Exception e) {
                Console.WriteLine(e.ToString());
                DBController dbc = new DBController();
                dbc.AddLog("AddFacts", (Int16)carIdLog, tripIdLog, null, e.ToString().Substring(0, Math.Min(e.ToString().Count(), 254)), facts.ToString().Substring(0, Math.Min(e.ToString().Count(), 499)));
                dbc.Close();
                return "failed";
            }

            return "succes";
        }