feat: Add a new Foundry VTT module for tracking and displaying session numbers with GM controls and auto-increment functionality.

This commit is contained in:
CPTN Cosmo 2025-12-21 19:11:07 +01:00
parent ad704b5fa2
commit 44b80af3d0
5 changed files with 320 additions and 0 deletions

90
scripts/main.js Normal file
View file

@ -0,0 +1,90 @@
import { SessionTrackerApp } from "./session-tracker-app.js";
Hooks.once("init", () => {
console.log("Session Tracker | Initializing");
// Register Session Count
game.settings.register("fvtt-session-tracker", "sessionCount", {
name: "Current Session Number",
hint: "The current session number displayed on the tracker.",
scope: "world",
config: true,
type: Number,
default: 1,
onChange: () => {
if (SessionTrackerApp.instance) SessionTrackerApp.instance.render(true);
}
});
// Register Auto-Increment Toggle
game.settings.register("fvtt-session-tracker", "autoIncrement", {
name: "Automate Session Tracking",
hint: "Automatically increment the session count when the selected user logs in.",
scope: "world",
config: true,
type: Boolean,
default: false
});
// Register Trigger User
game.settings.register("fvtt-session-tracker", "triggerUser", {
name: "Trigger User",
hint: "Selecting this user will trigger the session increment when they log in.",
scope: "world",
config: true,
type: String,
default: "",
choices: () => {
const users = game.users.reduce((acc, u) => {
acc[u.id] = u.name;
return acc;
}, {});
return users;
}
});
// Register tracker position (internal)
game.settings.register("fvtt-session-tracker", "position", {
scope: "client",
config: false,
type: Object,
default: { top: 10, left: 120 }
});
// Register visibility toggle for players
game.settings.register("fvtt-session-tracker", "showTracker", {
name: "Show Session Tracker",
hint: "Whether to display the session tracker on your canvas.",
scope: "client",
config: true,
type: Boolean,
default: true,
onChange: (value) => {
if (SessionTrackerApp.instance) {
if (value) SessionTrackerApp.instance.render(true);
else SessionTrackerApp.instance.close();
}
}
});
});
Hooks.once("ready", () => {
// Initialize the app
SessionTrackerApp.initialize();
// Logic for auto-incrementing
if (game.user.isGM) {
Hooks.on("userConnected", (user, connected) => {
if (!connected) return;
const isAuto = game.settings.get("fvtt-session-tracker", "autoIncrement");
const targetUser = game.settings.get("fvtt-session-tracker", "triggerUser");
if (isAuto && user.id === targetUser) {
const current = game.settings.get("fvtt-session-tracker", "sessionCount");
game.settings.set("fvtt-session-tracker", "sessionCount", current + 1);
ui.notifications.info(`Session Tracker | User ${user.name} logged in. Incrementing to session ${current + 1}.`);
}
});
}
});

View file

@ -0,0 +1,106 @@
const { ApplicationV2, HandlebarsApplicationMixin } = foundry.applications.api;
export class SessionTrackerApp extends HandlebarsApplicationMixin(ApplicationV2) {
static instance;
constructor(options = {}) {
super(options);
}
static DEFAULT_OPTIONS = {
id: "session-tracker-app",
tag: "aside",
classes: ["session-tracker"],
window: {
frame: false,
positioned: true,
},
position: {
width: "auto",
height: "auto",
},
actions: {
increment: SessionTrackerApp.#onIncrement,
decrement: SessionTrackerApp.#onDecrement,
}
};
static PARTS = {
content: {
template: "modules/fvtt-session-tracker/templates/session-tracker.hbs",
},
};
static initialize() {
this.instance = new SessionTrackerApp();
if (!game.settings.get("fvtt-session-tracker", "showTracker")) return;
const pos = game.settings.get("fvtt-session-tracker", "position");
this.instance.render(true, { position: pos });
}
async _prepareContext(options) {
return {
sessionNumber: game.settings.get("fvtt-session-tracker", "sessionCount"),
isGM: game.user.isGM
};
}
static async #onIncrement(event, target) {
if (!game.user.isGM) return;
const current = game.settings.get("fvtt-session-tracker", "sessionCount");
await game.settings.set("fvtt-session-tracker", "sessionCount", current + 1);
}
static async #onDecrement(event, target) {
if (!game.user.isGM) return;
const current = game.settings.get("fvtt-session-tracker", "sessionCount");
if (current <= 1) return;
await game.settings.set("fvtt-session-tracker", "sessionCount", current - 1);
}
// Drag and drop support for positioning
_onRender(context, options) {
if (!game.user.isGM) return;
const dragHandle = this.element;
let isDragging = false;
let startX, startY, startLeft, startTop;
dragHandle.addEventListener('mousedown', (e) => {
if (e.button !== 0) return; // Only left click
isDragging = true;
startX = e.clientX;
startY = e.clientY;
const rect = this.element.getBoundingClientRect();
startLeft = rect.left;
startTop = rect.top;
this.element.style.cursor = 'grabbing';
});
window.addEventListener('mousemove', (e) => {
if (!isDragging) return;
const dx = e.clientX - startX;
const dy = e.clientY - startY;
const newLeft = startLeft + dx;
const newTop = startTop + dy;
this.element.style.left = `${newLeft}px`;
this.element.style.top = `${newTop}px`;
});
window.addEventListener('mouseup', () => {
if (!isDragging) return;
isDragging = false;
this.element.style.cursor = 'move';
// Save position
const rect = this.element.getBoundingClientRect();
game.settings.set("fvtt-session-tracker", "position", {
top: rect.top,
left: rect.left
});
});
}
}