Auctions #

Read more about the Auctions system in Hiro here.

Initializing the auctions system #

The auctions system relies on the Nakama System and an ILogger, both must be passed in as dependencies via the constructor.

1
2
var auctionsSystem = new AuctionsSystem(logger, nakamaSystem);
systems.Add(auctionsSystem);

Subscribing to changes in the auctions system #

You can listen for changes in the auctions system so that you can respond appropriately, such as updating the UI, by implementing the IObserver pattern, or use the SystemObserver<T> type which handles it for you.

1
2
3
4
5
var disposer = SystemObserver<AuctionsSystem>.Create(auctionsSystem, system => {
    Instance.Logger.Info($"System updated.");

    // Update UI elements etc as necessary here...
});

Refreshing the auctions system #

To ensure the auctions system has the latest information from Nakama you can refresh it.

1
await auctionsSystem.RefreshAsync();

Creating a New Auction #

To create a new auction, use the CreateAsync function.

1
2
3
4
5
6
var itemInstanceIds = new List<string> { "item_instance_001", "item_instance_002" };
string auctionId = "template_auction_001";
string conditionId = "condition_001";

var auction = await auctionsSystem.CreateAsync(auctionId, conditionId, itemInstanceIds);
Console.WriteLine($"Auction created with ID: {auction.Id}, Start Time: {auction.StartTimeSecDecoded}");

Placing a Bid on an Auction #

You can place a bid on an auction using the BidAsync function.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
var currencies = new Dictionary<string, long>
{
    { "gold", 1000 }
};

string auctionId = "auction_instance_123";
string version = "1.0";

var auction = await auctionsSystem.BidAsync(auctionId, version, currencies);
Console.WriteLine($"Bid placed on auction {auction.Id}. Current bid: {auction.BidDecoded}");

Canceling an Auction #

If an auction needs to be canceled before it ends, the CancelAsync method can be used.

1
2
3
4
string auctionId = "auction_instance_123";

var canceledAuction = await auctionsSystem.CancelAsync(auctionId);
Console.WriteLine($"Auction {canceledAuction.Auction.Id} has been canceled.");

Claiming a Winning Bid #

Once an auction has ended, the winning bidder can claim their item using ClaimBidAsync.

1
2
3
4
string auctionId = "auction_instance_123";

var claimBid = await auctionsSystem.ClaimBidAsync(auctionId);
Console.WriteLine($"Claimed item from auction {claimBid.Auction.Id}. Winning bid: {claimBid.BidAmount}");

Listing Available Auctions #

You can list the currently available auctions using the ListAsync function.

1
2
3
4
5
6
7
8
9
string query = "active";
var sortCriteria = new List<string> { "end_time" };
int limit = 10;

var auctionList = await auctionsSystem.ListAsync(query, sortCriteria, limit);
foreach (var auction in auctionList.Auctions)
{
    Console.WriteLine($"Auction ID: {auction.Id}, Current Bid: {auction.BidDecoded}");
}