Esempio n. 1
0
        public async Task <IActionResult> AddWordToBoard(Guid id, [FromBody] WordEntry addWord, CancellationToken cancellationToken)
        {
            if (id != Guid.Empty && ModelState.IsValid)
            {
                BoardState board = await context.BoardStates.FindAsync(new object[] { id }, cancellationToken);

                if (board != null && DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds - board.TimeCreated < maxTimeInMs)
                {
                    if (WordIsValid(addWord, board) && !(await WordAlreadyOnBoard(addWord, board, cancellationToken)) && OrderIsValid(addWord))
                    {
                        Guid substringId = await SubstringOnBoard(addWord, board, cancellationToken);

                        if (substringId != Guid.Empty)
                        {
                            WordEntry wordEntry =
                                await context.Words.FindAsync(new object[] { id = substringId }, cancellationToken);

                            wordEntry.Word        = addWord.Word;
                            wordEntry.LetterOrder = addWord.LetterOrder;
                            board.Score           = await CalculateScore(board, cancellationToken);

                            await context.SaveChangesAsync(cancellationToken);

                            board.Score = await CalculateScore(board, cancellationToken);

                            await context.SaveChangesAsync(cancellationToken);

                            return(Ok(wordEntry));
                        }
                        else
                        {
                            addWord.BoardId = board.StateId;
                            var wordEntity = await context.Words.AddAsync(addWord, cancellationToken);

                            await context.SaveChangesAsync(cancellationToken);

                            board.Score = await CalculateScore(board, cancellationToken);

                            await context.SaveChangesAsync(cancellationToken);

                            return(Ok(wordEntity.Entity));
                        }
                    }
                    else
                    {
                        return(BadRequest("the given word is invalid"));
                    }
                }

                if (board != null)
                {
                    return(StatusCode(410, "Out of time for this board"));
                }
                return(NotFound("The requested board could not be found"));
            }

            return(BadRequest("The format of the request is invalid"));
        }
Esempio n. 2
0
        public async Task <IActionResult> GetNewBoard(CancellationToken cancellationToken = default(CancellationToken))
        {
            BoardState board = new BoardState
            {
                Board       = BoardService.GetNewBoardLayout(),
                TimeCreated = DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds
            };

            await context.BoardStates.AddAsync(board, cancellationToken);

            await context.SaveChangesAsync(cancellationToken);

            return(Ok(board));
        }
Esempio n. 3
0
        public async Task <IActionResult> PostHighScore([FromBody] Highscore highscore, Guid id, CancellationToken cancellationToken = default(CancellationToken))
        {
            if (ModelState.IsValid)
            {
                var highScoreEntry = await _context.Highscores.AddAsync(highscore, cancellationToken);

                await _context.SaveChangesAsync(cancellationToken);

                return(Ok(highScoreEntry.Entity));
            }
            else
            {
                return(BadRequest());
            }
        }