Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enh/random music track #2628

Merged
merged 3 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions client/apps/game/src/hooks/store/useUIStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { ContractAddress } from "@bibliothecadao/eternum";
import React from "react";
import { create } from "zustand";
import { subscribeWithSelector } from "zustand/middleware";
import { tracks } from "../useMusic";
import { BuildModeStore, createBuildModeStoreSlice } from "./_buildModeStore";
import { PopupsStore, createPopupsSlice } from "./_popupsStore";
import { ThreeStore, createThreeStoreSlice } from "./_threeStore";
Expand Down Expand Up @@ -73,6 +74,8 @@ interface UIStore {

export type AppStore = UIStore & PopupsStore & ThreeStore & BuildModeStore & RealmStore & BlockchainStore & WorldStore;

const initialTrackIndex = Math.floor(Math.random() * tracks.length);
edisontim marked this conversation as resolved.
Show resolved Hide resolved

Comment on lines +77 to +78
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Consider adding safety checks and memoization for track initialization.

The random track selection has potential issues:

  1. No validation for empty tracks array
  2. Random selection occurs on every store initialization
  3. Value isn't memoized

Consider this safer implementation:

-const initialTrackIndex = Math.floor(Math.random() * tracks.length);
+const getInitialTrackIndex = () => {
+  if (!tracks.length) return 0;
+  return Math.floor(Math.random() * tracks.length);
+};
+const initialTrackIndex = getInitialTrackIndex();

Also, consider memoizing this value to prevent unexpected track changes on store reinitialization:

// At the top of the file
const MEMOIZED_INITIAL_TRACK_INDEX = getInitialTrackIndex();

const useUIStore = create(
subscribeWithSelector<AppStore>((set, get) => ({
theme: "light",
Expand All @@ -84,9 +87,9 @@ const useUIStore = create(
isSideMenuOpened: true,
toggleSideMenu: () => set((state) => ({ isSideMenuOpened: !state.isSideMenuOpened })),
isSoundOn: localStorage.getItem("soundEnabled") ? localStorage.getItem("soundEnabled") === "true" : true,
trackName: "Day Break",
trackName: tracks[initialTrackIndex].name,
setTrackName: (name) => set({ trackName: name }),
trackIndex: 1,
trackIndex: initialTrackIndex,
setTrackIndex: (index) => set({ trackIndex: index }),
toggleSound: () =>
set((state) => {
Expand Down
3 changes: 2 additions & 1 deletion client/apps/game/src/hooks/useMusic.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Track = {
};

// Your tracks list
const tracks: Track[] = [
export const tracks: Track[] = [
{ name: "Day Break", url: "/sound/music/DayBreak.mp3" },
{ name: "Morning Ember", url: "/sound/music/MorningEmber.mp3" },
{ name: "Beyond The Horizon", url: "/sound/music/BeyondTheHorizon.mp3" },
Expand All @@ -22,6 +22,7 @@ const tracks: Track[] = [
{ name: "Strangers Arrival", url: "/sound/music/StrangersArrival.mp3" },
{ name: "Twilight Harvest", url: "/sound/music/TwilightHarvest.mp3" },
{ name: "Wanderers Chronicle", url: "/sound/music/WanderersChronicle.mp3" },
{ name: "Happy Realm", url: "/sound/music/happy_realm.mp3" },
];

export const useMusicPlayer = () => {
Expand Down
Loading