Skip to main content

Introduction to the loot box roll function

Last updated on December 18, 2023
note

Extend is in Open Beta for AGS Premium Clients! This means Extend available for you to try on your development environment. You can submit your feedback through this link.

note

Extend is currently available for AGS Starter Closed Beta partners only.

Overview

AccelByte Gaming Services (AGS) has the capability to customize the behavior of the loot box roll function via Extend. In this guide, we will present the contract of the customization with an example of how to implement the roll function that will return a random reward item.

There's only one function on the contract. It is shown in the snippet below, which is a unary function called RollLootBoxRewards:

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

RollLootBoxRewards

RollLootBoxRewards is called 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'll generate 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.

Code example:

async def RollLootBoxRewards(self, request: RollLootBoxRewardsRequest, context):
self.log_payload(f'{self.RollLootBoxRewards.__name__} request: %s', request)
final_items: List[RewardObject] = []
rewards: List[LootBoxItemInfo.LootBoxRewardObject] = request.itemInfo.lootBoxRewards
reward_weight_sum: int = 0
reward_weight_sum = sum(reward.weight for reward in rewards)
for i in range(request.quantity):
for sel_idx in range(len(rewards)):
r = random.random() * reward_weight_sum
r -= rewards[sel_idx].weight
if r <= 0.0:
break
sel_reward: LootBoxItemInfo.LootBoxRewardObject = rewards[sel_idx]
item_count: int = len(sel_reward.items)
sel_item_idx: int = random.randint(0, item_count-1)
sel_item: BoxItemObject = sel_reward.items[sel_item_idx]
reward_item: RewardObject = RewardObject(
itemId=sel_item.itemId,
itemSku=sel_item.itemSku,
count=sel_item.count,
)
final_items.append(reward_item)
response: RollLootBoxRewardsResponse = RollLootBoxRewardsResponse(
rewards=final_items
)
self.log_payload(f'{self.RollLootBoxRewards.__name__} response: %s', response)
return response