/// <summary> /// Fired when a user disconnects. /// </summary> /// <returns></returns> public Task Disconnect() { // 1: Get the user that disconnected // 2: Remove him from the list of connected users // 3: If the user was playing, notify the opponent that the user disconnected // 4: Re-queue the opponent in the waiting list // 5: Remove the room from the list User user = _userRepository.ConnectedUsers.Where(u => u.Id.Equals(Context.ConnectionId)).FirstOrDefault(); if (user != null) { _userRepository.RemoveUser(user); _userRepository.RemoveFromWaitingList(user); PlayRoom room = _roomRepository.Rooms.Where(r => (r.Player1.Id.Equals(user.Id) || r.Player2.Id.Equals(user.Id))).FirstOrDefault(); // if the user was in the middle of a match if (room != null) { var opponent = room.Player1.Id.Equals(user.Id) ? room.Player2 : room.Player1; _userRepository.AddToWaitingList(opponent); _roomRepository.Remove(room); Engine.RemoveGame(room.Id); return(Clients[opponent.Id].opponentLeft()); } } return(null); }