Example #1
0
        public void NoGapsInNewTilesTest()
        {
            Game game = GetGameWithSingleVerticalWord();
            Move move = new Move(game, new List <TilePlacement>
            {
                new TilePlacement(0, 5, 'P'),
                new TilePlacement(3, 5, 'T')
            });

            Assert.False(move.IsValidMove(out string error));
            Assert.Equal("Invalid tile placement", error);

            move.AddPlacement(new TilePlacement(1, 5, 'A'));
            Assert.True(move.IsValidMove(out _));
        }
Example #2
0
        public async Task <ActionResult <MoveResultDto> > Post(List <TilePlacement> placements, int id)
        {
            var      userId   = User.FindFirst(System.Security.Claims.ClaimTypes.NameIdentifier).Value;
            GameData gameData = await context.Games.Include(g => g.LastMoveTiles).SingleAsync(g => g.GameId == id);

            if (gameData.Player1Id != userId && gameData.Player2Id != userId)
            {
                return(Forbid());
            }

            Game game = await context.Games.Where(g => g.GameId == id).ToGames(context, mapper).SingleAsync();

            game.WordChecker = wordCheckerFactory.GetWordChecker();

            var move = new Move(game, userId);

            foreach (var placement in placements)
            {
                move.AddPlacement(placement);
            }

            int score = move.GetScore(out string error);

            if (!string.IsNullOrEmpty(error))
            {
                return(UnprocessableEntity(new MoveResultDto(error)));
            }

            var badWords = move.InvalidWords().ToList();

            if (badWords.Count() != 0)
            {
                var result = new MoveResultDto("The following words are not allowed: " + string.Join(", ", badWords));
                result.InvalidWords = badWords;
                return(UnprocessableEntity(result));
            }

            move.Play();

            mapper.Map(game, gameData);

            await context.SaveChangesAsync();

            var otherUser = gameData.Player1Id == userId ? gameData.Player2Id : gameData.Player1Id;
            await hubContext.Clients.All.SendAsync("ReceiveMove", otherUser, id);

            return(Ok(new MoveResultDto(game.ToDto(userId))));
        }