Beispiel #1
0
        public static IEnumerable <IRace> GetNextRaces(int tournamentID)
        {
            List <Race>    result = new List <Race>();
            IRlmRepository repo   = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                // CurrentRace starts at 1
                if (tournament.CurrentRace > 0)
                {
                    if (tournament.CurrentRace < tournament.RaceData.Count)
                    {
                        result.Add(tournament.RaceData[tournament.CurrentRace]);
                    }

                    if (tournament.CurrentRace + 1 < tournament.RaceData.Count)
                    {
                        result.Add(tournament.RaceData[tournament.CurrentRace + 1]);
                    }
                }
            }

            return(result);
        }
Beispiel #2
0
        public static void StartRace(int tournamentID, int raceNum)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);
                Race       race       = tournament.RaceData.Where(r => r.RaceNumber == raceNum).Single();
                tournament.CurrentRace = raceNum;
                foreach (LaneAssignment assignment in race.LaneAssignmentData)
                {
                    assignment.ElapsedTime = 0;
                    assignment.Points      = 0;
                    assignment.Position    = 0;
                    assignment.ScaleSpeed  = 0.0;
                }
                race.State = RaceState.Racing;
                repo.SaveTournament(tournament);
            }

            //RacesUpdated?.Invoke(tournamentID, new RacesUpdatedEventArgs(GetRaces(tournamentID)));
            RaceUpdated?.Invoke(tournamentID, new RaceUpdatedEventArgs(GetCurrentRace(tournamentID)));
            //NextRacesUpdated?.Invoke(tournamentID, new NextRacesUpdatedEventArgs(GetNextRaces(tournamentID)));

            RaceMonitor.LaneResultAdded += RaceMonitor_LaneResultAdded;
            RaceMonitor.Monitor(TournamentManager.ComPort, TournamentManager.BaudRate, tournamentID, raceNum, TournamentManager.Simulate);
        }
Beispiel #3
0
        public static ITournament AddTournament(string name, int numLanes)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            Tournament tournament = new Tournament();

            lock (_lock)
            {
                tournament.Name     = name;
                tournament.NumLanes = numLanes;

                // find an ID to assign to this new Tournament
                int maxID = 0;
                foreach (int i in repo.GetAllTournamentIDs())
                {
                    if (i > maxID)
                    {
                        maxID = i;
                    }
                }

                tournament.ID = maxID + 1;

                repo.SaveTournament(tournament);
            }

            TournamentsUpdated?.Invoke(new TournamentsUpdatedEventArgs(GetTournaments()));

            return(tournament);
        }
Beispiel #4
0
        public static ICar AddCar(int tournamentID, ICar car)
        {
            IRlmRepository repo   = RepositoryManager.GetDefaultRepository();
            Car            newCar = Car.From(car);

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                // find an ID to assign to this new Car
                int maxID = 0;
                foreach (Car c in tournament.CarData)
                {
                    if (c.ID > maxID)
                    {
                        maxID = c.ID;
                    }
                }

                newCar.ID = maxID + 1;
                tournament.CarData.Add(newCar);

                repo.SaveTournament(tournament);
            }

            CarsUpdated?.Invoke(tournamentID, new CarsUpdatedEventArgs(GetCars(tournamentID)));
            StandingsUpdated?.Invoke(tournamentID, new StandingsUpdatedEventArgs(GetStandings(tournamentID)));

            return(newCar);
        }
        public static IRlmRepository GetDefaultRepository()
        {
            if (_repo == null)
            {
                _repo = new RlmFileRepository();
            }

            return(_repo);
        }
Beispiel #6
0
        public static ITournament GetTournament(int tournamentID)
        {
            Tournament     tournament = null;
            IRlmRepository repo       = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                tournament = repo.LoadTournament(tournamentID);
            }

            return(tournament);
        }
Beispiel #7
0
        public static RlmGetRacesResponse GetRaces(int tournamentID)
        {
            IRlmRepository      repo     = RepositoryManager.GetDefaultRepository();
            RlmGetRacesResponse response = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);
                response = GetRaces(tournament);
            }

            return(response);
        }
Beispiel #8
0
        public static IEnumerable <IStanding> GetStandings(int tournamentID)
        {
            IEnumerable <IStanding> result = null;
            IRlmRepository          repo   = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);
                result = GetStandings(tournament, false);
            }

            return(result);
        }
