Esempio n. 1
0
        public async Task <IActionResult> Join([FromBody] JoinRoomDTO data)
        {
            var room = await _dbContext.Rooms.FirstOrDefaultAsync(r => r.Code == data.RoomCode);

            if (room == null)
            {
                return(NotFound(new { error = "The room doesn't exist" }));
            }

            var userId = HttpContext.User.FindFirst("UserId").Value;

            var userRoom = await _dbContext.UserRooms.FirstOrDefaultAsync(ur => ur.Room.Code == data.RoomCode && ur.User.Id.ToString() == userId);

            if (userRoom != null)
            {
                return(Ok(new { roomId = room.Id, roomCode = data.RoomCode, roomName = room.Name, description = room.Description, role = userRoom.Role, jiraDomain = room.JiraDomain }));
            }

            await _dbContext.UserRooms.AddAsync(new UserRoom
            {
                User = await _dbContext.Users.FirstAsync(u => u.Id.ToString() == userId),
                Room = room,
                Role = Role.player
            });

            await _dbContext.SaveChangesAsync();

            return(StatusCode(201, new { roomId = room.Id, roomCode = data.RoomCode, roomName = room.Name, description = room.Description, role = Role.player, jiraDomain = room.JiraDomain }));
        }
Esempio n. 2
0
        public async Task <PlayerInRoomDTO> JoinRoom(JoinRoomDTO joinRoomDTO)
        {
            var gameRoom = await _repository.GetById(joinRoomDTO.GameRoomId);

            var player = await _repositoryPlayer.GetById(joinRoomDTO.PlayerId);

            var previousGameRoomPlayer = await _repositoryGameRoomPlayer.Get(
                grp => (grp.PlayerId == player !.Id) && (grp.GameRoomId == joinRoomDTO.GameRoomId));

            if (previousGameRoomPlayer != null)
            {
                _logger.LogInformation("Player had previously join this room...");
                previousGameRoomPlayer.IsInRoom = true;
                await _repositoryGameRoomPlayer.Update(previousGameRoomPlayer);

                var alreadyJoinedRoomDTO = _mapper.Map <GameRoomPlayer, PlayerInRoomDTO>(previousGameRoomPlayer);
                return(alreadyJoinedRoomDTO);
            }

            _logger.LogInformation("First time player is joining this room...");
            var gameRoomPlayer = new GameRoomPlayer()
            {
                GameRoom = gameRoom !,
                Player   = player !,
                IsHost   = joinRoomDTO.IsHost,
                IsBanned = false,
                IsInRoom = true,
            };
            await _repositoryGameRoomPlayer.Save(gameRoomPlayer);

            var playerInRoomDTO = _mapper.Map <GameRoomPlayer, PlayerInRoomDTO>(gameRoomPlayer);

            return(playerInRoomDTO);
        }
        public async Task ReconnectEvent(string roomId, string userName, bool isScrumMaster)
        {
            String logText = "User " + userName + " (" + Context.ConnectionId + ") is trying to reconnect to room " + roomId + " as scrummaster: " + isScrumMaster;

            Trace.WriteLine(logText, "Reconnect");

            JoinRoomDTO joinRoomDTO = new JoinRoomDTO()
            {
                RoomId        = roomId,
                Name          = userName,
                IsScrumMaster = isScrumMaster
            };

            await JoinRoom(joinRoomDTO);
        }
Esempio n. 4
0
        public async Task <ActionResult <PlayerInRoomDTO> > JoinRoom(Guid gameRoomId, PlayerDTO playerDTO)
        {
            var joinRoomDTO = new JoinRoomDTO {
                GameRoomId = gameRoomId, PlayerId = playerDTO.PlayerId, IsHost = false
            };

            _logger.LogInformation("Start join room validation: {@joinRoomDTO}", joinRoomDTO);

            var validator        = new GameRoomPlayerValidator(_gameRoomServiceAsync, _playerServiceAsync);
            var validationResult = validator.Validate(joinRoomDTO);

            if (!validationResult.IsValid)
            {
                return(BadRequest(validationResult.Errors));
            }

            _logger.LogInformation("Validations were successfull, adding player to the room...");
            var playerInRoomDTO = await _gameRoomServiceAsync.JoinRoom(joinRoomDTO);

            return(StatusCode(200, playerInRoomDTO));
        }
        public async Task JoinRoom(JoinRoomDTO joinRoomDTO)
        {
            int     id;
            Boolean hasJoined = false;

            if (Int32.TryParse(joinRoomDTO.RoomId, out id))
            {
                string name = joinRoomDTO.Name;
                hasJoined = BusinessLogic.JoinRoom(name, id, Context.ConnectionId, joinRoomDTO.IsScrumMaster);
            }
            RoomJoinedDTO roomJoinedDTO = new RoomJoinedDTO
            {
                Success       = hasJoined,
                RoomId        = id,
                UserName      = joinRoomDTO.Name,
                IsScrumMaster = joinRoomDTO.IsScrumMaster
            };

            Clients.Caller.roomJoined(roomJoinedDTO);

            if (hasJoined)
            {
                await Groups.Add(Context.ConnectionId, id.ToString());

                ICollection <User> participants = BusinessLogic.GetParticipants(id);
                Clients.Group(id.ToString()).getParticipants(participants);

                String logText = "Room joined with id " + roomJoinedDTO.RoomId + " created by " + roomJoinedDTO.UserName + "(" + Context.ConnectionId + ")";
                Trace.WriteLine(logText, "JoinRoom");
            }
            else
            {
                String logText = "User " + roomJoinedDTO.UserName + "(" + Context.ConnectionId + ") tried to join room " + roomJoinedDTO.RoomId + " but failed.";
                Trace.WriteLine(logText, "JoinRoom");
            }
        }