From 4c775bffa03e2fa1d2ab0d1869e1e8e828689d12 Mon Sep 17 00:00:00 2001 From: nak Date: Sat, 21 Mar 2026 22:16:30 +0000 Subject: [PATCH] Trying to fix sit/stand/sit operation --- scripts/poker_cards.js | 36 +++++++++++++++++++++--------------- scripts/poker_sit.js | 30 ++++++++++++++++++++++++++++-- 2 files changed, 49 insertions(+), 17 deletions(-) diff --git a/scripts/poker_cards.js b/scripts/poker_cards.js index e8081fb..1414ceb 100644 --- a/scripts/poker_cards.js +++ b/scripts/poker_cards.js @@ -57,16 +57,26 @@ _ws.onopen = function() { print("[pokerCards] WS connected"); // Identify so server can route private messages + print("[pokerCards] WS connected, identifying as: " + _username); _ws.send(JSON.stringify({ type: "identify", username: _username })); }; _ws.onmessage = function(event) { + print("[pokerCards] WS message received: " + event.data.substring(0, 100)); try { var msg = JSON.parse(event.data); if (msg.type === "hand:dealt") { onCardsDealt(msg.cards); } else if (msg.type === "hand:end") { hideCards(); + } else if (msg.type === "player:disconnect:stand") { + if (msg.username === _username) { + print("[pokerCards] deferred stand complete, chips returned: " + msg.stack); + Messages.sendLocalMessage("poker:standComplete", JSON.stringify({ + username: msg.username, + stack: msg.stack, + })); + } } } catch(e) { print("[pokerCards] WS parse error: " + e); @@ -86,32 +96,23 @@ // ── card rendering ─────────────────────────────────────────── function onCardsDealt(cards) { - // cards is ["As", "Kh"] format from server if (!cards || cards.length < 2) return; - var card1 = parseCard(cards[0]); var card2 = parseCard(cards[1]); - print("[pokerCards] dealt " + cards[0] + " " + cards[1]); - // Spawn hidden web entity to render card texture + _pendingCards = { card1: card1, card2: card2 }; + _rendererID = Entities.addEntity({ type: "Web", name: "card-renderer", sourceUrl: "https://wizards.cyou/tablet/card-face.html", - position: MyAvatar.position, // doesn't matter, it's invisible + position: MyAvatar.position, dimensions: { x: 0.01, y: 0.01, z: 0.01 }, visible: false, }, "local"); - // Wait for web entity to load then send card data - Script.setTimeout(function() { - Entities.emitScriptEvent(_rendererID, JSON.stringify({ - type: "dealCards", - card1: card1, - card2: card2, - })); - }, 1500); + print("[pokerCards] spawned renderer entity: " + _rendererID); } function onTextureReady(dataURI) { @@ -183,20 +184,25 @@ } function onWebEventReceived(id, data) { + print("[pokerCards] webEvent from " + id + " renderer is " + _rendererID); if (id !== _rendererID) return; try { var msg = JSON.parse(data); + print("[pokerCards] renderer msg type: " + msg.type); if (msg.type === 'rendererReady') { - // Web entity loaded — now send card data + print("[pokerCards] renderer ready, sending cards"); Entities.emitScriptEvent(_rendererID, JSON.stringify({ type: 'dealCards', card1: _pendingCards.card1, card2: _pendingCards.card2, })); } else if (msg.type === 'cardTexture') { + print("[pokerCards] texture received, length: " + msg.dataURI.length); onTextureReady(msg.dataURI); } - } catch(e) {} + } catch(e) { + print("[pokerCards] webEvent parse error: " + e); + } } // ── web entity event listener ──────────────────────────────── diff --git a/scripts/poker_sit.js b/scripts/poker_sit.js index 8fad8fe..9e1993d 100644 --- a/scripts/poker_sit.js +++ b/scripts/poker_sit.js @@ -21,6 +21,7 @@ var _token = null; var _username = null; var _busy = false; + var _pendingStand = false; var isSeated = false; // ── helpers ────────────────────────────────────────────────── @@ -61,7 +62,7 @@ function ensureSession(cb) { if (_token) { cb(null, _token); return; } - _username = MyAvatar.displayName; + _username = AccountServices.username; if (!_username) { cb("Not logged in to Overte"); return; } print("[pokerSeat] requesting session from: " + API_BASE + "/session for " + _username); @@ -88,13 +89,31 @@ // ── sit / stand ────────────────────────────────────────────── function doStand(cb) { + _username = _username || AccountServices.username; var tok = _token; + print("[pokerSeat] doStand called, token: " + (tok ? "ok" : "null") + " username: " + _username + " pokerID: " + _pokerID); + var xhr = new XMLHttpRequest(); xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/stand", true); xhr.setRequestHeader("Content-Type", "application/json"); xhr.setRequestHeader("Authorization", "Bearer " + tok); - xhr.onreadystatechange = function () { + xhr.onreadystatechange = function() { if (xhr.readyState !== 4) return; + print("[pokerSeat] stand response: " + xhr.status + " " + xhr.responseText); + + var resp = {}; + try { resp = JSON.parse(xhr.responseText); } catch(e) {} + + if (xhr.status === 200 && resp.returned === 0) { + // Deferred stand — mid-hand, wait for hand to end + // Don't null token yet — we may need it for a retry + print("[pokerSeat] deferred stand, waiting for hand to end"); + _pendingStand = true; + if (cb) cb(); + return; + } + + // Immediate stand — clear token and clean up now _token = null; if (isSeated) { MyAvatar.endSit(getSeatPosition(), getSeatRotation()); @@ -129,6 +148,8 @@ ensureSession(function (err) { if (err) { notify(err); _busy = false; return; } + _username = AccountServices.username; + if (!_username) { cb("Not logged in to Overte"); return; } var xhr = new XMLHttpRequest(); xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/sit", true); @@ -173,6 +194,10 @@ function onStand() { if (!isSeated) return; + if (_pendingStand) { + print("[pokerSeat] stand already pending, ignoring"); + return; + } doStand(null); } @@ -219,6 +244,7 @@ if (isSeated) { Controller.actionEvent.disconnect(onActionEvent); isSeated = false; + _pendingStand = false; } _token = null; };