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 ShipGame/ShipGame/GameClass/CreateNewGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Hwdtech;
namespace ShipGame.Move;


public class CreateNewGame : IStrategy
{
int quantum;
public CreateNewGame(int _quantum = 500)
{
quantum = _quantum;
}
public object RunStrategy(params object[] args)
{
Queue<ICommand> queue = new Queue<ICommand>();
object scope = new InitGameScope().RunStrategy(quantum);
return IoC.Resolve<ICommand>("Commands.GameCommand", scope, queue);
}
}
11 changes: 11 additions & 0 deletions ShipGame/ShipGame/GameClass/DeleteGame.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using Hwdtech;
namespace ShipGame.Move;

public class DeleteGame : IStrategy
{
public object RunStrategy(params object[] args)
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.Outer")).Execute();
return new object();
}
}
17 changes: 17 additions & 0 deletions ShipGame/ShipGame/GameClass/GetItem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using Hwdtech;
namespace ShipGame.Move;


public class GetItem : IStrategy
{
public object RunStrategy(params object[] args)
{
IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").TryGetValue((string) args[0], out IUObject? obj);

if (obj != null)
{
return obj;
}
throw new Exception();
}
}
32 changes: 32 additions & 0 deletions ShipGame/ShipGame/GameClass/InitGameScope.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using Hwdtech;
using Hwdtech.Ioc;
namespace ShipGame.Move;

public class InitGameScope : IStrategy
{
public object RunStrategy(params object[] _args)
{
new InitScopeBasedIoCImplementationCommand().Execute();
object scopePrev = IoC.Resolve<object>("Scopes.Current");

object scope = IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"));
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", scope).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Scopes.Outer", (Func<object[], object>) (args => scopePrev)).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "GetQuantum", (Func<object[], object>) (args => (int) _args[0])).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "QueueDequeue", (Func<object[], ICommand>) (args => (ICommand) new QueueDequeue().RunStrategy((Queue<ICommand>) args[0]))).Execute();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "QueueEnqueue", (Func<object[], ICommand>) (args => new QueueEnqueueCommand((Queue<ICommand>) args[0], (ICommand) args[1]))).Execute();

Dictionary<string, IUObject> gameObjects = new Dictionary<string, IUObject>();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "General.Objects", (Func<object[], Dictionary<string, IUObject>>) (args => gameObjects)).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "General.GetItem", (Func<object[], IUObject>) (args => (IUObject) new GetItem().RunStrategy(args[0]))).Execute();
IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "General.RemoveItem", (Func<object[], ICommand>) (args => new RemoveItemCommand((string)args[0]))).Execute();

IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.Outer")).Execute();

return scope;
}
}
17 changes: 17 additions & 0 deletions ShipGame/ShipGame/GameClass/QueueDequeue.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ShipGame.Move;

public class QueueDequeue : IStrategy
{
public object RunStrategy(params object[] args)
{
var queue = (Queue<ICommand>)args[0];
if(!queue.TryDequeue(out ICommand? command))
{
throw new Exception();
}
else
{
return command;
}
}
}
17 changes: 17 additions & 0 deletions ShipGame/ShipGame/GameClass/QueueEnqueueCommand.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
namespace ShipGame.Move;


public class QueueEnqueueCommand : ICommand
{
Queue<ICommand> target;
ICommand command;
public QueueEnqueueCommand(Queue<ICommand> _target, ICommand cmd)
{
this.target = _target;
this.command = cmd;
}
public void Execute()
{
target.Enqueue(command);
}
}
16 changes: 16 additions & 0 deletions ShipGame/ShipGame/GameClass/Removeitem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace ShipGame.Move;
using Hwdtech;


public class RemoveItemCommand : ICommand
{
string key;
public RemoveItemCommand(string _key)
{
key = _key;
}
public void Execute()
{
IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Remove(key);
}
}
96 changes: 96 additions & 0 deletions ShipGame/Tests/TestGameClass/GameObjectsTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
using Hwdtech;
using Hwdtech.Ioc;
using Moq;

using ShipGame.Move;
using ShipGame.Server;
using ICommand = ShipGame.Move.ICommand;
using SpaceBattle.ServerStrategies;

namespace Tests.TestGameClass;
public class GameObjectsTests
{
public GameObjectsTests()
{
new InitScopeBasedIoCImplementationCommand().Execute();
}
[Test]
public void getItemTest()
{
new InitScopeBasedIoCImplementationCommand().Execute();
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"))).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();

ICommand gameCommand = (ICommand) new CreateNewGame().RunStrategy();
gameCommand.Execute();

var mockObj = new Mock<IUObject>();

IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Add("0", mockObj.Object);

var resolvedObj = IoC.Resolve<IUObject>("General.GetItem", "0");

Assert.True(mockObj.Object == resolvedObj);
}
[Test]
public void removeItemTest()
{
new InitScopeBasedIoCImplementationCommand().Execute();
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"))).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();

ICommand gameCommand = (ICommand) new CreateNewGame().RunStrategy();
gameCommand.Execute();

var mockObj = new Mock<IUObject>();

IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Add("0", mockObj.Object);
Assert.True( IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Count() == 1);

IoC.Resolve<ICommand>("General.RemoveItem", "0").Execute();
Assert.True( IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Count() == 0);
}
[Test]
public void getItemTestObjectNotExists()
{
new InitScopeBasedIoCImplementationCommand().Execute();
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", IoC.Resolve<object>("Scopes.New", IoC.Resolve<object>("Scopes.Root"))).Execute();

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();

ICommand gameCommand = (ICommand) new CreateNewGame().RunStrategy();
gameCommand.Execute();

var mockObj = new Mock<IUObject>();

IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Add("0", mockObj.Object);

IoC.Resolve<ICommand>("General.RemoveItem", "0").Execute();
Assert.True( IoC.Resolve<Dictionary<string, IUObject>>("General.Objects").Count() == 0);

Assert.Throws<Exception>(
() =>
{
IoC.Resolve<IUObject>("General.GetItem", "0");
}
);
}

}
72 changes: 72 additions & 0 deletions ShipGame/Tests/TestGameClass/QueueStrategiesTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
using Hwdtech;
using Hwdtech.Ioc;
using Moq;

using ShipGame.Move;
using ShipGame.Server;
using ICommand = ShipGame.Move.ICommand;



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

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();
}
[Test]
public void enqueueTest()
{
var queue = new Queue<ICommand>();
var cmd = new Mock<ICommand>();

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

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();

ICommand gameCommand = (ICommand) new CreateNewGame().RunStrategy();
gameCommand.Execute();

IoC.Resolve<ICommand>("QueueEnqueue", queue, cmd.Object).Execute();
Assert.True(queue.Count() == 1);
}
[Test]
public void dequeueTest()
{
var queue = new Queue<ICommand>();
var cmd = new Mock<ICommand>();

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

IoC.Resolve<Hwdtech.ICommand>("IoC.Register", "Commands.GameCommand", (object[] args) => new ActionCommand(
() =>
{
IoC.Resolve<Hwdtech.ICommand>("Scopes.Current.Set", args[0]).Execute();
}
)).Execute();

ICommand gameCommand = (ICommand) new CreateNewGame().RunStrategy();
gameCommand.Execute();

IoC.Resolve<ICommand>("QueueEnqueue", queue, cmd.Object).Execute();

var cmd1 = IoC.Resolve<ICommand>("QueueDequeue", queue);
Assert.True(cmd.Object == cmd1);
}
}
Loading