Skip to content

Commit

Permalink
feat: assign random dice side when null (#49)
Browse files Browse the repository at this point in the history
* feat: assign random dice side when null

* feat: move randomizing missing dice side

* feat: update type declaration

* fix: use a random value from array for generation/regeneration

Signed-off-by: Raphael Arce <[email protected]>

* move random fallback to statejs

* feat: clean up

---------

Signed-off-by: Raphael Arce <[email protected]>
Co-authored-by: Raphael Arce <[email protected]>
  • Loading branch information
aeschi and raphael-arce authored Oct 1, 2024
1 parent f32ef4c commit 79c5777
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
54 changes: 33 additions & 21 deletions services/rest/paths/pick-idea/pick-idea.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
getAllLabelsForCurrentSides,
getLabelsForCurrentSides,
getLabelFromSide,
} from "../../../state/state.js";
import { supabase } from "../../../supabase/supabase.js";
import { generateIdea } from "../../idea-generation/idea-generation.js";
Expand Down Expand Up @@ -56,11 +56,9 @@ export async function handlePickIdea(response) {
async function pickIdea() {
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);
return { error: errorMessage}
}
console.log({ focusGroup });
console.log({ topic });
console.log({ medium });

const { data, error } = await supabase
.from("pregenerated_ideas")
Expand All @@ -75,12 +73,15 @@ async function pickIdea() {
}

if (data.length === 0) {
const { focusGroup, topic, medium } = getLabelsForCurrentSides();
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,
});

Expand All @@ -101,7 +102,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,
Expand All @@ -116,45 +117,52 @@ let isRegenerating = false;

/**
* Regenerates an idea for the current dice sides
* @param {string[]} focusGroup
* @param {string[]} topic
* @param {string[]} medium
* @returns {Promise<void>}
*/
export async function regenerate() {
export async function regenerate(focusGroup, topic, medium) {
if (isRegenerating) {
return;
}
isRegenerating = true;

console.time("regeneration");

const { focusGroup, medium, topic } = getLabelsForCurrentSides();
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");

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<boolean>}
*/
async function hasEnoughIdeas() {
const { focusGroup, topic, medium } = getAllLabelsForCurrentSides();

async function hasEnoughIdeas(focusGroup, topic, medium) {
const { data, error } = await supabase
.from("pregenerated_ideas")
.select("*")
Expand All @@ -172,3 +180,7 @@ async function hasEnoughIdeas() {

return true;
}

function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}
22 changes: 12 additions & 10 deletions services/state/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(",")) {
Expand Down Expand Up @@ -194,21 +194,19 @@ export function getAllLabelsForSides({ A, B, C }) {
*/
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,
};
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);

return { focusGroup, topic, medium };
}

Expand All @@ -224,3 +222,7 @@ export function setLabels(newLabels) {
console.error(error);
}
}

function getRandomNumber(min, max) {
return Math.floor(Math.random() * (max - min + 1) + min);
}

0 comments on commit 79c5777

Please sign in to comment.