Worker fredy

This commit is contained in:
Lucidiot 2018-08-24 18:03:06 +00:00
parent 54f4fd0c5b
commit e534371c61
50 changed files with 731 additions and 387 deletions

8
.gitlab-ci.yml Normal file
View File

@ -0,0 +1,8 @@
lyokocmd-tests:
image: microsoft/dotnet:2.1-sdk
stage: test
before_script:
- git clone https://gitlab-ci-token:${CI_JOB_TOKEN}@gitlab.com/Lucidiot/DasConzole /builds/Lucidiot/DasConzole
- dotnet restore
script:
- dotnet test test/LyokoCMD.Sim.Tests

8
.vscode/launch.json vendored
View File

@ -10,8 +10,10 @@
"request": "launch",
"preLaunchTask": "build",
// If you have changed target frameworks, make sure to update the program path.
"program": "${workspaceFolder}/bin/Debug/netcoreapp2.0/LyokoCMD.dll",
"args": [],
"program": "${workspaceFolder}/src/LyokoCMD.CommandLine/bin/Debug/netcoreapp2.1/LyokoCMD.CommandLine.dll",
"args": [
"--debug",
],
"cwd": "${workspaceFolder}",
// For more information about the 'console' field, see https://github.com/OmniSharp/omnisharp-vscode/blob/master/debugger-launchjson.md#console-terminal-window
"console": "integratedTerminal",
@ -25,4 +27,4 @@
"processId": "${command:pickProcess}"
}
]
}
}

View File

@ -1,64 +1,35 @@

