UNPKG

cbt-game-generator

Version:

Configuration generator for CBT animation apps

85 lines (71 loc) 2.51 kB
import { Color } from "three"; import { useGameStore } from "../experience/shared/store/zustandStore"; import { ELanguages } from "../experience/shared/constants/Games"; /** * x and y are exclusive * @param x * @param y * @returns */ export function randomNumber(x: number, y: number): number { return Math.floor(Math.random() * (y - (x + 1))) + (x + 1); } /** * Generate a list of random numbers of given length * can have duplicates * @param x * @param y * @param length */ export function randomNumbers(x: number, y: number, length: number): number[] { const randomGroup = new Array(length).fill(undefined); randomGroup.forEach((_, index) => { randomGroup[index] = randomNumber(x, y); }); return randomGroup; } export function shuffleArray<T>(array: T[]): T[] { for (let i = array.length - 1; i > 0; i--) { // Generate a random index from 0 to i const randomIndex = Math.floor(Math.random() * (i + 1)); // Swap the elements at i and randomIndex [array[i], array[randomIndex]] = [array[randomIndex], array[i]]; } return array; } export function choose<T>(array: T[], numberOfItems: number): T[] { const arrayCopy = [...array]; return arrayCopy.slice(0, numberOfItems); } const parseRGBA = (rgbaString: string) => { const match = rgbaString.match(/\d+/g); if (!match || match.length < 3) throw new Error("Invalid RGBA format"); const [r, g, b] = match.map(Number); return new Color(r / 255, g / 255, b / 255).convertSRGBToLinear(); }; export const convertColors = (colors: string[]) => colors.map(parseRGBA); export const getCurrentLanguage = () => { const pathSegments = window.location.pathname.split("/"); if (pathSegments.length > 3) { const languageFromURL = pathSegments[3]; // Extract language useGameStore.getState().setLanguage(languageFromURL); // Update Zustand store return languageFromURL; } return useGameStore.getState().language; // Return default if not found }; export const languageMap: Record<ELanguages, string> = { [ELanguages.ARABIC]: "arabic", [ELanguages.ENGLISH]: "english", // Add more language mappings here if needed }; export const uniqueShuffleArray = <T>(array: T[]): T[] => { const uniqueArray = Array.from(new Set(array)); for (let i = uniqueArray.length - 1; i > 0; i--) { const randomIndex = Math.floor(Math.random() * (i + 1)); [uniqueArray[i], uniqueArray[randomIndex]] = [ uniqueArray[randomIndex], uniqueArray[i], ]; } return uniqueArray; };