Skip to content

Commit

Permalink
feat: move randomizing missing dice side
Browse files Browse the repository at this point in the history
  • Loading branch information
aeschi committed Sep 26, 2024
1 parent 21e1f74 commit ffe0e40
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
34 changes: 20 additions & 14 deletions services/rest/paths/pick-idea/pick-idea.js
Original file line number Diff line number Diff line change
@@ -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";
Expand Down Expand Up @@ -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
Expand All @@ -75,8 +81,6 @@ async function pickIdea() {
}

if (data.length === 0) {
const { focusGroup, topic, medium } = getLabelsForCurrentSides();

const idea = await generateIdea({
focusGroup,
medium,
Expand All @@ -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,
Expand All @@ -116,18 +120,19 @@ 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();

await generateIdea({
focusGroup,
medium,
Expand All @@ -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<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 Down
19 changes: 8 additions & 11 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 All @@ -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 };
}

Expand Down

0 comments on commit ffe0e40

Please sign in to comment.