This repository has been archived on 2022-08-04. You can view files and clone it, but cannot push or open issues or pull requests.
LyokoCMD/src/LyokoCMD.CommandLine/Commands/VirtualWorldCommand.cs

99 lines
4.0 KiB
C#

using System;
using DasConzoleCore;
using DasConzoleCore.Commands;
using DasConzoleCore.UI;
using LyokoCMD.Sim;
using LyokoCMD.Sim.Virtual;
using LyokoCMD.Sim.Virtual.World;
using LyokoCMD.CommandLine.Apps;
using LyokoCMD.Sim.Workers;
namespace LyokoCMD.CommandLine.Commands
{
public class VirtualWorldCommand : Command
{
public override string[] Names => new string[] { "vw" };
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)
{
if (args.Length < 1) throw new ArgumentException(HelpText);
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":
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(
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);
}
}
}