diff --git a/CHANGELOG.md b/CHANGELOG.md
index ec7f1c1..4169788 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -1,5 +1,13 @@
# Changelog
+## 0.3.0
+
+- `HibiraAuctionHouse`: follows an auction house published by
+ `@hibira-io/auction` (`createAuctionHouse()` on a Node server), so Unity and
+ browser players share it: open listings, recent sales, countdowns on the
+ server's clock, sale prices per item type, and a fresh snapshot on every
+ subscription. Bids go to your server.
+
## 0.2.1
- README: cover image and screenshots.
diff --git a/README.md b/README.md
index 988aa5b..f2e50d0 100644
--- a/README.md
+++ b/README.md
@@ -221,6 +221,34 @@ times here are in seconds.
Coming from Photon PUN 2? See
[Moving from Photon PUN 2](Documentation~/moving-from-photon-pun-2.md).
+## Auction house
+
+`HibiraAuctionHouse` follows an auction house run by
+[`@hibira-io/auction`](https://github.com/hibira-io/auction-js) on your Node
+server, so Unity and browser players trade in the same house. Listings,
+bids, extensions and sales arrive live; the countdown follows the server's
+clock.
+
+```csharp
+var house = HibiraAuctionHouse.Open(hibira, "main", "https://api.your-game.example/auction");
+house.Changed += () => DrawListings(house.Listings);
+house.Updated += (kind, listing) =>
+{
+ if (kind == "sold" && listing.BidderId == myId) ShowWon(listing.Item["type"]);
+};
+
+void Update()
+{
+ foreach (var l in house.Listings) SetTimer(l.Id, house.TimeLeft(l)); // seconds
+}
+```
+
+The URL is your server's `GET` returning `house.snapshot()`; it loads on
+every subscription, including after a reconnect. Bids never go over the
+socket: POST them to your own server, which checks them with `house.bid()`
+and publishes the result to everyone. `listing.MinBid` is the next lowest
+bid, and `house.Prices(type)` the recent sale prices of an item type.
+
## WebGL
WebGL builds use the browser's WebSocket and `fetch` (through
@@ -264,6 +292,12 @@ events `Joined`, `Updated`, `Matched`, `TimedOut`, `JoinFailed`.
`HibiraMatch`: `FindMatches(tickets, rules, nowMs)`, `AllowedRange`,
`CriteriaFit`.
+`HibiraAuctionHouse`: `Open(client, house, snapshotUrl)` (or a `load`
+function), `Listings`, `Closed`, `Get(id)`, `TimeLeft(listing)`,
+`Prices(type)`, `NowMs()`, `Reload()`, `Close()`, and the events `Ready`,
+`Changed`, `Updated(kind, listing)`, `Failed`. `HibiraListing` carries the
+listing's fields (`Price`, `MinBid`, `Buyout`, `EndsAt`, `Status`, `Item`, …).
+
Not supported yet: end-to-end encrypted channels (`private-encrypted-`) and
user sign-in (`pusher:signin`).
diff --git a/Runtime/HibiraAuctionHouse.cs b/Runtime/HibiraAuctionHouse.cs
new file mode 100644
index 0000000..dca3bd4
--- /dev/null
+++ b/Runtime/HibiraAuctionHouse.cs
@@ -0,0 +1,307 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Threading.Tasks;
+
+namespace Hibira
+{
+ /// One listing of an auction house, as the server published it.
+ public sealed class HibiraListing
+ {
+ public string Id { get; }
+ public string House { get; }
+ public string SellerId { get; }
+ /// Public info about the seller (name...), as your server set it.
+ public Dictionary Seller { get; }
+ /// What is sold, as your server set it; Item["type"] groups sale prices.
+ public Dictionary Item { get; }
+ public long StartPrice { get; }
+ public long? Buyout { get; }
+ /// The highest bid, or the sale price once sold; null before the first bid.
+ public long? Price { get; }
+ public string BidderId { get; }
+ public Dictionary Bidder { get; }
+ public int Bids { get; }
+ /// The lowest bid the server accepts next.
+ public long MinBid { get; }
+ /// Server time, ms since epoch.
+ public double EndsAt { get; }
+ /// "open", "sold", "unsold" or "cancelled".
+ public string Status { get; }
+ public double? ClosedAt { get; }
+ public long Version { get; }
+ /// The listing as JSON values, for fields your server added.
+ public Dictionary Raw { get; }
+
+ public bool IsOpen => Status == "open";
+ /// Item["type"] as text, or null.
+ public string ItemType => Json.Get(Item, "type") is object t ? Json.ToText(t) : null;
+
+ HibiraListing(Dictionary d)
+ {
+ Raw = d;
+ Id = Json.ToText(Json.Get(d, "id"));
+ House = Json.Get(d, "house") as string;
+ SellerId = Json.Get(d, "sellerId") is object s ? Json.ToText(s) : null;
+ Seller = Json.Get(d, "seller") as Dictionary ?? new Dictionary();
+ Item = Json.Get(d, "item") as Dictionary ?? new Dictionary();
+ StartPrice = (long)Json.ToDouble(Json.Get(d, "startPrice"));
+ Buyout = Long(Json.Get(d, "buyout"));
+ Price = Long(Json.Get(d, "price"));
+ BidderId = Json.Get(d, "bidderId") is object b ? Json.ToText(b) : null;
+ Bidder = Json.Get(d, "bidder") as Dictionary;
+ Bids = (int)Json.ToDouble(Json.Get(d, "bids"));
+ MinBid = (long)Json.ToDouble(Json.Get(d, "minBid"));
+ EndsAt = Json.ToDouble(Json.Get(d, "endsAt"));
+ Status = Json.Get(d, "status") as string ?? "open";
+ ClosedAt = Json.IsNumber(Json.Get(d, "closedAt")) ? Json.ToDouble(Json.Get(d, "closedAt")) : (double?)null;
+ Version = (long)Json.ToDouble(Json.Get(d, "version"));
+ }
+
+ /// Read a listing from parsed JSON; null when it has no id or version.
+ public static HibiraListing From(object value)
+ {
+ if (!(value is Dictionary d)) return null;
+ if (!(Json.Get(d, "id") is string id) || id.Length == 0 || !Json.IsNumber(Json.Get(d, "version"))) return null;
+ return new HibiraListing(d);
+ }
+
+ static long? Long(object v) => Json.IsNumber(v) ? (long)Json.ToDouble(v) : (long?)null;
+ }
+
+ /// A recent sale price of one item type.
+ public readonly struct HibiraPricePoint
+ {
+ public readonly long Price;
+ public readonly double At;
+ public readonly string ListingId;
+
+ public HibiraPricePoint(long price, double at, string listingId)
+ {
+ Price = price;
+ At = at;
+ ListingId = listingId;
+ }
+ }
+
+ ///
+ /// An auction house that follows a channel published by @hibira-io/auction's
+ /// server (createAuctionHouse in Node), so Unity and browser players share
+ /// the same house. The channel carries one event, auction-update, with the
+ /// whole listing and a version; the newest version of each listing wins.
+ /// On every subscription, including after a reconnect, it loads the
+ /// server's snapshot (house.snapshot()) so nothing is missed.
+ ///
+ /// Players never bid over the socket: send bids to your own server, which
+ /// checks them with house.bid().
+ ///
+ public sealed class HibiraAuctionHouse
+ {
+ public const string EventUpdate = "auction-update";
+
+ /// The first snapshot has loaded.
+ public event Action Ready;
+ /// Something changed: a server event or a (re)load.
+ public event Action Changed;
+ /// One server event. Arguments: kind (listed, bid, extended, sold, unsold, cancelled) and the listing.
+ public event Action Updated;
+ /// Loading the snapshot or subscribing failed. Argument: a message.
+ public event Action Failed;
+
+ public HibiraChannel Channel { get; }
+ public string Name { get; }
+ public bool IsReady { get; private set; }
+ /// Server clock minus this device's clock, in ms.
+ public double OffsetMs { get; private set; }
+ public int KeepClosed { get; set; } = 50;
+ public int HistoryPerType { get; set; } = 20;
+
+ /// Wall clock in ms since epoch.
+ internal Func EpochMs = () => DateTimeOffset.UtcNow.ToUnixTimeMilliseconds();
+
+ readonly Func> _load;
+ readonly Action _onUpdate;
+ readonly Action