Esempio n. 1
0
        public void Start_ThrowsArgumentNullWhenBotIsNull()
        {
            var socket = new StubWebSocket(WebSocketState.Open);
            var client = new SnakeClient(socket);

            Assert.Throws <ArgumentNullException>(() => client.Start(null, true));
        }
Esempio n. 2
0
        public void Start_RequestsMoveFromSnakeBotOnMapUpdate()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.GameStarting }
            });
            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.MapUpdated },
                { "map", _sampleMapJson }
            });

            var bot = new Mock <StubSnakeBot>();

            bot.Setup(b => b.GetNextMove(It.IsAny <Map>()));

            var client = new SnakeClient(socket);

            client.Start(bot.Object, true);

            bot.Verify(o => o.GetNextMove(It.IsAny <Map>()), Times.Once);
        }
Esempio n. 3
0
        public void Start_RequestsMoveFromSnakeBotOnMapUpdate()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.GameStarting }
            });
            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.MapUpdated },
                { "map", _sampleMapJson }
            });

            bool receivedCall = false;
            var  bot          = new StubSnakeBot(map => { receivedCall = true; return(Direction.Down); });

            var client = new SnakeClient(socket);

            client.Start(bot);

            Assert.True(receivedCall);
        }
Esempio n. 4
0
        public void Start_ThrowsInvalidOperationWhenSocketIsNotOpen(WebSocketState state)
        {
            var socket = new StubWebSocket(state);
            var client = new SnakeClient(socket);

            Assert.Throws <InvalidOperationException>(() => client.Start(new StubSnakeBot(), true));
        }
Esempio n. 5
0
        public void Start_SendsRegisterMoveRequestFromBot(Direction direction, string expectedDirectionString)
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.GameStarting }
            });
            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.MapUpdated },
                { "map", _sampleMapJson }
            });

            var client = new SnakeClient(socket, Mock.Of <IGameObserver>());

            client.Start(new StubSnakeBot(direction), true);

            var moveMessage = socket.OutgoingJson.Last();

            Assert.Equal("se.cygni.snake.api.request.RegisterMove", (string)moveMessage["type"]);
            Assert.Equal(expectedDirectionString, (string)moveMessage["direction"]);
            Assert.Equal(_sampleMapJson["worldTick"], (string)moveMessage["gameTick"]);
        }
Esempio n. 6
0
        public void Start_ThrowsInvalidOperationWhenServerSaysPlayerNameIsInvalid()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.InvalidPlayerName }, { "reason", "taken" }
            });
            var client = new SnakeClient(socket);

            Assert.Throws <InvalidOperationException>(() => client.Start(new StubSnakeBot(), true));
        }
Esempio n. 7
0
        public static void Main(string[] args)
        {
            var ws = new ClientWebSocket();

            ws.ConnectAsync(new Uri("ws://snake.cygni.se:80/training"), CancellationToken.None).Wait();

            var client = new SnakeClient(ws, new GamePrinter());

            client.Start(new MySnakeBot("dotnetSnake"));

            Console.ReadLine();
        }
Esempio n. 8
0
        public void Start_RequestsMoveFromBotEventIfNoGameStartingMessageHasBeenReceived()
        {
            // Not an actual requirement, but this test is here for completness.
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8)));

            var client    = new SnakeClient(socket, Mock.Of <IGameObserver>());
            var mockSnake = new Mock <StubSnakeBot>();

            mockSnake.Setup(s => s.GetNextMove(It.IsAny <Map>()));
            client.Start(mockSnake.Object, true);

            mockSnake.Verify(s => s.GetNextMove(It.IsAny <Map>()), Times.Once());
        }
Esempio n. 9
0
        public void Start_SendsStartGameRequestAfterServerHasConfirmedRegistration()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            var client = new SnakeClient(socket, Mock.Of <IGameObserver>());

            client.Start(new StubSnakeBot(), true);

            var startGameMessage = socket.OutgoingJson[2];

            Assert.Equal("se.cygni.snake.api.request.StartGame", (string)startGameMessage["type"]);
        }
Esempio n. 10
0
        public void Start_RequestsMoveFromBotEventIfNoGameStartingMessageHasBeenReceived()
        {
            // Not an actual requirement, but this test is here for completness.
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8)));

            bool receivedCall = false;
            var  bot          = new StubSnakeBot(map => { receivedCall = true; return(Direction.Down); });

            var client = new SnakeClient(socket);

            client.Start(bot);

            Assert.True(receivedCall);
        }
Esempio n. 11
0
        public void Start_DoesNotSendStartGameRequestAfterServerHasConfirmedRegistrationWhenAutoStartDisabled()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            var client = new SnakeClient(socket, new StubGameObserver());

            client.Start(new StubSnakeBot()
            {
                AutoStart = false
            });

            Assert.Equal(2, socket.OutgoingJson.Count);
        }