Microsoft Visual Studio Solution File, Format Version 12.00
#
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "src", "src", "{966AF5F1-1C78-494B-9F92-41811EA71E38}"
#
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.Xana", "src\LyokoCMD.Xana\LyokoCMD.Xana.csproj", "{AEB4CEA6-3EA5-43A8-BCF7-6BFECFB9093D}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.Sim", "src\LyokoCMD.Sim\LyokoCMD.Sim.csproj", "{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.Sim", "src\LyokoCMD.Sim\LyokoCMD.Sim.csproj", "{BB2905DA-B7FD-4B24-AEC0-0036AD2F1E50}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.Xana", "src\LyokoCMD.Xana\LyokoCMD.Xana.csproj", "{7320636A-F45B-4CB1-8527-D5969437DB46}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.CommandLine", "src\LyokoCMD.CommandLine\LyokoCMD.CommandLine.csproj", "{4A0C3E56-0278-414F-9D71-B298F1E97F84}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.CommandLine", "src\LyokoCMD.CommandLine\LyokoCMD.CommandLine.csproj", "{A72A569E-20C6-43B6-96EE-E0C5130BE127}"
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LyokoCMD.Sim.Tests", "test\LyokoCMD.Sim.Tests\LyokoCMD.Sim.Tests.csproj", "{F39B6988-2D20-4A80-96D3-75A473413665}"
EndProject
Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4A0C3E56-0278-414F-9D71-B298F1E97F84}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4A0C3E56-0278-414F-9D71-B298F1E97F84}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4A0C3E56-0278-414F-9D71-B298F1E97F84}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4A0C3E56-0278-414F-9D71-B298F1E97F84}.Release|Any CPU.Build.0 = Release|Any CPU
{BB2905DA-B7FD-4B24-AEC0-0036AD2F1E50}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{BB2905DA-B7FD-4B24-AEC0-0036AD2F1E50}.Debug|Any CPU.Build.0 = Debug|Any CPU
{BB2905DA-B7FD-4B24-AEC0-0036AD2F1E50}.Release|Any CPU.ActiveCfg = Release|Any CPU
{BB2905DA-B7FD-4B24-AEC0-0036AD2F1E50}.Release|Any CPU.Build.0 = Release|Any CPU
{F39B6988-2D20-4A80-96D3-75A473413665}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{F39B6988-2D20-4A80-96D3-75A473413665}.Debug|Any CPU.Build.0 = Debug|Any CPU
{F39B6988-2D20-4A80-96D3-75A473413665}.Release|Any CPU.ActiveCfg = Release|Any CPU
{F39B6988-2D20-4A80-96D3-75A473413665}.Release|Any CPU.Build.0 = Release|Any CPU
{AEB4CEA6-3EA5-43A8-BCF7-6BFECFB9093D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AEB4CEA6-3EA5-43A8-BCF7-6BFECFB9093D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AEB4CEA6-3EA5-43A8-BCF7-6BFECFB9093D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{AEB4CEA6-3EA5-43A8-BCF7-6BFECFB9093D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Debug|x64 = Debug|x64
Debug|x86 = Debug|x86
Release|Any CPU = Release|Any CPU
Release|x64 = Release|x64
Release|x86 = Release|x86
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|Any CPU.Build.0 = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|x64.ActiveCfg = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|x64.Build.0 = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|x86.ActiveCfg = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Debug|x86.Build.0 = Debug|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|Any CPU.ActiveCfg = Release|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|Any CPU.Build.0 = Release|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|x64.ActiveCfg = Release|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|x64.Build.0 = Release|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|x86.ActiveCfg = Release|Any CPU
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18}.Release|x86.Build.0 = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|Any CPU.Build.0 = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|x64.ActiveCfg = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|x64.Build.0 = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|x86.ActiveCfg = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Debug|x86.Build.0 = Debug|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|Any CPU.ActiveCfg = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|Any CPU.Build.0 = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|x64.ActiveCfg = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|x64.Build.0 = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|x86.ActiveCfg = Release|Any CPU
{7320636A-F45B-4CB1-8527-D5969437DB46}.Release|x86.Build.0 = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|x64.ActiveCfg = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|x64.Build.0 = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|x86.ActiveCfg = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Debug|x86.Build.0 = Debug|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|Any CPU.Build.0 = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|x64.ActiveCfg = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|x64.Build.0 = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|x86.ActiveCfg = Release|Any CPU
{A72A569E-20C6-43B6-96EE-E0C5130BE127}.Release|x86.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{11A6075C-7FC0-42F3-B1BB-2B421CDD9E18} = {966AF5F1-1C78-494B-9F92-41811EA71E38}
{7320636A-F45B-4CB1-8527-D5969437DB46} = {966AF5F1-1C78-494B-9F92-41811EA71E38}
{A72A569E-20C6-43B6-96EE-E0C5130BE127} = {966AF5F1-1C78-494B-9F92-41811EA71E38}
EndGlobalSection
EndGlobal

25
Makefile Normal file
View File

@ -0,0 +1,25 @@
run: require-dasconzole
cd src/LyokoCMD.CommandLine && dotnet run
debug: require-dasconzole
cd src/LyokoCMD.CommandLine && dotnet run -- --debug
all: require-dasconzole
dotnet build
clean:
rm -rf {test/LyokoCMD.Sim.Tests,src/LyokoCMD.{Sim,CommandLine,Xana}}/bin
tests: require-dasconzole
dotnet test test/LyokoCMD.Sim.Tests/LyokoCMD.Sim.Tests.csproj
restore: require-dasconzole
dotnet restore
require-dasconzole:
@if [ ! -d "../DasConzole/src/DasConzoleCore" ]; then echo "Missing DasConzoleCore dependency in ../DasConzole/src/DasConzoleCore. Use make clone to clone the DasConzole repository in the parent folder."; exit 1; fi
clone:
git clone https://gitlab.com/Lucidiot/DasConzole ../DasConzole

View File

@ -0,0 +1,33 @@
using System;
using DasConzoleCore;
using DasConzoleCore.Data;
using DasConzoleCore.UI;
using LyokoCMD.Sim.Virtual;
using LyokoCMD.Sim.Virtual.World;
namespace LyokoCMD.CommandLine.Apps {
public class SectorMapApp : Application {
public Sector Sector { get; }
public SectorMapApp(Sector s) : this(s, new NonPersistentSettingsManager()) { }
public SectorMapApp(Sector s, ISettingsManager settings) : base($"Carte de {s.Name}", new Version(0, 1), settings) {
this.Sector = s;
}
public override void Run() {
Console.Clear();
foreach (Virtualizable virtualizable in Sector.Objects)
{
int x = (int) ((virtualizable.Coordinates.Item1 + 90) * Console.WindowWidth /
(Coordinates.MaxValue.Item1 + 90));
int y = (int) ((virtualizable.Coordinates.Item2 + 180) * Console.WindowHeight /
(Coordinates.MaxValue.Item2 + 180));
Text.WriteAt(x, y, virtualizable.Name.Substring(0, 1));
}
Console.ReadKey(true);
Console.Clear();
}
}
}

View File

@ -1,7 +1,7 @@
using System;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using DasConzoleCore.UI;
namespace LyokoCMD.CommandLine.Commands
{

View File

@ -1,12 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using DasConzoleCore.UI;
using LyokoCMD.Sim;
namespace LyokoCMD.Sim.Commands
namespace LyokoCMD.CommandLine.Commands
{
public class EnergyCommand : Command
{
@ -24,7 +22,7 @@ namespace LyokoCMD.Sim.Commands
Console.WriteLine("Énergie utilisée {0} unités", sc.UsedEnergy.ToString().PadLeft(10));
Console.WriteLine("Énergie disponible {0} unités", sc.AvailableEnergy.ToString().PadLeft(10));
Console.WriteLine();
Console.WriteLine(Text.BuildTable(sc.EnergyProviders.ToArray()));
Console.WriteLine(Text.BuildTable(sc.EnergyProviders.All()));
}
}
}

View File

@ -1,10 +1,11 @@
using System;
using System.Linq;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using DasConzoleCore.UI;
using LyokoCMD.Sim;
namespace LyokoCMD.Sim.Commands
namespace LyokoCMD.CommandLine.Commands
{
public class ServiceCommand : Command
{
@ -27,7 +28,7 @@ suspend Suspendre un service.
switch (args[0].Trim().ToLower())
{
case "list":
Console.WriteLine(Text.BuildTable(sc.Workers.ToArray()));
Console.WriteLine(Text.BuildTable(sc.Workers.All()));
break;
case "start":
foreach (string name in args.Skip(1))

View File

@ -1,9 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using DasConzoleCore.UI;
namespace LyokoCMD.CommandLine.Commands
{

View File

@ -3,8 +3,9 @@ using System.Linq;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using LyokoCMD.Sim;
namespace LyokoCMD.Sim.Commands
namespace LyokoCMD.CommandLine.Commands
{
public class SupercomputerCommand : Command
{
@ -42,7 +43,7 @@ cooling status État du système de refroidissement";
);
if (!sc.Running) return;
Console.WriteLine("{0} service(s) en cours d'exécution, {1} au total",
sc.Workers.Count(w => w.Status == SCWorkerStatus.Running), sc.Workers.Count);
sc.Workers.All().Count(w => w.Running), sc.Workers.Count);
Console.WriteLine($"{sc.Tasks.Count} tâche(s) disponible(s)");
Console.WriteLine("{0} % d'énergie utilisée", Math.Round((sc.UsedEnergy / sc.TotalEnergy) * 100));
break;

View File

@ -3,8 +3,9 @@ using System.Linq;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using LyokoCMD.Sim;
namespace LyokoCMD.Sim.Commands
namespace LyokoCMD.CommandLine.Commands
{
public class TaskCommand : Command
{
@ -15,7 +16,7 @@ namespace LyokoCMD.Sim.Commands
{
Supercomputer sc = Game.Objects.Get<Supercomputer>();
if (!sc.Running) throw new InvalidOperationException("Impossible de se connecter au supercalculateur.");
if (args.Length < 1) Console.WriteLine(Text.BuildTable(sc.Tasks.ToArray()));
if (args.Length < 1) Console.WriteLine(Text.BuildTable(sc.Tasks.All()));
else sc.RunTask(args[0], args.Skip(1).ToArray());
}
}

View File

@ -0,0 +1,25 @@
using System;
using DasConzoleCore;
using DasConzoleCore.Commands;
using LyokoCMD.Sim;
namespace LyokoCMD.CommandLine.Commands
{
public class TimeWarpCommand : Command
{
public override string[] Names => new string[] { "timewarp" };
public override string Description => "Lance un retour vers le passé.";
public override string HelpText => "Alias pour la tâche de retour vers le passé du supercalculateur.\nUsage: timewarp [hours] [delay_seconds]";
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 (args.Length < 1) throw new ArgumentException(HelpText);
if (args.Length < 2)
{
args[2] = "0";
}
sc.RunTask("TimeWarp", args);
}
}
}

View File

@ -0,0 +1,46 @@
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;
namespace LyokoCMD.CommandLine.Commands
{
public class VirtualWorldCommand : Command
{
public override string[] Names => new string[] { "vw" };
public override string Description => "Permet d'éxecuter des fonctions sur le 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())
{
case "sectors":
Console.WriteLine(Text.BuildTable(Game.Objects.Get<VirtualWorld>().Sectors));
break;
case "show":
if (args.Length < 2) throw new ArgumentException(HelpText);
new SectorMapApp(
Game.Objects.Get<VirtualWorld>().Sectors.Find(
s => s.ShortName.Equals(args[1],
StringComparison.CurrentCultureIgnoreCase
)),
app.Settings
).Run();
break;
default:
throw new ArgumentException(HelpText);
}
}
}
}

View File

@ -5,8 +5,10 @@ using System.Text;
using DasConzoleCore;
using DasConzoleCore.UI;
using DasConzoleCore.Commands;
using LyokoCMD.Sim;
using LyokoCMD.Sim.Virtual;
namespace LyokoCMD.Sim.Commands
namespace LyokoCMD.CommandLine.Commands
{
public class VirtualizationCommand : Command
{

View File

@ -1,9 +1,10 @@
using System;
using System.Linq;
using DasConzoleCore;
using DasConzoleCore.Data;
using DasConzoleCore.Commands;
using LyokoCMD.Sim;
using LyokoCMD.Sim.Commands;
using DasConzoleCore.Data;
using LyokoCMD.CommandLine.Commands;
using LyokoCMD.Sim;
namespace LyokoCMD.CommandLine
{
@ -24,10 +25,13 @@ namespace LyokoCMD.CommandLine
new SupercomputerCommand(),
new TaskCommand(),
new ServiceCommand(),
new VirtualizationCommand()
new VirtualizationCommand(),
new TimeWarpCommand(),
new VirtualWorldCommand()
);
app.AppStartup += new System.EventHandler(app_AppStartup);
app.AppStop += new System.EventHandler(app_AppStop);
app.DebugMode = args.Intersect(new [] {"-d", "-debug", "--debug", "/d", "/debug"}, StringComparer.CurrentCultureIgnoreCase).Any();
app.Run();
}
@ -41,6 +45,7 @@ namespace LyokoCMD.CommandLine
static void app_AppStop(object sender, System.EventArgs e)
{
Array.ForEach(Game.Objects.Get<Supercomputer>().Workers.All(), w => w.ForceStop());
var app = (PersistenceCommandApp)sender;
app.SettingsPersistence.Save(app.Settings);
System.Console.Clear();

View File

@ -1,11 +1,11 @@
using DasConzoleCore.UI;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.EnergyProviders
{
/// <summary>
/// Décrit un fournisseur d'énergie du supercalculateur.
/// </summary>
abstract class EnergyProvider : ITableLineOutput
public abstract class EnergyProvider : ITableLineOutput
{
/// <summary>
/// Nom affiché par les systèmes de gestion d'énergie.

View File

@ -0,0 +1,15 @@
using System;
namespace LyokoCMD.Sim.EnergyProviders
{
public class NoMoreEnergyException : Exception
{
public NoMoreEnergyException() : base("Le supercalculateur n'a plus d'énergie pour fonctionner")
{
}
public NoMoreEnergyException(string what) : base("Le supercalculateur n'a plus d'énergie pour " + what)
{
}
}
}

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.EnergyProviders
{
/// <summary>
/// Décrit une pile nucléaire du supercalculateur.

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.EnergyProviders
{
class TimeWarpEnergyProvider : EnergyProvider
{
@ -15,8 +10,7 @@ namespace LyokoCMD.Sim
public void Warp()
{
// TODO: Retour vers le passé
throw new NotImplementedException();
_WarpCount++;
}
}
}

View File

@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD
namespace LyokoCMD.Sim.Extensions
{
public static class IEnumerableExtensions
{

View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;
namespace LyokoCMD
namespace LyokoCMD.Sim.Extensions
{
public static class ListExtensions
{

View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD
namespace LyokoCMD.Sim.Extensions
{
public static class ObjectExtensions
{

View File

@ -1,9 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD
namespace LyokoCMD.Sim.Extensions
{
public static class StringExtensions
{

View File

@ -1,4 +1,8 @@
using DasConzoleCore;
using LyokoCMD.Sim.EnergyProviders;
using LyokoCMD.Sim.Tasks;
using LyokoCMD.Sim.Virtual;
using LyokoCMD.Sim.Virtual.World;
using LyokoCMD.Sim.Workers;
namespace LyokoCMD.Sim
{
@ -9,18 +13,22 @@ namespace LyokoCMD.Sim
public static void registerObjects()
{
Supercomputer sc = new Supercomputer();
sc.EnergyProviders.Add(new NuclearCell());
sc.EnergyProviders.Register(new NuclearCell());
// Lyoko
VirtualWorld lyoko = new VirtualWorldBuilder("Lyoko")
.AddSector("Territoire Montagne", "Territoire Désert", "Territoire Forêt", "Territoire Banquise")
.AddTowers(9)
.AddSector("Territoire Montagne", "montagne")
.AddSector("Territoire Désert", "desert")
.AddSector("Territoire Forêt", "foret")
.AddSector("Territoire Banquise", "banquise")
.AddTowers(20)
.AddWayTowers()
.AddSector("5ème territoire", true, 1)
.AddSector("5ème territoire", "cinq", true, 1)
.Build();
sc.Workers.Add(lyoko);
sc.Workers.Register(new VirtualWorldWorker(lyoko));
Objects.Register(lyoko, "Lyoko");
// Lyokoguerriers
Objects.Register(new Human("Aelita", true), "Aelita");
Human aelita = new Human("Aelita", true) {DNAStatus = DNAStatus.Missing};
Objects.Register(aelita, "Aelita");
Objects.Register(new Human("Odd"), "Odd");
Objects.Register(new Human("Ulrich"), "Ulrich");
Objects.Register(new Human("Yumi"), "Yumi");
@ -29,12 +37,12 @@ namespace LyokoCMD.Sim
Objects.Register(new Vehicle("Overbike"), "Overbike");
Objects.Register(new Vehicle("Overwing"), "Overwing");
// Virtualisation
sc.Tasks.Add(new VirtualizationTask());
sc.Tasks.Register(new VirtualizationTask());
// Retour vers le passé
TimeWarpEnergyProvider tw = new TimeWarpEnergyProvider();
sc.EnergyProviders.Add(tw);
Objects.Register(tw);
sc.Tasks.Add(new TimeWarpTask());
sc.EnergyProviders.Register(tw);
sc.Tasks.Register(new TimeWarpTask());
sc.Workers.Register(new TimeWarpWorker());
Objects.Register(sc);
}
}

View File

@ -1,7 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LyokoCMD.Sim.Virtual;
namespace LyokoCMD.Sim
{
@ -10,7 +7,13 @@ namespace LyokoCMD.Sim
protected bool _AnnexProgram;
public bool AnnexProgram => _AnnexProgram;
protected DNAStatus _DNAStatus;
public DNAStatus DNAStatus => _DNAStatus;
public DNAStatus DNAStatus
{
get => _DNAStatus;
set => _DNAStatus = value;
}
public override bool CanMaterialize => _DNAStatus == DNAStatus.Normal;
public Human(string name, bool annexProgram = false) : base(name)
{

View File

@ -1,35 +1,41 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LyokoCMD.Sim.Extensions;
namespace LyokoCMD.Sim
{
/// <summary>
/// Registre d'objets utilisé par un jeu en cours.
/// A registry capable of holding named and unnamed objects.
/// </summary>
public class ObjectsRegistry
/// <typeparam name="BaseT">Base type of all objects of the registry.</typeparam>
public class ObjectsRegistry<BaseT>
{
protected Dictionary<object, string> objects = new Dictionary<object,string>();
protected Dictionary<BaseT, string> objects = new Dictionary<BaseT,string>();
public void Register(object obj) => Register(obj, String.Empty);
public void Register(BaseT obj) => Register(obj, String.Empty);
public void Register(object obj, string name) => objects.Add(obj, name);
public void Register(BaseT obj, string name) => objects.Add(obj, name);
public T Get<T>() => (T)objects.First(o => o.Key.CanCast<T>()).Key;
public T Get<T>() where T: BaseT => (T)objects.First(o => o.Key.CanCast<T>()).Key;
public T Get<T>(string name) => (T)objects.First(o => o.Key.CanCast<T>() && o.Value == name).Key;
public T Get<T>(string name) where T: BaseT => (T)objects.First(o => o.Key.CanCast<T>() && o.Value == name).Key;
public object[] All() => objects.Keys.ToArray();
public BaseT[] All() => objects.Keys.ToArray();
public T[] All<T>() => objects.Where(o => o.Key.CanCast<T>()).Select(p => p.Key).Cast<T>().ToArray();
public T[] All<T>() where T: BaseT => objects.Where(o => o.Key.CanCast<T>()).Select(p => p.Key).Cast<T>().ToArray();
public T[] All<T>(string name) => objects.Where(o => o.Key.CanCast<T>() && o.Value == name).Select(p => p.Key).Cast<T>().ToArray();
public T[] All<T>(string name) where T: BaseT => objects.Where(o => o.Key.CanCast<T>() && o.Value == name).Select(p => p.Key).Cast<T>().ToArray();
public int Count => objects.Count;
public string[] Names => objects.Values.Where(o => o != String.Empty).ToArray();
public Dictionary<object, string> NamedObjects => objects.Where(p => p.Value != String.Empty).ToDictionary(p => p.Key, p => p.Value);
public Dictionary<BaseT, string> NamedObjects => objects.Where(p => p.Value != String.Empty).ToDictionary(p => p.Key, p => p.Value);
}
/// <summary>
/// A registry capable of holding named and unnamed objects.
/// </summary>
public class ObjectsRegistry : ObjectsRegistry<Object> { }
}

View File

@ -1,29 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LyokoCMD.Sim.EnergyProviders;
using LyokoCMD.Sim.Extensions;
using LyokoCMD.Sim.Tasks;
using LyokoCMD.Sim.Workers;
namespace LyokoCMD.Sim
{
/// <summary>
/// Décrit un supercalculateur de Code Lyoko.
/// </summary>
class Supercomputer
public class Supercomputer
{
private bool _Running = false, _Cooling = true;
public bool Running => _Running;
public bool Cooling => _Cooling;
private const double BaseTemperature = 90.0;
public List<EnergyProvider> EnergyProviders = new List<EnergyProvider>();
public List<SCWorker> Workers = new List<SCWorker>();
public List<SCTask> Tasks = new List<SCTask>();
public ObjectsRegistry<EnergyProvider> EnergyProviders = new ObjectsRegistry<EnergyProvider>();
public ObjectsRegistry<SCWorker> Workers = new ObjectsRegistry<SCWorker>();
public ObjectsRegistry<SCTask> Tasks = new ObjectsRegistry<SCTask>();
public double Temperature => Math.Round((Running ? BaseTemperature : 20.0) + (Cooling ? -70 : 0) + (UsedEnergy / 100), 1);
public double TotalEnergy => EnergyProviders.Sum(e => e.AvailableEnergy);
public double TotalEnergy => EnergyProviders.All().Sum(e => e.AvailableEnergy);
public double UsedEnergy => Workers.Where(w => w.Status == SCWorkerStatus.Running || w.Status == SCWorkerStatus.Suspended).Sum(w => w.RequiredEnergy);
public double UsedEnergy => Workers.All().Where(w => w.Running).Sum(w => w.RequiredEnergy);
public double AvailableEnergy => TotalEnergy - UsedEnergy;
@ -32,7 +34,7 @@ namespace LyokoCMD.Sim
public void Stop()
{
Console.WriteLine("Arrêt des services...");
Workers.Where(w => w.Status == SCWorkerStatus.Running || w.Status == SCWorkerStatus.Suspended || w.Status == SCWorkerStatus.Waiting).ForEach(w => w.Stop());
Workers.All().Where(w => w.Running).ForEach(w => w.Stop());
_Running = false;
}
@ -42,8 +44,8 @@ namespace LyokoCMD.Sim
public SCTask GetTask(String name)
{
if (!Tasks.Exists(t => t.Name.ToLower() == name.ToLower())) throw new ArgumentException("Tâche introuvable");
return Tasks.Find(t => t.Name.ToLower() == name.ToLower());
if (Tasks.All().All(t => t.Name.ToLower() != name.ToLower())) throw new ArgumentException("Tâche introuvable");
return Tasks.All().First(t => t.Name.ToLower() == name.ToLower());
}
public void RunTask(string name) => RunTask(name, new string[] { });
@ -51,7 +53,7 @@ namespace LyokoCMD.Sim
public void RunTask(String name, string[] args)
{
if (!Running) throw new InvalidOperationException("Impossible d'exécuter une tâche sur un supercalculateur éteint");
if (!Tasks.Exists(t => t.Name.ToLower() == name.ToLower())) throw new ArgumentException("Tâche introuvable");
if (Tasks.All().All(t => t.Name.ToLower() != name.ToLower())) throw new ArgumentException("Tâche introuvable");
SCTask task = GetTask(name);
if (task.RequiredEnergy > AvailableEnergy) throw new InvalidOperationException("Pas assez d'énergie pour exécuter la tâche");
task.Run(args);
@ -59,8 +61,8 @@ namespace LyokoCMD.Sim
public SCWorker GetWorker(String name)
{
if (!Workers.Exists(w => w.Name.ToLower() == name.ToLower())) throw new ArgumentException("Service introuvable");
return Workers.Find(w => w.Name.ToLower() == name.ToLower());
if (Workers.All().All(w => w.Name.ToLower() != name.ToLower())) throw new ArgumentException("Service introuvable");
return Workers.All().First(w => w.Name.ToLower() == name.ToLower());
}
public void StartWorker(String name)
@ -83,7 +85,7 @@ namespace LyokoCMD.Sim
{
if (!Running) throw new InvalidOperationException("Impossible d'arrêter un service sur un supercalculateur éteint");
SCWorker worker = GetWorker(name);
if (worker.Status == SCWorkerStatus.Running || worker.Status == SCWorkerStatus.Suspended || worker.Status == SCWorkerStatus.Waiting)
if (worker.Running)
worker.Stop();
else throw new InvalidOperationException("Ce service n'est pas en cours d'exécution.");
}

View File

@ -1,6 +1,6 @@
using DasConzoleCore.UI;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Tasks
{
/// <summary>
/// Décrit une tâche exécutable au premier plan par le supercalculateur.

View File

@ -1,14 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LyokoCMD.Sim.Workers;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Tasks
{
class TimeWarpTask : SCTask
{
public override string Name => "TimeWarp";
public override double RequiredEnergy => 200.0;
public override void Run(string[] args) => Game.Objects.Get<TimeWarpEnergyProvider>().Warp();
public override void Run(string[] args)
{
TimeWarpWorker timeWarpWorker = (TimeWarpWorker) Game.Objects.Get<Supercomputer>().GetWorker("TimeWarp");
timeWarpWorker.Delay = int.Parse(args[2]);
timeWarpWorker.Start();
}
}
}

View File

@ -1,9 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Tasks
{
class VirtualizationTask : SCTask
{

View File

@ -1,11 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Virtual
{
class Coordinates : Tuple<double, double>
public class Coordinates : Tuple<double, double>
{
public Coordinates(Tuple<double, double> tuple) : this(tuple.Item1, tuple.Item2) { }
public Coordinates(Coordinates coords) : this(coords.Item1, coords.Item2) { }
@ -33,7 +30,7 @@ namespace LyokoCMD.Sim
}
}
enum Direction
public enum Direction
{
North = 'N',
South = 'S',

View File

@ -0,0 +1,22 @@
namespace LyokoCMD.Sim.Virtual.Towers
{
public class Tower : Virtualizable
{
protected TowerStatus _Status = TowerStatus.Inactive;
public TowerStatus Status => _Status;
public Tower() : this(Coordinates.Random())
{
}
public Tower(Coordinates coords) : base("Tour", coords) {}
}
public enum TowerStatus
{
Inactive,
ActiveXANA,
ActiveHopper,
ActiveJeremy
}
}

View File

@ -1,18 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
/// <summary>
/// Décrit une tour de passage.
/// </summary>
class WayTower : Tower
{
private Sector _Destination;
public Sector Destination => _Destination;
public WayTower(Sector destination) : base() => this._Destination = destination;
public WayTower(Coordinates coords, Sector destination) : base(coords) => this._Destination = destination;
}
}
using LyokoCMD.Sim.Virtual.World;
namespace LyokoCMD.Sim.Virtual.Towers
{
/// <summary>
/// Décrit une tour de passage.
/// </summary>
public class WayTower : Tower
{
private Sector _Destination;
public Sector Destination => _Destination;
public WayTower(Sector destination) : base() => this._Destination = destination;
public WayTower(Coordinates coords, Sector destination) : base(coords) => this._Destination = destination;
}
}

View File

@ -0,0 +1,10 @@
namespace LyokoCMD.Sim.Virtual
{
/// <summary>
/// Spécifie qu'un objet peut être virtualisable sur Lyoko par l'opérateur du supercalculateur.
/// </summary>
public abstract class UserVirtualizable : Virtualizable
{
public UserVirtualizable(string name, Coordinates coords = null) : base(name, coords) {}
}
}

View File

@ -1,16 +1,14 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Virtual
{
class Vehicle : UserVirtualizable
class Vehicle : Virtualizable
{
protected List<Human> _Riders;
public List<Human> Riders => _Riders;
public Vehicle(string name) : base(name) => _Riders = new List<Human>();
public Vehicle(string name, Coordinates coords = null) : base(name, coords) => _Riders = new List<Human>();
public void Mount(Human h)
{

View File

@ -0,0 +1,47 @@
using System;
namespace LyokoCMD.Sim.Virtual
{
/// <summary>
/// Spécifie qu'un objet peut être virtualisable sur Lyoko.
/// </summary>
public abstract class Virtualizable
{
protected bool _IsVirtualized = false;
public bool Virtualized => _IsVirtualized;
protected string _Name;
public string Name => _Name;
protected Coordinates _Coordinates;
public Coordinates Coordinates => _Coordinates;
protected bool _CanMaterialize;
public virtual bool CanMaterialize => _CanMaterialize;
protected Virtualizable(string name, Coordinates coords = null, bool canMaterialize = true)
{
_Name = name;
_Coordinates = coords;
_IsVirtualized = coords != null;
_CanMaterialize = canMaterialize;
}
public void Virtualize()
{
if (Virtualized) throw new InvalidOperationException("Cette entité a déjà été virtualisée.");
else _IsVirtualized = true;
}
public 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}";
}
}
}

View File

@ -0,0 +1,74 @@
using System;
using System.Collections.Generic;
namespace LyokoCMD.Sim.Virtual.World
{
/// <summary>
/// Décrit un coeur de Lyoko.
/// </summary>
public class Heart
{
private int _life = 100;
public int Life
{
get => _life;
set
{
if (_life > 1 && _life - value < 1)
{
_life = 1;
}
else
{
_life -= value;
}
}
}
public DateTime LastHurt;
public List<Sector> Sectors = new List<Sector>();
public HeartStatus Status
{
get
{
if (Life <= 0)
{
return HeartStatus.DEAD;
}
if (Life == 1)
{
return HeartStatus.OPEN;
}
if (Life <= 15)
{
return HeartStatus.FIRST_SHIELD;
}
if (Life <= 50)
{
return HeartStatus.SECOND_SHIELD;
}
return HeartStatus.THIRD_SHIELD;
}
}
private VirtualWorld _virtualWorld;
public Heart(VirtualWorld virtualWorld)
{
_virtualWorld = virtualWorld;
}
}
public enum HeartStatus
{
DEAD,
OPEN,
FIRST_SHIELD,
SECOND_SHIELD,
THIRD_SHIELD
}
}

View File

@ -0,0 +1,51 @@
using System.Collections.Generic;
using DasConzoleCore.UI;
using LyokoCMD.Sim.Virtual.Towers;
namespace LyokoCMD.Sim.Virtual.World
{
/// <summary>
/// Décrit un territoire de Lyoko.
/// </summary>
public class Sector : ITableLineOutput
{
protected string _Name;
protected bool _Locked;
public string Name => _Name;
protected string _ShortName;
public string ShortName { get => _ShortName; }
public bool Locked => _Locked;
public List<Virtualizable> Objects = new List<Virtualizable>();
public Sector(string Name, string shortName, bool locked = false)
{
this._Name = Name;
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 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);
}
}
}

View File

@ -0,0 +1,31 @@
using System.Collections.Generic;
using System.Linq;
namespace LyokoCMD.Sim.Virtual.World
{
/// <summary>
/// Décrit le monde virtuel Lyoko.
/// </summary>
public class VirtualWorld
{
public List<Sector> Sectors;
public Heart Heart;
public string Name;
public VirtualWorld(string name = "lyoko") : this(name, new List<Sector>())
{
}
public VirtualWorld(string name, IEnumerable<Sector> sectors)
{
Name = name;
Sectors = sectors.ToList();
Heart = new Heart(this);
}
public void MaterializeAll()
{
Sectors.ForEach(s => s.MaterializeAll());
}
}
}

View File

@ -1,9 +1,8 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LyokoCMD.Sim.Virtual.Towers;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Virtual.World
{
/// <summary>
/// Classe simplifiant la création d'un monde virtuel.
@ -15,24 +14,19 @@ namespace LyokoCMD.Sim
public VirtualWorldBuilder(String name) => this.Name = name;
public VirtualWorldBuilder AddSector(params string[] names) {
names.ForEach(name => Sectors.Add(new Sector(name)));
public VirtualWorldBuilder AddSector(string name, string shortName, bool locked = false)
{
Sectors.Add(new Sector(name, shortName, locked));
return this;
}
public VirtualWorldBuilder AddSector(string name, bool locked)
public VirtualWorldBuilder AddSector(string name, string shortName, bool locked, int towerCount)
{
Sectors.Add(new Sector(name, locked));
return this;
}
public VirtualWorldBuilder AddSector(string name, bool locked, int towerCount)
{
Sector s = new Sector(name, locked);
Sector s = new Sector(name, shortName, locked);
s.CreateTowers(towerCount);
return this;
}
public VirtualWorldBuilder AddTowers(int towerCount)
{
Sectors.ForEach(s => s.CreateTowers(towerCount));

View File

@ -1,47 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
/// <summary>
/// Décrit un territoire de Lyoko.
/// </summary>
class Sector
{
protected string _Name;
protected bool _Locked;
public string Name => _Name;
public bool Locked => _Locked;
public List<Virtualizable> Objects = new List<Virtualizable>();
public Sector(string Name) : this(Name, false) { }
public Sector(string Name, bool locked)
{
this._Name = Name;
this._Locked = locked;
}
public void CreateTowers(int count)
{
for (int i = 1; i <= count; i++) Objects.Add(new Tower());
}
public void Virtualize(Virtualizable obj)
{
throw new NotImplementedException(); // TODO: Virtualisation
}
public void Unvirtualize(Virtualizable obj)
{
throw new NotImplementedException(); // TODO: Dévirtualisation
}
public void UnvirtualizeAll()
{
Objects.Clear(); // TODO: Destruction du territoire
}
}
}

View File

@ -1,29 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
class Tower : Virtualizable
{
protected Coordinates _Coordinates;
protected TowerStatus _Status = TowerStatus.Inactive;
public Coordinates Coordinates => _Coordinates;
public TowerStatus Status => _Status;
public Tower() : this(Coordinates.Random())
{
}
public Tower(Coordinates coords) => _Coordinates = coords;
}
enum TowerStatus
{
Inactive,
ActiveXANA,
ActiveHopper,
ActiveJeremy
}
}

View File

@ -1,18 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
/// <summary>
/// Spécifie qu'un objet peut être virtualisable sur Lyoko par l'opérateur du supercalculateur.
/// </summary>
abstract class UserVirtualizable : Virtualizable
{
protected string _Name;
public string Name => _Name;
public UserVirtualizable(string name) : base() => this._Name = name;
public override string ToString() => this.Name;
}
}

View File

@ -1,41 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
/// <summary>
/// Décrit le monde virtuel Lyoko.
/// </summary>
class VirtualWorld : SCWorker
{
public List<Sector> Sectors;
public VirtualWorld() : this("Lyoko")
{
}
public VirtualWorld(string name) : this(name, new List<Sector>())
{
}
public VirtualWorld(string name, IEnumerable<Sector> sectors)
: base(name) => Sectors = sectors.ToList();
public override void Start() => this._Status = SCWorkerStatus.Running;
public override void Suspend()
{
throw new InvalidOperationException("Ce service ne peut pas être suspendu.");
}
public override void Stop()
{
Sectors.ForEach(s => s.UnvirtualizeAll());
this._Status = SCWorkerStatus.Stopped;
}
public override double RequiredEnergy => 300.0;
}
}

View File

@ -1,27 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
{
/// <summary>
/// Spécifie qu'un objet peut être virtualisable sur Lyoko.
/// </summary>
abstract class Virtualizable
{
protected bool _isVirtualized = false;
public bool Virtualized => _isVirtualized;
public void Virtualize()
{
if (Virtualized) throw new InvalidOperationException("Cette entité a déjà été virtualisée.");
else _isVirtualized = true;
}
public void Materialize() {
if (!Virtualized) throw new InvalidOperationException("Cette entité n'a pas été virtualisée.");
_isVirtualized = false;
}
}
}

View File

@ -1,27 +1,108 @@
using DasConzoleCore.UI;
using System;
using System.Threading;
using DasConzoleCore.UI;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Workers
{
/// <summary>
/// Décrit un programme en arrière-plan du super-calculateur.
/// </summary>
abstract class SCWorker : ITableLineOutput
public abstract class SCWorker : ITableLineOutput
{
protected string _Name;
protected readonly string _Name;
public string Name => _Name;
protected SCWorkerStatus _Status;
public SCWorkerStatus Status => _Status;
protected internal SCWorkerStatus Status;
public bool Running => Status == SCWorkerStatus.Running ||
Status == SCWorkerStatus.Suspended ||
Status == SCWorkerStatus.Waiting;
private readonly Thread _thread;
public SCWorker(string name)
{
_Name = name;
_Status = SCWorkerStatus.Ready;
Status = SCWorkerStatus.Ready;
_thread = new Thread(() =>
{
while (Status != SCWorkerStatus.Stopped)
{
if (Status == SCWorkerStatus.Running)
{
Run();
} else if (Status == SCWorkerStatus.Waiting && CanDeWait())
{
Status = SCWorkerStatus.Running;
}
Thread.Sleep(100);
}
});
}
protected virtual bool CanDeWait()
{
return false;
}
public abstract double RequiredEnergy { get; }
public abstract void Start();
public abstract void Suspend();
public abstract void Stop();
public void Start()
{
if (!Running && Status != SCWorkerStatus.Ready)
{
throw new InvalidOperationException("Ce Worker n'est pas prêt pour démarrer");
}
PreStart();
if (!Running)
{
_thread.Start();
}
this.Status = SCWorkerStatus.Running;
PostStart();
}
protected virtual void PreStart() {}
protected virtual void PostStart() {}
public void Suspend()
{
if (!Running)
{
throw new InvalidOperationException("Ce Worker n'est pas démarré");
}
PreSuspend();
this.Status = SCWorkerStatus.Suspended;
PostSuspend();
}
protected virtual void PreSuspend() {}
protected virtual void PostSuspend() {}
public void Stop()
{
if (!Running)
{
throw new InvalidOperationException("Ce Worker n'est pas démarré");
}
PreStop();
this.Status = SCWorkerStatus.Stopped;
PostStop();
}
protected virtual void PreStop() {}
protected virtual void PostStop() {}
/// <summary>
/// Stop a worker by force. Only intended for internal use.
/// </summary>
public void ForceStop()
{
this.Status = SCWorkerStatus.Stopped;
}
protected abstract void Run();
public string[] GetColumnNames() => new string[] { "Nom", "Énergie", "Statut" };
public string[] GetLine() => new string[] { Name, RequiredEnergy.ToString(), Status.ToString() };

View File

@ -1,14 +1,9 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace LyokoCMD.Sim
namespace LyokoCMD.Sim.Workers
{
/// <summary>
/// Décrit un état de service de supercalculateur.
/// </summary>
enum SCWorkerStatus
public enum SCWorkerStatus
{
/// <summary>
/// Service prêt à être exécuté manuellement.

View File

@ -0,0 +1,49 @@
using System;
using LyokoCMD.Sim.EnergyProviders;
using LyokoCMD.Sim.Virtual.World;
namespace LyokoCMD.Sim.Workers
{
/// <summary>
/// Décrit le monde virtuel Lyoko en arrière plan
/// </summary>
public class TimeWarpWorker : SCWorker
{
private long _LastTime;
public long Delay
{
get => Delay;
set
{
if (Delay <= 0)
{
Delay = value * 3600;
}
}
}
public TimeWarpWorker() : base("TimeWarp") { }
public override double RequiredEnergy => 0.0;
protected override void PostStart()
{
_LastTime = DateTimeOffset.UtcNow.ToUnixTimeSeconds();
}
protected override void Run()
{
if (Delay <= 0)
{
Game.Objects.Get<TimeWarpEnergyProvider>().Warp();
Game.Objects.Get<VirtualWorld>().MaterializeAll();
Console.WriteLine("Retour vers le passé enclenché");
Stop();
}
else
{
Delay -= DateTimeOffset.UtcNow.ToUnixTimeSeconds() - _LastTime;
}
}
}
}

View File

@ -0,0 +1,34 @@
using System;
using LyokoCMD.Sim.Virtual.World;
namespace LyokoCMD.Sim.Workers
{
/// <summary>
/// Décrit le monde virtuel Lyoko en arrière plan
/// </summary>
public class VirtualWorldWorker : SCWorker
{
private VirtualWorld _World;
public VirtualWorldWorker(VirtualWorld world) : base(world.Name)
{
_World = world;
}
public override double RequiredEnergy => 300.0;
protected override void PreSuspend()
{
throw new InvalidOperationException("Ce service ne peut pas être suspendu.");
}
protected override void PreStop()
{
_World.MaterializeAll();
}
protected override void Run()
{
}
}
}

View File

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>netcoreapp2.1</TargetFramework>