From 21e1f740ee7d3d863c5308ff2e53c388dca709f6 Mon Sep 17 00:00:00 2001 From: aeschi Date: Thu, 26 Sep 2024 12:41:49 +0200 Subject: [PATCH 1/6] feat: assign random dice side when null --- services/state/state.js | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/services/state/state.js b/services/state/state.js index 4875956..16c139a 100644 --- a/services/state/state.js +++ b/services/state/state.js @@ -188,18 +188,19 @@ export function getAllLabelsForSides({ A, B, C }) { return { focusGroup, topic, medium }; } +function assignRandomSide(cube) { + const random = Math.floor(Math.random() * 6) + 1; + setDiceSide(`${cube}${random}`); +} + /** * Returns all labels as array for the current dice sides * @returns {{focusGroup: string[], topic: string[], medium: string[]}} */ export function getAllLabelsForCurrentSides() { - if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { - return { - focusGroup: dices.A.side, - topic: dices.B.side, - medium: dices.C.side, - }; - } + dices.A.side === null && assignRandomSide("A"); + dices.B.side === null && assignRandomSide("B"); + dices.C.side === null && assignRandomSide("C"); const A = `A${dices.A.side}`; const B = `B${dices.B.side}`; From ffe0e40add7192ac6e7404c5694d4a8b4e55b27e Mon Sep 17 00:00:00 2001 From: aeschi Date: Thu, 26 Sep 2024 16:28:25 +0200 Subject: [PATCH 2/6] feat: move randomizing missing dice side --- services/rest/paths/pick-idea/pick-idea.js | 34 +++++++++++++--------- services/state/state.js | 19 +++++------- 2 files changed, 28 insertions(+), 25 deletions(-) diff --git a/services/rest/paths/pick-idea/pick-idea.js b/services/rest/paths/pick-idea/pick-idea.js index dbde235..ecadaf2 100644 --- a/services/rest/paths/pick-idea/pick-idea.js +++ b/services/rest/paths/pick-idea/pick-idea.js @@ -1,6 +1,7 @@ import { getAllLabelsForCurrentSides, getLabelsForCurrentSides, + getLabelFromSide, } from "../../../state/state.js"; import { supabase } from "../../../supabase/supabase.js"; import { generateIdea } from "../../idea-generation/idea-generation.js"; @@ -54,12 +55,17 @@ export async function handlePickIdea(response) { * @returns {Promise<{error: string}|{idea: dbIdea}|{idea:Idea}>} */ async function pickIdea() { - const { focusGroup, topic, medium } = getAllLabelsForCurrentSides(); + let { focusGroup, topic, medium } = getAllLabelsForCurrentSides(); if (!focusGroup || !topic || !medium) { - const errorMessage = `Some dices have not set a side yet: ${JSON.stringify({focusGroup, topic, medium})}`; + const errorMessage = `Some dices have not set a side yet: ${JSON.stringify({ focusGroup, topic, medium })}`; console.error(errorMessage); - return { error: errorMessage} + + const random = Math.floor(Math.random() * 6) + 1; + + focusGroup = focusGroup || getLabelFromSide(`A${random}`); + topic = topic || getLabelFromSide(`B${random}`); + medium = medium || getLabelFromSide(`C${random}`); } const { data, error } = await supabase @@ -75,8 +81,6 @@ async function pickIdea() { } if (data.length === 0) { - const { focusGroup, topic, medium } = getLabelsForCurrentSides(); - const idea = await generateIdea({ focusGroup, medium, @@ -101,7 +105,7 @@ async function pickIdea() { /** * this promise is not awaited by design, so it runs in the background */ - regenerate(); + regenerate(focusGroup, topic, medium); return { idea: pickedIdea, @@ -116,9 +120,12 @@ let isRegenerating = false; /** * Regenerates an idea for the current dice sides + * @param {string} focusGroup + * @param {string} topic + * @param {string} medium * @returns {Promise} */ -export async function regenerate() { +export async function regenerate(focusGroup, topic, medium) { if (isRegenerating) { return; } @@ -126,8 +133,6 @@ export async function regenerate() { console.time("regeneration"); - const { focusGroup, medium, topic } = getLabelsForCurrentSides(); - await generateIdea({ focusGroup, medium, @@ -138,23 +143,24 @@ export async function regenerate() { isRegenerating = false; - if (await hasEnoughIdeas()) { + if (await hasEnoughIdeas(focusGroup, topic, medium)) { return; } /** * this promise is not awaited by design, so it runs in the background */ - regenerate(); + regenerate(focusGroup, topic, medium); } /** * Checks if there are enough pregenerated ideas for the current dice sides + * @param {string} focusGroup + * @param {string} topic + * @param {string} medium * @returns {Promise} */ -async function hasEnoughIdeas() { - const { focusGroup, topic, medium } = getAllLabelsForCurrentSides(); - +async function hasEnoughIdeas(focusGroup, topic, medium) { const { data, error } = await supabase .from("pregenerated_ideas") .select("*") diff --git a/services/state/state.js b/services/state/state.js index 16c139a..68a2ae5 100644 --- a/services/state/state.js +++ b/services/state/state.js @@ -163,7 +163,7 @@ export function getLabelsForCurrentSides() { * @param side * @returns {string[]} */ -function getLabelFromSide(side) { +export function getLabelFromSide(side) { const label = /** @type string */ labels[side]; if (label.includes(",")) { @@ -188,28 +188,25 @@ export function getAllLabelsForSides({ A, B, C }) { return { focusGroup, topic, medium }; } -function assignRandomSide(cube) { - const random = Math.floor(Math.random() * 6) + 1; - setDiceSide(`${cube}${random}`); -} - /** * Returns all labels as array for the current dice sides * @returns {{focusGroup: string[], topic: string[], medium: string[]}} */ export function getAllLabelsForCurrentSides() { - dices.A.side === null && assignRandomSide("A"); - dices.B.side === null && assignRandomSide("B"); - dices.C.side === null && assignRandomSide("C"); + if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { + return { + focusGroup: dices.A.side, + topic: dices.B.side, + medium: dices.C.side, + }; + } const A = `A${dices.A.side}`; const B = `B${dices.B.side}`; const C = `C${dices.C.side}`; - const focusGroup = getLabelFromSide(A); const topic = getLabelFromSide(B); const medium = getLabelFromSide(C); - return { focusGroup, topic, medium }; } From 43eb7d6ee302feeada4932b6060c395456e5704d Mon Sep 17 00:00:00 2001 From: aeschi Date: Fri, 27 Sep 2024 08:42:44 +0200 Subject: [PATCH 3/6] feat: update type declaration --- services/rest/paths/pick-idea/pick-idea.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/services/rest/paths/pick-idea/pick-idea.js b/services/rest/paths/pick-idea/pick-idea.js index ecadaf2..a476470 100644 --- a/services/rest/paths/pick-idea/pick-idea.js +++ b/services/rest/paths/pick-idea/pick-idea.js @@ -120,9 +120,9 @@ let isRegenerating = false; /** * Regenerates an idea for the current dice sides - * @param {string} focusGroup - * @param {string} topic - * @param {string} medium + * @param {string[]} focusGroup + * @param {string[]} topic + * @param {string[]} medium * @returns {Promise} */ export async function regenerate(focusGroup, topic, medium) { @@ -155,9 +155,9 @@ export async function regenerate(focusGroup, topic, medium) { /** * Checks if there are enough pregenerated ideas for the current dice sides - * @param {string} focusGroup - * @param {string} topic - * @param {string} medium + * @param {string[]} focusGroup + * @param {string[]} topic + * @param {string[]} medium * @returns {Promise} */ async function hasEnoughIdeas(focusGroup, topic, medium) { From caf2ce7ead63fad5ebb69918a8ca2306b234f6a2 Mon Sep 17 00:00:00 2001 From: Raphael Arce Date: Mon, 30 Sep 2024 16:04:33 +0200 Subject: [PATCH 4/6] fix: use a random value from array for generation/regeneration Signed-off-by: Raphael Arce --- services/rest/paths/pick-idea/pick-idea.js | 35 ++++++++++++++-------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/services/rest/paths/pick-idea/pick-idea.js b/services/rest/paths/pick-idea/pick-idea.js index a476470..7dd0b58 100644 --- a/services/rest/paths/pick-idea/pick-idea.js +++ b/services/rest/paths/pick-idea/pick-idea.js @@ -1,6 +1,5 @@ import { getAllLabelsForCurrentSides, - getLabelsForCurrentSides, getLabelFromSide, } from "../../../state/state.js"; import { supabase } from "../../../supabase/supabase.js"; @@ -61,11 +60,9 @@ async function pickIdea() { const errorMessage = `Some dices have not set a side yet: ${JSON.stringify({ focusGroup, topic, medium })}`; console.error(errorMessage); - const random = Math.floor(Math.random() * 6) + 1; - - focusGroup = focusGroup || getLabelFromSide(`A${random}`); - topic = topic || getLabelFromSide(`B${random}`); - medium = medium || getLabelFromSide(`C${random}`); + focusGroup = focusGroup || getLabelFromSide(`A${getRandomNumber(1, 6)}`); + topic = topic || getLabelFromSide(`B${getRandomNumber(1, 6)}`); + medium = medium || getLabelFromSide(`C${getRandomNumber(1, 6)}`); } const { data, error } = await supabase @@ -81,10 +78,15 @@ async function pickIdea() { } if (data.length === 0) { + const randomFocusGroup = + focusGroup[getRandomNumber(0, focusGroup.length - 1)]; + const randomTopic = topic[getRandomNumber(0, topic.length - 1)]; + const randomMedium = medium[getRandomNumber(0, medium.length - 1)]; + const idea = await generateIdea({ - focusGroup, - medium, - topic, + focusGroup: randomFocusGroup, + medium: randomMedium, + topic: randomTopic, strategy: strategies.realtime, }); @@ -133,10 +135,15 @@ export async function regenerate(focusGroup, topic, medium) { console.time("regeneration"); + const randomFocusGroup = + focusGroup[getRandomNumber(0, focusGroup.length - 1)]; + const randomTopic = topic[getRandomNumber(0, topic.length - 1)]; + const randomMedium = medium[getRandomNumber(0, medium.length - 1)]; + await generateIdea({ - focusGroup, - medium, - topic, + focusGroup: randomFocusGroup, + medium: randomMedium, + topic: randomTopic, strategy: strategies.pregenerate, }); console.timeEnd("regeneration"); @@ -178,3 +185,7 @@ async function hasEnoughIdeas(focusGroup, topic, medium) { return true; } + +function getRandomNumber(min, max) { + return Math.floor(Math.random() * (max - min + 1) + min); +} From 54be08944dccd28962ee40662cc7bbcd7dc79579 Mon Sep 17 00:00:00 2001 From: aeschi Date: Tue, 1 Oct 2024 11:48:47 +0200 Subject: [PATCH 5/6] move random fallback to statejs --- services/rest/paths/pick-idea/pick-idea.js | 13 ++++------ services/state/state.js | 28 +++++++++++++++------- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/services/rest/paths/pick-idea/pick-idea.js b/services/rest/paths/pick-idea/pick-idea.js index 7dd0b58..5722082 100644 --- a/services/rest/paths/pick-idea/pick-idea.js +++ b/services/rest/paths/pick-idea/pick-idea.js @@ -54,16 +54,11 @@ export async function handlePickIdea(response) { * @returns {Promise<{error: string}|{idea: dbIdea}|{idea:Idea}>} */ async function pickIdea() { - let { focusGroup, topic, medium } = getAllLabelsForCurrentSides(); + const { focusGroup, topic, medium } = getAllLabelsForCurrentSides(); - if (!focusGroup || !topic || !medium) { - const errorMessage = `Some dices have not set a side yet: ${JSON.stringify({ focusGroup, topic, medium })}`; - console.error(errorMessage); - - focusGroup = focusGroup || getLabelFromSide(`A${getRandomNumber(1, 6)}`); - topic = topic || getLabelFromSide(`B${getRandomNumber(1, 6)}`); - medium = medium || getLabelFromSide(`C${getRandomNumber(1, 6)}`); - } + console.log({ focusGroup }); + console.log({ topic }); + console.log({ medium }); const { data, error } = await supabase .from("pregenerated_ideas") diff --git a/services/state/state.js b/services/state/state.js index 68a2ae5..9862c0e 100644 --- a/services/state/state.js +++ b/services/state/state.js @@ -193,17 +193,25 @@ export function getAllLabelsForSides({ A, B, C }) { * @returns {{focusGroup: string[], topic: string[], medium: string[]}} */ export function getAllLabelsForCurrentSides() { + // if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { + // return { + // focusGroup: dices.A.side, + // topic: dices.B.side, + // medium: dices.C.side, + // }; + // } + if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { - return { - focusGroup: dices.A.side, - topic: dices.B.side, - medium: dices.C.side, - }; + const errorMessage = `Some dices have not set a side yet: ${JSON.stringify(dices.A.side, dices.B.side, dices.C.side)}`; + console.error(errorMessage); } - const A = `A${dices.A.side}`; - const B = `B${dices.B.side}`; - const C = `C${dices.C.side}`; + const A = dices.A.side ? `A${dices.A.side}` : `A${getRandomNumber(1, 6)}`; + const B = dices.B.side ? `B${dices.B.side}` : `B${getRandomNumber(1, 6)}`; + const C = dices.C.side ? `C${dices.C.side}` : `C${getRandomNumber(1, 6)}`; + + console.log(A, B, C); + const focusGroup = getLabelFromSide(A); const topic = getLabelFromSide(B); const medium = getLabelFromSide(C); @@ -222,3 +230,7 @@ export function setLabels(newLabels) { console.error(error); } } + +function getRandomNumber(min, max) { + return Math.floor(Math.random() * (max - min + 1) + min); +} From 98b21afa7d05d3a33f9005d824df5803432cf107 Mon Sep 17 00:00:00 2001 From: aeschi Date: Tue, 1 Oct 2024 13:20:26 +0200 Subject: [PATCH 6/6] feat: clean up --- services/state/state.js | 8 -------- 1 file changed, 8 deletions(-) diff --git a/services/state/state.js b/services/state/state.js index 9862c0e..3bcc72a 100644 --- a/services/state/state.js +++ b/services/state/state.js @@ -193,14 +193,6 @@ export function getAllLabelsForSides({ A, B, C }) { * @returns {{focusGroup: string[], topic: string[], medium: string[]}} */ export function getAllLabelsForCurrentSides() { - // if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { - // return { - // focusGroup: dices.A.side, - // topic: dices.B.side, - // medium: dices.C.side, - // }; - // } - if (dices.A.side === null || dices.B.side === null || dices.C.side === null) { const errorMessage = `Some dices have not set a side yet: ${JSON.stringify(dices.A.side, dices.B.side, dices.C.side)}`; console.error(errorMessage);