Esempio n. 12
0
        public void Start_NotifiesObserverWhenGameIsStarting()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.GameStarting }
            });

            var observer = new StubGameObserver();
            var client   = new SnakeClient(socket, observer);

            client.Start(new StubSnakeBot());

            Assert.Equal(1, observer.GameStartCalls);
        }
Esempio n. 13
0
        public void Start_SendsRegisterPlayerRequest()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            var client = new SnakeClient(socket, Mock.Of <IGameObserver>());

            var bot = new StubSnakeBot();

            client.Start(bot, true);

            var registerMessage = socket.OutgoingJson[0];

            Assert.Equal("se.cygni.snake.api.request.RegisterPlayer", (string)registerMessage["type"]);
            Assert.Equal(bot.Name, (string)registerMessage["playerName"]);
        }
Esempio n. 14
0
        public void Start_NotifiesObserverWhenSnakeHasDied()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.SnakeDead },
                { "playerId", "snake-id" },
                { "deathReason", "CollisionWithWall" },
            });

            var observer = new StubGameObserver();

            var client = new SnakeClient(socket, observer);

            client.Start(new StubSnakeBot());

            Assert.Equal(1, observer.SnakeDiedCalls);
        }
Esempio n. 15
0
        public void Start_NotifiesObserverWhenGameHasEnded()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.GameEnded },
                { "map", _sampleMapJson }
            });


            var observer = new StubGameObserver();

            var client = new SnakeClient(socket, observer);

            client.Start(new StubSnakeBot());

            Assert.Equal(observer.GameEndCalls, 1);
        }
Esempio n. 16
0
        public void Start_NotifiesObserverWhenGameHasEnded()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.GameEnded },
                { "map", _sampleMapJson }
            });


            var observer = new Mock <IGameObserver>();

            observer.Setup(o => o.OnGameEnd(It.IsAny <Map>()));

            var client = new SnakeClient(socket, observer.Object);

            client.Start(new StubSnakeBot(), true);

            observer.Verify(o => o.OnGameEnd(It.IsAny <Map>()), Times.Once);
        }
Esempio n. 17
0
        public void Start_NotifiesObserverWhenGameIsStarting()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.PlayerRegistered }
            });
            socket.IncomingJson.Enqueue(new JObject {
                { "type", MessageType.GameStarting }
            });

            var observer = new Mock <IGameObserver>();

            observer.Setup(o => o.OnGameStart());

            var client = new SnakeClient(socket, observer.Object);

            client.Start(new StubSnakeBot(), true);

            observer.Verify(o => o.OnGameStart(), Times.Once);
        }
Esempio n. 18
0
        public void RegisterPlayer_StartGame_RegisterMove()
        {
            // Setting up socket with normal flow of events
            var socket = new StubWebSocket(WebSocketState.Open)
            {
                CloseWhenNoMoreMessages = true
            };

            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("player-registered.json", Encoding.UTF8)));
            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("game-starting.json", Encoding.UTF8)));
            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("map-update.json", Encoding.UTF8)));
            socket.IncomingJson.Enqueue(JObject.Parse(TestResources.GetResourceText("game-ended.json", Encoding.UTF8)));

            var client = new SnakeClient(socket, Mock.Of <IGameObserver>());

            client.Start(new StubSnakeBot(Direction.Left));

            // Assert correct sequence of messages back to the socket.
            Assert.Equal(MessageType.RegisterPlayer, socket.OutgoingJson[0]["type"]);
            Assert.Equal(MessageType.StartGame, socket.OutgoingJson[1]["type"]);
            Assert.Equal(MessageType.RegisterMove, socket.OutgoingJson[2]["type"]);
        }
Esempio n. 19
0
        public void Start_NotifiesObserverWhenSnakeHasDied()
        {
            var socket = new StubWebSocket(WebSocketState.Open);

            socket.IncomingJson.Enqueue(new JObject
            {
                { "type", MessageType.SnakeDead },
                { "playerId", "snake-id" },
                { "deathReason", "CollisionWithWall" },
            });

            var observer = new Mock <IGameObserver>();

            observer.Setup(o => o.OnSnakeDied(It.IsAny <string>(), It.IsAny <string>()));

            var client = new SnakeClient(socket, observer.Object);

            client.Start(new StubSnakeBot(), true);

            observer.Verify(o => o.OnSnakeDied(
                                It.Is <string>(s => s.Equals("CollisionWithWall", StringComparison.Ordinal)),
                                It.Is <string>(s => s.Equals("snake-id"))), Times.Once);
        }