Beispiel #1
0
        public async Task Configuring_a_game_assigns_the_game_configuration_instance()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>();
            var game = new MudGame();

            // Act
            await game.Configure(configuration);

            // Assert
            Assert.IsNotNull(game.Configuration);
            Assert.AreEqual(configuration, game.Configuration);
        }
        /// <summary>
        /// Initializes the server and game.
        /// </summary>
        /// <param name="startedCallback">The callback to invoke when initalization is completed.</param>
        public void Initialize(Action<IGame, StandardServer> startedCallback)
        {
            var serverConfig = new ServerConfiguration();
            var server = new StandardServer(new TestPlayerFactory(), new ConnectionFactory());
            server.Configure(serverConfig);
            server.Owner = "@Scionwest";
			this.Server = server;

            var gameConfig = new GameConfiguration();
            gameConfig.UseAdapter(server);

            var game = new MudGame();
            game.Configure(gameConfig);
			this.Game = game;

            game.BeginStart(runningGame => startedCallback(runningGame, server));
        }
Beispiel #3
0
        public async Task BeginStart_with_null_synchronizationcontext_does_not_throw_exception()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>();
            var game = new MudGame();
            await game.Configure(configuration);

            // We must ensure the synchronization context is null to test that the MudGame creates one itself
            SynchronizationContext.SetSynchronizationContext(null);

            // Act
            var taskCompletionSource = new TaskCompletionSource<bool>();
            game.BeginStart(g => taskCompletionSource.TrySetResult(true));

            bool callbackHit = await taskCompletionSource.Task;

            // Assert
            Assert.IsTrue(callbackHit, "Callback was not hit.");
            Assert.IsTrue(game.IsRunning);
        }
Beispiel #4
0
        public async Task BeginStart_invokes_callback()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>();
            var game = new MudGame();
            await game.Configure(configuration);
            bool callbackHit = false;

            // We must create a synchronization context as the BeginStart needs one to exist.
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());

            // Act
            game.BeginStart(g => callbackHit = true);

            await Task.Delay(100);

            // Assert
            Assert.IsTrue(callbackHit, "Callback was not hit.");
            Assert.IsTrue(game.IsRunning);
        }
Beispiel #5
0
        public async Task Start_runs_the_game()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>();
            var game = new MudGame();
            await game.Configure(configuration);

#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
                              // Act
            Task.Run(async () => await game.StartAsync());
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            await Task.Delay(TimeSpan.FromSeconds(2));

            // Assert
            Assert.IsTrue(game.IsRunning);
            Assert.IsTrue(game.IsEnabled);
        }
Beispiel #6
0
        public async Task Delete_game_will_delete_adapter()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>(mock => mock.GetAdapters() == new IAdapter[1] { new AdapterFixture() });
            var game = new MudGame();
            SynchronizationContext.SetSynchronizationContext(new SynchronizationContext());
            await game.Configure(configuration);

            IAdapter[] adapters = game.Configuration.GetAdapters();

            // Act
            game.BeginStart(async (runningGame) => await game.Delete());
            var timer = new EngineTimer<AdapterFixture>((AdapterFixture)adapters[0]);
            timer.Start(TimeSpan.FromSeconds(20).TotalMilliseconds, 0, 1, (fixture, runningTimer) =>
            {
                runningTimer.Stop();
            });

            while (!((AdapterFixture)adapters[0]).IsDeleted)
            {
                await Task.Delay(1);
                if (!timer.IsRunning)
                {
                    break;
                }
            }

            // Assert
            Assert.IsTrue(((AdapterFixture)adapters[0]).IsDeleted);
        }
Beispiel #7
0
        public async Task Start_game_will_start_adapter()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>(mock => mock.GetAdapters() == new IAdapter[1] 
            {
                new AdapterFixture()
            });
            var game = new MudGame();
            await game.Configure(configuration);

            IAdapter[] adapter = game.Configuration.GetAdapters();

            // Act
            game.BeginStart(runningGame => { });
            
            while (!game.IsRunning)
            {
                await Task.Delay(1);
            }

            // Assert
            Assert.IsTrue(((AdapterFixture)adapter[0]).IsInitialized);
            Assert.IsTrue(((AdapterFixture)adapter[0]).IsStarted);
        }
Beispiel #8
0
        public async Task Initialize_enables_the_game()
        {
            // Arrange
            var configuration = Mock.Of<IGameConfiguration>();
            var game = new MudGame();
            await game.Configure(configuration);

            // Act
            await game.Initialize();

            // Assert
            Assert.IsTrue(game.IsEnabled);
        }