Beispiel #9
0
        public static IEnumerable <ICar> GetCars(int tournamentID)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            Tournament tournament = null;

            lock (_lock)
            {
                tournament = repo.LoadTournament(tournamentID);
            }

            return(tournament.CarData);
        }
Beispiel #10
0
        public static List <GroupResults> GetTournamentResults(int tournamentID)
        {
            IRlmRepository      repo    = RepositoryManager.GetDefaultRepository();
            List <GroupResults> results = new List <GroupResults>();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                results = GenerateTournamentResults(tournament);
            }

            return(results);
        }
Beispiel #11
0
        public static IEnumerable <ITournament> GetTournaments()
        {
            List <Tournament> result = new List <Tournament>();
            IRlmRepository    repo   = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                foreach (int i in repo.GetAllTournamentIDs())
                {
                    result.Add(repo.LoadTournament(i));
                }
            }

            return(result);
        }
Beispiel #12
0
        public static void SetCurrentRace(int tournamentID, int raceNum)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            IRace previousRace = GetCurrentRace(tournamentID);

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);
                Race       race       = tournament.RaceData.Where(r => r.RaceNumber == raceNum).Single();
                tournament.CurrentRace = raceNum;
                repo.SaveTournament(tournament);
            }

            CurrentRaceChanged?.Invoke(tournamentID, new CurrentRaceChangedEventArgs(previousRace, GetCurrentRace(tournamentID)));
            NextRacesUpdated?.Invoke(tournamentID, new NextRacesUpdatedEventArgs(GetNextRaces(tournamentID)));
        }
Beispiel #13
0
        public static IRace GetCurrentRace(int tournamentID)
        {
            IRlmRepository repo   = RepositoryManager.GetDefaultRepository();
            IRace          result = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                // CurrentRace starts at 1
                if ((tournament.CurrentRace >= 1) && (tournament.RaceData.Count > (tournament.CurrentRace - 1)))
                {
                    result = tournament.RaceData[tournament.CurrentRace - 1];
                }
            }

            return(result);
        }
Beispiel #14
0
        public static ICar UpdateCar(int tournamentID, ICar car)
        {
            IRlmRepository repo       = RepositoryManager.GetDefaultRepository();
            Car            updatedCar = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                updatedCar = tournament.CarData.Where(c => c.ID == car.ID).SingleOrDefault();
                updatedCar.CopyFrom(car);

                repo.SaveTournament(tournament);
            }

            CarUpdated?.Invoke(tournamentID, new CarUpdatedEventArgs(updatedCar));
            StandingsUpdated?.Invoke(tournamentID, new StandingsUpdatedEventArgs(GetStandings(tournamentID)));

            return(updatedCar);
        }
Beispiel #15
0
        public static void StopRace(int tournamentID, int raceNum)
        {
            RaceMonitor.Stop();
            RaceMonitor.LaneResultAdded -= RaceMonitor_LaneResultAdded;

            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);
                Race       race       = tournament.RaceData.Where(r => r.RaceNumber == raceNum).Single();
                tournament.CurrentRace = raceNum;
                race.State             = RaceState.Done;
                repo.SaveTournament(tournament);
            }

            //RacesUpdated?.Invoke(tournamentID, new RacesUpdatedEventArgs(GetRaces(tournamentID)));
            RaceUpdated?.Invoke(tournamentID, new RaceUpdatedEventArgs(GetCurrentRace(tournamentID)));
            //NextRacesUpdated?.Invoke(tournamentID, new NextRacesUpdatedEventArgs(GetNextRaces(tournamentID)));
        }
Beispiel #16
0
        public static ICar DeleteCar(int tournamentID, int carID)
        {
            IRlmRepository repo       = RepositoryManager.GetDefaultRepository();
            Car            deletedCar = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                deletedCar = tournament.CarData.Where(c => c.ID == carID).SingleOrDefault();
                tournament.CarData.Remove(deletedCar);

                repo.SaveTournament(tournament);
            }

            CarsUpdated?.Invoke(tournamentID, new CarsUpdatedEventArgs(GetCars(tournamentID)));
            StandingsUpdated?.Invoke(tournamentID, new StandingsUpdatedEventArgs(GetStandings(tournamentID)));

            return(deletedCar);
        }
