189 lines
17 KiB
C++
189 lines
17 KiB
C++
// Synthetic: native commodity quote / confirm / cancel over Tortoise whole-stack auctions
|
|
// (Codex decisions 2026-11-21): fresh name-scoped Source snapshot, single exact stack, revalidated
|
|
// confirm, one Source buyout, exactly one correlated native result; no result on a lost acknowledgement.
|
|
#include "world_translation.hpp"
|
|
#include <iostream>
|
|
using namespace gateway;
|
|
static void expect(bool ok,const char* what){if(!ok)throw Error(what);}
|
|
static Guid128 g(uint64_t lo,uint64_t hi){Guid128 x{};Bytes b;le(b,lo,8);le(b,hi,8);std::copy(b.begin(),b.end(),x.begin());return x;}
|
|
struct Row{uint32_t id,entry,count,buyout;uint64_t owner=7,bidder=0;};
|
|
static Bytes page(const std::vector<Row>& rows,uint32_t total){Bytes b;le(b,rows.size(),4);
|
|
for(const auto& r:rows){le(b,r.id,4);le(b,r.entry,4);le(b,0,4);le(b,0,4);le(b,0,4);le(b,r.count,4);le(b,0,4);le(b,r.owner,8);
|
|
le(b,1,4);le(b,1,4);le(b,r.buyout,4);le(b,60000,4);le(b,r.bidder,8);le(b,r.bidder?2:0,4);}
|
|
le(b,total,4);return b;}
|
|
static int ops(const Bytes& bundle,uint32_t want,Bytes* body=nullptr){if(bundle.empty())return 0;Reader z(bundle);auto n=z.le(4);int k=0;
|
|
for(uint64_t i=0;i<n;++i){auto op=z.le(4);auto sz=z.le(4);auto b=z.take(sz);if(op==want){++k;if(body)*body=Bytes(b.begin(),b.end());}}return k;}
|
|
int main(){try{
|
|
NativeWorldTranslation w{};w.realm=70;w.selected.guid=4509;w.selected.race=1;w.selected.cls=1;w.selected.name="Buyer";
|
|
w.self_created=true;w.location=WorldLocation{0,{0,0,0,0}};w.virtual_realm_address=0x10046;
|
|
const uint64_t npc=0xF130000000000505ull;const auto npc_m=g(0x505,0x2000000000000005ull);
|
|
NativeEntityTranslation::Actor a;a.kind=3;a.modern=npc_m;a.sent=true;a.generation=1;w.entities.actors[npc]=a;
|
|
ItemTemplateReply linen;linen.entry=2589;linen.found=true;linen.cls=7;linen.item_level=5;linen.stack=20;linen.name="Linen Cloth";
|
|
w.inventory.peer_templates[2589]=linen;
|
|
// Unknown balance: no quote at all.
|
|
{Bytes b=pack_guid(npc_m);le(b,2589,4);le(b,5,4);b.push_back(0);
|
|
w.gameplay_sent(0x3f006c,b);Bytes d;
|
|
Bytes pg;le(pg,1,4);for(uint32_t v:{31u,2589u,0u,0u,0u,5u,0u})le(pg,v,4);le(pg,7,8);for(uint32_t v:{1u,1u,50u,60000u})le(pg,v,4);le(pg,0,8);le(pg,0,4);le(pg,1,4);
|
|
auto fk=[](uint32_t,const uint8_t* in,uint32_t size,uint32_t,uint8_t* out,uint32_t cap,uint32_t* written)->int{*written=size;if(cap<size)return GW_CODEC_CAPACITY;std::copy(in,in+size,out);return GW_CODEC_OK;};
|
|
expect(ops(w.translate({0x25c,pg},0x10046,0,fk),0x4702fb,&d)==1 && !commodity_quote_from_input(d).total && !w.auction_commodity,"no balance, no quote");
|
|
w.requests.clear();w.ah_list_inflight=false;}
|
|
// Authoritative Source balance from the selected character's full create.
|
|
w.self_coinage=1000;
|
|
auto fake=[](uint32_t mode,const uint8_t* in,uint32_t size,uint32_t,uint8_t* out,uint32_t cap,uint32_t* written)->int{
|
|
if(mode!=GW_CODEC_COMMODITY_QUOTE && mode!=GW_CODEC_AUCTION_BUCKETS && mode!=GW_CODEC_AUCTION_ITEMS)return GW_CODEC_PACKET;
|
|
*written=size;if(cap<size)return GW_CODEC_CAPACITY;std::copy(in,in+size,out);return GW_CODEC_OK;};
|
|
auto req=[&](uint32_t item,uint32_t qty){Bytes b=pack_guid(npc_m);le(b,item,4);le(b,qty,4);b.push_back(0);return b;};
|
|
auto send=[&](uint32_t op,const Bytes& b){
|
|
if(w.auction_commodity_local(op,b))return LegacyPacket{0,{}};
|
|
if(w.defer_personal_list(op,b))return LegacyPacket{1,{}};
|
|
auto p=w.gameplay(op,b);w.gameplay_sent(op,b);return p;};
|
|
const std::vector<Row> market{{30,2589,10,700},{31,2589,5,50},{32,2589,5,51},{33,2589,4,0}};
|
|
// Quote 5: Source name-scoped list; cheapest eligible whole stack id 31 (10/unit) fills exactly.
|
|
auto p=send(0x3f006c,req(2589,5));expect(p.opcode==0x258,"quote searches the Source");
|
|
{Reader q(p.body);expect(q.le(8)==npc && q.le(4)==0 && q.cstring(255)=="Linen Cloth","name-scoped page");}
|
|
Bytes dto;expect(ops(w.translate({0x25c,page(market,4)},0x10046,0,fake),0x4702fb,&dto)==1,"quote published");
|
|
{auto r=commodity_quote_from_input(dto);expect(r.total==50 && r.quantity==5 && r.remaining_ms==30000 && r.item==2589,"available quote");}
|
|
expect(w.auction_commodity && w.auction_commodity->quote->auction_id==31 && !w.ah_list_inflight,"quote held");
|
|
// The mismatched confirm must wake an otherwise idle presentation poll without another Source packet.
|
|
expect(!w.local_presentation_pending(),"presentation idle before local confirmation");
|
|
p=send(0x3f006d,req(2589,4));expect(p.opcode==0,"mismatched confirm answered locally");
|
|
expect(w.local_presentation_pending(),"local confirmation must wake the presentation poll");
|
|
const auto presentation=w.local_presentation(0x10046,fake);
|
|
Reader local(presentation);
|
|
expect(local.le(4)==1 && local.le(4)==forever_opcodes::SMSG_AUCTION_COMMAND_RESULT,"one local confirmation result");
|
|
const auto result=local.take(local.le(4));
|
|
expect(Bytes(result.begin(),result.end())==commodity_purchase_command_result(false),"CommodityPurchaseFailed");
|
|
local.end();
|
|
expect(!w.local_presentation_pending(),"presentation idle after consuming the local confirmation");
|
|
expect(!w.auction_commodity,"quote retired");
|
|
// Re-quote and confirm: revalidated snapshot, one buyout at the stack price, one result.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);w.requests.clear();
|
|
p=send(0x3f006d,req(2589,5));expect(p.opcode==0x258,"confirm refreshes");
|
|
w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
expect(w.requests.size()==1 && w.requests[0].opcode==0x25a,"one Source buyout");
|
|
{Reader q(w.requests[0].body);expect(q.le(8)==npc && q.le(4)==31 && q.le(4)==50,"bid at buyout");q.end();}
|
|
expect(w.auction_commodity->bid_queued && !w.auction_commodity->bid_sent,"queued, not yet sent");
|
|
// A repeat confirm while the buyout is outstanding is answered after it, in order.
|
|
expect(send(0x3f006d,req(2589,5)).opcode==0 && w.take_social_presentation(fake).empty() && w.auction_commodity->late_failures==1,"repeat deferred");
|
|
w.auction_bid_dispatched(w.requests[0].body);w.requests.clear();expect(w.auction_commodity->bid_sent,"dispatched");
|
|
// An unrelated bid result is not taken; the correlated success is answered once.
|
|
{Bytes other;le(other,99,4);le(other,2,4);le(other,0,4);le(other,10,4);auto out=w.translate({0x25b,other},0x10046,0,fake);
|
|
expect(ops(out,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT)==1 && w.auction_commodity,"ordinary bid result passes through");}
|
|
{Bytes zero;le(zero,0,4);le(zero,2,4);le(zero,2,4);w.translate({0x25b,zero},0x10046,0,fake);
|
|
expect(w.auction_commodity && w.auction_commodity->bid_sent,"a zero-ID PlaceBid error is not taken as the commodity result");}
|
|
{Bytes partial;le(partial,31,4);le(partial,2,4);le(partial,0,4);w.translate({0x25b,partial},0x10046,0,fake);
|
|
expect(w.auction_commodity && w.auction_commodity->bid_sent,"incomplete success body is not proof");}
|
|
{Bytes okr;le(okr,31,4);le(okr,2,4);le(okr,0,4);le(okr,0,4);
|
|
auto out=w.translate({0x25b,okr},0x10046,0,fake);expect(!w.auction_commodity,"resolved");
|
|
Reader z(out);auto n=z.le(4);std::vector<uint32_t> codes;
|
|
for(uint64_t i=0;i<n;++i){auto op=z.le(4);auto sz=z.le(4);auto b=z.take(sz);if(op==forever_opcodes::SMSG_AUCTION_COMMAND_RESULT){Reader q(b);q.le(4);q.le(4);codes.push_back(uint32_t(q.le(4)));}}
|
|
expect(codes.size()==2 && codes[0]==0 && codes[1]==21,"success, then the repeat confirm's failure");}
|
|
// Stock changed between quote and confirm (stack 31 gone): failure, no bid.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);w.requests.clear();
|
|
send(0x3f006d,req(2589,5));
|
|
{std::vector<Row> later{{30,2589,10,700},{32,2589,5,51},{33,2589,4,0}};
|
|
auto out=w.translate({0x25c,page(later,3)},0x10046,0,fake);
|
|
expect(w.requests.empty() && ops(out,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT)==1 && !w.auction_commodity,"revalidation failure");}
|
|
// Balance below the stack price: no quote; and a balance drop before confirm fails without a bid.
|
|
w.self_coinage=49;send(0x3f006c,req(2589,5));
|
|
{auto out=w.translate({0x25c,page(market,4)},0x10046,0,fake);expect(ops(out,0x4702fb,&dto)==1 && !commodity_quote_from_input(dto).total,"unaffordable quote suppressed");}
|
|
w.self_coinage=1000;send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);w.requests.clear();
|
|
send(0x3f006d,req(2589,5));w.self_coinage=10;
|
|
{auto out=w.translate({0x25c,page(market,4)},0x10046,0,fake);expect(w.requests.empty() && ops(out,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT)==1 && !w.auction_commodity,"confirm rechecks funds");}
|
|
w.self_coinage=1000;
|
|
// No exact single stack (quantity 7): unavailable quote, state cleared.
|
|
send(0x3f006c,req(2589,7));
|
|
{auto out=w.translate({0x25c,page(market,4)},0x10046,0,fake);expect(ops(out,0x4702fb,&dto)==1 && !commodity_quote_from_input(dto).total && !w.auction_commodity,"unavailable");}
|
|
// Multi-stack: quantity 10 over two whole 5-stacks at the same unit price -> two chained Source buyouts
|
|
// (newest id first), one native result after the last success.
|
|
{const std::vector<Row> two{{40,2589,5,50},{41,2589,5,50},{42,2589,20,400}};
|
|
send(0x3f006c,req(2589,10));
|
|
{auto out=w.translate({0x25c,page(two,3)},0x10046,0,fake);expect(ops(out,0x4702fb,&dto)==1,"multi quote published");
|
|
auto r=commodity_quote_from_input(dto);expect(r.total==100 && r.quantity==10,"two stacks quoted at their summed buyout");}
|
|
w.requests.clear();send(0x3f006d,req(2589,10));w.translate({0x25c,page(two,3)},0x10046,0,fake);
|
|
expect(w.requests.size()==1,"first buyout only");{Reader q(w.requests[0].body);q.le(8);expect(q.le(4)==41 && q.le(4)==50,"newest stack first");}
|
|
w.auction_bid_dispatched(w.requests[0].body);w.requests.clear();
|
|
{Bytes ok1;le(ok1,41,4);le(ok1,2,4);le(ok1,0,4);le(ok1,0,4);auto out=w.translate({0x25b,ok1},0x10046,0,fake);
|
|
expect(!ops(out,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT) && w.requests.size()==1 && w.auction_commodity,"second buyout chained, no result yet");
|
|
Reader q(w.requests[0].body);q.le(8);expect(q.le(4)==40 && q.le(4)==50,"second stack");}
|
|
w.auction_bid_dispatched(w.requests[0].body);w.requests.clear();
|
|
{Bytes ok2;le(ok2,40,4);le(ok2,2,4);le(ok2,0,4);le(ok2,0,4);auto out=w.translate({0x25b,ok2},0x10046,0,fake);
|
|
expect(ops(out,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT)==1 && !w.auction_commodity,"one success after the last stack");}
|
|
// 7 of 5+5 would split a stack: no quote.
|
|
send(0x3f006c,req(2589,7));
|
|
{auto out=w.translate({0x25c,page(two,3)},0x10046,0,fake);expect(ops(out,0x4702fb,&dto)==1 && !commodity_quote_from_input(dto).total,"no split");}}
|
|
// Lost acknowledgement: no result is ever claimed; the state is forgotten after 60 s.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);send(0x3f006d,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
w.auction_bid_dispatched(w.requests.at(0).body);
|
|
expect(w.auction_commodity && w.auction_commodity->bid_sent,"bid outstanding");w.requests.clear();
|
|
{Bytes zero;le(zero,0,4);le(zero,2,4);le(zero,4,4);w.ordinary_bid_since=std::chrono::steady_clock::now();w.translate({0x25b,zero},0x10046,0,fake);
|
|
expect(w.auction_commodity.has_value(),"zero-ID error with a recent ordinary bid is not attributed");}
|
|
expect(send(0x3f006e,[&]{Bytes cb=pack_guid(npc_m);cb.push_back(0);return cb;}()).opcode==0 && w.auction_commodity,"cancel keeps a sent buyout correlated");
|
|
w.auction_commodity->bid_since-=std::chrono::seconds(61);
|
|
expect(send(0x3f006e,[&]{Bytes cb=pack_guid(npc_m);cb.push_back(0);return cb;}()).opcode==0 && !w.auction_commodity && w.take_social_presentation(fake).empty(),"forgotten silently");
|
|
// Quote for an item without a Source template: unavailable locally, template requested.
|
|
expect(send(0x3f006c,req(4306,1)).opcode==0,"local unavailable");
|
|
{auto out=w.take_social_presentation(fake);expect(out.size()==1 && out[0].opcode==0x4702fb && w.inventory.queries.contains(4306),"template asked");}
|
|
// Non-stackable item: unavailable locally.
|
|
ItemTemplateReply sword;sword.entry=25;sword.found=true;sword.cls=2;sword.stack=1;sword.name="Worn Shortsword";w.inventory.peer_templates[25]=sword;
|
|
expect(send(0x3f006c,req(25,1)).opcode==0 && w.take_social_presentation(fake).size()==1,"non-stackable");
|
|
// Cancel withdraws a queued (undispatched) buyout: that confirm fails, nothing is sent.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);send(0x3f006d,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
expect(w.requests.size()==1 && w.auction_commodity->bid_queued,"queued buyout");
|
|
{Bytes cb=pack_guid(npc_m);cb.push_back(0);expect(send(0x3f006e,cb).opcode==0 && w.requests.empty() && !w.auction_commodity,"withdrawn");
|
|
auto out=w.take_social_presentation(fake);expect(out.size()==1 && out[0].opcode==forever_opcodes::SMSG_AUCTION_COMMAND_RESULT,"one failure");}
|
|
// Generic CloseInteraction of the house withdraws a queued buyout exactly like the explicit cancel.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);send(0x3f006d,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
expect(w.requests.size()==1 && w.auction_commodity->bid_queued,"queued buyout before close");
|
|
{w.close_interaction(pack_guid(npc_m));expect(w.requests.empty() && !w.auction_commodity,"close withdrew");
|
|
auto out=w.take_social_presentation(fake);expect(out.size()==1 && out[0].opcode==forever_opcodes::SMSG_AUCTION_COMMAND_RESULT,"close: one failure");}
|
|
// Closing or cancelling while a repeated confirmation awaits revalidation answers both clicks.
|
|
for(bool close_window : {false,true}){
|
|
send(0x3f006c,req(2589,5));
|
|
w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
expect(send(0x3f006d,req(2589,5)).opcode==0x258,"first confirmation refreshes");
|
|
expect(send(0x3f006d,req(2589,5)).opcode==0,"repeated confirmation is local");
|
|
Bytes action=pack_guid(npc_m);
|
|
if(close_window)w.close_interaction(action);
|
|
else{action.push_back(0);send(0x3f006e,action);}
|
|
const auto results=w.take_social_presentation(fake);
|
|
expect(results.size()==2,"cancelled refresh must answer both confirmations");
|
|
for(const auto& result:results){
|
|
Reader fields(result.body);
|
|
expect(result.opcode==forever_opcodes::SMSG_AUCTION_COMMAND_RESULT &&
|
|
fields.le(4)==0 && fields.le(4)==2 && fields.le(4)==21,"expected a purchase failure");
|
|
}
|
|
const auto late=w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
expect(w.requests.empty() && !w.auction_commodity &&
|
|
!ops(late,forever_opcodes::SMSG_AUCTION_COMMAND_RESULT),"retired refresh produced another action");
|
|
}
|
|
// Tainted / padded cancel is refused.
|
|
{Bytes cb=pack_guid(npc_m);cb.push_back(0x80);bool r=false;try{w.auction_commodity_local(0x3f006e,cb);}catch(const Error&){r=true;}expect(r,"tainted cancel");}
|
|
// Cancel before confirm clears the quote.
|
|
send(0x3f006c,req(2589,5));w.translate({0x25c,page(market,4)},0x10046,0,fake);
|
|
send(0x3f006e,[&]{Bytes cb=pack_guid(npc_m);cb.push_back(0);return cb;}());expect(!w.auction_commodity,"cancel");
|
|
// Guard: a quote while a browse list is in flight is deferred, then replayed.
|
|
w.ah_list_inflight=true;w.ah_list_reply=0x25c;w.ah_list_since=std::chrono::steady_clock::now();
|
|
expect(send(0x3f006c,req(2589,5)).opcode==1 && w.auction_personal_deferred.contains(0x3f006c),"deferred");
|
|
w.ah_list_inflight=false;w.requests.clear();w.start_deferred_personal_list();
|
|
expect(w.requests.size()==1 && w.requests[0].opcode==0x258 && w.auction_commodity && w.auction_commodity->awaiting,"replayed");
|
|
// A deferred quote cancelled before it reaches Source still gets its one (unavailable) answer.
|
|
w.auction_commodity.reset();w.ah_list_inflight=true;w.ah_list_since=std::chrono::steady_clock::now();w.take_social_presentation(fake);
|
|
expect(send(0x3f006c,req(2589,5)).opcode==1,"deferred again");
|
|
send(0x3f006e,[&]{Bytes cb=pack_guid(npc_m);cb.push_back(0);return cb;}());
|
|
{auto out=w.take_social_presentation(fake);expect(!w.auction_personal_deferred.contains(0x3f006c) && out.size()==1 && out[0].opcode==0x4702fb,"cancelled deferral answered");}
|
|
// Coinage from a real Source self values delta (word 1176); an absent word preserves it, explicit zero clears.
|
|
w.ah_list_inflight=false;
|
|
auto money=[&](std::optional<uint32_t> v){Bytes b;le(b,1,4);le(b,0,1);le(b,0,1);{Bytes g8;le(g8,uint64_t(4509),8);append(b,pack_guid(g8));}
|
|
const unsigned idx=v?1176u:192u;const unsigned n=idx/32+1;le(b,n,1);std::vector<uint32_t> mask(n);mask[idx/32]|=1u<<(idx%32);
|
|
for(auto m:mask)le(b,m,4);le(b,v?*v:w.guild_rank,4);return LegacyPacket{0xa9,b};};
|
|
w.self_coinage=1000;
|
|
try{w.translate(money(uint32_t(4321)),0x10046,0,fake);}catch(const Error&){}
|
|
expect(w.self_coinage==4321u,"delta sets the balance");
|
|
try{w.translate(money(std::nullopt),0x10046,0,fake);}catch(const Error&){}
|
|
expect(w.self_coinage==4321u,"absent word preserves it");
|
|
try{w.translate(money(uint32_t(0)),0x10046,0,fake);}catch(const Error&){}
|
|
expect(w.self_coinage==0u,"explicit zero clears it");
|
|
std::cout<<"PASS native auction commodities: quote, local mismatch, revalidated single buyout, correlated result, lost ack, cancel, guard\n";return 0;
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
|