Exemplo n.º 1
0
        public static RacerStatus Decode(byte[] bytes)
        {
            MemoryStream mstream = new MemoryStream(bytes);
            DataContractJsonSerializer serializer = new DataContractJsonSerializer(typeof(RacerStatus));
            RacerStatus result = (RacerStatus)serializer.ReadObject(mstream);

            return(result);
        }
Exemplo n.º 2
0
        public void UpdateStatus(RacerStatus status)
        {
            Sensor sensor = Sensors[status.SensorId];
            Racer racer = Racers[status.RacerBibNumber];
            DateTime timestamp = new DateTime(status.Timestamp);

            racer.UpdateLocation(sensor.Location, timestamp);
        }
Exemplo n.º 3
0
        public void Setup()
        {
            // Note: without changing functionality we will refactor this method to improve cohesion

            // Setup defaults
            if (NumberOfSensors <= 0)
                NumberOfSensors = 20;
            if (LengthOfRace <= 0)
                LengthOfRace = 100;

            // Setup sensors
            sensors = new List<Sensor>();
            for (int i = 0; i < NumberOfSensors; i++)
            {
                double mileMarker = (i+1) * Convert.ToDouble(LengthOfRace) / Convert.ToDouble(NumberOfSensors);
                Sensor sensor = new Sensor() { Id = i, MileMarker = mileMarker };
                sensors.Add(sensor);
            }

            // Create race groups
            groups = new List<RaceGroup>()
            {
                new RaceGroup() { Id=1, Label="Mens Cat 1-2", MinBibNumber=1, MaxBibNumber=99, StartTime=0, AverageSpeed=25.0, MinNumberOfRacers = 20, MaxNumberOfRacers=50 },
                new RaceGroup() { Id=2, Label="Womens Cat 1-3", MinBibNumber=100, MaxBibNumber=199, StartTime=5*60*1000, AverageSpeed=25.0, MinNumberOfRacers = 20, MaxNumberOfRacers=50 },
                new RaceGroup() { Id=3, Label="Womens Cat 4", MinBibNumber=200, MaxBibNumber=299, StartTime=10*60*1000, AverageSpeed=25.0, MinNumberOfRacers = 20, MaxNumberOfRacers=50 },
                new RaceGroup() { Id=4, Label="Mens Cat 3", MinBibNumber=300, MaxBibNumber=399, StartTime=15*60*1000, AverageSpeed=24.0, MinNumberOfRacers = 20, MaxNumberOfRacers=50 },
                new RaceGroup() { Id=5, Label="Mens Cat 4", MinBibNumber=400, MaxBibNumber=499, StartTime=20*60*1000, AverageSpeed=22.0, MinNumberOfRacers = 20, MaxNumberOfRacers=50 },
                new RaceGroup() { Id=6, Label="Mens Cat 5A", MinBibNumber=5000, MaxBibNumber=5099, StartTime=25*60*1000, AverageSpeed=20.0, MinNumberOfRacers = 40, MaxNumberOfRacers=60 },
                new RaceGroup() { Id=7, Label="Mens Cat 5B", MinBibNumber=5100, MaxBibNumber=5199, StartTime=30*60*1000, AverageSpeed=20.0, MinNumberOfRacers = 40, MaxNumberOfRacers=60 },
                new RaceGroup() { Id=8, Label="Mens Cat 5C", MinBibNumber=5200, MaxBibNumber=5299, StartTime=35*60*1000, AverageSpeed=20.0, MinNumberOfRacers = 40, MaxNumberOfRacers=60 },
            };

            // Setup race groups
            foreach (RaceGroup group in groups)
                group.Setup();

            // Setup a 1 cheater in each group
            for (int raceGroupA = 0; raceGroupA < groups.Count - 1; raceGroupA++)
            {
                int raceGroupB = raceGroupA + 1;
                int cheaterIndex1 = RandomChooser.ChooseInt(0, groups[raceGroupA].Count);
                Racer cheater1 = groups[raceGroupA][cheaterIndex1];
                int cheaterIndex2 = RandomChooser.ChooseInt(0, groups[raceGroupB].Count);
                Racer cheater2 = groups[raceGroupB][cheaterIndex2];

                cheater1.WillTryToCheatWith = cheater2;
                cheater2.WillTryToCheatWith = cheater1;
            }

            // Compute race times
            messagesToSend = new List<RacerStatus>();
            double previousMilageMarker = 0;
            for (int sensorIndex = 0; sensorIndex < sensors.Count; sensorIndex++)
            {
                Sensor sensor = sensors[sensorIndex];
                double deltaMilage = sensor.MileMarker - previousMilageMarker;
                foreach (RaceGroup group in groups)
                {
                    for (int i = 0; i < group.Count; i++)
                    {
                        Racer racer = group[i];
                        if (racer.Time == 0)
                            racer.Time = group.StartTime;

                        double speed = racer.RelativeSpeed + group.AverageSpeed;
                        racer.Time += Convert.ToInt32(60*60*1000*deltaMilage / speed);
                    }
                }

                // Converge cheaters
                foreach (RaceGroup group in groups)
                {
                    foreach (Racer racer in group)
                    {
                        if (racer.WillTryToCheatWith != null)
                        {
                            int averageTime = (racer.Time + racer.WillTryToCheatWith.Time) / 2;
                            int delta = averageTime - racer.Time;
                            racer.Time += delta / 2;
                        }
                    }
                }

                // Create race status object
                foreach (RaceGroup group in groups)
                {
                    foreach (Racer racer in group)
                    {
                        RacerStatus status = new RacerStatus() { SensorId = sensor.Id, RacerBibNumber = racer.RaceBibNumber, Timestamp = racer.Time };
                        sensor.RacerTimes.Add(status);
                        messagesToSend.Add(status);
                    }
                }

                previousMilageMarker = sensor.MileMarker;
            }

            messagesToSend.Sort(delegate(RacerStatus x, RacerStatus y)
            {
                if (x.Timestamp < y.Timestamp) return -1;
                else if (x.Timestamp > y.Timestamp) return 1;
                return 0;
            });
        }
