Fix invalid token on changing seats

This commit is contained in:
nak 2026-03-18 06:34:58 +00:00
parent 091e9df295
commit aec4344851

View file

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