Files

150 lines
12 KiB
C++

// SPDX-License-Identifier: GPL-2.0-or-later
// Native 70334 auction browse (bucket protocol) over Tortoise 50-row list pages.
// Phase 1 (Codex decisions 2026-11-21): browse + details only, nonstackable items
// without random properties, complete bounded snapshots, TC ordering rules.
#pragma once
#include "gateway/protocol.hpp"
#include <compare>
#include <functional>
#include <optional>
#include <span>
#include <string>
#include <vector>
namespace gateway {
// TC AuctionSortDef / AuctionHouseSortOrder.
struct AuctionSort {uint8_t order=0;bool reverse=false;};
// Native CMSG_AUCTION_BROWSE_QUERY (TC AuctionBrowseQuery::Read).
struct NativeAuctionSubclassFilter {uint64_t inv_type_mask=0;int32_t subclass=0;};
struct NativeAuctionClassFilter {int32_t item_class=0;std::vector<NativeAuctionSubclassFilter> subclasses;};
struct NativeAuctionBrowseQuery {
Bytes auctioneer;uint32_t offset=0;uint8_t min_level=0,max_level=0;uint32_t filters=0;
std::string name;std::vector<NativeAuctionClassFilter> classes;std::vector<AuctionSort> sorts;
};
NativeAuctionBrowseQuery native_auction_browse_query(std::span<const uint8_t>);
// Native bucket key (TC AuctionBucketKey); pet species / suffix are never represented.
struct AuctionKey {uint32_t item=0;uint16_t item_level=0;auto operator<=>(const AuctionKey&) const=default;};
// Native CMSG_AUCTION_LIST_ITEMS_BY_BUCKET_KEY (TC AuctionListItemsByBucketKey::Read).
struct NativeAuctionItemsQuery {Bytes auctioneer;AuctionKey key;uint32_t offset=0;std::vector<AuctionSort> sorts;};
NativeAuctionItemsQuery native_auction_items_query(std::span<const uint8_t>);
// Native CMSG_AUCTION_LIST_ITEMS_BY_ITEM_ID (TC AuctionListItemsByItemID::Read); a suffix description holds.
struct NativeAuctionItemIdQuery {Bytes auctioneer;uint32_t item=0,offset=0;std::vector<AuctionSort> sorts;};
NativeAuctionItemIdQuery native_auction_items_by_item_query(std::span<const uint8_t>);
// Native CMSG_AUCTION_LIST_BUCKETS_BY_BUCKET_KEYS (commodity sell frame / favorites).
struct NativeAuctionBucketKeysQuery {Bytes auctioneer;std::vector<AuctionKey> keys;std::vector<AuctionSort> sorts;};
NativeAuctionBucketKeysQuery native_auction_bucket_keys_query(std::span<const uint8_t>);
// Source 0x258 page searching by the item's own name, every other filter open: a superset of that
// item's auctions, narrowed by entry afterwards (commodity quote / confirm revalidation).
Bytes source_auction_name_request(uint64_t auctioneer,const std::string& name,uint32_t listfrom);
// Tortoise CMSG_AUCTION_LIST_ITEMS 0x258 for one Source page, or nullopt when the native
// filter has no exact Source equivalent (held, never widened or narrowed).
std::optional<Bytes> source_auction_list_request(const NativeAuctionBrowseQuery&,uint64_t auctioneer,uint32_t listfrom);
// Always-answerable Source page: the exact request when one exists, else the narrowest wider one;
// rows are then narrowed by auction_row_matches against the Source item templates.
Bytes source_auction_superset_request(const NativeAuctionBrowseQuery&,uint64_t auctioneer,uint32_t listfrom);
bool auction_query_exact(const NativeAuctionBrowseQuery&);
// Native (retail Classic) auction subclass of a Source item: rows are matched with this, not the Source 1.12 value.
uint32_t auction_native_subclass(uint32_t item,uint32_t cls,uint32_t subclass);
bool auction_row_matches(const NativeAuctionBrowseQuery&,uint32_t cls,uint32_t subclass,uint32_t inventory_type,uint32_t quality,uint32_t required_level);
// Tortoise SMSG_AUCTION_LIST_RESULT 0x25c (AuctionEntry::BuildAuctionInfo rows).
struct TurtleAuctionRow {
uint32_t id=0,entry=0,enchant=0,random_property=0,suffix_factor=0,count=0,charges=0;uint64_t owner=0;
uint32_t start_bid=0,min_outbid=0,buyout=0,time_left_ms=0;uint64_t bidder=0;uint32_t bid=0;
bool operator==(const TurtleAuctionRow&) const=default;
};
struct TurtleAuctionList {std::vector<TurtleAuctionRow> rows;uint32_t total=0;};
TurtleAuctionList turtle_auction_list(std::span<const uint8_t>);
struct AuctionHold : Error {using Error::Error;};
// Accumulates sequential Source pages until the snapshot is complete.
struct AuctionSnapshot {
static constexpr uint32_t MaxRows=1000; // 20 Source pages
std::vector<TurtleAuctionRow> rows;uint32_t total=0,next_listfrom=0;bool complete=false;
// Bidder lists: Tortoise lists a tracked outbid ID a second time when it is also a current bid.
// With repeats, an identical repeated row counts toward the Source total but is stored once.
bool repeats=false;uint32_t raw=0;
// Next listfrom to request, or nullopt once complete. Throws AuctionHold when the
// Source result cannot form one consistent complete snapshot.
std::optional<uint32_t> add(const TurtleAuctionList&);
};
// Source item facts from the real 0x58 template reply.
struct AuctionItemFacts {uint32_t cls=0,subclass=0,item_level=0,required_level=0,stack=0,container_slots=0,required_skill_rank=0;std::string name;};
struct AuctionBucket {
AuctionKey key;int32_t total_quantity=0,required_level=0;uint64_t min_price=0;int32_t sort_level=0;
std::string sort_name;bool contains_owner=false;std::vector<TurtleAuctionRow> rows;
bool commodity=false; // stackable: rows are only exactly-priced whole buyout stacks, min_price is a unit price
};
// Commodity phase (Codex decisions 2026-11-21): a Source stack of a stackable item is shown as a native
// commodity posting only when it has a buyout that divides exactly by its count and no active bid.
// Every other stackable row is omitted from the public view and counted here (never rounded).
struct AuctionCommodityOmissions {uint32_t bid_only=0,fractional=0,active_bid=0;
bool hold_ineligible=false; // personal views: a real record that cannot be shown holds the whole list
uint32_t total() const{return bid_only+fractional+active_bid;}};
std::optional<uint64_t> commodity_unit_price(const TurtleAuctionRow&);
// Groups a complete snapshot into TC-ordered buckets. Throws AuctionHold for any row this
// phase cannot represent truthfully (random property, stackable, missing template, key bounds).
using AuctionFactsLookup=std::function<const AuctionItemFacts*(uint32_t)>;
// Without `commodities`, any stackable row holds (personal views keep that until mode 77 carries commodity rows).
std::vector<AuctionBucket> auction_buckets(const AuctionSnapshot&,const AuctionFactsLookup&,std::span<const AuctionSort>,uint64_t self,
AuctionCommodityOmissions* commodities=nullptr);
// Codec DTOs (version 1) for GW_CODEC_AUCTION_BUCKETS / GW_CODEC_AUCTION_ITEMS.
struct AuctionBucketsResult {uint32_t filters=0;int32_t total_count=0;bool has_more=false;std::vector<AuctionBucket> buckets;};
// TC AuctionsResultBuilder window (Browse limit 500) over the ordered buckets.
AuctionBucketsResult auction_buckets_result(const std::vector<AuctionBucket>&,uint32_t offset,uint32_t filters);
Bytes auction_buckets_input(const AuctionBucketsResult&);
AuctionBucketsResult auction_buckets_from_input(std::span<const uint8_t>);
struct AuctionItemRow {
uint32_t item=0,count=0,charges=0,auction_id=0,duration_left=0,enchant=0;Bytes owner; // packed Guid128 (16 bytes)
std::optional<uint64_t> min_bid,min_increment,buyout;std::optional<Bytes> bidder;uint64_t bid_amount=0;
std::optional<uint64_t> unit_price;std::optional<AuctionKey> key; // commodity unit price; per-row key (ByItemID)
};
// list_type 0 Items, 1 Commodities (TC AuctionHouseListType). by_item: ByItemID detail (header key has
// only the item; every row carries its full key) instead of ByBucketKey (header key echoed, no row key).
struct AuctionItemsResult {uint32_t total_count=0,total_quantity=0;bool has_more=false;AuctionKey key;std::vector<AuctionItemRow> items;
uint8_t list_type=0;bool by_item=false;};
// TC BuildListAuctionItems by bucket key (Items limit 50, AuctionPosting::Sorter).
AuctionItemsResult auction_items_result(const AuctionBucket&,std::span<const AuctionSort>,uint32_t offset,uint16_t realm);
// TC BuildListAuctionItems by item ID over every ordered bucket of that item (totals before the window).
AuctionItemsResult auction_items_by_item_result(const std::vector<AuctionBucket>&,uint32_t item,std::span<const AuctionSort>,uint32_t offset,uint16_t realm,bool stackable=false);
// DTO: version 1 for a plain ByBucketKey Items list (unchanged); version 2 otherwise:
// v1 header, u8 list_type, u8 by_item, rows = v1 row + u8 ext (1 key, 2 unit price) + [u32 item, u16 level] + [u64 unit].
Bytes auction_items_input(const AuctionItemsResult&);
AuctionItemsResult auction_items_from_input(std::span<const uint8_t>);
// Personal lists (owned / bidded): every row of a complete Source snapshot, TC AuctionPosting::Sorter
// order (sorts, then newest ID), each with its bucket key. DTO v1 for GW_CODEC_AUCTION_PERSONAL:
// u8 version, u8 kind (1 owned, 2 bidded), u32 n, n x {u32 item, u16 item_level, item row as in auction_items_input}.
// v2 (only when a commodity row is present): each row is followed by u8 has_unit, [u64 unit price].
// Commodity rows (stackable, exact whole buyout, no bid) keep the item and key and carry UnitPrice
// instead of bid/buyout; any other stackable record holds the list (never shown as a commodity).
struct AuctionPersonalRow {AuctionKey key;AuctionItemRow row;};
struct AuctionPersonalResult {bool owned=true;std::vector<AuctionPersonalRow> items;};
AuctionPersonalResult auction_personal_result(const AuctionSnapshot&,const AuctionFactsLookup&,std::span<const AuctionSort>,bool owned,uint16_t realm);
Bytes auction_personal_input(const AuctionPersonalResult&);
AuctionPersonalResult auction_personal_from_input(std::span<const uint8_t>);
// Native CMSG_AUCTION_GET_COMMODITY_QUOTE / CONFIRM_COMMODITIES_PURCHASE {Guid npc, i32 item, u32 quantity, opt TaintedBy}.
struct NativeCommodityRequest {Bytes auctioneer;uint32_t item=0,quantity=0;};
NativeCommodityRequest native_commodity_request(std::span<const uint8_t>);
// Restricted quote (Codex 2026-11-21): the single cheapest eligible whole stack of exactly `quantity`,
// excluding the player's own rows. nullopt (native "not available") when the snapshot is incomplete,
// the item is not stackable, a variant row exists, the cheapest tier would need a split or several stacks,
// equal-price stacks of other counts make the Source order matter, an omitted row is as cheap or cheaper,
// or known money is short. Equal-price whole stacks: highest ID (TC newest-first surrogate).
// lots: whole Source auctions (id, buyout) bought one after another, cheapest unit price first; auction_id = lots[0], buyout = total.
struct CommodityQuote {uint32_t item=0,quantity=0,auction_id=0;uint64_t buyout=0;std::vector<std::pair<uint32_t,uint64_t>> lots;
bool operator==(const CommodityQuote&)const=default;};
std::optional<CommodityQuote> commodity_quote(const AuctionSnapshot&,const AuctionFactsLookup&,uint32_t item,uint32_t quantity,
uint64_t self,std::optional<uint64_t> money);
// Quote-result DTO v1 (new codec mode, number pending Codex): u8 version, i32 item, u32 desired_delay, u8 present,
// [u64 total_price, u32 quantity, i64 remaining_ms] only when present (TC sets all three or none).
struct CommodityQuoteResult {int32_t item=0;uint32_t desired_delay=0;std::optional<uint64_t> total;uint32_t quantity=0;int64_t remaining_ms=0;};
Bytes commodity_quote_input(const CommodityQuoteResult&);
CommodityQuoteResult commodity_quote_from_input(std::span<const uint8_t>);
// SMSG_AUCTION_COMMAND_RESULT body for a commodity confirm (TC: AuctionID 0, PlaceBid, Ok or CommodityPurchaseFailed 21).
Bytes commodity_purchase_command_result(bool ok);
// Codec side: original TC writers (src/auction_browse_serializer.cpp).
Bytes serialize_auction_buckets(const Bytes&);
Bytes serialize_auction_items(const Bytes&);
Bytes serialize_auction_personal(const Bytes&);
Bytes serialize_commodity_quote(const Bytes&);
}