Files
AVA2/tests/offline_shop_test.cpp
T
shenleiandClaude Opus 5.5 9077aa389a AVA2 client Windows parity passes (gr2 BitKnit2/64-bit, GamePhase handlers, python modules)
- gr2: BitKnit2 decompression, 64-bit LE files, compressed curves
- net: remaining GamePhase handlers, generated GC packet table
- python: event/chat/chr/minimap/guild/quest/messenger/shop-search and
  other native modules ported against the AVA2 Windows client
- render: D3D8 fixed-function lighting in Gr2ModelRenderer, terrain
  without baked shade; cape/NPC equipment attachment
- tests + audits updated; docs/PORT-PLAN.md and WINDOWS-PARITY.md

Batch 4 (render pipeline: background.*, app.RenderGame/UpdateGame,
camera bindings, main.cpp fake-content removal) is still in progress.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-08 10:43:01 +09:00

94 lines
8.7 KiB
C++

#include "python_runtime.h"
#include "eter_net/PythonNetworkStream.h"
#include <iostream>
#include <stdexcept>
using namespace ava2;
using Bytes=std::vector<uint8_t>;
void require(bool ok,const char* why) {if(!ok)throw std::runtime_error(why);}
void put(Bytes& b,uint64_t v,unsigned width=4) {for(unsigned i=0;i<width;++i)b.push_back(uint8_t(v>>(8*i)));}
void text(Bytes& b,const std::string& s,size_t width) {for(size_t i=0;i<width;++i)b.push_back(i<s.size()?s[i]:0);}
Bytes frame(uint8_t sub,const Bytes& payload) {Bytes b{47};put(b,4+payload.size(),2);b.push_back(sub);b.insert(b.end(),payload.begin(),payload.end());return b;}
void send(uint8_t sub,const Bytes& data) {auto& s=PythonNetworkStream::Instance();s.PushRecvData(frame(sub,data));s.ProcessRecvData();}
Bytes item(uint32_t vnum=0x80000001) {
Bytes b;put(b,123);put(b,vnum);put(b,uint64_t(-900000000000LL),8);put(b,uint32_t(-3));b.push_back(77);
for(int i=0;i<8;++i)put(b,uint32_t(-i-1));
for(int i=0;i<15;++i){b.push_back(uint8_t(i+1));put(b,uint16_t(-i-1),2);}
b.push_back(2);text(b,std::string(25,'x'),25);put(b,0xffffffff);return b;
}
Bytes log(const std::string& name,int64_t price) {Bytes b;text(b,name,25);text(b,"date",25);put(b,0xffffffff);put(b,0x80000000);put(b,uint64_t(price),8);return b;}
Bytes start() {
Bytes b;put(b,0xffffffff);auto row=item();b.insert(b.end(),row.begin(),row.end());b.resize(4+80*128,0);
put(b,0xffffffff);text(b,std::string(46,'t'),46);put(b,uint64_t(-700000000000LL),8);put(b,0x80000000);
for(int i=0;i<80;++i){auto entry=log(i==79?"last":"first",i==79?10:i==0?-20:0);b.insert(b.end(),entry.begin(),entry.end());}
put(b,0xffffffffffffffffULL,8);put(b,uint32_t(-12));put(b,0xffffffff);b.push_back(2);return b;
}
int main(int argc,char** argv) {
if(argc!=3)return 2;
try {
PythonRuntime python(argv[1],argv[2]);auto& s=PythonNetworkStream::Instance();s.ResetConnection();s.SetPhase(PHASE_GAME);
python.execute(R"PY(import net,shop
class Game:
def __init__(self): self.events=[]
def __getattr__(self,name):
def event(*args):
self.events.append((name,)+args)
if name=='StartOfflineShop': assert shop.GetOfflineShopItemVnum(0)==-2147483647
return event
game=Game()
net.SetPhaseWindow(net.PHASE_WINDOW_GAME,game)
shop.Clear()
assert shop.GetSign()=='NONE' and shop.HasOfflineShop()==0 and shop.IsOfflineShop()==0
assert shop.GetOfflineShopItemBuyerName(0)=='None' and shop.GetOfflineShopItemAttribute(0,0)==(0,0)
shop.AddOfflineShopItemStock(1,4,7,900000000000)
assert shop.GetOfflineShopItemPriceReal(1,4)==900000000000
)PY");
auto payload=start();require(payload.size()==15603&&item().size()==128&&log("",0).size()==66,"offline independent fixture sizes");
auto wire=frame(0,payload);wire.push_back(44);s.PushRecvData(wire.data(),wire.size()-2);s.ProcessRecvData();python.execute("assert game.events==[]\n");s.PushRecvData(wire.data()+wire.size()-2,2);s.ProcessRecvData();require(s.PopSendBytes()==Bytes{254},"offline fragmented start lost ping");
python.execute(R"PY(assert game.events==[('ClearOfflineShopLog',),('AppendShopLog','last','date',-1,-2147483648,10),('AppendShopLog','first','date',-1,-2147483648,-20),('StartOfflineShop',-1,1)]
assert shop.IsOfflineShop()==0 and shop.HasOfflineShop()==0
assert shop.GetSign()=='t'*46 and shop.GetDisplayedCount()==-1 and shop.GetRealWatcherCount()==-2147483648
assert shop.GetCurrentOfflineShopMoney()==-700000000000 and shop.GetShopFlag()==-1 and shop.GetShopType()==-1 and shop.GetShopTime()==-12
assert shop.GetOfflineShopItemID(0)==shop.GetOfflineShopItemVnum(0)==-2147483647
assert shop.GetOfflineShopItemCount(0)==-3 and shop.GetOfflineShopItemPrice(0)==-900000000000
assert shop.GetOfflineShopItemMetinSocket(0,7)==-8 and shop.GetOfflineShopItemMetinSocket(0,8)==0
assert shop.GetOfflineShopItemAttribute(0,14)==(15,-15) and shop.GetOfflineShopItemAttribute(0,15)==(0,0)
assert shop.GetOfflineShopItemStatus(0)==2 and shop.GetOfflineShopID(0)==-1 and shop.GetOfflineShopItemBuyerName(0)=='x'*25
assert shop.GetOfflineShopItemVnum(80)==0 and shop.GetOfflineShopItemVnum(-1)==0
assert shop.GetOfflineShopItemPriceReal(1,4)==0
game.events=[]
)PY");
Bytes update{1};auto row=item(0);update.insert(update.end(),row.begin(),row.end());put(update,17);put(update,18);text(update,"Updated",46);put(update,900000000000ULL,8);auto entry=log("buyer",30);update.insert(update.end(),entry.begin(),entry.end());put(update,5,8);put(update,uint32_t(-7));put(update,6);require(update.size()==273,"update fixture size");send(2,update);
python.execute("assert game.events==[('AppendShopLogFirst','buyer','date',-1,-2147483648,30),('RefreshOfflineShop',)]\nassert shop.GetOfflineShopItemBuyerName(1)=='None' and shop.GetCurrentOfflineShopMoney()==900000000000\nassert shop.GetSign()=='Updated' and shop.GetShopFlag()==5 and shop.GetShopTime()==-7 and shop.GetShopType()==6\ngame.events=[]\n");
// UPDATE stores second-tab data and does not append a zero-price log.
update[0]=81;row=item(1234);std::copy(row.begin(),row.end(),update.begin()+1);
std::fill(update.begin()+249,update.begin()+257,0);send(2,update);
python.execute("assert shop.GetOfflineShopItemVnum(81)==1234 and game.events==[('RefreshOfflineShop',)]\ngame.events=[]\n");
send(14,{1,44});require(s.PopSendBytes()==Bytes{254},"scalar offline branch consumed trailing payload");
for(uint8_t flag:{1,2,0}) {send(14,{flag});python.execute(flag==1?"assert shop.HasOfflineShop()==1\n":"assert shop.HasOfflineShop()==0\n");}
Bytes scalar;put(scalar,uint64_t(-90),8);send(13,scalar);scalar.clear();put(scalar,20);send(15,scalar);scalar.clear();put(scalar,30);send(16,scalar);scalar.clear();put(scalar,40);put(scalar,50);send(17,scalar);scalar.clear();text(scalar,"Title",46);send(18,scalar);
python.execute("assert shop.GetCurrentOfflineShopMoney()==-90 and shop.GetDisplayedCount()==40 and shop.GetRealWatcherCount()==50 and shop.GetSign()=='Title'\nassert game.events==[('RefreshOfflineShop',)]*5\ngame.events=[]\n");
for(uint8_t sub:{5,9,7,8,19,4,6,255})send(sub,{44});
require(s.PopSendBytes().empty(),"unknown/error offline subheader did not consume payload");
python.execute("assert game.events==[('OnShopError','NOT_ENOUGH_MONEY'),('OnShopError','SOLDOUT'),('OnShopError','INVENTORY_FULL'),('OnShopError','INVALID_POS'),('OnShopError','NOT_ENOUGH_LUCKY')]\ngame.events=[]\nshop.SetShopFlag(-1)\nshop.SetShopType(-2)\nshop.SetShopTime(-3)\nassert shop.GetShopFlag()==-1 and shop.GetShopType()==-2 and shop.GetShopTime()==-3\n");
send(1,{});python.execute("assert game.events==[('ClearOfflineShopLog',),('EndOfflineShop',)]\nassert shop.GetOfflineShopItemVnum(0)==0 and shop.GetSign()=='NONE' and shop.GetShopFlag()==0 and shop.GetShopTime()==0 and shop.GetShopType()==0\ngame.events=[]\n");
python.execute(R"PY(shop.AddOfflineShopItemStock(1,8,20,10)
shop.AddOfflineShopItemStock(1,4,9,900000000000)
shop.AddOfflineShopItemStock(1,4,2,-900000000000)
shop.AddOfflineShopItemStock(257,65544,21,20)
assert shop.GetOfflineShopItemPriceReal(1,8)==20
shop.AddOfflineShopItemStock(2,5,3,30)
shop.DelOfflineShopItemStock(2,5)
shop.BuildOfflineShop('a'*60,-1,258)
)PY");
Bytes expected(53,0);expected[0]=58;std::fill(expected.begin()+1,expected.begin()+46,'a');expected[47]=2;std::fill(expected.begin()+48,expected.begin()+52,255);expected[52]=2;
for(auto [cell,price,display]:{std::tuple{4,-900000000000LL,2},std::tuple{8,20LL,21}}) {put(expected,0);put(expected,0);expected.push_back(1);put(expected,cell,2);put(expected,uint64_t(price),8);expected.push_back(display);}
require(s.PopSendBytes()==expected,"build offline shop wire/sort/replace mismatch");
python.execute("shop.ChangeDecoration('literal %s',258,259)\n");expected.assign(110,0);expected[0]=57;expected[1]=12;std::string sign="literal %s";std::copy(sign.begin(),sign.end(),expected.begin()+58);expected[108]=2;expected[109]=3;require(s.PopSendBytes()==expected,"decoration wire mismatch");
python.execute("shop.ClearOfflineShopStock()\nassert shop.GetOfflineShopItemPriceReal(1,4)==0\nshop.BuildOfflineShop('',0,0)\n");expected.assign(53,0);expected[0]=58;require(s.PopSendBytes()==expected,"empty stock build mismatch");
// Short payloads reject without mutating state or reading a following frame.
for(uint8_t sub:{0,2,13,14,15,16,17,18}) {auto bad=frame(sub,{});bad.push_back(44);s.PushRecvData(bad);s.ProcessRecvData();require(s.PopSendBytes().empty(),"short offline payload consumed next frame");}
python.execute("assert game.events==[]\n");require(s.GetUnportedPackets().empty(),"offline shop still skipped");s.ResetConnection();
std::cout<<"AVA2 offline shop state, wire and Python callback parity passed\n";
}catch(const std::exception& e){std::cerr<<e.what()<<'\n';return 1;}
}