diff --git a/scripts/poker_cards.js b/scripts/poker_cards.js index 1414ceb..49a51b9 100644 --- a/scripts/poker_cards.js +++ b/scripts/poker_cards.js @@ -165,24 +165,6 @@ return { rank: rank, suit: suit }; } - function onCardsDealt(cards) { - if (!cards || cards.length < 2) return; - var card1 = parseCard(cards[0]); - var card2 = parseCard(cards[1]); - print("[pokerCards] dealt " + cards[0] + " " + cards[1]); - - _pendingCards = { card1: card1, card2: card2 }; - - _rendererID = Entities.addEntity({ - type: "Web", - name: "card-renderer", - sourceUrl: "https://wizards.cyou/tablet/card-face.html", - position: MyAvatar.position, - dimensions: { x: 0.01, y: 0.01, z: 0.01 }, - visible: false, - }, "local"); - } - function onWebEventReceived(id, data) { print("[pokerCards] webEvent from " + id + " renderer is " + _rendererID); if (id !== _rendererID) return; diff --git a/scripts/poker_sit.js b/scripts/poker_sit.js index 9e1993d..b2cdd2f 100644 --- a/scripts/poker_sit.js +++ b/scripts/poker_sit.js @@ -90,7 +90,17 @@ function doStand(cb) { _username = _username || AccountServices.username; - var tok = _token; + var tok = _token + + // If no token, need to re-auth first + if (!_token) { + ensureSession(function(err) { + if (err) { print("[pokerSeat] doStand: no session: " + err); if (cb) cb(); return; } + doStand(cb); + }); + return; + } + print("[pokerSeat] doStand called, token: " + (tok ? "ok" : "null") + " username: " + _username + " pokerID: " + _pokerID); var xhr = new XMLHttpRequest(); @@ -104,17 +114,33 @@ 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"); + // Mid-hand block — server refused, queue a deferred stand + if (xhr.status === 400) { + print("[pokerSeat] stand deferred (mid-hand)"); _pendingStand = true; + notify("You'll stand at the end of this hand"); if (cb) cb(); return; } - // Immediate stand — clear token and clean up now + // Auth failure — token expired, don't touch avatar state + if (xhr.status === 401) { + print("[pokerSeat] stand 401, token expired"); + _token = null; + if (cb) cb(); + return; + } + + // Any other non-200 — server error, log and bail without touching avatar + if (xhr.status !== 200) { + print("[pokerSeat] stand failed (" + xhr.status + "), leaving avatar state intact"); + if (cb) cb(); + return; + } + + // 200 — successful stand, clean up everything _token = null; + _pendingStand = false; if (isSeated) { MyAvatar.endSit(getSeatPosition(), getSeatRotation()); isSeated = false; @@ -130,7 +156,7 @@ })); } if (cb) cb(); - }; + }; xhr.send(JSON.stringify({ seatIndex: parseInt(_seatIndex) })); } @@ -149,7 +175,7 @@ ensureSession(function (err) { if (err) { notify(err); _busy = false; return; } _username = AccountServices.username; - if (!_username) { cb("Not logged in to Overte"); return; } + if (!_username) { notify("Not logged in to Overte"); _busy = false; return; } var xhr = new XMLHttpRequest(); xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/sit", true); @@ -246,6 +272,22 @@ isSeated = false; _pendingStand = false; } + Messages.messageReceived.disconnect(onMessage); + Messages.unsubscribe("poker:standComplete"); _token = null; }; + Messages.subscribe("poker:standComplete"); + Messages.messageReceived.connect(onMessage); + + function onMessage(channel, message) { + if (channel !== "poker:standComplete") return; + try { + var data = JSON.parse(message); + if (data.username !== _username) return; + } catch(e) { return; } + + _pendingStand = false; + // Now actually stand + doStand(null); + } });