Esempio n. 1
0
        public static async Task StartRaceAsync(int id)
        {
            SQLiteDataAccess.InstanceDB();

            var race = GetRace(id);

            race.RaceStatus = (int)Enums.RaceStatus.running;
            UpdateRace(race);

            //Update all vehicle status to 0
            VehicleDataLayer.UpdateVehicleStatus();

            List <VehicleModel>         allVehicle = VehicleDataLayer.GetAllVehicles();
            List <Task <VehicleModel> > tasks      = new List <Task <VehicleModel> >();

            foreach (var v in allVehicle)
            {
                tasks.Add(Task.Run(() => VehicleStartRace(v)));
            }

            var raceResult = await Task.WhenAll(tasks);

            race.RaceStatus = (int)Enums.RaceStatus.finished;
            UpdateRace(race);
        }
Esempio n. 2
0
        private static VehicleModel VehicleStartRace(VehicleModel vehicle)
        {
            int    i             = 1;
            int    totalDistance = 10000;
            Random random        = new Random();
            var    stopWatc      = System.Diagnostics.Stopwatch.StartNew();

            while (vehicle.Distance <= totalDistance)
            {
                // 1h is equal 1s in simulation
                var randomNum = random.Next(1, 101);
                vehicle.Distance = i * vehicle.Speed;


                if (randomNum <= vehicle.HeavyMalFun)
                {
                    vehicle.VehicleStatus = (int)Enums.VehicleStatus.heavyMalFun;
                }
                else if (randomNum > vehicle.HeavyMalFun && randomNum <= (vehicle.LightMalFun + vehicle.HeavyMalFun))
                {
                    vehicle.VehicleStatus = (int)Enums.VehicleStatus.lightMalFun;
                }

                //Update vehicle data
                vehicle.Time = (stopWatc.ElapsedMilliseconds / 1000).ToString();
                VehicleDataLayer.UpdateVehicle(vehicle);

                if (vehicle.VehicleStatus == (int)Enums.VehicleStatus.heavyMalFun)
                {
                    break;
                }
                else if (vehicle.VehicleStatus == (int)Enums.VehicleStatus.lightMalFun)
                {
                    Thread.Sleep(vehicle.MalfunctionTime * 1000);
                    vehicle.VehicleStatus = (int)Enums.VehicleStatus.good;
                    VehicleDataLayer.UpdateVehicle(vehicle);
                }
                else
                {
                    VehicleDataLayer.UpdateVehicle(vehicle);
                    Thread.Sleep(1000);
                }

                if (vehicle.Distance >= totalDistance)
                {
                    vehicle.VehicleStatus = (int)Enums.VehicleStatus.finishRace;
                    vehicle.Time          = (stopWatc.ElapsedMilliseconds / 1000).ToString();
                    VehicleDataLayer.UpdateVehicle(vehicle);
                    break;
                }

                i++;
            }

            return(vehicle);
        }