using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Diagnostics;
namespace Nibriboard.RippleSpace
{
public class RippleSpaceManager
{
///
/// The master list of planes that this PlaneManager is in charge of.
///
public List Planes = new List();
///
/// The number of milliseconds between each maintenance run.
///
public readonly int MaintenanceInternal = 5000;
///
/// The number of milliseconds the last maintenance run took.
///
public long LastMaintenanceDuration = 0;
public int DefaultChunkSize { get; set; } = 512;
public RippleSpaceManager()
{
Log.WriteLine("[RippleSpace] New blank ripplespace initialised.");
}
///
/// Gets the plane with the specified name from this RippleSpace.
///
/// The plane name to retrieve.
public Plane this[string planeName] {
get {
return GetById(planeName);
}
}
///
/// Gets the plane with the specified name from this RippleSpace.
///
/// The plane name to retrieve.
/// The plane wwith the specified name.
protected Plane GetById(string targetName)
{
foreach (Plane plane in Planes)
{
if (plane.Name == targetName)
return plane;
}
return null;
}
///
/// Creates a new plane, adds it to this RippleSpaceManager, and then returns it.
///
/// The name of the new plane to create.
/// The newly created plane.
public Plane CreatePlane(string newPlaneName)
{
if(this[newPlaneName] != null)
throw new InvalidOperationException($"Error: A plane with the name '{newPlaneName}' already exists in this RippleSpaceManager.");
Plane newPlane = new Plane(newPlaneName, DefaultChunkSize);
Planes.Add(newPlane);
return newPlane;
}
public async Task StartMaintenanceMonkey()
{
Log.WriteLine("[RippleSpace/Maintenance] Automated maintenance monkey created.");
while (true)
{
Stopwatch maintenanceStopwatch = Stopwatch.StartNew();
foreach (Plane plane in Planes)
plane.PerformMaintenance();
LastMaintenanceDuration = maintenanceStopwatch.ElapsedMilliseconds;
await Task.Delay(MaintenanceInternal);
}
}
}
}