Implement server authoritative player statistics
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
- You are familiar with statistics configuration
- Access to the AccelByte admin portal
- Access to the AccelByte Unreal or Unity SDK
- Access to AccelByte statistics API documentation for further reference
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
- Unity
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 is successful
})
, FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
// Do something if BulkUpdateUserStatItemValue has an error
}));
ServerStatistic serverStatistic = AccelByteSDK.GetServerRegistry().GetApi().GetStatistic();
string additionalKey = " Your additional key";
string userId = "Player User Id";
StatItemUpdate userStatItem = new StatItemUpdate
{
statCode = " Your stat code ",
updateStrategy = StatisticUpdateStrategy.OVERRIDE,
value = 100
};
StatItemUpdate[] bulkUpdateUserStatItems = { userStatItem };
serverStatistic.UpdateUserStatItems(userId, additionalKey, bulkUpdateUserStatItems, result =>
{
if (result.IsError)
{
// Do something if UpdateUserStatItems an error
Debug.Log($"Error UpdateUserStatItems, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if UpdateUserStatItems is successful
});
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
- Unity
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 is successful
})
, FErrorHandler::CreateLambda([](int32 ErrorCode, FString ErrorMessage)
{
// Do something if GetUserStatItems has an error
}));
ServerStatistic serverStatistic = AccelByteSDK.GetServerRegistry().GetApi().GetStatistic();
string userId = "Player User Id";
string[] statCodes = { "Stat Code 1", "Stat Code 2" };
string[] tags = { "Tag 1", "Tag 2", "Tag 3" };
serverStatistic.GetUserStatItems(userId, statCodes, tags, result =>
{
if (result.IsError)
{
// Do something if GetUserStatItems an error
Debug.Log($"Error GetUserStatItems, Error Code: {result.Error.Code} Error Message: {result.Error.Message}");
return;
}
// Do something if GetUserStatItems is successful
});