diff --git a/scripts/poker_sit.js b/scripts/poker_sit.js index 59f8a70..547851b 100644 --- a/scripts/poker_sit.js +++ b/scripts/poker_sit.js @@ -108,60 +108,66 @@ xhr.setRequestHeader("Authorization", "Bearer " + _token); xhr.onreadystatechange = function () { if (xhr.readyState !== 4) return; - print("[pokerSeat] sit error response: " + xhr.status + " " + xhr.responseText); + print("[pokerSeat] sit response: " + xhr.status + " " + xhr.responseText); if (xhr.status === 200) { - // Seat reserved — now physically sit the avatar MyAvatar.beginSit(getSeatPosition(), getSeatRotation()); isSeated = true; Entities.editEntity(_entityID, { visible: false }); Controller.actionEvent.connect(onActionEvent); - Messages.sendMessage("poker:seat", JSON.stringify({ - event: "sit", - tableID: _tableID, - pokerID: _pokerID, - seatIndex: _seatIndex, - username: _username, + event: "sit", tableID: _tableID, pokerID: _pokerID, + seatIndex: _seatIndex, username: _username, })); + _busy = false; } else { try { - var err = JSON.parse(xhr.responseText); - notify(err.error || "Could not sit down"); + var resp = JSON.parse(xhr.responseText); + if (resp.error === "already seated") { + // Server thinks we're seated — stand first, then re-sit + print("[pokerSeat] recovering stale seat, standing first"); + doStand(function() { onSit(); }); + return; + } + notify(resp.error || "Could not sit down"); } catch (e) { notify("Could not sit down (" + xhr.status + ")"); } + _busy = false; } - _busy = false; }; - xhr.send(JSON.stringify({ - seatIndex: parseInt(_seatIndex), - })); + var payload = JSON.stringify({ seatIndex: parseInt(_seatIndex) }); + print("[pokerSeat] sit payload: " + payload + " token: " + _token + " url: " + POKER_BASE + "/tables/" + _pokerID + "/sit"); + xhr.send(payload); }); } + function doStand(cb) { + var tok = getSharedToken(); // or _token if keeping per-instance + 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 () { + if (xhr.readyState !== 4) return; + _token = null; // invalidate immediately on stand + if (isSeated) { + MyAvatar.endSit(getSeatPosition(), getSeatRotation()); + isSeated = false; + Entities.editEntity(_entityID, { visible: true }); + Controller.actionEvent.disconnect(onActionEvent); + Messages.sendMessage("poker:seat", JSON.stringify({ + event: "stand", tableID: _tableID, + seatIndex: _seatIndex, username: _username, + })); + } + if (cb) cb(); + }; + xhr.send(JSON.stringify({ seatIndex: parseInt(_seatIndex) })); + } + function onStand() { if (!isSeated) return; - - var xhr = new XMLHttpRequest(); - xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/stand", true); - xhr.setRequestHeader("Content-Type", "application/json"); - xhr.setRequestHeader("Authorization", "Bearer " + _token); - xhr.onreadystatechange = function () { - if (xhr.readyState !== 4) return; - // Stand regardless of server response — worst case server cleans up on disconnect - MyAvatar.endSit(getSeatPosition(), getSeatRotation()); - isSeated = false; - Entities.editEntity(_entityID, { visible: true }); - Controller.actionEvent.disconnect(onActionEvent); - - Messages.sendMessage("poker:seat", JSON.stringify({ - event: "stand", - tableID: _tableID, - seatIndex: _seatIndex, - username: _username, - })); - }; - xhr.send(JSON.stringify({ seatIndex: parseInt(_seatIndex) })); + doStand(null); } function onActionEvent(action, value) {