private void CheckSwitchPlan() { if (plans.Any(x => x.ChangeSwitchAt.ContainsKey(GlobalTime))) { TrainPlanner planner = plans.Where(x => x.ChangeSwitchAt.ContainsKey(GlobalTime)).First(); planner.ChangeSwitchAt[GlobalTime].Item1._Direction = planner.ChangeSwitchAt[GlobalTime].Item2; ControllerLog.Content = $"Switch at position {planner.ChangeSwitchAt[GlobalTime].Item1.Position.ToString()} changed direction."; TimeLine.Add($"[TIMELINE][{GlobalTime.ToString("HH:mm")}]: Switch at position {planner.ChangeSwitchAt[GlobalTime].Item1.Position.ToString()} changed direction."); } }
public void BeginObserving() { //Assign switches with their grid positions Switch.Switches = Switch.GetSwitches(); TrainTrack.TrackGrid = TrainTrack.READONLYGRID; //this will be our console and position refresh rate (once every 0.5s) ObserverTimer = new Timer(500); ObserverTimer.Elapsed += UpdateConsole; ObserverTimer.AutoReset = true; ObserverTimer.Enabled = true; ObserverTimer.Start(); //Remove all the trains that aren't being operated from our list trains.RemoveAll(x => !x.Operated); TrainPlanner planner = new TrainPlanner(trains[0]) .CreateTimeTable(timeTables) .SwitchPlan(Switch.Switches[0], "10:23", Switch.Direction.Left) .CrossingPlan("10:20", "10:24") .SwitchPlan(Switch.Switches[1], "10:44", Switch.Direction.Forward) .ToPlan(); TrainPlanner planner2 = new TrainPlanner(trains[1]) .CreateTimeTable(timeTables) .SwitchPlan(Switch.Switches[1], "10:38", Switch.Direction.Right) .CrossingPlan("11:28", "11:34") .SwitchPlan(Switch.Switches[0], "11:20", Switch.Direction.Forward) .ToPlan(); plans.Add(planner); plans.Add(planner2); //Assign the plans to their respective trains trains[0].SetPlan(planner); trains[1].SetPlan(planner2); //Assign the trains start positions based on their start stations position AssignStartPosition(trains[0], planner); AssignStartPosition(trains[1], planner2); //Start each train on a separate thread trains.ForEach(x => x.Start()); }
private void AssignStartPosition(Train train, TrainPlanner planner) { //Find the start station ID int StartStation = planner.Table.Where(x => x.ArrivalTime == null).First().StationId; bool finished = false; //Loop through the grid to find the YX position of the startstation ID for (int y = 0; y < TrainTrack.READONLYGRID.Length; y++) { for (int x = 0; x < TrainTrack.READONLYGRID[y].Length; x++) { if (TrainTrack.READONLYGRID[y][x] == char.Parse(StartStation.ToString())) { train.SetStartPosition(new Position(y, x)); finished = true; break; } } if (finished) { break; } } }
public static void SavePlan(TrainPlanner plan, string planName) { string path = @"C:\Windows\Temp\" + planName; string timeTablePath = path + @"\timetables.txt"; string trainPath = path + @"\trains.txt"; string crossingPath = path + @"\crossing.txt"; string switchPath = path + @"\switch.txt"; try { if (Directory.Exists(path)) { Console.WriteLine("A plan with than name already exists."); return; } DirectoryInfo folder = Directory.CreateDirectory(path); Console.WriteLine("Plan was created successfully at {0}.", Directory.GetCreationTime(path)); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); return; } if (!File.Exists(path)) { using (StreamWriter swTable = File.CreateText(timeTablePath)) { swTable.WriteLine("trainId, stationId, departure, arrival"); foreach (var timeTable in plan.Table) { swTable.WriteLine($"{timeTable.TrainId}," + $"{timeTable.StationId}," + $"{(timeTable.DepartureTime != null ? $"{timeTable.DepartureTime.Value.Hour}:{timeTable.DepartureTime.Value.Minute}" : "null")}," + $"{(timeTable.ArrivalTime != null ? $"{timeTable.ArrivalTime.Value.Hour}:{timeTable.ArrivalTime.Value.Minute}" : "null")}"); } swTable.Close(); } using (StreamWriter swTrain = File.CreateText(trainPath)) { swTrain.WriteLine("Id,Name,MaxSpeed,Operated"); swTrain.WriteLine($"{plan.Train.Id},{plan.Train.Name},{plan.Train.TopSpeed},{plan.Train.Operated}"); swTrain.Close(); } using (StreamWriter swCrossing = File.CreateText(crossingPath)) { swCrossing.WriteLine("CloseAt,OpenAt"); swCrossing.WriteLine($"{plan.CrossingCloseAt.Hour}:{plan.CrossingCloseAt.Minute},{plan.CrossingOpenAt.Hour}:{plan.CrossingOpenAt.Minute}"); swCrossing.Close(); } using (StreamWriter swSwitch = File.CreateText(switchPath)) { swSwitch.WriteLine("ChangeAt(Time),Position[X,Y],Direction"); foreach (KeyValuePair <DateTime, (Switch, Switch.Direction)> kvp in plan.ChangeSwitchAt) { swSwitch.WriteLine($"{kvp.Key.Hour}:{kvp.Key.Minute},{kvp.Value.Item1.Position.X}:{kvp.Value.Item1.Position.Y}, {kvp.Value.Item2}"); } swSwitch.Close(); } } }
public static TrainPlanner LoadPlan(string path) { string timeTablePath = @"C:\Windows\Temp\" + path + @"\timetables.txt"; string trainPath = @"C:\Windows\Temp\" + path + @"\trains.txt"; string crossingPath = @"C:\Windows\Temp\" + path + @"\crossing.txt"; string switchPath = @"C:\Windows\Temp\" + path + @"\switch.txt"; List <TimeTable> timeTableList = DeserializeTimeTables(timeTablePath, ','); List <Train> train1 = DeserializeTrains(trainPath, ','); // Deserialize crossing string[] crossings; crossings = File.ReadAllLines(crossingPath); string openAt = String.Empty; string closeAt = String.Empty; //int crossingId = 0; //bool something; foreach (string lines in crossings.Skip(1)) { string[] parts = lines.Split(','); openAt = parts[0]; closeAt = parts[1]; } // Deserialize Switches string[] switches; switches = File.ReadAllLines(switchPath); List <string> dateTime = new List <string>(); List <Switch> tempSwitch = new List <Switch>(); List <Switch.Direction> directions = new List <Switch.Direction>(); foreach (string lines in switches.Skip(1)) { string[] parts = lines.Split(','); string[] positionXY = parts[1].Split(':'); Position position = new Position(int.Parse(positionXY[1]), int.Parse(positionXY[0])); dateTime.Add(parts[0]); if (parts[2] == "Left") { directions.Add(Switch.Direction.Left); } else if (parts[2] == "Forward") { directions.Add(Switch.Direction.Forward); } else { directions.Add(Switch.Direction.Right); } tempSwitch.Add(Switch.Switches.Where(x => x.Position.X == position.X).First()); } // Create TrainPlanner TrainPlanner trainPlan = new TrainPlanner(train1[0]) .CreateTimeTable(timeTableList) .CrossingPlan(openAt, closeAt) .SwitchPlan(tempSwitch[0], dateTime[0], directions[0]) .SwitchPlan(tempSwitch[1], dateTime[1], directions[1]) .ToPlan(); return(trainPlan); }