メインコンテンツまでスキップ

ショップアイテムのローテーション入門

Last updated on October 23, 2024

Overview

This article walks you through how to modify the Extend Override app template for rotating shop item and transform it into your own app that fits your requirements.

Contract functions

There are two functions on the contract as shown in the snippet below.

service SectionService {
/**
GetRotationItems: get current rotation items, this method will be called by rotation type is CUSTOM
*/
rpc GetRotationItems(GetRotationItemsRequest) returns (GetRotationItemsResponse);

/**
Backfill method trigger condition:
1. Rotation type is FIXED_PERIOD
2. Bulkfill type is CUSTOM
3. User already owned any one of current rotation items.
*/
rpc Backfill(BackfillRequest) returns (BackfillResponse);
}

GetRotationItems

This method will be called by the AGS platform Commerce service for a store display section with rotation type CUSTOM. The function is called when users are trying to fetch active display sections. The developer can implement logic that determines what to show for that specified in-game store section for the particular user's request.

In this example, we'll implement the logic for GetRotationItems to return different items that will be rotated every hour.

In the app template, the following function can be found in src/AccelByte.PluginArch.ItemRotation.Demo.Server/Services/SectionFunctionService.cs.

public override Task<GetRotationItemsResponse> GetRotationItems(GetRotationItemsRequest request, ServerCallContext context)
{
List<SectionItemObject> items = new List<SectionItemObject>(request.SectionObject.Items);
float inputCount = items.Count;


float currentPoint = DateTime.Now.Hour;
int selectedIndex = (int)Math.Floor((inputCount / _UpperLimit) * currentPoint);

SectionItemObject selectedItem = items[selectedIndex];

GetRotationItemsResponse response = new GetRotationItemsResponse();
response.ExpiredAt = 0;
response.Items.Add(selectedItem);

return Task.FromResult(response);
}

Backfill

Backfill is called when the section rotation type is FIXED_PERIOD, the backfill type is CUSTOM, and there's already an owned item in the current item rotation. One of the use cases for this is you can implement the replacement logic for those owned items.

In this example, we'll replace items that are already owned by a new random item ID in its current index.

In the app template, the following function can be found in src/AccelByte.PluginArch.ItemRotation.Demo.Server/Services/SectionFunctionService.cs.

public override Task<BackfillResponse> Backfill(BackfillRequest request, ServerCallContext context)
{
BackfillResponse response = new BackfillResponse();

foreach (var item in request.Items)
{
if (item.Owned)
{
BackfilledItemObject newItem = new BackfilledItemObject()
{
ItemId = Guid.NewGuid().ToString().Replace("-", ""),
Index = item.Index
};
response.BackfilledItems.Add(newItem);
}
}

return Task.FromResult(response);
}