Esempio n. 1
0
        /// <summary>
        /// Initializes the server and game.
        /// </summary>
        /// <param name="startedCallback">The callback to invoke when initalization is completed.</param>
        public Task Initialize()
        {
            // Server setup
            IServerConfiguration serverConfig = new ServerConfiguration();

            serverConfig.Port = 2000;
            var server = new StandardServer(new TestPlayerFactory(), new ConnectionFactory());

            server.Configure(serverConfig);
            server.Owner = "@Scionwest";
            this.Server  = server;

            // Commanding setup
            var commandManager = new CommandManager();

            commandManager.Configure(new CommandingConfiguration(new CommandFactory(new Type[] { typeof(LoginCommand), typeof(WalkCommand) })));

            IGameConfiguration gameConfig = new GameConfiguration();

            gameConfig.UseAdapter(server);
            gameConfig.UseAdapter(commandManager);

            IGame game = new MudGame();

            game.Configure(gameConfig);
            this.Game = game;

            return(game.StartAsync());
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        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);
        }
        public async Task BeginStart_invokes_callback()
        {
            // Arrange
            var configuration = Mock.Of <IGameConfiguration>();
            var game          = new MudGame();
            await game.Configure(configuration);

            var taskCompletionSource = new TaskCompletionSource <bool>();

            // Act
            game.BeginStart(g =>
            {
                taskCompletionSource.SetResult(true);
            });

            bool result = await taskCompletionSource.Task;

            // Assert
            Assert.IsTrue(result, "Callback was not hit.");
            Assert.IsTrue(game.IsRunning);
        }
        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);
        }