Пример #1
0
        private static ChessBoardModel UpdatePosition(ChessBoardModel board, MoveJsonEntity item)
        {
            if (item.To == null || item.From == null || item.Player == (Data.Player) 7)
            {
                return(board);
            }
            var p = new Position()
            {
                Column = item.From.Column,
                Row    = item.From.Row
            };
            var pTarget = new Position
            {
                Column = item.To.Column,
                Row    = item.To.Row
            };
            var toChange = board.Board[p];
            //if (board.LastMove != default && (int)toChange.player != (int)item.Player)
            //{
            //    throw new Exception("Cannot change non player pawn");
            //}
            var dict = new Dictionary <Position, BasePawn>(board.Board);

            dict.Remove(p);
            dict[pTarget] = toChange;
            board         = new ChessBoardModel()
            {
                State         = board.State,
                PlayerOneId   = board.PlayerOneId,
                PlayerOneName = board.PlayerOneName,
                PlayerTwoId   = board.PlayerTwoId,
                PlayerTwoName = board.PlayerTwoName,
                LastMove      = DateTime.Now,
                Started       = board.Started,
                Board         = dict,
                CurrentPlayer = (Player)(int)(item?.Player ?? Data.Player.Two) == Player.One ? Player.Two : Player.One
            };
            return(board);
        }
Пример #2
0
        public async Task Move(Client client, Room room, PawnPosition @from, PawnPosition target)
        {
            var clientRoomsResult = await this.RoomService.Status(client);

            var clientRooms = clientRoomsResult.Match(x => x, e => throw e);

            if (clientRooms.FirstOrDefault(r => r.Name == room.Name) is Room r)
            {
                var actualGame = this.ApplicationDbContext.Games.FirstOrDefault(x => x.State == GameState.InPlay && x.RoomId == room.Id);
                if (actualGame == null)
                {
                    var items = new List <MoveJsonEntity>();
                    actualGame = new GameEntity
                    {
                        PlayerOneId = client.Id,
                        State       = GameState.InPlay,
                        RoomId      = r.Id,
                        Moves       = items
                    };
                    this.ApplicationDbContext.Add(actualGame);
                    try
                    {
                        await this.ApplicationDbContext.SaveChangesAsync();
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                }
                var player = actualGame.PlayerOneId == client.Id ? Player.One : actualGame.PlayerTwoId == client.Id ? Player.Two : throw new Exception("Player can be matched");
                var board  = Map(actualGame);
                var mje    = new MoveJsonEntity
                {
                    From = new PositionEntity
                    {
                        Column = @from.Col,
                        Row    = @from.Row
                    },
                    To = new PositionEntity
                    {
                        Column = target.Col,
                        Row    = target.Row
                    },
                    Player = (Data.Player)((int)player)
                };
                board = UpdatePosition(board, mje);
                actualGame.Moves.Add(mje);
                actualGame.Moves = actualGame.Moves;

                if (board.Board.Values.Count(x => x is King) < 2)
                {
                    actualGame.State = GameState.Finished;
                }

                await this.ApplicationDbContext.SaveChangesAsync();
            }
            else
            {
                throw new Exception("Client hadn't subscribed current room.");
            }
        }