Example #1
0
 /// <summary>
 /// Find color by name
 /// </summary>
 /// <param name="filePath"></param>
 /// <param name="name"></param>
 /// <returns></returns>
 public async Task <Color> FindAsync(string filePath, string name) =>
 await _colorRepository.FindAsync(filePath, name);
Example #2
0
        public async Task <Game> MakeMove(long gameId, string userId, string move)
        {
            var game = await _gameRepository.GetAsync(gameId);

            if (game == null)
            {
                return(game);
            }

            if (game.IsDone || game.IsWaiting)
            {
                return(game);
            }

            var user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                return(game);
            }

            var side = (await _sideRepository.GetGameSides(game))
                       .FirstOrDefault(x => x.UserId == user.Id);

            if (side == null)
            {
                return(game);
            }

            var fen       = game.Fen;
            var chessGame = new ChessGame(fen);
            var color     = await _colorRepository.FindAsync(side.ColorId);

            if (color.ToChar() != (char)chessGame.ActiveColor)
            {
                return(game);
            }

            var moveEntry = new Move
            {
                Fen      = game.Fen,
                Game     = game,
                MoveNext = move,
                User     = user
            };

            var moveNext = moveEntry.MoveNext;

            try
            {
                var squares       = MoveInputParser.ParseMove(moveNext);
                var nextChessGame = chessGame.Move(squares);

                ChangeGameState(nextChessGame, ref game, moveEntry, color);

                await _moveRepository.AddAsync(moveEntry);

                _gameRepository.Update(game);
                await _gameRepository.SaveChangesAsync();

                return(game);
            }

            catch (FormatException e)
            {
                Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                return(game);
            }

            catch (ChessGameException e)
            {
                Debug.WriteLine(e.Message + "\n" + e.StackTrace);
                return(game);
            }
        }