/// <summary> /// Loads player profile /// </summary> /// <param name="successCallback"></param> public void LoadPlayerProfile(string username, SuccessCallback successCallback) { if (roomPlayersByUsername.ContainsKey(username)) { MirrorRoomPlayer player = roomPlayersByUsername[username]; Msf.Server.Profiles.FillProfileValues(player.Profile, (isSuccess, error) => { if (!isSuccess) { logger.Error("Room server cannot retrieve player profile from master server"); successCallback?.Invoke(false, "Room server cannot retrieve player profile from master server"); if (disconnectIfProfileFailed) { MsfTimer.WaitForSeconds(1f, () => player.MirrorPeer.Disconnect()); } return; } logger.Debug($"Profile of player {username} is successfully loaded. Player info: {player}"); successCallback?.Invoke(true, string.Empty); }); } }
/// <summary> /// Finalize player joining to server room /// </summary> /// <param name="conn"></param> protected virtual void FinalizePlayerJoining(NetworkConnection conn) { if (roomPlayersByMirrorPeerId.ContainsKey(conn.connectionId)) { MirrorRoomPlayer player = roomPlayersByMirrorPeerId[conn.connectionId]; logger.Debug($"Client {conn.connectionId} has become a player of this room. Congratulations to {player.Username}"); conn.Send(new ValidateRoomAccessResultMessage() { Error = string.Empty, Status = ResponseStatus.Success }); // Inform subscribers about this player OnPlayerJoinedRoomEvent?.Invoke(player); } }
/// <summary> /// Fires when client that wants to connect to this room made request to validate the access token /// </summary> /// <param name="conn"></param> /// <param name="msg"></param> protected virtual void ValidateRoomAccessRequestHandler(NetworkConnection conn, ValidateRoomAccessRequestMessage msg) { logger.Debug($"Room client {conn.connectionId} asked to validate access token [{msg.Token}]"); // Triying to validate given token Msf.Server.Rooms.ValidateAccess(CurrentRoomController.RoomId, msg.Token, (usernameAndPeerId, error) => { // If token is not valid if (usernameAndPeerId == null) { logger.Error(error); conn.Send(new ValidateRoomAccessResultMessage() { Error = error, Status = ResponseStatus.Failed }); MsfTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } logger.Debug($"Client {conn.connectionId} is successfully validated"); logger.Debug("Getting his account info..."); Msf.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (accountInfo, accountError) => { if (accountInfo == null) { logger.Error(accountError); conn.Send(new ValidateRoomAccessResultMessage() { Error = accountError, Status = ResponseStatus.Error }); MsfTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } // Create new room player var player = new MirrorRoomPlayer(usernameAndPeerId.PeerId, conn, accountInfo.Username, accountInfo.CustomOptions) { Profile = ProfileFactory(accountInfo.Username) }; // Add this player to filtered lists roomPlayersByMsfPeerId.Add(usernameAndPeerId.PeerId, player); roomPlayersByMirrorPeerId.Add(conn.connectionId, player); roomPlayersByUsername.Add(accountInfo.Username, player); // If server is required user profile if (autoLoadUserProfile) { LoadPlayerProfile(accountInfo.Username, (isLoadProfileSuccess, loadProfileError) => { if (isLoadProfileSuccess) { FinalizePlayerJoining(conn); } }); } else { FinalizePlayerJoining(conn); } }); }); }
/// <summary> /// Fires when client that wants to connect to this room made request to validate the access token /// </summary> /// <param name="conn"></param> /// <param name="msg"></param> private void ValidateRoomAccessRequestHandler(NetworkConnection conn, ValidateRoomAccessRequestMessage msg) { logger.Debug($"Room client {conn.connectionId} asked to validate access token [{msg.Token}]"); // Triying to validate given token Msf.Server.Rooms.ValidateAccess(CurrentRoomController.RoomId, msg.Token, (usernameAndPeerId, error) => { // If token is not valid if (usernameAndPeerId == null) { logger.Error(error); conn.Send(new RoomAccessValidationResultMessage() { Error = error, Status = ResponseStatus.Failed }); MsfTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } logger.Debug($"Client {conn.connectionId} is successfully validated"); logger.Debug("Getting his account info..."); Msf.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (accountInfo, accountError) => { if (accountInfo == null) { logger.Error(accountError); conn.Send(new RoomAccessValidationResultMessage() { Error = accountError, Status = ResponseStatus.Error }); MsfTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } logger.Debug($"Client {conn.connectionId} has become a player of this room. Congratulations to {accountInfo.Username}"); conn.Send(new RoomAccessValidationResultMessage() { Error = string.Empty, Status = ResponseStatus.Success }); // Create new room player var player = new MirrorRoomPlayer(usernameAndPeerId.PeerId, conn, accountInfo.Username, accountInfo.CustomOptions); // Add this player to filtered lists roomPlayersByMsfPeerId.Add(usernameAndPeerId.PeerId, player); roomPlayersByMirrorPeerId.Add(conn.connectionId, player); roomPlayersByUsername.Add(accountInfo.Username, player); // Inform subscribers about this player OnPlayerJoinedRoomEvent?.Invoke(player); }); }); }