Clear messages on scene change and log goal milestones
This commit is contained in:
33
src/goals.js
33
src/goals.js
@@ -12,6 +12,10 @@
|
|||||||
}) => {
|
}) => {
|
||||||
const goalMilestoneThresholds = [0.5, 0.75, 0.9];
|
const goalMilestoneThresholds = [0.5, 0.75, 0.9];
|
||||||
let announcedGoalMilestones = new Set();
|
let announcedGoalMilestones = new Set();
|
||||||
|
const logGoalEvent = (event, data = {}) => {
|
||||||
|
const sceneId = getCurrentScene()?.id || "unknown";
|
||||||
|
console.log("[Goals]", event, { sceneId, ...data });
|
||||||
|
};
|
||||||
|
|
||||||
const getGoalState = () => {
|
const getGoalState = () => {
|
||||||
const scene = getCurrentScene();
|
const scene = getCurrentScene();
|
||||||
@@ -65,7 +69,10 @@
|
|||||||
const totalTarget = targets.reduce((sum, t) => sum + t.count, 0);
|
const totalTarget = targets.reduce((sum, t) => sum + t.count, 0);
|
||||||
let totalAchieved = 0;
|
let totalAchieved = 0;
|
||||||
const parts = targets.map((t) => {
|
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);
|
const got = Math.min(t.count, achieved);
|
||||||
totalAchieved += got;
|
totalAchieved += got;
|
||||||
const remaining = Math.max(0, t.count - got);
|
const remaining = Math.max(0, t.count - got);
|
||||||
@@ -104,11 +111,25 @@
|
|||||||
if (fraction >= threshold && !announcedGoalMilestones.has(threshold)) {
|
if (fraction >= threshold && !announcedGoalMilestones.has(threshold)) {
|
||||||
announcedGoalMilestones.add(threshold);
|
announcedGoalMilestones.add(threshold);
|
||||||
const text = config.messages?.text || formatGoalMessage(goal);
|
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 =
|
const colors =
|
||||||
(Array.isArray(config.messages?.colors) && config.messages.colors) ||
|
(Array.isArray(config.messages?.colors) &&
|
||||||
|
config.messages.colors) ||
|
||||||
goal?.colors ||
|
goal?.colors ||
|
||||||
null;
|
null;
|
||||||
|
logGoalEvent("announce-progress", {
|
||||||
|
threshold,
|
||||||
|
progress: goal.progress,
|
||||||
|
colors,
|
||||||
|
text,
|
||||||
|
});
|
||||||
ui.showFloatingMessage(
|
ui.showFloatingMessage(
|
||||||
{ text, colors },
|
{ text, colors },
|
||||||
{
|
{
|
||||||
@@ -131,6 +152,11 @@
|
|||||||
(Array.isArray(config.messages?.colors) && config.messages.colors) ||
|
(Array.isArray(config.messages?.colors) && config.messages.colors) ||
|
||||||
goal?.colors ||
|
goal?.colors ||
|
||||||
null;
|
null;
|
||||||
|
logGoalEvent("goal-intro", {
|
||||||
|
progress: goal?.progress,
|
||||||
|
colors,
|
||||||
|
text,
|
||||||
|
});
|
||||||
ui.showFloatingMessage(
|
ui.showFloatingMessage(
|
||||||
{ text, colors },
|
{ text, colors },
|
||||||
{
|
{
|
||||||
@@ -142,6 +168,7 @@
|
|||||||
|
|
||||||
const resetMilestones = () => {
|
const resetMilestones = () => {
|
||||||
announcedGoalMilestones = new Set();
|
announcedGoalMilestones = new Set();
|
||||||
|
logGoalEvent("reset-milestones");
|
||||||
};
|
};
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@@ -179,6 +179,7 @@
|
|||||||
const applyScene = (sceneId) => {
|
const applyScene = (sceneId) => {
|
||||||
const next = (getSceneById && getSceneById(scenes, sceneId)) || scenes[0];
|
const next = (getSceneById && getSceneById(scenes, sceneId)) || scenes[0];
|
||||||
if (!next) return;
|
if (!next) return;
|
||||||
|
ui.clearMessages();
|
||||||
currentScene = next;
|
currentScene = next;
|
||||||
ui.setSceneSelection(next.id);
|
ui.setSceneSelection(next.id);
|
||||||
setSceneIdInUrl(next.id);
|
setSceneIdInUrl(next.id);
|
||||||
|
|||||||
@@ -234,6 +234,12 @@
|
|||||||
|
|
||||||
const activeMessages = [];
|
const activeMessages = [];
|
||||||
|
|
||||||
|
const clearMessages = () => {
|
||||||
|
if (!floatingMessagesEl) return;
|
||||||
|
activeMessages.length = 0;
|
||||||
|
floatingMessagesEl.innerHTML = "";
|
||||||
|
};
|
||||||
|
|
||||||
const showFloatingMessage = (message, options = {}) => {
|
const showFloatingMessage = (message, options = {}) => {
|
||||||
if (!floatingMessagesEl) return;
|
if (!floatingMessagesEl) return;
|
||||||
const msgObj =
|
const msgObj =
|
||||||
@@ -285,6 +291,7 @@
|
|||||||
setGoal,
|
setGoal,
|
||||||
showFloatingMessage,
|
showFloatingMessage,
|
||||||
setMessageDefaults,
|
setMessageDefaults,
|
||||||
|
clearMessages,
|
||||||
};
|
};
|
||||||
return api;
|
return api;
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user