Exemple #1
0
        public async Task TestAcceptMessageBegForInfoAccept()
        {
            BegForInfoForwardedPayload payloadBeg = new BegForInfoForwardedPayload()
            {
                AskingID = 2,
                Leader   = false,
                TeamID   = Team.Red,
            };
            Message messageBeg   = new Message(MessageID.BegForInfoForwarded, agentID, payloadBeg);
            Message messageStart = CreateStartMessage();

            BufferBlock <Message> inputBuffer = new BufferBlock <Message>();

            inputBuffer.Post(messageStart);
            inputBuffer.Post(messageBeg);

            PlayerConfiguration configuration = GenerateSampleConfiguration();
            var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger);

            await player.AcceptMessage(CancellationToken.None);

            await player.AcceptMessage(CancellationToken.None);

            Assert.Single(player.WaitingPlayers);
        }
Exemple #2
0
        public async Task TestAcceptMessageDiscoverAccept()
        {
            DiscoveryAnswerPayload payloadDiscover = new DiscoveryAnswerPayload()
            {
                DistanceFromCurrent = 0,
                DistanceE           = 0,
                DistanceW           = 0,
                DistanceS           = 0,
                DistanceN           = 0,
                DistanceNE          = 0,
                DistanceSE          = 0,
                DistanceNW          = 0,
                DistanceSW          = 0,
            };
            Message messageDiscover = new Message(MessageID.DiscoverAnswer, agentID, payloadDiscover);
            Message messageStart    = CreateStartMessage();

            BufferBlock <Message> inputBuffer = new BufferBlock <Message>();

            inputBuffer.Post(messageStart);
            inputBuffer.Post(messageDiscover);

            PlayerConfiguration configuration = GenerateSampleConfiguration();
            var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger);

            await player.AcceptMessage(CancellationToken.None);

            await player.AcceptMessage(CancellationToken.None);

            Assert.Equal(0, player.Board[0, 0].DistToPiece);
        }
Exemple #3
0
        public async Task TestAcceptMessageDestructionAnswerShouldDestroyHoldingPiece()
        {
            // Arrange
            EmptyAnswerPayload destructionPayload = new EmptyAnswerPayload();
            Message            destructionMessage = new Message(MessageID.DestructionAnswer, agentID, destructionPayload);

            EmptyAnswerPayload pickPayload  = new EmptyAnswerPayload();
            Message            pickMessage  = new Message(MessageID.PickAnswer, agentID, pickPayload);
            Message            startMessage = CreateStartMessage();

            BufferBlock <Message> inputBuffer = new BufferBlock <Message>();

            inputBuffer.Post(startMessage);
            inputBuffer.Post(pickMessage);
            inputBuffer.Post(destructionMessage);

            PlayerConfiguration configuration = GenerateSampleConfiguration();
            var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger);

            // Act
            bool expectedHasPieceValue = false;

            await player.AcceptMessage(CancellationToken.None);

            await player.AcceptMessage(CancellationToken.None);

            await player.AcceptMessage(CancellationToken.None);

            bool realHasPieceValue        = player.HasPiece;
            bool?realIsHeldPieceShamValue = player.IsHeldPieceSham;

            // Assert
            Assert.Equal(expectedHasPieceValue, realHasPieceValue);
            Assert.Null(realIsHeldPieceShamValue);
        }
Exemple #4
0
        public async Task TestAcceptMessageMoveAnswerShouldChangePlayerPosition()
        {
            // Arrange
            Position newPosition = new Position()
            {
                X = 1,
                Y = 2,
            };
            int distToClosestPiece = 1;

            MoveAnswerPayload moveAnswerPayload = new MoveAnswerPayload()
            {
                MadeMove        = true,
                CurrentPosition = newPosition,
                ClosestPiece    = distToClosestPiece,
            };
            Message moveAnswerMessage = new Message(MessageID.MoveAnswer, agentID, moveAnswerPayload);
            Message startMessage      = CreateStartMessage();

            BufferBlock <Message> inputBuffer = new BufferBlock <Message>();

            inputBuffer.Post <Message>(startMessage);
            inputBuffer.Post <Message>(moveAnswerMessage);

            PlayerConfiguration configuration = GenerateSampleConfiguration();
            var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger);

            // Act
            (int, int)expectedPosition = (newPosition.Y, newPosition.X);

            await player.AcceptMessage(CancellationToken.None);

            await player.AcceptMessage(CancellationToken.None);

            var realPosition           = player.Position;
            var realDistToClosestPiece = player.Board[player.Position.y, player.Position.x].DistToPiece;

            // Assert
            Assert.Equal(expectedPosition, realPosition);
            Assert.Equal(distToClosestPiece, realDistToClosestPiece);
        }
