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

ルートボックスロール関数入門

Last updated on May 21, 2025

Overview

This article explains the API contract (Protobuf) used in the Extend Override app for loot box roll.

service LootBox {
rpc RollLootBoxRewards(RollLootBoxRewardsRequest) returns (RollLootBoxRewardsResponse);
}

API Contract

RollLootBoxRewards

RollLootBoxRewards is called by AccelByte Gaming Services (AGS) when a player consumes a loot box, and it is used for implementing the logic to decide which reward items will be given to a specified player from a given loot box rewards list.

In this example, we are generating as many reward item results in the requested quantity.

First, we select rewards randomly based on the total reward weight sum. The bigger the weight, the higher probability for the reward to be selected. We will also get a random item from the selected reward. Finally, we add that item to the final results as the response.

In the app, the following function can be found in src/AccelByte.PluginArch.LootBox.Demo.Server/Services/LootboxFunctionService.cs.

public override Task<RollLootBoxRewardsResponse> RollLootBoxRewards(RollLootBoxRewardsRequest request, ServerCallContext context)
{
var rewards = request.ItemInfo.LootBoxRewards;

int rewardWeightSum = 0;
foreach (var reward in rewards)
rewardWeightSum += reward.Weight;

Random rand = new Random();

List<RewardObject> result = new List<RewardObject>();
for (int i = 0; i < request.Quantity; i++)
{
int selectedIdx = 0;
for (double r = rand.NextDouble() * rewardWeightSum; selectedIdx < rewards.Count - 1; selectedIdx++)
{
r -= rewards[selectedIdx].Weight;
if (r <= 0.0)
break;
}

var selectedReward = rewards[selectedIdx];
int itemCount = selectedReward.Items.Count;

int selectedItemIdx = (int)Math.Round(rand.NextDouble() * (double)(itemCount - 1));
BoxItemObject selectedItem = selectedReward.Items[selectedItemIdx];

var rewardObject = new RewardObject()
{
ItemId = selectedItem.ItemId,
ItemSku = selectedItem.ItemSku,
Count = selectedItem.Count
};
result.Add(rewardObject);
}

RollLootBoxRewardsResponse response = new RollLootBoxRewardsResponse();
response.Rewards.AddRange(result);

return Task.FromResult(response);
}
備考

You could find more information about gRPC request handling here.