Microservices - Currency

Create and deploy server-authoritative C# [CLCP-Microservices-04]

Beamable Microservices supports interactions with the Beamable Currency feature.

Example

Here are examples which cover common programming needs.

📘

Beamable SDK Examples

The following example code is available for download at GitHub.com/Beamable_Microservices_Examples

Calling MyCurrencyMicroservice

The key methods here return a bool to determine the success of the operation. As an alternative game makers can choose to return additional information such as the affected currencyId or the new currency amount:

private async void SetupBeamable()
{
    var beamContext = BeamContext.Default;
    await beamContext.OnReady;

    Debug.Log($"beamContext.PlayerId = {beamContext.PlayerId}");
    
    _myCurrencyMicroserviceClient = new MyCurrencyMicroserviceClient();

    // #1 - Call Microservice
    bool isSuccess1 = await _myCurrencyMicroserviceClient.AddToActiveCurrency();
    Debug.Log ($"AddToActiveCurrency() isSuccess = {isSuccess1}");
    
    // #2 - Call Microservice
    bool isSuccess2 = await _myCurrencyMicroserviceClient.RemoveFromActiveCurrency();
    Debug.Log ($"RemoveFromActiveCurrency() isSuccess = {isSuccess2}");
    
}

Implementing MyCurrencyMicroservice

Here 1 is added to the current currency amount.

❗️

Security

  • Notice that AddToActiveCurrency() accepts no arguments. This is more limiting but also more secure
  • Game makers may pass arguments to Microservice methods, but be wary of the security implications of an alternative such as AddToActiveCurrency(contentId, amountToAdd) which could be called repeated by malicious clients
[ClientCallable]
public async Task<bool> AddToActiveCurrency()
{
   // Add Value
   long amountDelta = 1;
  
   // Change the amount of currency
   bool isSuccess = false;
   string currencyId = await GetActiveCurrencyId();
   long amountOld = await GetActiveCurrencyValue();

   if (!string.IsNullOrEmpty(currencyId))
   {
      long amountNew = amountOld + amountDelta;
      await Services.Inventory.SetCurrency(currencyId, amountNew);

      // Validate
      long amountConfirmed = await Services.Inventory.GetCurrency(currencyId);
      isSuccess = amountConfirmed == amountNew;
   }

   return isSuccess;
}

Content Preloading

As of Beamable 1.8.0, every microservice instance downloads the entire content manifest before it starts accepting HTTP traffic. This feature can be disabled by disabling the EnableEagerContentLoading flag on the MicroserviceAttribute. This code snippet would create a microservice that would not pre-download content. Be aware that this means the first time the Microservice needs to fetch content can result in long request times.

[Microservice("example", EnableEagerContentLoading = false)]
public class Example : Microservice
{
}