diff --git a/scripts/poker_sit.js b/scripts/poker_sit.js index 547851b..b501651 100644 --- a/scripts/poker_sit.js +++ b/scripts/poker_sit.js @@ -6,19 +6,16 @@ // // tableID — Overte entity UUID of the table mesh, used for position calculations // pokerID — poker table slug used for API calls (matches table config id) - // - // Seat layout is loaded from poker_constants.js so this script - // does not need to know anything about the table geometry. - var constants = Script.require(Script.resolvePath("poker_constants.js")); + var constants = Script.require(Script.resolvePath("poker_constants.js")); var POKER_SEATS = constants.POKER_SEATS; var API_BASE = constants.API_BASE; var POKER_BASE = constants.POKER_BASE; var JUMP_ACTION = 50; var _entityID = null; - var _tableID = null; // Overte entity UUID of the table mesh - var _pokerID = null; // poker table slug for API calls + var _tableID = null; + var _pokerID = null; var _seatIndex = null; var _seatData = null; var _token = null; @@ -61,6 +58,7 @@ function ensureSession(cb) { if (_token) { cb(null, _token); return; } + _username = MyAvatar.displayName; if (!_username) { cb("Not logged in to Overte"); return; } @@ -87,6 +85,32 @@ // ── sit / stand ────────────────────────────────────────────── + function doStand(cb) { + var tok = _token; + 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; + 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 onSit() { if (isSeated || _busy) return; _busy = true; @@ -100,8 +124,6 @@ ensureSession(function (err) { if (err) { notify(err); _busy = false; return; } - // POST to server — handles buy-in deduction and seat reservation. - // Server will reject if seat is taken or balance insufficient. var xhr = new XMLHttpRequest(); xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/sit", true); xhr.setRequestHeader("Content-Type", "application/json"); @@ -115,17 +137,19 @@ 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 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(); }); + doStand(function () { onSit(); }); return; } notify(resp.error || "Could not sit down"); @@ -135,36 +159,10 @@ _busy = false; } }; - var payload = JSON.stringify({ seatIndex: parseInt(_seatIndex) }); - print("[pokerSeat] sit payload: " + payload + " token: " + _token + " url: " + POKER_BASE + "/tables/" + _pokerID + "/sit"); - xhr.send(payload); + xhr.send(JSON.stringify({ seatIndex: parseInt(_seatIndex) })); }); } - 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; doStand(null); @@ -191,7 +189,6 @@ return; } - // Load seat geometry from constants keyed by table's seat count var seatCount = getTableSeatCount(); var layout = POKER_SEATS[seatCount]; if (!layout || !layout[parseInt(_seatIndex)]) { @@ -203,7 +200,6 @@ print("[pokerSeat] loaded seat " + _seatIndex + " at table " + _tableID); }; - // Mouse + controller triggers this.clickDownOnEntity = function () { onSit(); }; this.clickUpOnEntity = function () { _busy = false; }; this.startNearTrigger = function () { onSit(); };