Trying to fix sit/stand/sit operation
This commit is contained in:
parent
af50d821ef
commit
4c775bffa0
2 changed files with 49 additions and 17 deletions
|
|
@ -57,16 +57,26 @@
|
||||||
_ws.onopen = function() {
|
_ws.onopen = function() {
|
||||||
print("[pokerCards] WS connected");
|
print("[pokerCards] WS connected");
|
||||||
// Identify so server can route private messages
|
// Identify so server can route private messages
|
||||||
|
print("[pokerCards] WS connected, identifying as: " + _username);
|
||||||
_ws.send(JSON.stringify({ type: "identify", username: _username }));
|
_ws.send(JSON.stringify({ type: "identify", username: _username }));
|
||||||
};
|
};
|
||||||
|
|
||||||
_ws.onmessage = function(event) {
|
_ws.onmessage = function(event) {
|
||||||
|
print("[pokerCards] WS message received: " + event.data.substring(0, 100));
|
||||||
try {
|
try {
|
||||||
var msg = JSON.parse(event.data);
|
var msg = JSON.parse(event.data);
|
||||||
if (msg.type === "hand:dealt") {
|
if (msg.type === "hand:dealt") {
|
||||||
onCardsDealt(msg.cards);
|
onCardsDealt(msg.cards);
|
||||||
} else if (msg.type === "hand:end") {
|
} else if (msg.type === "hand:end") {
|
||||||
hideCards();
|
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) {
|
} catch(e) {
|
||||||
print("[pokerCards] WS parse error: " + e);
|
print("[pokerCards] WS parse error: " + e);
|
||||||
|
|
@ -86,32 +96,23 @@
|
||||||
// ── card rendering ───────────────────────────────────────────
|
// ── card rendering ───────────────────────────────────────────
|
||||||
|
|
||||||
function onCardsDealt(cards) {
|
function onCardsDealt(cards) {
|
||||||
// cards is ["As", "Kh"] format from server
|
|
||||||
if (!cards || cards.length < 2) return;
|
if (!cards || cards.length < 2) return;
|
||||||
|
|
||||||
var card1 = parseCard(cards[0]);
|
var card1 = parseCard(cards[0]);
|
||||||
var card2 = parseCard(cards[1]);
|
var card2 = parseCard(cards[1]);
|
||||||
|
|
||||||
print("[pokerCards] dealt " + cards[0] + " " + 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({
|
_rendererID = Entities.addEntity({
|
||||||
type: "Web",
|
type: "Web",
|
||||||
name: "card-renderer",
|
name: "card-renderer",
|
||||||
sourceUrl: "https://wizards.cyou/tablet/card-face.html",
|
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 },
|
dimensions: { x: 0.01, y: 0.01, z: 0.01 },
|
||||||
visible: false,
|
visible: false,
|
||||||
}, "local");
|
}, "local");
|
||||||
|
|
||||||
// Wait for web entity to load then send card data
|
print("[pokerCards] spawned renderer entity: " + _rendererID);
|
||||||
Script.setTimeout(function() {
|
|
||||||
Entities.emitScriptEvent(_rendererID, JSON.stringify({
|
|
||||||
type: "dealCards",
|
|
||||||
card1: card1,
|
|
||||||
card2: card2,
|
|
||||||
}));
|
|
||||||
}, 1500);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function onTextureReady(dataURI) {
|
function onTextureReady(dataURI) {
|
||||||
|
|
@ -183,20 +184,25 @@
|
||||||
}
|
}
|
||||||
|
|
||||||
function onWebEventReceived(id, data) {
|
function onWebEventReceived(id, data) {
|
||||||
|
print("[pokerCards] webEvent from " + id + " renderer is " + _rendererID);
|
||||||
if (id !== _rendererID) return;
|
if (id !== _rendererID) return;
|
||||||
try {
|
try {
|
||||||
var msg = JSON.parse(data);
|
var msg = JSON.parse(data);
|
||||||
|
print("[pokerCards] renderer msg type: " + msg.type);
|
||||||
if (msg.type === 'rendererReady') {
|
if (msg.type === 'rendererReady') {
|
||||||
// Web entity loaded — now send card data
|
print("[pokerCards] renderer ready, sending cards");
|
||||||
Entities.emitScriptEvent(_rendererID, JSON.stringify({
|
Entities.emitScriptEvent(_rendererID, JSON.stringify({
|
||||||
type: 'dealCards',
|
type: 'dealCards',
|
||||||
card1: _pendingCards.card1,
|
card1: _pendingCards.card1,
|
||||||
card2: _pendingCards.card2,
|
card2: _pendingCards.card2,
|
||||||
}));
|
}));
|
||||||
} else if (msg.type === 'cardTexture') {
|
} else if (msg.type === 'cardTexture') {
|
||||||
|
print("[pokerCards] texture received, length: " + msg.dataURI.length);
|
||||||
onTextureReady(msg.dataURI);
|
onTextureReady(msg.dataURI);
|
||||||
}
|
}
|
||||||
} catch(e) {}
|
} catch(e) {
|
||||||
|
print("[pokerCards] webEvent parse error: " + e);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ── web entity event listener ────────────────────────────────
|
// ── web entity event listener ────────────────────────────────
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@
|
||||||
var _token = null;
|
var _token = null;
|
||||||
var _username = null;
|
var _username = null;
|
||||||
var _busy = false;
|
var _busy = false;
|
||||||
|
var _pendingStand = false;
|
||||||
var isSeated = false;
|
var isSeated = false;
|
||||||
|
|
||||||
// ── helpers ──────────────────────────────────────────────────
|
// ── helpers ──────────────────────────────────────────────────
|
||||||
|
|
@ -61,7 +62,7 @@
|
||||||
function ensureSession(cb) {
|
function ensureSession(cb) {
|
||||||
if (_token) { cb(null, _token); return; }
|
if (_token) { cb(null, _token); return; }
|
||||||
|
|
||||||
_username = MyAvatar.displayName;
|
_username = AccountServices.username;
|
||||||
if (!_username) { cb("Not logged in to Overte"); return; }
|
if (!_username) { cb("Not logged in to Overte"); return; }
|
||||||
|
|
||||||
print("[pokerSeat] requesting session from: " + API_BASE + "/session for " + _username);
|
print("[pokerSeat] requesting session from: " + API_BASE + "/session for " + _username);
|
||||||
|
|
@ -88,13 +89,31 @@
|
||||||
// ── sit / stand ──────────────────────────────────────────────
|
// ── sit / stand ──────────────────────────────────────────────
|
||||||
|
|
||||||
function doStand(cb) {
|
function doStand(cb) {
|
||||||
|
_username = _username || AccountServices.username;
|
||||||
var tok = _token;
|
var tok = _token;
|
||||||
|
print("[pokerSeat] doStand called, token: " + (tok ? "ok" : "null") + " username: " + _username + " pokerID: " + _pokerID);
|
||||||
|
|
||||||
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 " + tok);
|
xhr.setRequestHeader("Authorization", "Bearer " + tok);
|
||||||
xhr.onreadystatechange = function () {
|
xhr.onreadystatechange = function() {
|
||||||
if (xhr.readyState !== 4) return;
|
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;
|
_token = null;
|
||||||
if (isSeated) {
|
if (isSeated) {
|
||||||
MyAvatar.endSit(getSeatPosition(), getSeatRotation());
|
MyAvatar.endSit(getSeatPosition(), getSeatRotation());
|
||||||
|
|
@ -129,6 +148,8 @@
|
||||||
|
|
||||||
ensureSession(function (err) {
|
ensureSession(function (err) {
|
||||||
if (err) { notify(err); _busy = false; return; }
|
if (err) { notify(err); _busy = false; return; }
|
||||||
|
_username = AccountServices.username;
|
||||||
|
if (!_username) { cb("Not logged in to Overte"); return; }
|
||||||
|
|
||||||
var xhr = new XMLHttpRequest();
|
var xhr = new XMLHttpRequest();
|
||||||
xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/sit", true);
|
xhr.open("POST", POKER_BASE + "/tables/" + _pokerID + "/sit", true);
|
||||||
|
|
@ -173,6 +194,10 @@
|
||||||
|
|
||||||
function onStand() {
|
function onStand() {
|
||||||
if (!isSeated) return;
|
if (!isSeated) return;
|
||||||
|
if (_pendingStand) {
|
||||||
|
print("[pokerSeat] stand already pending, ignoring");
|
||||||
|
return;
|
||||||
|
}
|
||||||
doStand(null);
|
doStand(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -219,6 +244,7 @@
|
||||||
if (isSeated) {
|
if (isSeated) {
|
||||||
Controller.actionEvent.disconnect(onActionEvent);
|
Controller.actionEvent.disconnect(onActionEvent);
|
||||||
isSeated = false;
|
isSeated = false;
|
||||||
|
_pendingStand = false;
|
||||||
}
|
}
|
||||||
_token = null;
|
_token = null;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue