Clear messages on scene change and log goal milestones

This commit is contained in:
Daddy32
2025-12-16 13:15:13 +01:00
parent f12fb477cb
commit 1bef87022b
3 changed files with 38 additions and 3 deletions

View File

@@ -12,6 +12,10 @@
}) => {
const goalMilestoneThresholds = [0.5, 0.75, 0.9];
let announcedGoalMilestones = new Set();
const logGoalEvent = (event, data = {}) => {
const sceneId = getCurrentScene()?.id || "unknown";
console.log("[Goals]", event, { sceneId, ...data });
};
const getGoalState = () => {
const scene = getCurrentScene();
@@ -65,7 +69,10 @@
const totalTarget = targets.reduce((sum, t) => sum + t.count, 0);
let totalAchieved = 0;
const parts = targets.map((t) => {
const achieved = Math.max(0, clearedByColor[normalizeColor(t.color)] || 0);
const achieved = Math.max(
0,
clearedByColor[normalizeColor(t.color)] || 0,
);
const got = Math.min(t.count, achieved);
totalAchieved += got;
const remaining = Math.max(0, t.count - got);
@@ -104,11 +111,25 @@
if (fraction >= threshold && !announcedGoalMilestones.has(threshold)) {
announcedGoalMilestones.add(threshold);
const text = config.messages?.text || formatGoalMessage(goal);
if (!text) return;
if (!text) {
logGoalEvent("skip-progress-announcement", {
threshold,
progress: goal.progress,
reason: "missing text",
});
return;
}
const colors =
(Array.isArray(config.messages?.colors) && config.messages.colors) ||
(Array.isArray(config.messages?.colors) &&
config.messages.colors) ||
goal?.colors ||
null;
logGoalEvent("announce-progress", {
threshold,
progress: goal.progress,
colors,
text,
});
ui.showFloatingMessage(
{ text, colors },
{
@@ -131,6 +152,11 @@
(Array.isArray(config.messages?.colors) && config.messages.colors) ||
goal?.colors ||
null;
logGoalEvent("goal-intro", {
progress: goal?.progress,
colors,
text,
});
ui.showFloatingMessage(
{ text, colors },
{
@@ -142,6 +168,7 @@
const resetMilestones = () => {
announcedGoalMilestones = new Set();
logGoalEvent("reset-milestones");
};
return {

View File

@@ -179,6 +179,7 @@
const applyScene = (sceneId) => {
const next = (getSceneById && getSceneById(scenes, sceneId)) || scenes[0];
if (!next) return;
ui.clearMessages();
currentScene = next;
ui.setSceneSelection(next.id);
setSceneIdInUrl(next.id);

View File

@@ -234,6 +234,12 @@
const activeMessages = [];
const clearMessages = () => {
if (!floatingMessagesEl) return;
activeMessages.length = 0;
floatingMessagesEl.innerHTML = "";
};
const showFloatingMessage = (message, options = {}) => {
if (!floatingMessagesEl) return;
const msgObj =
@@ -285,6 +291,7 @@
setGoal,
showFloatingMessage,
setMessageDefaults,
clearMessages,
};
return api;
};