Example #1
0
        static void Main(string[] args)
        {
            //Minutes for each step; 1 = 1 real life second
            const int Latency = 1;

            Console.WriteLine("Before CurrentOccupation:");
            using (var db = new BusSimulationContext())
            {
                foreach (var routeTime in db.BusRouteTime)
                {
                    RealLifeBus.RouteTimeList.Add(new RouteTimeHelper(routeTime));
                }
                Console.WriteLine("Before CurrentOccupation:");
                var busList = new List <RealLifeBus>();
                foreach (var bus in db.Bus)
                {
                    if (bus.BusWorkerId != null && bus.BusRouteId != null)
                    {
                        busList.Add(new RealLifeBus(bus));
                    }
                }

                while (true)
                {
                    foreach (var bus in busList)
                    {
                        Console.WriteLine("Before CurrentOccupation:");
                        Console.WriteLine(bus.Bus.CurrentOccupation);
                        Console.WriteLine("Before Current Location");
                        Console.WriteLine(bus.Bus.CurrentLocation);
                        Console.WriteLine("Before Current Progression:");
                        Console.WriteLine(bus.CurrentLocationProgression);
                        bus.MoveBus();
                        if (!bus.Bus.CurrentLocation.Contains("-"))
                        {
                            bus.RandomizePassenger();
                        }
                        Console.WriteLine("After CurrentOccupation:");
                        Console.WriteLine(bus.Bus.CurrentOccupation);
                        Console.WriteLine("After Current Location:");
                        Console.WriteLine(bus.Bus.CurrentLocation);
                        Console.WriteLine("After Current Progression:");
                        Console.WriteLine(bus.CurrentLocationProgression);
                    }
                    Thread.Sleep(Latency * 1000);
                    Console.Clear();
                    db.SaveChanges();
                }
            }
        }
Example #2
0
        public RealLifeBus(Bus bus)
        {
            this.Bus = bus;

            if (Bus.ReversedDirection == null)
            {
                Bus.ReversedDirection = false;
            }

            Route = new List <string>();

            string tempRouteString;

            using (var db = new BusSimulationContext())
            {
                tempRouteString = db.BusRoute.SingleOrDefault(x => x.Id == this.Bus.BusRouteId).Route;
            }
            var tempRoute = tempRouteString.Split('-');

            Route.Add(tempRoute[0]);
            for (int k = 1; k < tempRoute.Length * 2 - 1; k++)
            {
                if (k % 2 == 0)
                {
                    Route.Add(tempRoute[k / 2]);
                }
                else if ((bool)bus.ReversedDirection)
                {
                    Route.Add(tempRoute[((k - 1) / 2) + 1] + "-" + tempRoute[(k - 1) / 2]);
                }
                else
                {
                    Route.Add(tempRoute[(k - 1) / 2] + "-" + tempRoute[((k - 1) / 2) + 1]);
                }
            }

            if (Bus.CurrentLocation == null)
            {
                Bus.CurrentLocation = tempRoute[0];
            }

            CurrentLocationProgression = 0;
            CurrentRouteLocationIndex  = 0;
            MaximumLocationProgression = 1;
        }