Refactor virtual world base classes
This commit is contained in:
parent
e67fa62615
commit
8b672e8a2f
|
@ -6,6 +6,7 @@ using LyokoCMD.Sim;
|
|||
using LyokoCMD.Sim.Virtual;
|
||||
using LyokoCMD.Sim.Virtual.World;
|
||||
using LyokoCMD.CommandLine.Apps;
|
||||
using LyokoCMD.Sim.Workers;
|
||||
|
||||
namespace LyokoCMD.CommandLine.Commands
|
||||
{
|
||||
|
@ -13,34 +14,85 @@ namespace LyokoCMD.CommandLine.Commands
|
|||
{
|
||||
public override string[] Names => new string[] { "vw" };
|
||||
|
||||
public override string Description => "Permet d'éxecuter des fonctions sur le monde virtuel.";
|
||||
public override string Description => "Contrôle du monde virtuel.";
|
||||
|
||||
public override string HelpText => "Usage : vw [command]\n\tsectors\tListe les territoires.\n\tshow [sector]\tAffiche le territoire passé en paramètre.";
|
||||
|
||||
public override void Run(CommandApp app, string[] args)
|
||||
{
|
||||
Supercomputer sc = Game.Objects.Get<Supercomputer>();
|
||||
if (!sc.Running) throw new InvalidOperationException("Impossible de se connecter au supercalculateur.");
|
||||
if (!sc.GetWorker("lyoko").Running) throw new InvalidOperationException("Monde virtuel arrêté.");
|
||||
if (args.Length < 1) throw new ArgumentException(HelpText);
|
||||
switch (args[0].ToLower())
|
||||
|
||||
Supercomputer sc = Game.Objects.Get<Supercomputer>();
|
||||
if (!sc.Running)
|
||||
throw new InvalidOperationException("Impossible de se connecter au supercalculateur.");
|
||||
|
||||
VirtualWorldWorker worker = sc.Workers.Get<VirtualWorldWorker>();
|
||||
VirtualWorld vw = Game.Objects.Get<VirtualWorld>();
|
||||
switch (args[0].ToLower().Trim())
|
||||
{
|
||||
case "sectors":
|
||||
Console.WriteLine(Text.BuildTable(Game.Objects.Get<VirtualWorld>().Sectors));
|
||||
if (!worker.Running) throw new InvalidOperationException("Service de monde virtuel inactif.");
|
||||
Console.WriteLine(Text.BuildTable(vw.Sectors));
|
||||
break;
|
||||
case "show":
|
||||
if (args.Length < 2) throw new ArgumentException(HelpText);
|
||||
if (!worker.Running) throw new InvalidOperationException("Service de monde virtuel inactif.");
|
||||
new SectorMapApp(
|
||||
Game.Objects.Get<VirtualWorld>().Sectors.Find(
|
||||
vw.Sectors.Find(
|
||||
s => s.ShortName.Equals(args[1],
|
||||
StringComparison.CurrentCultureIgnoreCase
|
||||
)),
|
||||
app.Settings
|
||||
).Run();
|
||||
break;
|
||||
case "start":
|
||||
worker.Start();
|
||||
break;
|
||||
case "stop":
|
||||
if (!worker.Running) throw new InvalidOperationException("Service de monde virtuel inactif.");
|
||||
worker.Stop();
|
||||
break;
|
||||
case "status":
|
||||
Console.WriteLine("État du service : " + (worker.Running ? "Actif" : "Inactif"));
|
||||
if(!worker.Running) break;
|
||||
Console.WriteLine($"Énergie utilisée : {worker.RequiredEnergy.ToString()}");
|
||||
Console.WriteLine($"{vw.Sectors.Count} territoires");
|
||||
break;
|
||||
case "tree":
|
||||
if (!worker.Running) throw new InvalidOperationException("Service de monde virtuel inactif.");
|
||||
Console.WriteLine(vw.Name);
|
||||
PrintTree(vw, "");
|
||||
break;
|
||||
default:
|
||||
throw new ArgumentException(HelpText);
|
||||
}
|
||||
}
|
||||
|
||||
// Constants for drawing lines and spaces
|
||||
private const string _cross = "├──";
|
||||
private const string _corner = "└──";
|
||||
private const string _vertical = "│ ";
|
||||
private const string _space = " ";
|
||||
|
||||
private void PrintTree(IVirtualizableContainer container, string indent) {
|
||||
var childrenCount = container.Objects.Count;
|
||||
for (var i = 0; i < childrenCount; i++) {
|
||||
PrintChild(container.Objects[i], indent, i == childrenCount - 1);
|
||||
}
|
||||
}
|
||||
|
||||
private void PrintChild(Virtualizable child, string indent, bool isLast) {
|
||||
Console.Write(indent);
|
||||
if (isLast) {
|
||||
Console.Write(_corner);
|
||||
indent += _space;
|
||||
} else {
|
||||
Console.Write(_cross);
|
||||
indent += _vertical;
|
||||
}
|
||||
Console.WriteLine(child);
|
||||
|
||||
if (child is IVirtualizableContainer) PrintTree(child as IVirtualizableContainer, indent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,13 @@
|
|||
using System.Collections.Generic;
|
||||
|
||||
namespace LyokoCMD.Sim.Virtual {
|
||||
public interface IVirtualizableContainer {
|
||||
List<Virtualizable> Objects { get; }
|
||||
|
||||
void Virtualize(Virtualizable obj);
|
||||
|
||||
void Materialize(Virtualizable obj);
|
||||
|
||||
void MaterializeAll();
|
||||
}
|
||||
}
|
|
@ -27,21 +27,18 @@ namespace LyokoCMD.Sim.Virtual
|
|||
_CanMaterialize = canMaterialize;
|
||||
}
|
||||
|
||||
public void Virtualize()
|
||||
public virtual void Virtualize()
|
||||
{
|
||||
if (Virtualized) throw new InvalidOperationException("Cette entité a déjà été virtualisée.");
|
||||
else _IsVirtualized = true;
|
||||
}
|
||||
|
||||
public void Materialize() {
|
||||
public virtual void Materialize() {
|
||||
if (!Virtualized) {throw new InvalidOperationException("Cette entité n'a pas été virtualisée.");}
|
||||
if (!CanMaterialize) throw new InvalidOperationException("Cette entité ne peut être dévirtualisée");
|
||||
_IsVirtualized = false;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{nameof(Name)}: {Name}, {nameof(Coordinates)}: {Coordinates}";
|
||||
}
|
||||
public override string ToString() => Coordinates is null ? Name : $"{Name}, {Coordinates}";
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,28 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LyokoCMD.Sim.Virtual
|
||||
{
|
||||
public abstract class VirtualizableContainer : IVirtualizableContainer {
|
||||
|
||||
public List<Virtualizable> Objects { get; }
|
||||
|
||||
public VirtualizableContainer() => Objects = new List<Virtualizable>();
|
||||
|
||||
public void Virtualize(Virtualizable obj) {
|
||||
if (Objects.Contains(obj)) throw new InvalidOperationException("Cette entité est déjà virtualisée.");
|
||||
obj.Virtualize();
|
||||
Objects.Add(obj);
|
||||
}
|
||||
|
||||
public void Materialize(Virtualizable obj) {
|
||||
if(!Objects.Contains(obj)) throw new InvalidOperationException("Cette entité n'est pas virtualisée ici.");
|
||||
obj.Materialize();
|
||||
Objects.Remove(obj);
|
||||
}
|
||||
|
||||
public void MaterializeAll() {
|
||||
Objects.ForEach(this.Materialize);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,34 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace LyokoCMD.Sim.Virtual
|
||||
{
|
||||
public abstract class VirtualizableVirtualizableContainer : Virtualizable, IVirtualizableContainer {
|
||||
|
||||
public List<Virtualizable> Objects { get; }
|
||||
|
||||
public VirtualizableVirtualizableContainer(string name, Coordinates coords = null, bool canMaterialize = true)
|
||||
: base(name, coords,canMaterialize) => Objects = new List<Virtualizable>();
|
||||
|
||||
public void Virtualize(Virtualizable obj) {
|
||||
if (Objects.Contains(obj)) throw new InvalidOperationException("Cette entité est déjà virtualisée.");
|
||||
obj.Virtualize();
|
||||
Objects.Add(obj);
|
||||
}
|
||||
|
||||
public override void Materialize() {
|
||||
this.MaterializeAll();
|
||||
base.Materialize();
|
||||
}
|
||||
|
||||
public void Materialize(Virtualizable obj) {
|
||||
if(!Objects.Contains(obj)) throw new InvalidOperationException("Cette entité n'est pas virtualisée ici.");
|
||||
obj.Materialize();
|
||||
Objects.Remove(obj);
|
||||
}
|
||||
|
||||
public void MaterializeAll() {
|
||||
Objects.ForEach(this.Materialize);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -7,45 +7,25 @@ namespace LyokoCMD.Sim.Virtual.World
|
|||
/// <summary>
|
||||
/// Décrit un territoire de Lyoko.
|
||||
/// </summary>
|
||||
public class Sector : ITableLineOutput
|
||||
public class Sector : VirtualizableVirtualizableContainer, ITableLineOutput
|
||||
{
|
||||
protected string _Name;
|
||||
protected bool _Locked;
|
||||
public string Name => _Name;
|
||||
protected string _ShortName;
|
||||
public string ShortName { get => _ShortName; }
|
||||
public readonly string ShortName;
|
||||
public bool Locked => _Locked;
|
||||
public List<Virtualizable> Objects = new List<Virtualizable>();
|
||||
|
||||
public Sector(string Name, string shortName, bool locked = false)
|
||||
public Sector(string Name, string shortName, bool locked = false) : base(Name, null, false)
|
||||
{
|
||||
this._Name = Name;
|
||||
this._ShortName = shortName;
|
||||
this.ShortName = shortName;
|
||||
this._Locked = locked;
|
||||
}
|
||||
|
||||
public string[] GetColumnNames() => new [] {"ID", "Name", "Locked"};
|
||||
|
||||
public string[] GetLine() => new [] {this._ShortName, this._Name, this._Locked.ToString()};
|
||||
public string[] GetLine() => new [] {this.ShortName, this.Name, this.Locked.ToString()};
|
||||
|
||||
public void CreateTowers(int count)
|
||||
{
|
||||
for (int i = 1; i <= count; i++) Objects.Add(new Tower());
|
||||
}
|
||||
|
||||
public void Virtualize(Virtualizable obj)
|
||||
{
|
||||
obj.Virtualize();
|
||||
}
|
||||
|
||||
public void Materialize(Virtualizable obj)
|
||||
{
|
||||
obj.Materialize();
|
||||
}
|
||||
|
||||
public void MaterializeAll()
|
||||
{
|
||||
Objects.ForEach(Materialize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -6,26 +6,21 @@ namespace LyokoCMD.Sim.Virtual.World
|
|||
/// <summary>
|
||||
/// Décrit le monde virtuel Lyoko.
|
||||
/// </summary>
|
||||
public class VirtualWorld
|
||||
public class VirtualWorld : VirtualizableContainer
|
||||
{
|
||||
public List<Sector> Sectors;
|
||||
public List<Sector> Sectors => Objects.Cast<Sector>().ToList();
|
||||
public Heart Heart;
|
||||
public string Name;
|
||||
public readonly string Name;
|
||||
|
||||
public VirtualWorld(string name = "lyoko") : this(name, new List<Sector>())
|
||||
{
|
||||
}
|
||||
|
||||
public VirtualWorld(string name, IEnumerable<Sector> sectors)
|
||||
public VirtualWorld(string name, IEnumerable<Sector> sectors) : base()
|
||||
{
|
||||
Name = name;
|
||||
Sectors = sectors.ToList();
|
||||
this.Name = name;
|
||||
sectors.ToList().ForEach(this.Virtualize);
|
||||
Heart = new Heart(this);
|
||||
}
|
||||
|
||||
public void MaterializeAll()
|
||||
{
|
||||
Sectors.ForEach(s => s.MaterializeAll());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,12 +8,9 @@ namespace LyokoCMD.Sim.Workers
|
|||
/// </summary>
|
||||
public class VirtualWorldWorker : SCWorker
|
||||
{
|
||||
private VirtualWorld _World;
|
||||
public readonly VirtualWorld World;
|
||||
|
||||
public VirtualWorldWorker(VirtualWorld world) : base(world.Name)
|
||||
{
|
||||
_World = world;
|
||||
}
|
||||
public VirtualWorldWorker(VirtualWorld world) : base(world.Name) => World = world;
|
||||
|
||||
public override double RequiredEnergy => 300.0;
|
||||
|
||||
|
@ -24,7 +21,7 @@ namespace LyokoCMD.Sim.Workers
|
|||
|
||||
protected override void PreStop()
|
||||
{
|
||||
_World.MaterializeAll();
|
||||
World.MaterializeAll();
|
||||
}
|
||||
|
||||
protected override void Run()
|
||||
|
|
Reference in New Issue