#pragma once #include "economy_store.h" namespace PortEconomy { // Bounded, ASCII-only presentation over the existing authenticated DB reply. // Build the complete snapshot before emitting any frames. No UI read mutates // wallets, item ownership or order state, and no display slot is an asset ID. constexpr unsigned ViewPageSize=20; inline bool SignedNumber(const std::string& text,int64_t& out,int64_t minimum,int64_t maximum) { const bool negative=!text.empty()&&text[0]=='-';uint64_t value=0; if(!Number(negative?text.substr(1):text,value,negative?static_cast(-minimum):static_cast(maximum)))return false; out=negative?-static_cast(value):static_cast(value);return out>=minimum&&out<=maximum; } inline bool HexBytes(const std::string& text,size_t bytes) { if(text.size()!=bytes*2)return false; for(char c:text)if(!((c>='0'&&c<='9')||(c>='a'&&c<='f')||(c>='A'&&c<='F')))return false; return true; } template bool View(DB& db,const Request& r,std::function height,std::vector& frames) { frames.clear();Rows rows;if(!Valid(r)||r.operation!=UiStatus)return false; // Transaction-scoped isolation applies to this read only, not later wallet // settlements on the shared direct connection. if(!db.Query("SET TRANSACTION ISOLATION LEVEL REPEATABLE READ",rows)||!db.Begin())return false; auto fail=[&](){db.Rollback();frames.clear();return false;}; const auto pid=std::to_string(r.player),aid=std::to_string(r.account),seq=std::to_string(r.generation); if(!db.Query("SELECT amount FROM port_economy_credit WHERE pid="+pid,rows)||rows.size()>1)return fail(); uint64_t credit=0;if(!rows.empty()&&!Number(rows[0][0],credit,GoldLimit))return fail(); std::string columns="c.vnum,c.count,"+std::string(r.slot==2?"o.state":"l.state")+",c.socket0,c.socket1,c.socket2"; for(unsigned i=0;i<7;++i)columns+=",c.attrtype"+std::to_string(i)+",c.attrvalue"+std::to_string(i); columns+=",HEX(c.port_extra)"; std::string sql=r.slot==2 ? "SELECT o.id,o.price,l.owner,"+columns+" FROM port_economy_orders o JOIN port_economy_listings l ON l.id=o.listing LEFT JOIN port_economy_custody c ON c.id=l.item WHERE o.buyer="+pid+" AND o.account="+aid+" AND o.state='READY'" : "SELECT l.id,l.price,l.owner,"+columns+" FROM port_economy_listings l LEFT JOIN port_economy_custody c ON c.id=l.item WHERE "+ (r.slot==1?"l.owner="+pid+" AND l.state IN('OPEN','RESERVED')":std::string("l.state='OPEN'")+(r.item?" AND l.owner="+std::to_string(r.item):"")); sql+=" ORDER BY "+std::string(r.slot==2?"o.id":"l.id")+" DESC LIMIT 21 OFFSET "+std::to_string(r.reference*ViewPageSize); if(!db.Query(sql,rows)||rows.size()>ViewPageSize+1)return fail(); const bool more=rows.size()>ViewPageSize;if(more)rows.pop_back(); frames.push_back("MTECON1 begin "+seq+" "+std::to_string(r.slot)+" "+std::to_string(r.reference)+" "+std::to_string(rows.size())+" "+std::to_string(more)+" "+std::to_string(credit)); for(size_t index=0;index(vnum));if(state==3||size<1||size>3)return fail(); std::string item="MTECON1 row "+seq+" "+std::to_string(index)+" "+std::to_string(id)+" "+std::to_string(price)+" "+std::to_string(owner)+" "+std::to_string(state)+" "+std::to_string(vnum)+" "+std::to_string(count)+" "+std::to_string(size); for(unsigned i=6;i<9;++i){int64_t socket=0;if(!SignedNumber(row[i],socket,INT32_MIN,INT32_MAX))return fail();item+=" "+std::to_string(socket);} frames.push_back(item); uint8_t attrs[21]={}; for(unsigned i=0;i<7;++i){uint64_t type=0;int64_t value=0; if(!Number(row[9+2*i],type,UINT8_MAX)||!SignedNumber(row[10+2*i],value,INT16_MIN,INT16_MAX))return fail(); const auto bits=static_cast(value);attrs[3*i]=static_cast(type);attrs[3*i+1]=static_cast(bits&255);attrs[3*i+2]=static_cast(bits>>8); } std::string extra=row[23];if(extra.empty())extra=std::string(138,'0');if(!HexBytes(extra,69))return fail(); frames.push_back("MTECON1 meta "+seq+" "+std::to_string(index)+" "+Hex(attrs,sizeof(attrs))+extra); } // A supplied receipt is scoped to the current account/player. On a fresh // process the latest result is informational; actionable assets come from // the authoritative orders/listings/credit views above. if(!db.Query("SELECT state,result,reference FROM port_economy_requests WHERE pid="+pid+" AND account="+aid+ (std::string(r.token)==std::string(32,'0')?" ORDER BY created_at DESC,token DESC LIMIT 1":" AND token='"+std::string(r.token)+"'"),rows)||rows.size()>1)return fail(); if(!rows.empty()) { uint64_t reference=0; if(rows[0].size()!=3||!Number(rows[0][2],reference)||rows[0][1].size()>64||(rows[0][0]!="DONE"&&rows[0][0]!="PENDING"))return fail(); frames.push_back("MTECON1 result "+seq+" "+rows[0][0]+" "+(rows[0][1].empty()?std::string("00"):Hex(rows[0][1].data(),rows[0][1].size()))+" "+std::to_string(reference)); } frames.push_back("MTECON1 end "+seq); for(const auto& frame:frames)if(frame.size()>=sizeof(Reply::message))return fail(); if(!db.Commit())return fail();return true; } }