Exemple #5
0
        public async Task TestAcceptMessageStartGameShouldSetFields()
        {
            int  agentID  = 1;
            int  leaderId = 1;
            Team teamId   = Team.Red;

            int[] alliesId = new int[1] {
                2
            };
            int[] enemiesId = new int[2] {
                3, 4
            };
            BoardSize boardSize = new BoardSize {
                X = 3, Y = 3
            };
            int             goalAreaSize    = 1;
            NumberOfPlayers numberOfPlayers = new NumberOfPlayers {
                Allies = 2, Enemies = 2
            };
            int       numberOfPieces = 2;
            int       numberOfGoals  = 2;
            Penalties penalties      = new Penalties()
            {
                Move         = 100,
                Ask          = 100,
                Response     = 100,
                Discovery    = 100,
                Pickup       = 100,
                CheckForSham = 100,
                PutPiece     = 100,
                DestroyPiece = 100,
            };
            float    shanProbability = 0.5f;
            Position position        = new Position {
                X = 1, Y = 1
            };

            // Arrange
            StartGamePayload startGamePayload = new StartGamePayload
            {
                AgentID              = agentID,
                AlliesIDs            = alliesId,
                LeaderID             = leaderId,
                EnemiesIDs           = enemiesId,
                TeamID               = teamId,
                BoardSize            = boardSize,
                GoalAreaSize         = goalAreaSize,
                NumberOfPlayers      = numberOfPlayers,
                NumberOfPieces       = numberOfPieces,
                NumberOfGoals        = numberOfGoals,
                Penalties            = penalties,
                ShamPieceProbability = shanProbability,
                Position             = position
            };
            Message startMessage = new Message(MessageID.StartGame, agentID, startGamePayload);

            BufferBlock <Message> inputBuffer = new BufferBlock <Message>();

            inputBuffer.Post(startMessage);

            PlayerConfiguration configuration = GenerateSampleConfiguration();
            var player = new Player.Models.Player(configuration, inputBuffer, new TcpSocketClient <Message, Message>(logger), logger);

            // Act
            bool expectedisLeader = true;

            (int, int)expectedBoardSize = (boardSize.X, boardSize.Y);
            (int, int)expectedPosition  = (position.X, position.Y);

            await player.AcceptMessage(CancellationToken.None);

            var agentIDResult         = player.GetValue <Player.Models.Player, int>("id");
            var leaderIdResult        = player.LeaderId;
            var teamMatesResult       = player.TeamMatesIds;
            var isLeaderResult        = player.IsLeader;
            var teamResult            = player.Team;
            var boardSizeResult       = player.BoardSize;
            var penaltiesResult       = player.PenaltiesTimes;
            var positionResult        = player.Position;
            var enemiesResult         = player.EnemiesIds;
            var goalAreaSizeResult    = player.GoalAreaSize;
            var numOfPlayersResult    = player.NumberOfPlayers;
            var numOfPiecesResult     = player.NumberOfPieces;
            var numOfGoalsResult      = player.NumberOfGoals;
            var shamProbabilityResult = player.ShamPieceProbability;

            // Assert
            Assert.Equal(agentID, agentIDResult);
            Assert.Equal(leaderId, leaderIdResult);
            Assert.Equal(alliesId, teamMatesResult);
            Assert.Equal(expectedisLeader, isLeaderResult);
            Assert.Equal(teamId, teamResult);
            Assert.Equal(expectedBoardSize, boardSizeResult);
            Assert.True(penalties.AreAllPropertiesTheSame(penaltiesResult),
                        $"Penalties should be the same,\n expected: {penalties},\n actual {penaltiesResult}");
            Assert.Equal(expectedPosition, positionResult);
            Assert.Equal(enemiesId, enemiesResult);
            Assert.Equal(goalAreaSize, goalAreaSizeResult);
            Assert.True(numberOfPlayers.Allies == numOfPlayersResult.Allies &&
                        numberOfPlayers.Enemies == numOfPlayersResult.Enemies);
            Assert.Equal(numberOfPieces, numOfPiecesResult);
            Assert.Equal(numberOfGoals, numOfGoalsResult);
            Assert.Equal(shanProbability, shamProbabilityResult);
        }