Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions SpaceBattle.App/App.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Hwdtech;
using SpaceBattle.Lib;

public class App
{
public static void Main(int[] args)
{
var NumberOfTreads = args[0];
new InitCommand().Execute();
Console.WriteLine("Запуск сервера\nСоздание и регистрация потоков...");
IoC.Resolve<Hwdtech.ICommand>("Game.Commands.StartServerCommand", NumberOfTreads).Execute();
Console.WriteLine("Потоки запущены\nДля завершения работы сервера нажмите любую клавишу");
Console.ReadKey();
Console.WriteLine("Завершение работы сервера. Закрытие потоков...");
IoC.Resolve<Hwdtech.ICommand>("Game.Commands.StopServerCommand");
Console.WriteLine("Все потоки закрыты. Завершение работы программы");
}
}
169 changes: 0 additions & 169 deletions SpaceBattle.Lib.Tests/EndPointTests.cs

This file was deleted.

1 change: 0 additions & 1 deletion SpaceBattle.Lib.Tests/SpaceBattle.Lib.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

<ItemGroup>
<ProjectReference Include="..\SpaceBattle.Lib\SpaceBattle.Lib.csproj" />
<ProjectReference Include="..\WebHttp\WebHttp.csproj" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions SpaceBattle.Lib.Tests/StartServerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using Hwdtech;
using Hwdtech.Ioc;
using Moq;

namespace SpaceBattle.Lib.Test;

public class StartServerTest
{
public StartServerTest()
{
new InitScopeBasedIoCImplementationCommand().Execute();
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"))).Execute();
var a = new InitCommand();
a.Execute();
}
[Fact]
public void Start_Server_Test()
{
var startCommand = new Mock<ICommand>();
startCommand.Setup(cmd => cmd.Execute()).Verifiable();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.CreateAndStartThread", (object[] args) =>
{
return startCommand.Object;
}).Execute();
IoC.Resolve<ICommand>("Game.Commands.StartServerCommand", 5).Execute();
startCommand.Verify(cmd => cmd.Execute(), Times.Exactly(5));
}
}
70 changes: 70 additions & 0 deletions SpaceBattle.Lib.Tests/StopServerTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
using Hwdtech;
using Hwdtech.Ioc;
using Moq;

namespace SpaceBattle.Lib.Test;

public class StopServerTest
{
public StopServerTest()
{
new InitScopeBasedIoCImplementationCommand().Execute();
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"))).Execute();

var logfilepath = Path.GetTempFileName();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "GetLogFilePath", (object[] args) =>
{
return logfilepath;
}).Execute();
var a = new InitCommand();
a.Execute();
}

[Fact]

public void StopServer_Test()
{
var threadList = new List<int> { 1, 2, 3 };
var barrier = new Barrier(4);
var stopCommand = new Mock<ICommand>();
stopCommand.Setup(x => x.Execute()).Verifiable();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.GetThreadIDs", (object[] args) => { return threadList; }).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.StopServerBarrierRemove", (object[] args) =>
{
return () => { barrier.RemoveParticipant(); };
}).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.StopServerBarrierWait", (object[] args) =>
{
return new ActionCommand(() => { barrier.SignalAndWait(); });
}).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.SoftStopThread", (object[] args) =>
{
return new ActionCommand((Action)args[1]);
}).Execute();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.SendCommand", (object[] args) =>
{
return new ActionCommand(() =>
{
((ICommand)args[1]).Execute();
stopCommand.Object.Execute();
});
}).Execute();

IoC.Resolve<ICommand>("Game.Commands.StopServerCommand").Execute();

Assert.Equal(1, barrier.CurrentPhaseNumber);
stopCommand.Verify(cmd => cmd.Execute(), Times.Exactly(3));
}

[Fact]
public void StopServer_TestwithExeption()
{
IoC.Resolve<ICommand>("Game.Commands.ExeptionHandler", new ActionCommand(() => { }), new Exception()).Execute();

var testString = "Error occurred in command: SpaceBattle.Lib.ActionCommand\nException: System.Exception: Exception of type 'System.Exception' was thrown.\n";
Assert.Equal(File.ReadAllText(IoC.Resolve<string>("GetLogFilePath")), testString);
}
}
51 changes: 51 additions & 0 deletions SpaceBattle.Lib/InitCommands.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
namespace SpaceBattle.Lib;
using System.Linq;
using Hwdtech;

public class InitCommand : ICommand
{
public void Execute()
{
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.StartServerCommand", (object[] args) =>
{
var NumberOfThreads = (int)args[0];

return new ActionCommand(() =>
{
Enumerable.Range(0, NumberOfThreads).ToList().ForEach(i =>
{
IoC.Resolve<ICommand>("Game.Commands.CreateAndStartThread").Execute();
});
});
}).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.ExeptionHandler", (object[] args) =>
{
var errorFile = IoC.Resolve<string>("GetLogFilePath");
return new ActionCommand(() =>
{
using (var sw = File.AppendText(errorFile))
{
sw.WriteLine($"Error occurred in command: {args[0]}");
sw.WriteLine($"Exception: {args[1]}");
}
});
}).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Game.Commands.StopServerCommand", (object[] args) =>
{
var ThreadList = IoC.Resolve<List<int>>("Game.Commands.GetThreadIDs");
return new ActionCommand(() =>
{
ThreadList.ForEach(thread_id =>
{
IoC.Resolve<ICommand>("Game.Commands.SendCommand", thread_id,
IoC.Resolve<ICommand>("Game.Commands.SoftStopThread", thread_id,
IoC.Resolve<Action>("Game.Commands.StopServerBarrierRemove"))).Execute();
});

IoC.Resolve<ICommand>("Game.Commands.StopServerBarrierWait").Execute();
});
}).Execute();
}
}
16 changes: 0 additions & 16 deletions WebHttp/Endpoint.cs

This file was deleted.

19 changes: 0 additions & 19 deletions WebHttp/IWebApi.cs

This file was deleted.

Loading
Loading