From 980228945e3fbef0945e843be2dedaf0e292d544 Mon Sep 17 00:00:00 2001 From: donnyjang Date: Wed, 14 Oct 2026 20:58:59 -0700 Subject: [PATCH] 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 --- server/gframe/network.h | 21 +++++- server/gframe/single_duel.cpp | 116 +++++++++++++++++++++++++++++++- server/gframe/single_duel.h | 9 +++ server/gframe/tag_duel.cpp | 121 +++++++++++++++++++++++++++++++++- server/gframe/tag_duel.h | 10 +++ 5 files changed, 274 insertions(+), 3 deletions(-) diff --git a/server/gframe/network.h b/server/gframe/network.h index cddc20f..a01672c 100644 --- a/server/gframe/network.h +++ b/server/gframe/network.h @@ -224,7 +224,7 @@ static_assert(sizeof(CTOS_Appearance) == 352, "size mismatch: CTOS_Appearance"); check_trivially_copyable(CTOS_Dialogue); static_assert(sizeof(CTOS_Dialogue) == 204, "size mismatch: CTOS_Dialogue"); constexpr int SIZE_STOC_FEATURE = sizeof(uint16_t) + sizeof(CTOS_Appearance); -constexpr uint16_t FEATURE_PROTOCOL = 5; // YGOL host seats: 1 -> 2; YGOL seat state: 2 -> 3; YGOL lobby after match: 3 -> 4; YGOL coin toss: 4 -> 5 +constexpr uint16_t FEATURE_PROTOCOL = 6; // YGOL host seats: 1 -> 2; YGOL seat state: 2 -> 3; YGOL lobby after match: 3 -> 4; YGOL coin toss: 4 -> 5; YGOL deck lists: 5 -> 6 constexpr uint32_t FEATURE_APPEARANCE = 0x1; constexpr uint32_t FEATURE_DIALOGUE = 0x2; constexpr uint16_t FEATURE_SENDER_SERVER = 0xff; @@ -415,6 +415,24 @@ constexpr uint8_t COIN_FACE_HEADS = 0; constexpr uint8_t COIN_FACE_TAILS = 1; constexpr uint8_t COIN_FACE_NONE = 0xFF; constexpr uint8_t COIN_CHOOSE_SECONDS = 10; +// YGOL deck lists: the deck's owner gets its remaining cards SORTED (the order never leaves the server); when SpectatorsSeeAll() +// every observer gets the real order top first and the submitted Main Deck of the duelist whose deck is in the zone +#pragma pack(push, 1) +struct STOC_DeckList { + uint8_t version{}; + uint8_t side{}; + uint8_t kind{}; + uint16_t count{}; +}; +#pragma pack(pop) +check_trivially_copyable(STOC_DeckList); +static_assert(sizeof(STOC_DeckList) == 5, "size mismatch: STOC_DeckList"); +constexpr uint32_t FEATURE_DECK_LIST = 0x100; +constexpr uint8_t DECK_LIST_VERSION = 1; +constexpr uint8_t DECK_LIST_SORTED = 0; +constexpr uint8_t DECK_LIST_ORDERED = 1; +constexpr uint8_t DECK_LIST_SUBMITTED = 2; +constexpr int DECK_LIST_KINDS = 3; // the one-use bootstrap secret of a replacement server: a host-string token parsed apart from the ygol.* pairs, never stored anywhere else extern uint8_t bootstrap_secret[16]; extern bool bootstrap_closed; @@ -606,6 +624,7 @@ inline bool IsFeatureMessage(unsigned char pktType) { #define STOC_SEAT_STATE 0x59 // uint16_t + CTOS_SeatState (YGOL seat state) #define STOC_MATCH_OVER 0x5A // uint16_t 0xFF + STOC_MatchOver (YGOL lobby after match) #define STOC_COIN_TOSS 0x5B // STOC_CoinToss, no sender stamp (YGOL coin toss) +#define STOC_DECK_LIST 0x5C // STOC_DeckList + uint32_t codes[count], no sender stamp (YGOL deck lists) // STOC_GAME_MSG header #define MSG_WAITING 3 diff --git a/server/gframe/single_duel.cpp b/server/gframe/single_duel.cpp index 71ee822..ef0043a 100644 --- a/server/gframe/single_duel.cpp +++ b/server/gframe/single_duel.cpp @@ -1,5 +1,6 @@ #include #include +#include #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(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(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(pbuf); count = BufferIO::Read(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 codes; + if(BuildDeckList(i, kind, codes)) + SendDeckList(dp, i, kind, codes); + } + } else { + std::vector 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& 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 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(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& 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(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(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 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; diff --git a/server/gframe/single_duel.h b/server/gframe/single_duel.h index d857fac..51eb661 100644 --- a/server/gframe/single_duel.h +++ b/server/gframe/single_duel.h @@ -92,6 +92,15 @@ protected: CTOS_Appearance appearance[2]{}; CTOS_SeatState seat_state[2]{}; // YGOL seat state void BeginChoose(int chooser, uint8_t face); // YGOL coin toss + // YGOL deck lists + void MarkDeckLists(); + void ResetDeckLists(); + void FlushDeckLists(); + bool BuildDeckList(int side, uint8_t kind, std::vector& codes); + void SendDeckList(DuelPlayer* dp, int side, uint8_t kind, const std::vector& codes); + std::vector deck_list_sent[2][DECK_LIST_KINDS]; + bool deck_list_known[2][DECK_LIST_KINDS]{}; + bool deck_list_dirty{}; // YGOL host seats void SendToLobby(unsigned char proto, void* buffer, size_t len); size_t SeatPlanPacket(unsigned char* buf, uint16_t revision); diff --git a/server/gframe/tag_duel.cpp b/server/gframe/tag_duel.cpp index dd6dc0a..ad5c7af 100644 --- a/server/gframe/tag_duel.cpp +++ b/server/gframe/tag_duel.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "config.h" #include "tag_duel.h" #include "netserver.h" @@ -77,7 +78,7 @@ void TagDuel::SeatState(DuelPlayer* dp, unsigned char* pdata, int len) { void TagDuel::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); @@ -836,6 +837,7 @@ void TagDuel::ReturnToLobby() { hand_result[0] = 0; hand_result[1] = 0; NetServer::StopChooseTimer(); // YGOL coin toss + ResetDeckLists(); // YGOL deck lists turn_count = 0; time_elapsed = 0; last_response = 0; @@ -1348,6 +1350,8 @@ void TagDuel::TPResult(DuelPlayer* dp, unsigned char tp) { #endif NetServer::StartDuelTimer(); } + ResetDeckLists(); // YGOL deck lists: the opening lists ride the first engine batch + MarkDeckLists(); Process(); } void TagDuel::Process() { @@ -1369,6 +1373,8 @@ void TagDuel::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(); } @@ -1729,6 +1735,7 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { } case MSG_SHUFFLE_DECK: { player = BufferIO::Read(pbuf); + MarkDeckLists(); // YGOL deck lists NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset); NetServer::ReSendToPlayer(players[1]); NetServer::ReSendToPlayer(players[2]); @@ -1798,6 +1805,7 @@ int TagDuel::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]); NetServer::ReSendToPlayer(players[2]); @@ -1811,6 +1819,7 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { } case MSG_SWAP_GRAVE_DECK: { player = BufferIO::Read(pbuf); + MarkDeckLists(); // YGOL deck lists NetServer::SendBufferToPlayer(players[0], STOC_GAME_MSG, offset, pbuf - offset); NetServer::ReSendToPlayer(players[1]); NetServer::ReSendToPlayer(players[2]); @@ -1824,6 +1833,7 @@ int TagDuel::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]); NetServer::ReSendToPlayer(players[2]); @@ -1951,6 +1961,8 @@ int TagDuel::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 // [YGOL] shared team hand: both teammates see the moved card. tp = (cc == 0) ? 0 : 2; NetServer::SendBufferToPlayer(players[tp], STOC_GAME_MSG, offset, pbuf - offset); @@ -2017,6 +2029,8 @@ int TagDuel::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]); NetServer::ReSendToPlayer(players[2]); @@ -2295,6 +2309,7 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { case MSG_DRAW: { player = BufferIO::Read(pbuf); count = BufferIO::Read(pbuf); + MarkDeckLists(); // YGOL deck lists pbufw = pbuf; pbuf += count * 4; // [YGOL] shared team hand: both teammates see what was drawn. @@ -2624,6 +2639,8 @@ int TagDuel::Analyze(unsigned char* msgbuffer, unsigned int len) { } case MSG_TAG_SWAP: { player = BufferIO::Read(pbuf); + deck_in_zone[player & 1] ^= 1; // YGOL deck lists: the partner's deck is in the zone now + MarkDeckLists(); /*int mcount = */BufferIO::Read(pbuf); int ecount = BufferIO::Read(pbuf); /*int pcount = */BufferIO::Read(pbuf); @@ -2683,6 +2700,10 @@ void TagDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int len 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) { int resp_type = dp->type < 2 ? 0 : 1; @@ -2830,6 +2851,21 @@ void TagDuel::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 codes; + if(BuildDeckList(i, kind, codes)) + SendDeckList(dp, i, kind, codes); + } + } else { + std::vector codes; + if(BuildDeckList(player, DECK_LIST_SORTED, codes)) + SendDeckList(dp, player, DECK_LIST_SORTED, codes); + } + uint8_t query_buffer[SIZE_QUERY_BUFFER]; for(uint8_t i = 0; i < 2; ++i) { // get decktop card @@ -2874,6 +2910,89 @@ void TagDuel::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 (both teammates) 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 TagDuel::MarkDeckLists() { + deck_list_dirty = true; +} +void TagDuel::ResetDeckLists() { + deck_list_dirty = false; + for(auto& side : deck_list_known) + for(auto& known : side) + known = false; + deck_in_zone[0] = 0; + deck_in_zone[1] = 0; +} +bool TagDuel::BuildDeckList(int side, uint8_t kind, std::vector& codes) { + codes.clear(); + if(kind == DECK_LIST_SUBMITTED) { + for(auto cit : pdeck[side * 2 + deck_in_zone[side]].main) + codes.push_back(cit->code); + std::sort(codes.begin(), codes.end()); + return true; + } + if(!pduel) + return false; + static std::vector 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(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 TagDuel::SendDeckList(DuelPlayer* dp, int side, uint8_t kind, const std::vector& 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(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(p, codes[i]); + NetServer::SendBufferToPlayer(dp, STOC_DECK_LIST, buf, p - buf); +} +void TagDuel::FlushDeckLists() { + if(!deck_list_dirty || !pduel) + return; + deck_list_dirty = false; + std::vector 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 * 2], side, kind, codes); + SendDeckList(players[side * 2 + 1], side, kind, codes); + } else + for(auto oit = observers.begin(); oit != observers.end(); ++oit) + SendDeckList(*oit, side, kind, codes); + } + } +} void TagDuel::TimeConfirm(DuelPlayer* dp) { if(host_info.time_limit == 0) return; diff --git a/server/gframe/tag_duel.h b/server/gframe/tag_duel.h index b97fcd7..83789be 100644 --- a/server/gframe/tag_duel.h +++ b/server/gframe/tag_duel.h @@ -94,6 +94,16 @@ protected: // YGOL coin toss void BeginChoose(int chooser, uint8_t face); int choose_seat{}; + // YGOL deck lists + void MarkDeckLists(); + void ResetDeckLists(); + void FlushDeckLists(); + bool BuildDeckList(int side, uint8_t kind, std::vector& codes); + void SendDeckList(DuelPlayer* dp, int side, uint8_t kind, const std::vector& codes); + std::vector deck_list_sent[2][DECK_LIST_KINDS]; + bool deck_list_known[2][DECK_LIST_KINDS]{}; + bool deck_list_dirty{}; + uint8_t deck_in_zone[2]{}; // YGOL host seats void SendToLobby(unsigned char proto, void* buffer, size_t len); size_t SeatPlanPacket(unsigned char* buf, uint16_t revision);