server: 0019-deck-lists

TICKET-338: deck lists. STOC 0x5C: the owner gets its remaining cards sorted, observers the real order and the submitted Main Deck only when SpectatorsSeeAll(). Capability bit 0x100, protocol 6.

Replayed from Tools/YGOLegacy/patches/0019-deck-lists.patch in the Unity repo.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
donnyandClaude Fable 5.1 committed 2026-10-14 20:58:59 -07:00
1 parent a9e5f2d5eb
commit 980228945e
5 files changed
+274 -3

No files matched your search

+115 -1
View File
@@ -1,5 +1,6 @@
#include <array>
#include <utility>
#include <algorithm>
#include "config.h"
#include "single_duel.h"
#include "netserver.h"
@@ -79,7 +80,7 @@ void SingleDuel::SeatState(DuelPlayer* dp, unsigned char* pdata, int len) {
void SingleDuel::SendServerCapability(DuelPlayer* dp) {
CTOS_Capability cap;
cap.protocol = FEATURE_PROTOCOL;
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss
cap.flags = FEATURE_APPEARANCE | FEATURE_DIALOGUE | FEATURE_SETUP_PAIRS | FEATURE_SEAT_PLAN | FEATURE_RULES_RESTART | FEATURE_SEAT_STATE | FEATURE_LOBBY_AFTER_MATCH | FEATURE_COIN_TOSS | FEATURE_DECK_LIST; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match + coin toss + deck lists
unsigned char buf[SIZE_STOC_FEATURE];
const auto size = NetServer::CreateStampedPacket((unsigned char*)&cap, sizeof cap, buf, FEATURE_SENDER_SERVER);
NetServer::SendBufferToPlayer(dp, STOC_CAPABILITY, buf, size);
@@ -869,6 +870,7 @@ void SingleDuel::ReturnToLobby() {
hand_result[0] = 0;
hand_result[1] = 0;
NetServer::StopChooseTimer(); // YGOL coin toss
ResetDeckLists(); // YGOL deck lists
time_elapsed = 0;
last_response = 0;
registry_dump.clear();
@@ -1335,6 +1337,8 @@ void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
#endif
NetServer::StartDuelTimer();
}
ResetDeckLists(); // YGOL deck lists: the opening lists ride the first engine batch
MarkDeckLists();
Process();
}
void SingleDuel::Process() {
@@ -1356,6 +1360,8 @@ void SingleDuel::Process() {
stop = Analyze(engineBuffer.data(), engLen);
}
}
if(stop != 2) // YGOL deck lists: after every engine batch, before a prompt waits
FlushDeckLists();
if(stop == 2)
DuelEndProc();
}
@@ -1762,6 +1768,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
}
case MSG_SHUFFLE_DECK: {
player = BufferIO::Read<uint8_t>(pbuf);
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -1817,6 +1824,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
}
case MSG_REFRESH_DECK: {
pbuf++;
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -1828,6 +1836,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
}
case MSG_SWAP_GRAVE_DECK: {
player = BufferIO::Read<uint8_t>(pbuf);
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -1839,6 +1848,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
break;
}
case MSG_REVERSE_DECK: {
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -1945,6 +1955,8 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
if (cl & LOCATION_ONFIELD)
cp = NetServer::StripRevealFlag(pbufw, 8);
pbuf += 16;
if((pl | cl) & LOCATION_DECK)
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[cc], STOC_GAME_MSG, offset, pbuf - offset);
if(SpectatorsSeeAll()) // YGOL spectators see all: the full copy, before the mask
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -2001,6 +2013,8 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
int l2 = pbuf[13];
int s2 = pbuf[14];
pbuf += 16;
if((l1 | l2) & LOCATION_DECK)
MarkDeckLists(); // YGOL deck lists
NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
@@ -2243,6 +2257,7 @@ int SingleDuel::Analyze(unsigned char* msgbuffer, unsigned int len) {
case MSG_DRAW: {
player = BufferIO::Read<uint8_t>(pbuf);
count = BufferIO::Read<uint8_t>(pbuf);
MarkDeckLists(); // YGOL deck lists
pbufw = pbuf;
pbuf += count * 4;
NetServer::SendBufferToPlayer(players[player], STOC_GAME_MSG, offset, pbuf - offset);
@@ -2549,6 +2564,10 @@ void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int
std::memcpy(resb, pdata, len);
last_replay_response_size = last_replay.WriteResponse(resb, len);
set_responseb(pduel, resb);
#ifdef YGOPRO_SERVER_MODE
if(last_game_msg == MSG_SORT_CARD) // YGOL deck lists: a sorted deck top changes the order with no message of its own
MarkDeckLists();
#endif
players[dp->type]->state = 0xff;
if(host_info.time_limit) {
if(time_limit[dp->type] >= time_elapsed)
@@ -2682,6 +2701,21 @@ void SingleDuel::RequestField(DuelPlayer* dp) {
RefreshRemoved(1 - player, 0xefffff, 0, dp);
RefreshRemoved(player, 0xefffff, 0, dp);
// YGOL deck lists: the snapshot carries the lists this connection may see
if(watcher) {
if(SpectatorsSeeAll())
for(int i = 0; i < 2; ++i)
for(uint8_t kind = DECK_LIST_ORDERED; kind <= DECK_LIST_SUBMITTED; ++kind) {
std::vector<uint32_t> codes;
if(BuildDeckList(i, kind, codes))
SendDeckList(dp, i, kind, codes);
}
} else {
std::vector<uint32_t> codes;
if(BuildDeckList(player, DECK_LIST_SORTED, codes))
SendDeckList(dp, player, DECK_LIST_SORTED, codes);
}
// send MSG_REVERSE_DECK if deck is reversed
if(deck_reversed)
WriteMsg([&](uint8_t*& pbuf) {
@@ -2732,6 +2766,86 @@ void SingleDuel::RequestField(DuelPlayer* dp) {
NetServer::SendPacketToPlayer(dp, STOC_FIELD_FINISH);
}
#endif //YGOPRO_SERVER_MODE
// YGOL deck lists: STOC_DECK_LIST 0x5C. Marked by every message that touches a deck, flushed once per engine batch,
// sent only when a list differs from the last one that audience got. Kind 0 (sorted) goes to the deck's owner and never
// carries the order; kinds 1 (real order, top first) and 2 (the submitted Main Deck, sorted) go to observers only when SpectatorsSeeAll().
void SingleDuel::MarkDeckLists() {
deck_list_dirty = true;
}
void SingleDuel::ResetDeckLists() {
deck_list_dirty = false;
for(auto& side : deck_list_known)
for(auto& known : side)
known = false;
}
bool SingleDuel::BuildDeckList(int side, uint8_t kind, std::vector<uint32_t>& codes) {
codes.clear();
if(kind == DECK_LIST_SUBMITTED) {
for(auto cit : pdeck[side].main)
codes.push_back(cit->code);
std::sort(codes.begin(), codes.end());
return true;
}
if(!pduel)
return false;
static std::vector<unsigned char> query(SIZE_QUERY_BUFFER);
const int len = query_field_card(pduel, (uint8_t)side, LOCATION_DECK, QUERY_CODE, query.data(), 0);
unsigned char* qbuf = query.data();
const unsigned char* end = qbuf + len;
while(qbuf + 4 <= end) {
const auto clen = BufferIO::Read<int32_t>(qbuf);
if(clen < 12 || qbuf + clen - 4 > end)
break;
codes.push_back(*(uint32_t*)(qbuf + 4));
qbuf += clen - 4;
}
if(kind == DECK_LIST_ORDERED)
std::reverse(codes.begin(), codes.end()); // the engine lists the bottom card first
else
std::sort(codes.begin(), codes.end());
return true;
}
void SingleDuel::SendDeckList(DuelPlayer* dp, int side, uint8_t kind, const std::vector<uint32_t>& codes) {
if(!dp)
return;
unsigned char buf[sizeof(STOC_DeckList) + MAINC_MAX * sizeof(uint32_t)];
STOC_DeckList head;
head.version = DECK_LIST_VERSION;
head.side = (uint8_t)side;
head.kind = kind;
head.count = (uint16_t)std::min<size_t>(codes.size(), MAINC_MAX);
auto p = buf;
std::memcpy(p, &head, sizeof head);
p += sizeof head;
for(uint16_t i = 0; i < head.count; ++i)
BufferIO::Write<uint32_t>(p, codes[i]);
NetServer::SendBufferToPlayer(dp, STOC_DECK_LIST, buf, p - buf);
}
void SingleDuel::FlushDeckLists() {
if(!deck_list_dirty || !pduel)
return;
deck_list_dirty = false;
std::vector<uint32_t> codes;
for(int side = 0; side < 2; ++side) {
for(uint8_t kind = 0; kind < DECK_LIST_KINDS; ++kind) {
if(kind != DECK_LIST_SORTED && (!SpectatorsSeeAll() || observers.empty())) {
deck_list_known[side][kind] = false; // a later joiner's snapshot is the new baseline
continue;
}
if(!BuildDeckList(side, kind, codes))
continue;
if(deck_list_known[side][kind] && deck_list_sent[side][kind] == codes)
continue;
deck_list_known[side][kind] = true;
deck_list_sent[side][kind] = codes;
if(kind == DECK_LIST_SORTED) {
SendDeckList(players[side], side, kind, codes);
} else
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
SendDeckList(*oit, side, kind, codes);
}
}
}
void SingleDuel::TimeConfirm(DuelPlayer* dp) {
if(host_info.time_limit == 0)
return;