Fix poker table spawn and deletion
This commit is contained in:
parent
296ed70797
commit
4a2f8387c0
2 changed files with 86 additions and 21 deletions
|
|
@ -17,61 +17,81 @@
|
||||||
var webEventHandler = function(data) {
|
var webEventHandler = function(data) {
|
||||||
try {
|
try {
|
||||||
var msg = JSON.parse(data);
|
var msg = JSON.parse(data);
|
||||||
|
|
||||||
if (msg.type === "ready") {
|
if (msg.type === "ready") {
|
||||||
var username = AccountServices.username || "";
|
|
||||||
tablet.emitScriptEvent(JSON.stringify({
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
type: "init",
|
type: "init",
|
||||||
username: username,
|
username: AccountServices.username || "",
|
||||||
position: MyAvatar.position,
|
position: MyAvatar.position,
|
||||||
rotation: MyAvatar.orientation,
|
rotation: MyAvatar.orientation,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
} else if (msg.type === "getPosition") {
|
} else if (msg.type === "getPosition") {
|
||||||
// Tablet can request a fresh position snapshot any time
|
|
||||||
tablet.emitScriptEvent(JSON.stringify({
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
type: "position",
|
type: "position",
|
||||||
position: MyAvatar.position,
|
position: MyAvatar.position,
|
||||||
rotation: MyAvatar.orientation,
|
rotation: MyAvatar.orientation,
|
||||||
}));
|
}));
|
||||||
|
|
||||||
} else if (msg.type === "spawnSeats") {
|
} else if (msg.type === "spawnSeats") {
|
||||||
// HTML side has confirmed table created, now spawn seat pads in world
|
spawnTable(msg.pokerID, msg.seatCount, msg.position, msg.rotation);
|
||||||
spawnSeatPads(msg.tableEntityID, msg.pokerID, msg.seatCount,
|
|
||||||
msg.position, msg.rotation);
|
} else if (msg.type === "deleteTable") {
|
||||||
|
deleteTableEntities(msg.pokerID);
|
||||||
}
|
}
|
||||||
|
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
print("[pokerAdmin] web event error: " + e);
|
print("[pokerAdmin] web event error: " + e);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
function spawnSeatPads(tableEntityID, pokerID, seatCount, position, rotation) {
|
function spawnTable(pokerID, seatCount, avatarPosition, avatarRotation) {
|
||||||
var constants = Script.require(Script.resolvePath("poker_constants.js"));
|
var constants = Script.require(Script.resolvePath("poker_constants.js"));
|
||||||
var layout = constants.POKER_SEATS[seatCount];
|
var layout = constants.POKER_SEATS[seatCount];
|
||||||
if (!layout) {
|
if (!layout) {
|
||||||
print("[pokerAdmin] no layout for seatCount=" + seatCount);
|
print("[pokerAdmin] no layout for seatCount=" + seatCount);
|
||||||
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
|
type: "spawnError",
|
||||||
|
error: "No seat layout for seatCount=" + seatCount,
|
||||||
|
}));
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Spawn the table model entity first
|
// Place table in front of avatar at floor level
|
||||||
|
// Use only the yaw component of avatar rotation so table sits flat
|
||||||
|
var yaw = Quat.safeEulerAngles(avatarRotation).y;
|
||||||
|
var tableRot = Quat.fromPitchYawRollDegrees(0, yaw, 0);
|
||||||
|
|
||||||
|
// 2.5m in front of avatar, snapped to floor (y: -0.45 relative to avatar)
|
||||||
|
var forward = Vec3.multiplyQbyV(tableRot, { x: 0, y: 0, z: -2.5 });
|
||||||
|
var tablePos = {
|
||||||
|
x: avatarPosition.x + forward.x,
|
||||||
|
y: avatarPosition.y - 0.45,
|
||||||
|
z: avatarPosition.z + forward.z,
|
||||||
|
};
|
||||||
|
|
||||||
|
// Spawn table model — natural dimensions, not squished
|
||||||
var tableID = Entities.addEntity({
|
var tableID = Entities.addEntity({
|
||||||
type: "Model",
|
type: "Model",
|
||||||
name: "poker_table_" + pokerID,
|
name: "poker_table_" + pokerID,
|
||||||
modelURL: constants.POKER_TABLE_MODEL_URL,
|
modelURL: constants.POKER_TABLE_MODEL_URL,
|
||||||
position: position,
|
position: tablePos,
|
||||||
rotation: rotation,
|
rotation: tableRot,
|
||||||
dimensions: { x: 3.0, y: 1.0, z: 3.0 },
|
naturalDimensions: true,
|
||||||
userData: JSON.stringify({
|
userData: JSON.stringify({
|
||||||
pokerID: pokerID,
|
pokerID: pokerID,
|
||||||
seatCount: seatCount,
|
seatCount: seatCount,
|
||||||
}),
|
}),
|
||||||
grabbable: false,
|
grabbable: false,
|
||||||
|
locked: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
print("[pokerAdmin] spawned table entity " + tableID);
|
print("[pokerAdmin] spawned table entity " + tableID + " for " + pokerID);
|
||||||
|
|
||||||
// Spawn a seat pad for each seat
|
// Spawn seat pads as children of the table entity
|
||||||
|
// Offsets are in table-local space so no world transform needed
|
||||||
for (var i = 0; i < seatCount; i++) {
|
for (var i = 0; i < seatCount; i++) {
|
||||||
var seat = layout[i];
|
var seat = layout[i];
|
||||||
var worldOffset = Vec3.multiplyQbyV(rotation, seat.offset);
|
|
||||||
var seatPos = Vec3.sum(position, worldOffset);
|
|
||||||
var seatRot = Quat.fromPitchYawRollDegrees(0, seat.yaw, 0);
|
var seatRot = Quat.fromPitchYawRollDegrees(0, seat.yaw, 0);
|
||||||
|
|
||||||
Entities.addEntity({
|
Entities.addEntity({
|
||||||
|
|
@ -80,8 +100,9 @@
|
||||||
imageURL: constants.POKER_SEAT_PAD.imageURL,
|
imageURL: constants.POKER_SEAT_PAD.imageURL,
|
||||||
dimensions: constants.POKER_SEAT_PAD.dimensions,
|
dimensions: constants.POKER_SEAT_PAD.dimensions,
|
||||||
alpha: constants.POKER_SEAT_PAD.alpha,
|
alpha: constants.POKER_SEAT_PAD.alpha,
|
||||||
position: seatPos,
|
parentID: tableID,
|
||||||
rotation: seatRot,
|
localPosition: seat.offset,
|
||||||
|
localRotation: seatRot,
|
||||||
script: "https://wizards.cyou/scripts/poker_sit.js",
|
script: "https://wizards.cyou/scripts/poker_sit.js",
|
||||||
userData: JSON.stringify({
|
userData: JSON.stringify({
|
||||||
tableID: tableID,
|
tableID: tableID,
|
||||||
|
|
@ -91,9 +112,10 @@
|
||||||
triggerable: true,
|
triggerable: true,
|
||||||
grabbable: false,
|
grabbable: false,
|
||||||
ignorePickIntersection: false,
|
ignorePickIntersection: false,
|
||||||
|
locked: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
print("[pokerAdmin] spawned seat " + i + " for table " + pokerID);
|
print("[pokerAdmin] spawned seat " + i + " parented to " + tableID);
|
||||||
}
|
}
|
||||||
|
|
||||||
tablet.emitScriptEvent(JSON.stringify({
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
|
|
@ -102,6 +124,41 @@
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function deleteTableEntities(pokerID) {
|
||||||
|
// Find table entity by name
|
||||||
|
var results = Entities.findEntitiesByName(
|
||||||
|
"poker_table_" + pokerID, MyAvatar.position, 100, false
|
||||||
|
);
|
||||||
|
|
||||||
|
if (results.length === 0) {
|
||||||
|
print("[pokerAdmin] no entities found for table " + pokerID);
|
||||||
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
|
type: "deleteEntitiesComplete",
|
||||||
|
pokerID: pokerID,
|
||||||
|
found: false,
|
||||||
|
}));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
results.forEach(function(tableEntityID) {
|
||||||
|
// Find and delete all children (seat pads)
|
||||||
|
var children = Entities.getChildrenIDs(tableEntityID);
|
||||||
|
children.forEach(function(childID) {
|
||||||
|
Entities.deleteEntity(childID);
|
||||||
|
print("[pokerAdmin] deleted seat entity " + childID);
|
||||||
|
});
|
||||||
|
// Delete the table itself
|
||||||
|
Entities.deleteEntity(tableEntityID);
|
||||||
|
print("[pokerAdmin] deleted table entity " + tableEntityID);
|
||||||
|
});
|
||||||
|
|
||||||
|
tablet.emitScriptEvent(JSON.stringify({
|
||||||
|
type: "deleteEntitiesComplete",
|
||||||
|
pokerID: pokerID,
|
||||||
|
found: true,
|
||||||
|
}));
|
||||||
|
}
|
||||||
|
|
||||||
button.clicked.connect(function() {
|
button.clicked.connect(function() {
|
||||||
tablet.gotoWebScreen(APP_URL);
|
tablet.gotoWebScreen(APP_URL);
|
||||||
tablet.webEventReceived.connect(webEventHandler);
|
tablet.webEventReceived.connect(webEventHandler);
|
||||||
|
|
|
||||||
|
|
@ -487,10 +487,11 @@ function renderTables(tables) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function deleteTable(id) {
|
function deleteTable(id) {
|
||||||
|
|
||||||
ensureSession(function(err) {
|
ensureSession(function(err) {
|
||||||
if (err) { setStatus(err, "error"); return; }
|
if (err) { setStatus(err, "error"); return; }
|
||||||
setStatus("Deleting " + id + "...");
|
setStatus("Deleting " + id + "...");
|
||||||
|
|
||||||
|
// Delete server-side first
|
||||||
fetch(POKER_BASE + "/admin/tables/" + id, {
|
fetch(POKER_BASE + "/admin/tables/" + id, {
|
||||||
method: "DELETE",
|
method: "DELETE",
|
||||||
headers: { "Authorization": "Bearer " + state.token },
|
headers: { "Authorization": "Bearer " + state.token },
|
||||||
|
|
@ -500,8 +501,11 @@ function deleteTable(id) {
|
||||||
return r.json();
|
return r.json();
|
||||||
})
|
})
|
||||||
.then(function() {
|
.then(function() {
|
||||||
setStatus("Table " + id + " deleted", "ok");
|
// Then clean up in-world entities
|
||||||
loadTables();
|
EventBridge.emitWebEvent(JSON.stringify({
|
||||||
|
type: "deleteTable",
|
||||||
|
pokerID: id,
|
||||||
|
}));
|
||||||
})
|
})
|
||||||
.catch(function(e) { setStatus(e.message, "error"); });
|
.catch(function(e) { setStatus(e.message, "error"); });
|
||||||
});
|
});
|
||||||
|
|
@ -535,6 +539,10 @@ function init() {
|
||||||
} else if (msg.type === "spawnComplete") {
|
} else if (msg.type === "spawnComplete") {
|
||||||
setStatus("Table and seats spawned!", "ok");
|
setStatus("Table and seats spawned!", "ok");
|
||||||
document.getElementById("btn-create").disabled = false;
|
document.getElementById("btn-create").disabled = false;
|
||||||
|
} else if (msg.type === "deleteEntitiesComplete") {
|
||||||
|
var detail = msg.found ? "Table and entities deleted" : "Table deleted (no entities found)";
|
||||||
|
setStatus(detail, "ok");
|
||||||
|
loadTables();
|
||||||
}
|
}
|
||||||
} catch(e) {
|
} catch(e) {
|
||||||
print("poker-admin event error: " + e);
|
print("poker-admin event error: " + e);
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue