Skip to content

Commit

Permalink
differentiate btw occupying and reserving a parking spot
Browse files Browse the repository at this point in the history
  • Loading branch information
Grimmgockel committed May 24, 2023
1 parent 1bdcc1b commit 5523d5e
Show file tree
Hide file tree
Showing 7 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,19 @@ public void UpdateDestination(CarData carData)
carData.Destination = carData.World.ParkingSpotMap[pathRespone.ReservedParkingSpot].Target;

// reserve spot
carData.OccupiedSpot = pathRespone.ReservedParkingSpot;
carData.OccupiedSpot.Occupied = true;
carData.ReservedSpot = pathRespone.ReservedParkingSpot;
carData.ReservedSpot.Reserved = true;
}

public void SeekParkingSpot(CarData carData)
{
carData.Path = new List<StreetEdge> { carData.World.ParkingSpotMap[carData.OccupiedSpot] } ;
carData.Path = new List<StreetEdge> { carData.World.ParkingSpotMap[carData.ReservedSpot] } ;
}

public async Task<bool> AttemptLocalParking(CarData carData)
{
if (carData.Position.DistanceFromSource < carData.OccupiedSpot.DistanceFromSource) return false;
carData.Park();
if (carData.Position.DistanceFromSource < carData.ReservedSpot.DistanceFromSource) return false;
carData.Park(carData.ReservedSpot);
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,8 @@ public async Task<bool> AttemptLocalParking(CarData carData)
if (passedParkingSpots.Count == 0) return false;
var nearestUnoccupiedSpot = passedParkingSpots.Peek();

// occupy
carData.OccupiedSpot = nearestUnoccupiedSpot;
carData.OccupiedSpot.Occupied = true;

// physically park
carData.Park();
carData.Park(nearestUnoccupiedSpot);
return true;
}

Expand Down
2 changes: 1 addition & 1 deletion src/Solution1/ConsoleApp1/pgs/NearestParkingStrategy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public ParkingSpot FindParkingSpot(PhysicalWorld world, StreetNode destination)
foreach (var edge in adjacentEdges)
{
// filter unoccupied spots and sort by distance from source ascending
var unoccupiedSpots = edge.ParkingSpots.Where(spot => !spot.Occupied)
var unoccupiedSpots = edge.ParkingSpots.Where(spot => !spot.Occupied && !spot.Reserved)
.OrderBy(spot => spot.DistanceFromSource)
.ToList();

Expand Down
2 changes: 1 addition & 1 deletion src/Solution1/ConsoleApp1/pgs/ParkingGuidanceSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public PathResponse RequestGuidanceFromServer(StreetPosition position, StreetNod
if (shortestPaths.Invoke(edgeWithFreeSpot.Source, out var path))
{
// occupy free spot
freeSpot.Occupied = true;
freeSpot.Reserved = true;
return new PathResponse(path.Append(edgeWithFreeSpot).ToList(), freeSpot);
}
// pathing failed
Expand Down
7 changes: 5 additions & 2 deletions src/Solution1/ConsoleApp1/sim/CarData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ public class CarData
public StreetNode Destination { get; set; }

public ParkingSpot OccupiedSpot { get; set; } = null!;
public ParkingGuidanceSystem pgs { get; set; }
public CarStatus Status { get; set; }
public bool Logging { get; set; }
public const int MaxParkTime = 500;
Expand Down Expand Up @@ -66,9 +65,12 @@ public CarData(int id, PhysicalWorld world, ParkingGuidanceSystem parkingGuidanc
}

public ParkingGuidanceSystem Pgs { get; set; }
public ParkingSpot ReservedSpot { get; set; }

public void Park()
public void Park(ParkingSpot spot)
{
OccupiedSpot = spot;
OccupiedSpot.Occupied = true;
Position = new StreetPosition(Position.StreetEdge, OccupiedSpot.DistanceFromSource);
lock (Position.StreetEdge)
{
Expand Down Expand Up @@ -153,6 +155,7 @@ public void ResetAfterParking()
lock (Position.StreetEdge)
{
OccupiedSpot.Occupied = false;
OccupiedSpot.Reserved = false;
Position.StreetEdge.IncrementCarCount();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Solution1/ConsoleApp1/sim/ParkingSpot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,19 @@
public class ParkingSpot
{
public bool Occupied { get; set; }
public bool Reserved { get; set; }
public double Length { get; }
public double DistanceFromSource { get; }
public double StreetLength { get; }
public int Index { get; }


public ParkingSpot(int index, double distanceFromSource, double streetLength, double length, bool occupied)
public ParkingSpot(int index, double distanceFromSource, double streetLength, double length, bool occupied, bool reserved)
{
Length = length;
StreetLength = streetLength;
Occupied = occupied;
Reserved = reserved;
Index = index;
DistanceFromSource = distanceFromSource;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Solution1/ConsoleApp1/sim/graph/StreetEdge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void InitParkingSpots(Dictionary<ParkingSpot, StreetEdge> parkingSpotMap)
double distanceFromSource = (ParkingSpotLength + ParkingSpotSpacing) * i;
bool occupied = rand.NextDouble() >= InitialParkingSpotOccupancyRate;
if (!occupied) UnoccupiedSpotCount++;
return new ParkingSpot(i, distanceFromSource, Length, ParkingSpotLength, occupied);
return new ParkingSpot(i, distanceFromSource, Length, ParkingSpotLength, occupied, false);
}).ToList();

ParkingSpots.ForEach(parkingSpot => parkingSpotMap.Add(parkingSpot, this));
Expand Down

0 comments on commit 5523d5e

Please sign in to comment.