234 lines
19 KiB
C++
234 lines
19 KiB
C++
#include "optional_test_codec.hpp"
|
|
#include "loot_translation.hpp"
|
|
#include "serializer.h"
|
|
#ifdef GW_ROLL_TEST_WORLD
|
|
#include "world_translation.hpp"
|
|
#endif
|
|
#include <dlfcn.h>
|
|
#include <iostream>
|
|
#include <fstream>
|
|
#include <stdexcept>
|
|
using namespace gateway;
|
|
namespace {
|
|
constexpr uint64_t owner=0xf13000000600119dULL,selfid=4509,peer=4510;
|
|
void check(bool ok){if(!ok)throw std::runtime_error("group roll lifecycle differs");}
|
|
template<class F>void rejects(F f){try{f();}catch(const Error&){return;}throw std::runtime_error("invalid roll vote accepted");}
|
|
Bytes start(uint32_t slot,uint32_t item,uint32_t ms=60000){Bytes b;le(b,owner,8);le(b,slot,4);le(b,item,4);le(b,0,4);le(b,0,4);le(b,ms,4);return b;}
|
|
Bytes update(uint32_t slot,uint32_t item,uint64_t player,uint8_t number,uint8_t type){Bytes b;le(b,owner,8);le(b,slot,4);le(b,player,8);le(b,item,4);le(b,0,4);le(b,0,4);le(b,number,1);le(b,type,1);return b;}
|
|
Bytes won(uint32_t slot,uint32_t item,uint64_t player,uint8_t number=42,uint8_t type=2){Bytes b;le(b,owner,8);le(b,slot,4);le(b,item,4);le(b,0,4);le(b,0,4);le(b,player,8);le(b,number,1);le(b,type,1);return b;}
|
|
Bytes all_passed(uint32_t slot,uint32_t item){Bytes b;le(b,owner,8);le(b,slot,4);le(b,item,4);le(b,0,4);le(b,0,4);return b;}
|
|
Bytes response(){Bytes b;le(b,owner,8);le(b,1,1);le(b,0,4);le(b,2,1);for(auto slot:{0u,7u}){le(b,slot,1);le(b,25+slot,4);le(b,slot?4:2,4);le(b,100,4);le(b,0,4);le(b,0,4);le(b,1,1);}return b;}
|
|
Bytes vote(const Guid128& obj,uint8_t slot,uint8_t v){Bytes b=pack_guid(obj);le(b,slot,1);le(b,v,1);return b;}
|
|
}
|
|
int main(int argc,char** argv){try{
|
|
check(argc==1 || (argc==3 && std::string_view(argv[1])=="--emit-start-rolls"));
|
|
NativeEntityTranslation entities;auto& actor=entities.actors[owner];actor.kind=3;actor.entry=6;actor.generation=1;actor.sent=true;actor.modern=forever_creature_guid(owner,6,70,1);
|
|
Character self{};self.guid=selfid;self.name="Gatewaytest";NativeLootTranslation loot;
|
|
TurtleGroupList group;group.leader=selfid;TurtleGroupMember m;m.guid=peer;m.name="Gatewaypeer";group.members.push_back(m);group.loot=TurtleGroupLootPolicy{TurtleGroupLootMethod::group,0,2,0};loot.group=group;
|
|
NativeEntityTranslation::Codec codec=[](uint32_t,const Bytes& b){return b;};
|
|
#ifdef GW_ROLL_TEST_CODEC
|
|
if (test_fixture::optional_codec_missing(GW_ROLL_TEST_CODEC)) return 77;
|
|
void* library=dlopen(GW_ROLL_TEST_CODEC,RTLD_NOW|RTLD_LOCAL);check(library);
|
|
auto serialize=reinterpret_cast<decltype(&gw_codec_serialize)>(dlsym(library,"gw_codec_serialize"));check(serialize);
|
|
codec=[&](uint32_t mode,const Bytes& b){uint32_t size=0;check(mode==GW_CODEC_LOOT_EVENT);
|
|
check(serialize(mode,b.data(),b.size(),0,nullptr,0,&size)==GW_CODEC_CAPACITY);
|
|
Bytes result(size);check(serialize(mode,b.data(),b.size(),0,result.data(),size,&size)==GW_CODEC_OK);return result;};
|
|
#endif
|
|
#if defined(GW_ROLL_TEST_WORLD) && defined(GW_ROLL_TEST_CODEC)
|
|
{
|
|
unsigned assertions=0;
|
|
auto verify=[&](bool ok){++assertions;if(!ok)throw Error("Q56 source roll-slot assertion "+std::to_string(assertions));};
|
|
auto fresh=[&]{NativeWorldTranslation w;w.self_created=true;w.selected=self;w.realm=70;
|
|
w.location=WorldLocation{1,{0,0,0,0}};w.entities.actors[owner]=actor;w.loot.group=group;
|
|
w.loot_receipts.observe(w.inventory,selfid,true);return w;};
|
|
auto receive=[&](NativeWorldTranslation& w,uint32_t op,const Bytes& b){
|
|
auto raw=w.translate({op,b},0x46010046,0,serialize);Reader r(raw);std::vector<LegacyPacket> out;
|
|
for(auto n=r.le(4);n;--n){auto id=uint32_t(r.le(4));out.push_back({id,r.take(r.le(4))});}r.end();return out;};
|
|
auto has=[](const auto& out,uint32_t op){return std::any_of(out.begin(),out.end(),[&](const auto& p){return p.opcode==op;});};
|
|
auto golden=[&](const LegacyPacket& packet,const Guid128& object,uint8_t slot,uint8_t method){
|
|
verify(packet.opcode==0x4700c7);
|
|
const auto prefix=pack_guid(object);const size_t timer_at=prefix.size()+4+13;
|
|
verify(packet.body.size()>=timer_at+4);Reader timer(std::span(packet.body).subspan(timer_at,4));
|
|
const auto ms=timer.le(4);verify(ms>0 && ms<=60000);
|
|
Bytes expected=prefix;le(expected,1,4);le(expected,8,1);le(expected,1,4);
|
|
le(expected,0,1);le(expected,slot,1);le(expected,99,4);le(expected,0,2);
|
|
le(expected,ms,4);le(expected,7,1);expected.resize(expected.size()+20,0);le(expected,method,1);le(expected,0,4);
|
|
verify(packet.body==expected);
|
|
};
|
|
// R1: exercise server-to-client suppression at the world boundary,
|
|
// using the actual existing codec (not a fake serialization callback).
|
|
for(auto type:{TurtleGroupType::normal,TurtleGroupType::raid}){
|
|
auto w=fresh();w.loot.group->type=type;
|
|
Bytes fixture;le(fixture,16,4);
|
|
for(unsigned slot=0;slot<16;++slot){
|
|
auto packets=receive(w,0x2a1,start(slot,99));verify(has(packets,0x4700c7));
|
|
for(auto const& packet:packets)if(packet.opcode==0x4700c7){
|
|
golden(packet,w.loot.rolls.back().object,uint8_t(slot),uint8_t(w.loot.group->loot->method));
|
|
le(fixture,packet.opcode,4);le(fixture,packet.body.size(),4);
|
|
fixture.insert(fixture.end(),packet.body.begin(),packet.body.end());
|
|
}
|
|
}
|
|
if(argc==3 && type==TurtleGroupType::normal){
|
|
std::ofstream output(argv[2],std::ios::binary|std::ios::trunc);
|
|
output.write(reinterpret_cast<const char*>(fixture.data()),std::streamsize(fixture.size()));
|
|
check(bool(output));
|
|
}
|
|
verify(w.loot.rolls.size()==16); // More than four reach the client.
|
|
auto second_owner=owner+1;auto second_actor=actor;
|
|
second_actor.modern=forever_creature_guid(second_owner,6,70,1);
|
|
w.entities.actors[second_owner]=second_actor;
|
|
auto next=start(0,99);for(unsigned i=0;i<8;++i)next[i]=uint8_t(second_owner>>(8*i));
|
|
verify(!has(receive(w,0x2a1,next),0x4700c7)); // Session cap, not a per-corpse slot bound.
|
|
}
|
|
for(unsigned path=0;path<5;++path){
|
|
auto w=fresh();
|
|
if(path==0)w.entities.actors.clear();
|
|
if(path==1)w.entities.actors[owner].sent=false;
|
|
if(path==2)w.loot.group_transition=true;
|
|
if(path==3)w.loot.group.reset();
|
|
if(path==4)w.loot.group->loot->method=TurtleGroupLootMethod::master;
|
|
verify(!has(receive(w,0x2a1,start(0,99)),0x4700c7));
|
|
verify(w.loot.rolls.empty()); // Dropped, not queued for later recovery.
|
|
}
|
|
{auto w=fresh();w.self_created=false;
|
|
verify(has(receive(w,0x2a1,start(0,99)),0x4700c7)); // No loading/self-create gate here.
|
|
}
|
|
{auto w=fresh();w.loot.sent(0x3e00cc,pack_guid(actor.modern),w.entities);
|
|
verify(!has(receive(w,0x2a1,start(0,99)),0x4700c7));
|
|
verify(w.loot.rolls.size()==1&&!w.loot.rolls[0].published); // Opener waits for loot response.
|
|
}
|
|
// The original API's constant third result is one displayed roll slot,
|
|
// not a template-derived quantity. All cache states must behave alike.
|
|
for(auto method:{TurtleGroupLootMethod::group,TurtleGroupLootMethod::need_before_greed})
|
|
for(unsigned cache=0;cache<5;++cache){auto w=fresh();w.loot.group->loot->method=method;
|
|
if(cache){ItemTemplateReply tpl;tpl.entry=99;tpl.found=cache!=4;tpl.stack=cache==1?1:cache==2?20:0;
|
|
(cache==1 || cache==3?w.inventory.templates:w.inventory.peer_templates)[99]=tpl;}
|
|
auto shown=receive(w,0x2a1,start(3,99));verify(shown.size()==1);
|
|
auto object=w.loot.rolls[0].object;golden(shown[0],object,3,uint8_t(method));
|
|
verify(w.loot.rolls[0].published && !w.loot.rolls[0].quantity_known);
|
|
verify(!w.loot.window && !w.loot.opening && w.inventory.requests.empty() && w.inventory.queries.empty());
|
|
const auto deadline=w.loot.rolls[0].deadline;
|
|
verify(receive(w,0x2a1,start(3,99)).empty() && w.loot.rolls[0].deadline==deadline);
|
|
auto second=receive(w,0x2a1,start(4,99));verify(second.size()==1 && w.loot.rolls[1].object==object);
|
|
golden(second[0],object,4,uint8_t(method));
|
|
auto input=vote(object,3,1);auto source_vote=w.gameplay(0x3e00d3,input);
|
|
verify(source_vote.opcode==0x2a0 && source_vote.body==turtle_loot_roll_vote(owner,3,1));
|
|
verify(w.gameplay(0x3e00d3,input).body==source_vote.body);w.gameplay_sent(0x3e00d3,input);
|
|
rejects([&]{w.gameplay(0x3e00d3,input);});
|
|
verify(has(receive(w,0x2a2,update(3,99,peer,128,2)),0x4700c8));
|
|
auto result=receive(w,0x29f,won(3,99,selfid));verify(has(result,0x4700cc));
|
|
verify(w.loot_receipts.intents.empty()); // UI slot count must never be an award amount.
|
|
verify(receive(w,0x29f,won(3,99,selfid)).empty());
|
|
verify(has(receive(w,0x29e,all_passed(4,99)),0x4700cb) && w.loot.rolls.empty());
|
|
}
|
|
// Even max-stack 1 does not clamp LootItem::count. A real response with
|
|
// count 3 upgrades the quantity without duplicating the existing popup.
|
|
{auto w=fresh();ItemTemplateReply tpl;tpl.entry=99;tpl.found=true;tpl.stack=1;w.inventory.peer_templates[99]=tpl;
|
|
verify(receive(w,0x2a1,start(3,99)).size()==1);auto object=w.loot.rolls[0].object;
|
|
auto open=pack_guid(actor.modern);verify(w.gameplay(0x3e00cc,open).opcode==0x15d);w.gameplay_sent(0x3e00cc,open);
|
|
Bytes body;le(body,owner,8);le(body,1,1);le(body,0,4);le(body,1,1);
|
|
le(body,3,1);le(body,99,4);le(body,3,4);le(body,100,4);le(body,0,4);le(body,0,4);le(body,1,1);
|
|
auto window=receive(w,0x160,body);verify(has(window,0x4700bf) && !has(window,0x4700c7));
|
|
verify(w.loot.window->object==object && w.loot.rolls[0].object==object &&
|
|
w.loot.rolls[0].quantity_known && w.loot.rolls[0].item->count==3);
|
|
verify(has(receive(w,0x29f,won(3,99,selfid)),0x4700cc));
|
|
verify(w.loot_receipts.intents.size()==1 && w.loot_receipts.intents.front().count==3);
|
|
}
|
|
std::cout<<"Q56 source roll-slot checks "<<assertions<<" PASS\n";
|
|
}
|
|
#endif
|
|
auto event=[&](uint32_t op,const Bytes& b){return loot.translate({op,b},self,70,1,entities,codec);};
|
|
auto open=pack_guid(actor.modern);
|
|
// Opener waits for the matching actual loot response and its exact quantities.
|
|
loot.sent(0x3e00cc,open,entities);
|
|
check(event(0x2a1,start(0,25)).empty() && loot.rolls.size()==1 && !loot.rolls[0].published);
|
|
{ // Original source-client roll presentation is one slot, independent of templates.
|
|
NativeLootTranslation peer_loot;peer_loot.group=group;std::map<uint32_t,ItemTemplateReply> templates;
|
|
ItemTemplateReply sword;sword.entry=99;sword.found=true;sword.stack=1;templates[99]=sword;
|
|
ItemTemplateReply herb;herb.entry=98;herb.found=true;herb.stack=20;templates[98]=herb;peer_loot.templates=&templates;
|
|
auto shown=peer_loot.translate({0x2a1,start(3,99)},self,70,1,entities,codec);
|
|
check(shown.size()==1 && shown[0].opcode==0x4700c7 && peer_loot.rolls.size()==1 && peer_loot.rolls[0].published);
|
|
auto request=peer_loot.gameplay(0x3e00d3,vote(peer_loot.rolls[0].object,3,1),entities);
|
|
check(request.opcode==0x2a0 && request.body==turtle_loot_roll_vote(owner,3,1));
|
|
check(peer_loot.translate({0x2a1,start(4,98)},self,70,1,entities,codec).size()==1 && peer_loot.rolls[1].published); // stackable also has one roll slot
|
|
ItemTemplateReply mace;mace.entry=96;mace.found=true;mace.stack=1;templates[96]=mace;
|
|
auto second=peer_loot.translate({0x2a1,start(6,96)},self,70,1,entities,codec);
|
|
check(second.size()==1 && peer_loot.rolls.back().published && peer_loot.rolls.back().object==peer_loot.rolls[0].object); // one ID per owner+generation
|
|
check(peer_loot.translate({0x2a1,start(5,97)},self,70,1,entities,codec).size()==1 && peer_loot.rolls.back().published); // no invented item metadata
|
|
}
|
|
loot.sent(0x3e00cc,open,entities);
|
|
check(event(0x2a1,start(0,25)).empty() && loot.rolls.size()==1);
|
|
auto deadline=loot.rolls[0].deadline;event(0x2a1,start(0,25));check(loot.rolls.size()==1 && loot.rolls[0].deadline==deadline);
|
|
event(0x2a1,start(7,32));check(loot.rolls.size()==2);
|
|
event(0x2a2,update(0,25,peer,128,2));check(loot.rolls[0].updates.size()==1);
|
|
auto out=event(0x160,response());check(out.size()==4 && out[0].opcode==0x4700bf && out[1].opcode==0x4700c7 && out[2].opcode==0x4700c8 && out[3].opcode==0x4700c7);
|
|
auto object=loot.window->object;check(loot.rolls[0].item->count==2 && loot.rolls[0].start.slot==0 && loot.rolls[1].item->count==4 && loot.rolls[1].object==object);
|
|
auto first=vote(object,0,1);auto request=loot.gameplay(0x3e00d3,first,entities);check(request.opcode==0x2a0 && request.body==turtle_loot_roll_vote(owner,0,1));
|
|
check(loot.gameplay(0x3e00d3,first,entities).body==request.body); // Prepare does not mark transmission.
|
|
loot.sent(0x3e00d3,first,entities);rejects([&]{loot.gameplay(0x3e00d3,first,entities);});
|
|
rejects([&]{loot.gameplay(0x3e00d3,vote(object,7,3),entities);});
|
|
auto expiry=loot.rolls[1].deadline;loot.rolls[1].deadline=std::chrono::steady_clock::now();rejects([&]{loot.gameplay(0x3e00d3,vote(object,7,2),entities);});loot.rolls[1].deadline=expiry;
|
|
++actor.generation;rejects([&]{loot.gameplay(0x3e00d3,vote(object,7,2),entities);});--actor.generation;
|
|
Bytes released;le(released,owner,8);le(released,1,1);event(0x161,released);check(!loot.window && loot.rolls.size()==2);
|
|
loot.sent(0x3e00cc,open,entities);out=event(0x160,response());check(out.size()==1 && loot.window->object==object);
|
|
check(event(0x2a2,update(0,25,peer,42,2)).empty());
|
|
out=event(0x29f,won(0,25,peer));check(out.size()==2 && out[0].opcode==0x4700c8 && out[1].opcode==0x4700cc && loot.rolls.size()==1);
|
|
check(loot.roll_chat_notices.size()==2 && loot.roll_chat_notices[0].starts_with("GWLOOT1 N ") && loot.roll_chat_notices[1].starts_with("GWLOOT1 W "));
|
|
check(loot.roll_chat_notices[0].ends_with(" 25 42 2 0 Gatewaypeer") && loot.roll_chat_notices[1].ends_with(" 25 42 2 0 Gatewaypeer"));
|
|
#ifndef GW_ROLL_TEST_CODEC
|
|
check(out[0].body.back()==1); // Recipient is not winner.
|
|
#endif
|
|
auto forged=update(7,99,peer,0,0);check(event(0x2a2,forged).empty() && loot.rolls.size()==1);
|
|
event(0x2a2,update(7,32,selfid,128,2));rejects([&]{loot.gameplay(0x3e00d3,vote(object,7,2),entities);});
|
|
out=event(0x29e,all_passed(7,32));check(out.size()==1 && out[0].opcode==0x4700cb && loot.rolls.empty());
|
|
check(loot.roll_chat_notices.size()==2); // No invented number for selection/all-pass.
|
|
Bytes click;le(click,1,4);append(click,pack_guid(object));le(click,7,1);le(click,0,1);
|
|
auto item_requests=loot.items(click,entities);check(item_requests.size()==1 && item_requests[0].opcode==0x108 && item_requests[0].body==Bytes{7});
|
|
loot.group_transition=true;rejects([&]{loot.gameplay(0x3e00d3,vote(object,7,2),entities);});
|
|
NativeLootTranslation winner;winner.group=group;winner.sent(0x3e00cc,open,entities);
|
|
auto winning=[&](uint32_t op,const Bytes& b){return winner.translate({op,b},self,70,1,entities,codec);};
|
|
winning(0x2a1,start(0,25));check(winning(0x2a2,update(0,25,selfid,96,1)).empty());
|
|
check(winner.roll_chat_notices.empty()); // Source numeric packet alone is not terminal.
|
|
auto published=winning(0x160,response());check(published.size()==2); // No premature numeric result.
|
|
out=winning(0x29f,won(0,25,selfid,96,1));check(out.size()==2 && out[0].opcode==0x4700c8 && out[1].opcode==0x4700cc);
|
|
check(winner.roll_chat_notices.size()==2 && winner.roll_chat_notices[0].ends_with(" 25 96 1 1 Gatewaytest") && winner.roll_chat_notices[1].ends_with(" 25 96 1 1 Gatewaytest"));
|
|
#ifndef GW_ROLL_TEST_CODEC
|
|
check(out[0].body.back()==0); // Actual winner's result is ALLOW_LOOT.
|
|
#endif
|
|
check(winning(0x29f,won(0,25,selfid)).empty()); // No duplicate result or terminal.
|
|
check(winner.roll_chat_notices.size()==2); // Duplicate terminal cannot duplicate chat.
|
|
// A completed roll before 0x160 must never become a stale popup.
|
|
NativeLootTranslation late;late.group=group;
|
|
auto late_event=[&](uint32_t op,const Bytes& b){return late.translate({op,b},self,70,1,entities,codec);};
|
|
late.sent(0x3e00cc,open,entities);late_event(0x2a1,start(0,25));
|
|
check(late_event(0x29f,won(0,25,peer)).empty() && late.rolls.empty());
|
|
out=late_event(0x160,response());check(out.size()==1 && out[0].opcode==0x4700bf);
|
|
// A genuine start received after acquisition may use the existing row.
|
|
out=late_event(0x2a1,start(0,25));check(out.size()==1 && out[0].opcode==0x4700c7 && late.rolls[0].object==late.window->object);
|
|
// Unmatched item/slot cannot manufacture a quantity or offer a vote.
|
|
NativeLootTranslation mismatch;mismatch.group=group;
|
|
auto other=[&](uint32_t op,const Bytes& b){return mismatch.translate({op,b},self,70,1,entities,codec);};
|
|
mismatch.sent(0x3e00cc,open,entities);other(0x2a1,start(0,99));out=other(0x160,response());
|
|
check(out.size()==1 && !mismatch.rolls[0].published && !mismatch.rolls[0].item);
|
|
rejects([&]{mismatch.gameplay(0x3e00d3,vote(mismatch.window->object,0,1),entities);});
|
|
mismatch.rolls[0].deadline=std::chrono::steady_clock::now();other(0x2a2,update(0,99,peer,0,0));check(mismatch.rolls.empty());
|
|
NativeLootTranslation buffered;buffered.group=group;
|
|
auto early=[&](uint32_t op,const Bytes& b){return buffered.translate({op,b},self,70,1,entities,codec);};
|
|
buffered.sent(0x3e00cc,open,entities);early(0x2a1,start(0,25));early(0x2a2,update(0,25,selfid,0,0));early(0x160,response());
|
|
rejects([&]{buffered.gameplay(0x3e00d3,vote(buffered.window->object,0,1),entities);});
|
|
// A group transition invalidates even a previously admitted roll.
|
|
late_event(0x77,{});check(late.rolls.empty());rejects([&]{late.gameplay(0x3e00d3,vote(late.window->object,0,1),entities);});
|
|
#if defined(GW_ROLL_TEST_WORLD) && defined(GW_ROLL_TEST_CODEC)
|
|
// Verify the actual world dispatcher admits Source roll opcodes and the
|
|
// native request, paired with the real original-writer codec ABI.
|
|
NativeWorldTranslation world;world.self_created=true;world.selected=self;world.realm=70;
|
|
world.location=WorldLocation{1,{0,0,0,0}};world.entities.actors[owner]=actor;world.loot.group=group;
|
|
check(world.gameplay(0x3e00cc,open).opcode==0x15d);world.gameplay_sent(0x3e00cc,open);
|
|
auto raw=world.translate({0x2a1,start(0,25)},0x46010046,0,serialize);Reader before(raw);check(before.le(4)==0);
|
|
raw=world.translate({0x160,response()},0x46010046,0,serialize);Reader bundle(raw);check(bundle.le(4)==2);
|
|
check(bundle.le(4)==0x4700bf);bundle.take(bundle.le(4));check(bundle.le(4)==0x4700c7);bundle.take(bundle.le(4));bundle.end();
|
|
check(world.gameplay(0x3e00d3,vote(world.loot.window->object,0,1)).opcode==0x2a0);
|
|
#endif
|
|
std::cout<<"group roll same-window lifecycle PASS\n";return 0;
|
|
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}}
|