Exemplo n.º 4
0
        public void Setup()
        {
            // Note: This method has poor cohesion. Without changing functionality, we will refactor this method in class to improve cohesion

            SetupDefaults();

            SetupSensors();

            CreateRaceGroups();

            // Setup race groups
            foreach (RaceGroup group in groups)
                group.Setup();

            // Setup a 1 cheater in each group
            for (int raceGroupA = 0; raceGroupA < groups.Count - 1; raceGroupA++)
            {
                int raceGroupB = raceGroupA + 1;
                int cheaterIndex1 = RandomChooser.ChooseInt(0, groups[raceGroupA].Count);
                Racer cheater1 = groups[raceGroupA][cheaterIndex1];
                int cheaterIndex2 = RandomChooser.ChooseInt(0, groups[raceGroupB].Count);
                Racer cheater2 = groups[raceGroupB][cheaterIndex2];

                cheater1.WillTryToCheatWith = cheater2;
                cheater2.WillTryToCheatWith = cheater1;
            }

            // Compute race times
            messagesToSend = new List<RacerStatus>();
            double previousMilageMarker = 0;
            for (int sensorIndex = 0; sensorIndex < sensors.Count; sensorIndex++)
            {
                Sensor sensor = sensors[sensorIndex];
                double deltaMilage = sensor.MileMarker - previousMilageMarker;
                foreach (RaceGroup group in groups)
                {
                    for (int i = 0; i < group.Count; i++)
                    {
                        Racer racer = group[i];
                        if (racer.Time == 0)
                            racer.Time = group.StartTime;

                        double speed = racer.RelativeSpeed + group.AverageSpeed;
                        racer.Time += Convert.ToInt32(60*60*1000*deltaMilage / speed);
                    }
                }

                // Converge cheaters
                foreach (RaceGroup group in groups)
                {
                    foreach (Racer racer in group)
                    {
                        if (racer.WillTryToCheatWith != null)
                        {
                            int averageTime = (racer.Time + racer.WillTryToCheatWith.Time) / 2;
                            int delta = averageTime - racer.Time;
                            racer.Time += delta / 2;
                        }
                    }
                }

                // Create race status object
                foreach (RaceGroup group in groups)
                {
                    foreach (Racer racer in group)
                    {
                        RacerStatus status = new RacerStatus() { SensorId = sensor.Id, RacerBibNumber = racer.RaceBibNumber, Timestamp = racer.Time };
                        sensor.RacerTimes.Add(status);
                        messagesToSend.Add(status);
                    }
                }

                previousMilageMarker = sensor.MileMarker;
            }

            messagesToSend.Sort(delegate(RacerStatus x, RacerStatus y)
            {
                if (x.Timestamp < y.Timestamp) return -1;
                else if (x.Timestamp > y.Timestamp) return 1;
                return 0;
            });
        }