Beispiel #17
0
        public static IRace UpdateRace(int tournamentID, IRace race)
        {
            IRlmRepository repo        = RepositoryManager.GetDefaultRepository();
            Race           updatedRace = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                updatedRace = tournament.RaceData.Where(r => r.RaceNumber == race.RaceNumber).Single();

                if (updatedRace.LaneAssignmentData.Count != race.LaneAssignments.Count())
                {
                    throw new ArgumentException("Races do not have the same number of lane assignments");
                }

                updatedRace.State = race.State;

                ILaneAssignment[] laneAssignments = race.LaneAssignments.ToArray();
                for (int i = 0; i < updatedRace.LaneAssignmentData.Count; i++)
                {
                    LaneAssignment  to   = updatedRace.LaneAssignmentData[i];
                    ILaneAssignment from = laneAssignments[i];
                    to.ElapsedTime = from.ElapsedTime;
                    to.Points      = from.Points;
                    to.Position    = from.Position;
                }

                repo.SaveTournament(tournament);
            }

            //RacesUpdated?.Invoke(tournamentID, new RacesUpdatedEventArgs(GetRaces(tournamentID)));
            RaceUpdated?.Invoke(tournamentID, new RaceUpdatedEventArgs(updatedRace));
            //NextRacesUpdated?.Invoke(tournamentID, new NextRacesUpdatedEventArgs(GetNextRaces(tournamentID)));
            StandingsUpdated?.Invoke(tournamentID, new StandingsUpdatedEventArgs(GetStandings(tournamentID)));

            return(updatedRace);
        }
Beispiel #18
0
        private static void UpdateRaceTime(int tournamentID, int raceNum, int lane, long elapsedTime)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                Race race = tournament.RaceData.Where(r => r.RaceNumber == raceNum).Single();

                if (lane > race.LaneAssignmentData.Count)
                {
                    throw new ArgumentException(string.Format("Lane {0} does not existin in race {1}", lane, race.RaceNumber));
                }

                race.LaneAssignmentData[lane - 1].ElapsedTime = elapsedTime;
                race.LaneAssignmentData[lane - 1].ScaleSpeed  = CalculateSpeed(tournament.TrackLengthInches, elapsedTime / 100000.0);

                CalculatePoints(ref race);

                repo.SaveTournament(tournament);
            }
        }
Beispiel #19
0
        public static ITournament UpdateTournament(int tournamentID, string newName, int numLanes)
        {
            IRlmRepository repo = RepositoryManager.GetDefaultRepository();

            Tournament tournament = null;

            lock (_lock)
            {
                tournament = repo.LoadTournament(tournamentID);
                if (tournament == null)
                {
                    throw new ArgumentException(string.Format("Tournament with ID {0} does not exist.", tournamentID));
                }

                tournament.Name     = newName;
                tournament.NumLanes = numLanes;

                repo.SaveTournament(tournament);
            }

            TournamentUpdated?.Invoke(new TournamentUpdatedEventArgs(tournament));

            return(tournament);
        }
Beispiel #20
0
        public static RlmGetRacesResponse GenerateRaces(int tournamentID)
        {
            IRlmRepository      repo   = RepositoryManager.GetDefaultRepository();
            RlmGetRacesResponse result = null;

            lock (_lock)
            {
                Tournament tournament = repo.LoadTournament(tournamentID);

                // generate the races
                TournamentManager.GenerateRaces(ref tournament);

                repo.SaveTournament(tournament);
                result = GetRaces(tournament);
            }

            RacesUpdated?.Invoke(tournamentID, new RacesUpdatedEventArgs(result));
            CurrentRaceChanged?.Invoke(tournamentID, new CurrentRaceChangedEventArgs(null, GetCurrentRace(tournamentID)));
            //RaceUpdated?.Invoke(tournamentID, new RaceUpdatedEventArgs(GetCurrentRace(tournamentID)));
            NextRacesUpdated?.Invoke(tournamentID, new NextRacesUpdatedEventArgs(GetNextRaces(tournamentID)));
            StandingsUpdated?.Invoke(tournamentID, new StandingsUpdatedEventArgs(GetStandings(tournamentID)));

            return(result);
        }