server: 0017-coin-toss

TICKET-328: the server rolls the coin: STOC 0x5B to every seat and watcher, the stock SELECT_TP to the chooser, a one-shot 10 s timer that answers FIRST for them; game 2+ sends face 0xFF. Capability bit 0x80, protocol 5.

Replayed from Tools/YGOLegacy/patches/0017-coin-toss.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:58 -07:00
1 parent 06391120b5
commit 2c081162ff
7 files changed
+132 -105

No files matched your search

+40 -55
View File
@@ -79,7 +79,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; // YGOL setup pairs + host seats + rules restart + seat state + lobby after match
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
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);
@@ -868,6 +868,7 @@ void SingleDuel::ReturnToLobby() {
tp_player = 0;
hand_result[0] = 0;
hand_result[1] = 0;
NetServer::StopChooseTimer(); // YGOL coin toss
time_elapsed = 0;
last_response = 0;
registry_dump.clear();
@@ -1062,12 +1063,8 @@ void SingleDuel::UpdateDeck(DuelPlayer* dp, unsigned char* pdata, unsigned int l
if(DeckManager::LoadSide(pdeck[dp->type], deckbuf, mainc, sidec)) {
ready[dp->type] = true;
NetServer::SendPacketToPlayer(dp, STOC_DUEL_START);
if(ready[0] && ready[1]) {
NetServer::SendPacketToPlayer(players[tp_player], STOC_SELECT_TP);
players[1 - tp_player]->state = 0xff;
players[tp_player]->state = CTOS_TP_RESULT;
duel_stage = DUEL_STAGE_FIRSTGO;
}
if(ready[0] && ready[1])
BeginChoose(tp_player, COIN_FACE_NONE); // YGOL coin toss: no toss from game 2 on, the loser chooses
} else {
STOC_ErrorMsg scem;
scem.msg = ERRMSG_SIDEERROR;
@@ -1134,61 +1131,48 @@ void SingleDuel::StartDuel(DuelPlayer* dp) {
std::memcpy(deckbuff, deckbuff + 6, 6);
std::memcpy(deckbuff + 6, tempbuff, 6);
NetServer::SendBufferToPlayer(players[1], STOC_DECK_COUNT, deckbuff, 12);
NetServer::SendPacketToPlayer(players[0], STOC_SELECT_HAND);
NetServer::ReSendToPlayer(players[1]);
hand_result[0] = 0;
hand_result[1] = 0;
players[0]->state = CTOS_HAND_RESULT;
players[1]->state = CTOS_HAND_RESULT;
duel_stage = DUEL_STAGE_FINGER;
// YGOL coin toss: the server rolls instead of rock-paper-scissors; heads = seat 0 chooses, tails = seat 1
std::random_device rd;
const uint8_t face = (rd() & 1) ? COIN_FACE_TAILS : COIN_FACE_HEADS;
BeginChoose(face == COIN_FACE_HEADS ? 0 : 1, face);
}
void SingleDuel::HandResult(DuelPlayer* dp, unsigned char res) {
if(res == 0 || res > 3 || dp->state != CTOS_HAND_RESULT)
return;
auto player = dp->type;
if(hand_result[player])
return;
hand_result[player] = res;
if(hand_result[0] && hand_result[1]) {
STOC_HandResult schr;
schr.res1 = hand_result[0];
schr.res2 = hand_result[1];
NetServer::SendPacketToPlayer(players[0], STOC_HAND_RESULT, schr);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
NetServer::ReSendToPlayer(*oit);
// YGOL coin toss: everyone learns the toss and the chooser, the chooser gets SELECT_TP, the clock starts
void SingleDuel::BeginChoose(int chooser, uint8_t face) {
STOC_CoinToss toss;
toss.version = COIN_TOSS_VERSION;
toss.chooser_seat = (uint8_t)chooser;
toss.face = face;
toss.choose_seconds = COIN_CHOOSE_SECONDS;
NetServer::SendPacketToPlayer(players[0], STOC_COIN_TOSS, toss);
NetServer::ReSendToPlayer(players[1]);
for(auto oit = observers.begin(); oit != observers.end(); ++oit)
NetServer::ReSendToPlayer(*oit);
#ifdef YGOPRO_SERVER_MODE
NetServer::ReSendToPlayers(cache_recorder, replay_recorder);
NetServer::ReSendToPlayers(cache_recorder, replay_recorder);
#endif
schr.res1 = hand_result[1];
schr.res2 = hand_result[0];
NetServer::SendPacketToPlayer(players[1], STOC_HAND_RESULT, schr);
if(hand_result[0] == hand_result[1]) {
NetServer::SendPacketToPlayer(players[0], STOC_SELECT_HAND);
NetServer::ReSendToPlayer(players[1]);
hand_result[0] = 0;
hand_result[1] = 0;
players[0]->state = CTOS_HAND_RESULT;
players[1]->state = CTOS_HAND_RESULT;
} else if((hand_result[0] == 1 && hand_result[1] == 2)
|| (hand_result[0] == 2 && hand_result[1] == 3)
|| (hand_result[0] == 3 && hand_result[1] == 1)) {
NetServer::SendPacketToPlayer(players[1], STOC_SELECT_TP);
tp_player = 1;
players[0]->state = 0xff;
players[1]->state = CTOS_TP_RESULT;
duel_stage = DUEL_STAGE_FIRSTGO;
} else {
NetServer::SendPacketToPlayer(players[0], STOC_SELECT_TP);
players[1]->state = 0xff;
players[0]->state = CTOS_TP_RESULT;
tp_player = 0;
duel_stage = DUEL_STAGE_FIRSTGO;
}
}
NetServer::SendPacketToPlayer(players[chooser], STOC_SELECT_TP);
tp_player = chooser;
players[1 - chooser]->state = 0xff;
players[chooser]->state = CTOS_TP_RESULT;
duel_stage = DUEL_STAGE_FIRSTGO;
NetServer::StartChooseTimer(COIN_CHOOSE_SECONDS);
}
// YGOL coin toss: no answer in time -> the chooser goes FIRST
void SingleDuel::ChooseTimeout() {
if(duel_stage != DUEL_STAGE_FIRSTGO)
return;
auto* dp = players[tp_player];
if(!dp || dp->state != CTOS_TP_RESULT)
return;
TPResult(dp, 1);
}
// YGOL coin toss: rock-paper-scissors is gone; a stock client's answer is ignored
void SingleDuel::HandResult(DuelPlayer*, unsigned char) {
}
void SingleDuel::TPResult(DuelPlayer* dp, unsigned char tp) {
if(dp->state != CTOS_TP_RESULT)
return;
NetServer::StopChooseTimer(); // YGOL coin toss: answered in time
duel_stage = DUEL_STAGE_DUELING;
bool swapped = false;
pplayer[0] = players[0];
@@ -2563,6 +2547,7 @@ void SingleDuel::GetResponse(DuelPlayer* dp, unsigned char* pdata, unsigned int
last_replay_response_size = 0;
}
void SingleDuel::EndDuel() {
NetServer::StopChooseTimer(); // YGOL coin toss: a duel that ends before the choice drops the clock
if(!pduel)
return;
last_replay.EndRecord();