Skip to main content

Implement server authoritative player statistics

Last updated on April 1, 2024

Overview

Server authoritative statistics are suitable for multiplayer competitive gaming, or if you have a dedicated game server running for your game. This will allow you to have a server authoritative action from your game server, instead of maintaining statistics updates from the game client.

Some example use cases are:

  • Maintaining ELO or MMR scores, from your dedicated server, when a match ends
  • Tracking statistics for an online MMO character

Goals

  • Learn how to utilize the AccelByte Unreal and Unity server SDK to manage authoritative statistics.

Prerequisites

Using Statistics from the Server

Once the statistics are configured in the Admin Portal, you can use it from the server directly, using the example explained in this section.

Note that you can also utilize multiple update methods that we support as explained in Tracking Player and Game Wide Global Statistics.

Updating Statistics from the Server

The update feature from the server works similarly to the client. You can also check the client side updates from Tracking Player and Game Wide Global Statistics.

To use the server SDK functionality, refer to the snippets below:

Unreal Engine
FServerApiClientPtr ApiServer = FMultiRegistry::GetServerApiClient();

FString UserId = "Player User Id";
FString AdditionalKey = " Your additional key";

FAccelByteModelsUpdateUserStatItemWithStatCode UserStatItem{};
UserStatItem.StatCode = " Your stat code ";
UserStatItem.UpdateStrategy = EAccelByteStatisticUpdateStrategy::OVERRIDE;
UserStatItem.Value = 100.0f;

TArray<FAccelByteModelsUpdateUserStatItemWithStatCode> BulkUpdateUserStatItems = { UserStatItem };

ApiServer->ServerStatistic.BulkUpdateUserStatItemValue(UserId
, AdditionalKey
, BulkUpdateUserStatItems
, THandler<TArray<FAccelByteModelsUpdateUserStatItemsResponse>>::CreateLambda([](TArray<FAccelByteModelsUpdateUserStatItemsResponse> Result)
{
// Do something if BulkUpdateUserStatItemValue been successful
})
, FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
// Do something if BulkUpdateUserStatItemValue has an error
}));

Retrieving User Statistics from the Server

Retrieving user statistics from the server also works similarly with the client. This is useful if you want to implement additional logic, calculation or processing from the server.

To use the server SDK functionality, refer to the snippets below:

Unreal Engine
FServerApiClientPtr ApiServer = FMultiRegistry::GetServerApiClient();

FString UserId = "Player User Id";
TArray<FString> StatCodes = { "Stat Code 1", "Stat Code 2" };
TArray<FString> Tags = { "Tag 1", "Tag 2", "Tag 3" };

ApiServer->ServerStatistic.GetUserStatItems(UserId
, StatCodes
, Tags
, THandler<FAccelByteModelsUserStatItemPagingSlicedResult>::CreateLambda([](FAccelByteModelsUserStatItemPagingSlicedResult Result)
{
// Do something if GetUserStatItems been successful
})
, FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
// Do something if GetUserStatItems has an error
}));