Reservation timeout fix

This commit is contained in:
Dawid Pietrykowski 2022-09-04 13:36:19 +02:00
parent 85dd77265d
commit 554a30b09e

View File

@ -102,6 +102,10 @@ namespace Parking
// TODO: Check if car can stay before other reservation - may not work for priority-based reservations
var triesToPark = _currentTime <= driver.Times[3].TimeOfDay && _currentTime > driver.Times[2].TimeOfDay;
var leftTheParking = _currentTime > driver.Times[3].TimeOfDay && driver.Parked;
bool reservedButTimedout =
(_currentTime - driver.Times[0].TimeOfDay).TotalMinutes > driver.UpdateInterval &&
GetReservedSpotCount(driver.Priority, driver.Size) > 0
&& !driver.Parked && !driver.Rejected && _currentTime <= driver.Times[3].TimeOfDay;
if (triesToPark && !driver.Parked && !driver.Rejected) {
if (!PlaceCarOnParking(driver)) {
Debug.Log($"Placing failed for car {driver.Number}");
@ -114,8 +118,13 @@ namespace Parking
else if (leftTheParking) {
driver.Spot.Reserved = false;
driver.Reset();
} else if (reservedButTimedout) {
Debug.Log($"Reserved but timed out driver {driver.Number}");
driver.Rejected = true;
}
}
ReconfigureSpots();
}
private void ResetDrivers()
@ -129,6 +138,19 @@ namespace Parking
}
}
private int GetReservedSpotCount(int priority, Size size)
{
int count = 0;
foreach (List<Spot> list in _spotMap) {
foreach (Spot spot in list) {
if (spot.Size == size && spot.Reserved && spot.ReservedPriority == priority)
count++;
}
}
return count;
}
private bool FindReservedSpot(Driver driver, out Spot spot)
{
foreach (var list in _spotMap)
@ -209,8 +231,8 @@ namespace Parking
int[] neededSpots = {0, 0, 0, 0};
for (var i = 0; i < neededSpots.Length; i++)
neededSpots[i] = Math.Max(freeSpots[i] - plannedSpots[i], 0);
if (neededSpots.Max() > 0) {
neededSpots[i] = Math.Max(plannedSpots[i] - freeSpots[i], 0);
if (neededSpots.Sum() > 5) {
Debug.Log($"Needed spots = {neededSpots[0]} {neededSpots[1]} {neededSpots[2]} {neededSpots[3]}");
Debug.Log("Attempting reconfiguration...");
}
@ -233,8 +255,8 @@ namespace Parking
TimeSpan updatedTime = _currentTime + TimeSpan.FromMinutes(stepTime);
while (nextCars.Count < n && updatedTime < EndTime) {
foreach (Driver driver in DataImporter.Drivers)
if (_currentTime + updatedTime <= driver.Times[3].TimeOfDay &&
_currentTime > driver.Times[2].TimeOfDay &&
if (updatedTime <= driver.Times[1].TimeOfDay &&
updatedTime > driver.Times[0].TimeOfDay &&
!driver.Parked && !driver.Rejected && !nextCars.Contains(driver))
nextCars.Add(driver);
updatedTime += TimeSpan.FromMinutes(stepTime);