Example #1
0
        private static void GenerateRaces(ref Tournament tournament)
        {
            tournament.RaceData = new List <Race>();

            RaceGenerator generator = new RaceGenerator(null);

            // rawRaces has a format of [race,lane] where race and lane are 0 based
            // the number at [race,lane] is the 1-based racer number
            int[,] rawRaces = generator.Solve(tournament.Cars.Count(), tournament.NumLanes);

            List <ICar> cars = new List <ICar>(tournament.Cars);

            for (int raceNum = 0; raceNum < rawRaces.GetLength(0); raceNum++)
            {
                Race race = new Race();
                race.RaceNumber         = raceNum + 1;
                race.State              = RaceState.NotStarted;
                race.LaneAssignmentData = new List <LaneAssignment>();

                for (int laneNum = 0; laneNum < rawRaces.GetLength(1); laneNum++)
                {
                    LaneAssignment laneAssignment = new LaneAssignment();
                    laneAssignment.Lane        = laneNum + 1;
                    laneAssignment.ElapsedTime = 0;
                    int rawRacer = rawRaces[raceNum, laneNum];
                    if (rawRacer <= 0)
                    {
                        laneAssignment.Car = null;
                    }
                    else
                    {
                        laneAssignment.Car = Car.From(cars[rawRacer - 1]);
                    }
                    race.LaneAssignmentData.Add(laneAssignment);
                }

                tournament.RaceData.Add(race);
            }

            if (rawRaces.GetLength(0) > 0)
            {
                tournament.CurrentRace = 1;
            }
        }
Example #2
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);
        }