Search Unity

  1. We are migrating the Unity Forums to Unity Discussions by the end of July. Read our announcement for more information and let us know if you have any questions.
    Dismiss Notice
  2. Dismiss Notice

Question Cloud Code + Game Server Hosting to do Allocation Management

Discussion in 'Cloud Code' started by ANLevant, Apr 11, 2024.

  1. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    32
    So, I 'm making a card game with a special focus on PVP Competitive Gameplay. I've built a ranked system using matchmaker and all that, but now I'm in what the community really is looking for: Tournaments.
    I'm trying to make the process streamlined for users, so they see a list of tournaments, join said tournaments, and then can monitor their progress, and join matches when their time to play comes.
    In another part of the Game, I've already developped another way to match with players through lobbies. Players can make a match with custom rules (Ranked rules are always fixed / change each 24 hours), so the way I'm doing this is: a player chooses a custom ruleset, and this information is stored ina lobby that the player then joins. If said lobby has two players in it then the GAME CLIENT (I know, this is bad) creates an allocation for those two players and they join to play the custom game.

    I'm planning on using this very same ystem to make tournament matches, but the injected rules will be injected by the tournament manager (Which I am developing with Cloud Code), instead of the game client,

    As I'm sure yo u are aware, creating allocations on a gameclient creates a bunch of issues, such as exposing my API keys in the client and allowing the client code to have an entry point in which it can freely create allocations, where the code to be cracked open and hacked by someone.

    For this reason I decided to go with the approach of letting Cloud Code manage Allocations along side with the standard Matchmaker. My question is: Do I have to use raw .Net to make API calls to the admin API and create the allocation, thus manually storing the API token inside my cloud code? Oris there a stub inside CloucCode.Apis nugget packages that would allow me to seamlessly do this in a much cleaner and faster way?. By the way, I ened to do this using lobby because a list of joinable tournament games musst be generated later for tournaments that are swiss rounds / round robin, so a work around using MatchMaker would be a possibility, but by no means ideal for this problem

    I have been looking for this answer inside the doc for Cloud code but havent'found a way of creating allocations manually through Cloud Code. Thanks!
     
  2. Unity_DanM

    Unity_DanM

    Unity Technologies

    Joined:
    Mar 28, 2023
    Posts:
    11
    Hi ANLevant,

    Currently yes, to create an allocation you'll need to manually make an HTTP request to Multiplay as we don't have a C# SDK for that service yet. However, you shouldn't need to hardcode the API token or service account details: you can use the injected IExecutionContext.ServiceToken

    Hope that helps; I'll try to find some time to add Multiplay SDK support soon and will update this thread when it's available
     
    ANLevant and samg-unity like this.
  3. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    154
    ANLevant likes this.
  4. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    32
    It did help! thank you a lot to you too!
     
  5. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    32
    It's always something with cloud code man.

    I went ahead and used httpClient to create an allocation and now I'm getting this error:

    Code (CSharp):
    1. HttpException`1: (422) HTTP/1.1 422 Unprocessable Entity
    2.   at Unity.Services.CloudCode.Internal.Http.ResponseHandler.HandleAsyncResponse (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) [0x00089] in .\Library\PackageCache\com.unity.services.cloudcode@2.5.1\Runtime\com.unity.services.cloudcode.internal\Http\ResponseHandler.cs:122
    3.   at Unity.Services.CloudCode.Internal.Http.ResponseHandler.HandleAsyncResponse[T] (Unity.Services.CloudCode.Internal.Http.HttpClientResponse response, System.Collections.Generic.Dictionary`2[TKey,TValue] statusCodeToTypeMap) [0x00001] in .\Library\PackageCache\com.unity.services.cloudcode@2.5.1\Runtime\com.unity.services.cloudcode.internal\Http\ResponseHandler.cs:226
    4.   at Unity.Services.CloudCode.Internal.Apis.CloudCode.CloudCodeApiClient.RunModuleAsync (Unity.Services.CloudCode.Internal.CloudCode.RunModuleRequest request, Unity.Services.CloudCode.Internal.Configuration operationConfiguration) [0x001fd] in .\Library\PackageCache\com.unity.services.cloudcode@2.5.1\Runtime\com.unity.services.cloudcode.internal\Apis\CloudCodeApi.cs:137
    5.   at Unity.Services.CloudCode.CloudCodeInternal.GetModuleResponseAsync (System.String module, System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) [0x00082] in .\Library\PackageCache\com.unity.services.cloudcode@2.5.1\Runtime\CloudCode.cs:243
    6.   at Unity.Services.CloudCode.CloudCodeInternal.GetRunModuleScriptResponse (System.String module, System.String function, System.Collections.Generic.Dictionary`2[TKey,TValue] args) [0x00047] in .\Library\PackageCache\com.unity.services.cloudcode@2.5.1\Runtime\CloudCode.cs:137
    7. Rethrow as CloudCodeException: ScriptError
    8. (422) HTTP/1.1 422 Unprocessable Entity
    9. Unprocessable Entity
    10. Invocation Error
    11. ScriptRunner.Exceptions.InvalidModuleException: Could not load assemblies for module TripleWarringModule. Ensure that the zip file includes all necessary DLLs and is compiled for linux-x64
    I am using the unity editor to zip and upload the library as usual. And before adding this specific peace of code, everything was working.

    My code:

    Code (CSharp):
    1. [CloudCodeFunction("CreateAllocationForBracketGame")]
    2.     public async Task<string> CreateAllocationForBracketGame(IExecutionContext context, IGameApiClient gameApiClient, string code, int nodeId, string playerId)
    3.     {
    4.         _logger.LogInformation("Creating allocation for bracket game: " + code + ", for node: " + nodeId+ ", for player: "+playerId);
    5.         TournamentDTO tournamentDto = await GetTournamentByCode(context, gameApiClient, code);
    6.         //TournamentNode tournamentNode = SearchForTournamentNode(tournamentDto, nodeId);
    7.      
    8.         HttpClient client = new HttpClient();
    9.         client.DefaultRequestHeaders.Add("Authorization", "Bearer "+ context.ServiceToken);
    10.         string allocationId = Guid.NewGuid().ToString();
    11.         string payload = "";
    12.         //string payload = JsonConvert.SerializeObject(tournamentNode.rules);
    13.         string url = $"https://multiplay.services.api.unity.com/v1/allocations/projects/{context.ProjectId}/environments/{context.EnvironmentId}/fleets/{FLEET_ID}/allocations";
    14.      
    15.         string queueAllocationRequest = $"{{\"allocationId\":\"{allocationId}\",\"buildConfigurationId\":{BUILD_CONFIGURATION_ID},\"regionId\": \"{REGION_ID}\", \"payload\":\"{payload}\"}}";
    16.         var content = new StringContent(queueAllocationRequest, Encoding.UTF8, "application/json");
    17.      
    18.         HttpResponseMessage json = await client.PostAsync(url, content);
    19.         _logger.LogInformation("Answer to creating allocation: " + json);
    20.      
    21.         return json.ToString();
    22.     }

    BTW sorry if I sound frustrated, but I am
     
  6. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    32
    Alright fixed this. The issue was: I used test libraries because someone here recommended me to use build unit tests using NUnit, but this thing made the assembly bigger than 10 MB so it couldn'tbe packaged. I deleted the NuGet poackage from the project, but it turns out the deployment with unity editor Never executed a dotnet clean, thus old assemblies where expecting the test package to be there. And it was't
     
  7. GabKBelmonte

    GabKBelmonte

    Unity Technologies

    Joined:
    Dec 14, 2021
    Posts:
    154
    @ANLevant THanks for the feedback, will add a Rebuild option to the CloudCode Module from the editor to address this issue.

    On the other hand, I was not aware cloudcode had a upper limit of binary size.
    Could you please let me know what feedback, if any, the Deployment Window in the editor gave you when the size was too large?
     
    ANLevant likes this.
  8. ANLevant

    ANLevant

    Joined:
    Jun 10, 2014
    Posts:
    32
    Hello! sorry, i just read this. It has been some weeks, and I have pretty much deleted all the code that dependencies that caused the issues. But I had added NUnit + Cloud Code dependencies and Cloud Code APIs + .net core http client library
     
    GabKBelmonte likes this.