Exemple #1
0
        public async Task <IActionResult> JoinRoom([FromBody] RoomJoin roomJoin)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            var gameRoom = _gameRooms.GameRoomsList.Find(r => r.RoomID == roomJoin.RoomId);

            if (gameRoom == null)
            {
                return(NotFound("Game room could not be found"));
            }
            if (gameRoom.IsRoomFull())
            {
                return(NotFound("Game room is full"));
            }

            Battleships_MBernackiUser user = await GetCurrentUserAsync();

            int playerRoomKey = gameRoom.AddPlayer(user.UserName, user.Id);

            return(Ok(new JoinedRoomInfo()
            {
                PlayerRoomKey = playerRoomKey,
                RoomID = gameRoom.RoomID,
                OponentName = gameRoom.PlayersNames[0],
                RoomName = gameRoom.RoomName,
                ShipsList = gameRoom.ShipList,
                MapSize = gameRoom.MapSize
            }));
        }
 internal virtual void OnRoomJoin(User user, Room room)
 {
     RoomJoin?.Invoke(user, room);
 }