Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Bug 400 error occurred when updating lobby

Discussion in 'Lobby' started by kkm1010k, Nov 22, 2023.

  1. kkm1010k

    kkm1010k

    Joined:
    Mar 8, 2023
    Posts:
    3
    As the title says, a 400 error occurs when updating the lobby.
    here is my code

    Code (CSharp):
    1. public async Task UpdateLobbyDataAsync(Dictionary<string, string> data)
    2.     {
    3.         if (m_CurrentLobby == null)
    4.             return;
    5.  
    6.         Dictionary<string, DataObject> dataCurr = m_CurrentLobby.Data ?? new Dictionary<string, DataObject>();
    7.  
    8.         var shouldLock = false;
    9.         foreach (var dataNew in data)
    10.         {
    11.             DataObject dataObj = new DataObject(DataObject.VisibilityOptions.Public, dataNew.Value);
    12.             dataCurr[dataNew.Key] = dataObj;
    13.  
    14.             if (dataNew.Key == "LocalLobbyState")
    15.             {
    16.                 Enum.TryParse(dataNew.Value, out LobbyState lobbyState);
    17.                 shouldLock = lobbyState != LobbyState.Lobby;
    18.             }
    19.         }
    20.  
    21.         if (m_UpdateLobbyCooldown.TaskQueued)
    22.             return;
    23.         await m_UpdateLobbyCooldown.QueueUntilCooldown();
    24.  
    25.         UpdateLobbyOptions updateOptions = new UpdateLobbyOptions { Data = dataCurr, IsLocked = shouldLock };
    26.         m_CurrentLobby = await LobbyService.Instance.UpdateLobbyAsync(m_CurrentLobby.Id, updateOptions);
    27.      
    28.         LobbyConverters.RemoteToLocal(m_CurrentLobby, m_LocalLobby);
    29.     }
    30.  
    31.     public async Task UpdatePlayerDataAsync(Dictionary<string, string> data)
    32.     {
    33.         Debug.Log("update");
    34.         if (m_CurrentLobby == null)
    35.         {
    36.             Debug.LogError("lobby is null");
    37.             return;
    38.         }
    39.  
    40.         var playerId = AuthenticationService.Instance.PlayerId;
    41.         var dataCurr = new Dictionary<string, PlayerDataObject>();
    42.         foreach (var (key, value) in data)
    43.         {
    44.             PlayerDataObject dataObj = new PlayerDataObject(visibility: PlayerDataObject.VisibilityOptions.Member, value: value);
    45.             dataCurr[key] = dataObj;
    46.         }
    47.  
    48.         if (m_UpdatePlayerCooldown.TaskQueued)
    49.         {
    50.             Debug.LogError("too many request");
    51.             return;
    52.         }
    53.         await m_UpdatePlayerCooldown.QueueUntilCooldown();
    54.  
    55.         UpdatePlayerOptions updateOptions = new UpdatePlayerOptions
    56.         {
    57.             Data = dataCurr,
    58.             AllocationId = null,
    59.             ConnectionInfo = null
    60.         };
    61.         m_CurrentLobby = await LobbyService.Instance.UpdatePlayerAsync(m_CurrentLobby.Id, playerId, updateOptions);
    62.      
    63.         LobbyConverters.RemoteToLocal(m_CurrentLobby, m_LocalLobby);
    64.      
    65.         foreach (var plr in m_LocalLobby.LocalPlayers)
    66.         {
    67.             if (plr.ID.Value == AuthenticationService.Instance.PlayerId)
    68.             {
    69.                 m_LocalUser.DisplayName.Value = plr.DisplayName.Value;
    70.                 m_LocalUser.ID.Value = plr.ID.Value;
    71.                 m_LocalUser.IsHost.Value = plr.IsHost.Value;
    72.                 m_LocalUser.UserStatus.Value = plr.UserStatus.Value;
    73.                 m_LocalUser.Team.Value = plr.Team.Value;
    74.                 m_LocalUser.Index.Value = plr.Index.Value;
    75.                 m_LocalUser.LastUpdated = plr.LastUpdated;
    76.             }
    77.         }
    78.      
    79.         _spawnLocation.OnPlayerChanged();
    80.     }
    And this is the code that calls the function:

    Code (CSharp):
    1. m_LocalLobby.LocalLobbyState.Value = LobbyState.InGame;
    2.         await UpdateLobbyDataAsync(LobbyConverters.LocalToRemoteLobbyData(m_LocalLobby));
    and this is error

    HttpException`1: (400) HTTP/1.1 400 Bad Request
    Unity.Services.Lobbies.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.Lobbies.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/Http/ResponseHandler.cs:122)
    Unity.Services.Lobbies.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.Lobbies.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/Http/ResponseHandler.cs:226)
    Unity.Services.Lobbies.Apis.Lobby.LobbyApiClient.UpdateLobbyAsync (Unity.Services.Lobbies.Lobby.UpdateLobbyRequest request, Unity.Services.Lobbies.Configuration operationConfiguration) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/Apis/LobbyApi.cs:666)
    Unity.Services.Lobbies.Internal.WrappedLobbyService.TryCatchRequest[TRequest,TReturn] (System.String api, System.Func`3[T1,T2,TResult] func, TRequest request) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/SDK/WrappedLobbyService.cs:397)
    Rethrow as LobbyServiceException: request failed validation
    lobby data visibility cannot be changed
    Unity.Services.Lobbies.Internal.WrappedLobbyService.ResolveErrorWrapping (Unity.Services.Lobbies.LobbyExceptionReason reason, System.Exception exception) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/SDK/WrappedLobbyService.cs:459)
    Unity.Services.Lobbies.Internal.WrappedLobbyService.TryCatchRequest[TRequest,TReturn] (System.String api, System.Func`3[T1,T2,TResult] func, TRequest request) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/SDK/WrappedLobbyService.cs:402)
    Unity.Services.Lobbies.Internal.WrappedLobbyService.UpdateLobbyAsync (System.String lobbyId, Unity.Services.Lobbies.UpdateLobbyOptions options) (at ./Library/PackageCache/com.unity.services.lobby@1.1.0/Runtime/SDK/WrappedLobbyService.cs:278)
    NetworkController.UpdateLobbyDataAsync (System.Collections.Generic.Dictionary`2[TKey,TValue] data) (at Assets/Script/Lobby/NetworkController.cs:296)
    NetworkController.Game () (at Assets/Script/Lobby/NetworkController.cs:379)
    System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.<ThrowAsync>b__7_0 (System.Object state) (at <eef08f56e2e042f1b3027eca477293d9>:0)
    UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <56073df97ede4f769c2cc45d546d986d>:0)
    UnityEngine.UnitySynchronizationContext.Exec () (at <56073df97ede4f769c2cc45d546d986d>:0)
    UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <56073df97ede4f769c2cc45d546d986d>:0)
     
    Last edited: Nov 22, 2023