Time-based placement WIP

This commit is contained in:
Dawid Pietrykowski 2022-10-10 19:45:07 +02:00
parent 549691cc0c
commit 2574f9701d
2 changed files with 106 additions and 11 deletions

View File

@ -16,6 +16,12 @@ namespace Parking
public Spot Spot;
public DateTime[] Times;
public int UpdateInterval;
public Spot ReservedSpot;
public bool Reserved;
public TimeSpan RealArrival => Times[2].TimeOfDay;
public TimeSpan RealDeparture => Times[3].TimeOfDay;
public TimeSpan PlannedArrival => Times[0].TimeOfDay;
public TimeSpan PlannedDeparture => Times[1].TimeOfDay;
public Driver(Size size, int number, ParkingPreference parkingPreference, int priority, int updateInterval)
{

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using TMPro;
using UnityEngine;
@ -105,6 +106,7 @@ namespace Parking
private void ReserveInitialSpots()
{
return;
for (int j = 3; j >= 0; j--)
foreach (Spot spot in _spotMap[j]) {
spot.Reserved = false;
@ -124,6 +126,61 @@ namespace Parking
}
}
}
private void ReserveSpotsInTimeRange(float hours)
{
// for (int j = 3; j >= 0; j--)
// foreach (Spot spot in _spotMap[j]) {
// spot.Reserved = false;
// spot.ReservedPriority = 0;
// }
TimeSpan maxArrivalTime = _currentTime + TimeSpan.FromHours(hours);
List<Driver> nextDrivers = new List<Driver>();
foreach (Driver driver in DataImporter.Drivers) {
if (driver.RealArrival >= _currentTime && driver.PlannedArrival < maxArrivalTime) {
nextDrivers.Add(driver);
}
}
nextDrivers.Sort(((a, b) => Comparer<TimeSpan>.Default.Compare(a.PlannedDeparture, b.PlannedDeparture)));
List<Spot> availableSpots = new List<Spot>();
foreach (List<Spot> list in _spotMap)
foreach (Spot spot in list)
if (!spot.Reserved && spot.Free)
availableSpots.Add(spot);
availableSpots.Sort(((a, b) => Comparer<float>.Default.Compare(a.Position.x, b.Position.x)));
availableSpots.Reverse();
for (int i = 0; i < nextDrivers.Count; i++) {
Driver driver = nextDrivers[i];
for (int j = 0; j < availableSpots.Count; j++) {
Spot spot = availableSpots[j];
if (driver.Size == spot.Size && !spot.Reserved) {
spot.Reserved = true;
driver.Reserved = true;
driver.ReservedSpot = spot;
break;
}
}
}
// for (int i = 0; i < Math.Min(DataImporter.Drivers.Count, _initialConfigurationSpotCount); i++) {
// bool found = false;
// // Find spot
// for (int j = 3; j >= 0 && !found; j--)
// foreach (Spot spot in _spotMap[j])
// if (spot.Size == DataImporter.Drivers[i].Size && !spot.Reserved) {
// spot.Reserved = true;
// spot.ReservedPriority = DataImporter.Drivers[i].Priority;
// found = true;
// break;
// }
// }
}
public void AdvanceTime()
{
@ -155,6 +212,19 @@ namespace Parking
if (_reconfigurationActive)
ReconfigureSpots();
// if(_currentTime.Hours % 3 == 0 && _currentTime.Minutes == 0)
// ReserveSpotsInTimeRange(3);
if(_currentTime.Hours == 6 && _currentTime.Minutes == 0)
ReserveSpotsInTimeRange(10);
UpdateIncomingOutgoingDrivers();
UpdateCounts();
}
private void UpdateIncomingOutgoingDrivers()
{
foreach (Driver driver in DataImporter.Drivers) {
// TODO: Check if car can stay before other reservation - may not work for priority-based reservations
@ -165,6 +235,9 @@ namespace Parking
(_currentTime - driver.Times[0].TimeOfDay).TotalMinutes > driver.UpdateInterval &&
GetReservedSpotCount(driver.Priority, driver.Size) > 0
&& !driver.Parked && !driver.Rejected && _currentTime <= driver.Times[3].TimeOfDay;
// if (driver.Reserved && !reservedButTimedout) {
//
// }
if (triesToPark && !driver.Parked && !driver.Rejected) {
if (!PlaceCarOnParking(driver)) {
if (_reconfigurationActive && TryReconfigureSpotForSize(driver.Size)) {
@ -194,8 +267,6 @@ namespace Parking
driver.Rejected = true;
}
}
UpdateCounts();
}
private void UpdateCounts()
@ -236,6 +307,15 @@ namespace Parking
private bool FindReservedSpot(Driver driver, out Spot spot)
{
if (driver.Reserved) {
if(!driver.ReservedSpot.Free)
Debug.Log("Spot should be free");
else {
spot = driver.ReservedSpot;
return true;
}
}
foreach (List<Spot> list in _spotMap)
foreach (Spot spot1 in list)
if (spot1.Reserved && spot1.ReservedPriority == driver.Priority && driver.Size == spot1.Size) {
@ -285,6 +365,9 @@ namespace Parking
private void PlaceDriverOnSpot(Driver driver, Spot spot)
{
if (spot.GameObject == null) {
Debug.LogError("Spot is null");
}
spot.Free = false;
spot.Reserved = false;
driver.Spot = spot;
@ -331,15 +414,20 @@ namespace Parking
spotlessCars.Add(nextCar);
int count = 0;
foreach (Driver nextCar in spotlessCars)
TryReconfigureSpotForSize(nextCar.Size);
if (TryReconfigureSpotForSize(nextCar.Size))
count++;
Debug.Log($"Created {count} out of {spotlessCars.Count}");
}
}
private bool TryReconfigureSpotForSize(Size newSize)
{
if (newSize == Size.C) {
Debug.Log("Reconfiguring using new method");
// Debug.Log("Reconfiguring using new method");
int lane1 = 2;
int lane2 = 1;
for (int i = _spotMap[lane2].Count - 1; i >= 0; i--)
@ -380,11 +468,15 @@ namespace Parking
lowerSpot.Size, 0.0f);
// verify we can delete them
bool allSpotsFree = true;
foreach (int conflictingSpotId in conflictingSpots) {
if (!_spotMap[0][conflictingSpotId].Free)
continue;
Spot conflictingSpot = _spotMap[0][conflictingSpotId];
if (!conflictingSpot.Free || conflictingSpot.Reserved)
allSpotsFree = false;
}
if(!allSpotsFree)
continue;
// delete them
int removed = 0;
conflictingSpots.Sort();
@ -642,10 +734,7 @@ namespace Parking
newSpot.GameObject = Instantiate(_spotPrefabs[(int)size],
newPosition, Quaternion.Euler(0, 0, 90), mainPlanContainer);
_spotMap[0].Add(newSpot);
_spotMap[0].Sort((a, b) =>
{
return Comparer<float>.Default.Compare(b.Position.x, a.Position.x);
});
_spotMap[0].Sort((a, b) => Comparer<float>.Default.Compare(b.Position.x, a.Position.x));
return true;
}
lastRightBorder = currentSpot.RightBorder;