示例#1
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);
        }
示例#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);
        }
示例#3
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);
        }
示例#4
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)));
        }
示例#5
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)));
        }
示例#6
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);
        }
示例#7
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);
        }
示例#8
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);
        }
示例#9
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);
            }
        }
示例#10
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);
        }
示例#11
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);
        }