/// <summary> /// Loads player profile /// </summary> /// <param name="successCallback"></param> public void LoadPlayerProfile(string username, SuccessCallback successCallback) { if (roomPlayersByUsername.ContainsKey(username)) { RoomPlayer player = roomPlayersByUsername[username]; Mst.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) { MstTimer.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)) { RoomPlayer 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 Mst.Server.Rooms.ValidateAccess(RoomController.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 }); MstTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } logger.Debug($"Client {conn.connectionId} is successfully validated"); logger.Debug("Getting his account info..."); Mst.Server.Auth.GetPeerAccountInfo(usernameAndPeerId.PeerId, (accountInfo, accountError) => { if (accountInfo == null) { logger.Error(accountError); conn.Send(new ValidateRoomAccessResultMessage() { Error = accountError, Status = ResponseStatus.Error }); MstTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } // If we do not want guest users to play in our room if (!allowGuestUsers && accountInfo.Properties.Has(MstDictKeys.USER_IS_GUEST) && accountInfo.Properties.AsBool(MstDictKeys.USER_IS_GUEST)) { logger.Error("Guest users cannot play this room. Hands off..."); conn.Send(new ValidateRoomAccessResultMessage() { Error = "Guest users cannot play this room. Hands off...", Status = ResponseStatus.Error }); MstTimer.WaitForSeconds(1f, () => conn.Disconnect()); return; } // Create new room player var player = new RoomPlayer(usernameAndPeerId.PeerId, conn, accountInfo.UserId, accountInfo.Username, accountInfo.Properties) { Profile = ProfileFactory(accountInfo.UserId) }; // 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); } }); }); }