Skip to content

Commit

Permalink
feat() add music
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Nov 29, 2023
1 parent 9746bb1 commit 48ee45a
Show file tree
Hide file tree
Showing 11 changed files with 39 additions and 9 deletions.
12 changes: 11 additions & 1 deletion src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,26 @@ import { ReactNode, useEffect, useState } from 'react';

import { init } from './fhevmjs';
import { Connect, Layout } from './modules/common-ui';
import { pauseMusic, playMusic } from './utils/music';

import './App.css';

type AppProps = {
music?: boolean;
children: (account: string, provider: BrowserProvider) => ReactNode;
};

const App = ({ children }: AppProps) => {
const App = ({ music = false, children }: AppProps) => {
const [isInitialized, setIsInitialized] = useState(false);

useEffect(() => {
if (music) {
playMusic();
} else {
pauseMusic();
}
}, [music]);

useEffect(() => {
init()
.then(() => {
Expand Down
Binary file added src/assets/music.mp3
Binary file not shown.
6 changes: 3 additions & 3 deletions src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,23 +14,23 @@ const router = createHashRouter([
path: '/',
element: (
<React.StrictMode>
<App>{(account, provider) => <Home account={account} provider={provider} />}</App>
<App music>{(account, provider) => <Home account={account} provider={provider} />}</App>
</React.StrictMode>
),
},
{
path: 'rules',
element: (
<React.StrictMode>
<App>{(account, provider) => <Rules account={account} provider={provider} />}</App>
<App music>{(account, provider) => <Rules account={account} provider={provider} />}</App>
</React.StrictMode>
),
},
{
path: 'join',
element: (
<React.StrictMode>
<App>{(account, provider) => <JoinGame account={account} provider={provider} />}</App>
<App music>{(account, provider) => <JoinGame account={account} provider={provider} />}</App>
</React.StrictMode>
),
},
Expand Down
6 changes: 4 additions & 2 deletions src/modules/common-ui/components/Back/Back.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import { Link } from 'react-router-dom';

import './Back.css';

export const Back = () => {
return (
<a href="/" className="Back">
<Link to="/" className="Back">
⬅ Back to the main menu
</a>
</Link>
);
};
Binary file added src/modules/game/assets/badguyswin.mp3
Binary file not shown.
Binary file added src/modules/game/assets/card.mp3
Binary file not shown.
File renamed without changes.
4 changes: 3 additions & 1 deletion src/modules/game/components/Game/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import { useParams } from 'react-router-dom';
import { abi } from '../../../../abi/cipherbomb.json';
import { getEventContract, getReadContract, onNextBlock } from '../../../../utils/rpc';
import { Splash } from '../../../common-ui/components/Splash';
import badGuysWin from '../../assets/badguyswin.mp3';
import begin from '../../assets/begin.mp3';
import goodGuysWin from '../../assets/good_guys_win.mp3';
import goodGuysWin from '../../assets/goodguyswin.mp3';
import { Table } from '../Table';
import { WaitingRoom } from '../WaitingRoom';

Expand Down Expand Up @@ -111,6 +112,7 @@ export const Game = ({ account, provider }: GameProps) => {
};

const onBadGuysWin = (reason: string) => {
void new Audio(badGuysWin).play();
setEndGame(reason === 'bomb' ? 'bomb' : 'bad');
onNextBlock(refreshInformations);
onNextBlock(refreshPlayers);
Expand Down
4 changes: 3 additions & 1 deletion src/modules/game/components/Table/Table.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useCallback, useEffect, useState } from 'react';
import { getInstance, getTokenSignature } from '../../../../fhevmjs';
import { getEventContract, getReadContract, onNextBlock } from '../../../../utils/rpc';
import { Button, Loader, Subtitle, Title } from '../../../common-ui';
import card from '../../assets/card.mp3';
import { CardDisplay } from '../CardDisplay';
import { TablePlayers } from '../TablePlayers';

Expand Down Expand Up @@ -57,7 +58,7 @@ export const Table = ({ contract, account, players }: TableProps) => {

useEffect(() => {
const onCardPicked = (cp: number) => {
console.log('picked', cp);
void new Audio(card).play();
setCardPicked(Number(cp));
setCardOpen(true);
onNextBlock(refresh);
Expand All @@ -81,6 +82,7 @@ export const Table = ({ contract, account, players }: TableProps) => {
const instance = getInstance(account);

const onRole = async () => {
void new Audio(card).play();
if (role == null) {
const contractAddress = await contract.getAddress();
const token = await getTokenSignature(contractAddress, account);
Expand Down
3 changes: 2 additions & 1 deletion src/modules/home/components/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import classNames from 'classnames';
import { BrowserProvider, ContractFactory } from 'ethers';
import { useState } from 'react';
import { useEffect, useState } from 'react';

Check failure on line 3 in src/modules/home/components/Home/Home.tsx

View workflow job for this annotation

GitHub Actions / deploy

'useEffect' is declared but its value is never read.
import { Link, useNavigate } from 'react-router-dom';

import cipherbomb from '../../../../abi/cipherbomb.json';
Expand All @@ -16,6 +16,7 @@ type HomeProps = {
export const Home = ({ account, provider }: HomeProps) => {
const [createLoading, setCreateLoading] = useState(false);
const navigate = useNavigate();

const createARoom = async () => {
if (createLoading) return;
const contractFactory = new ContractFactory(cipherbomb.abi, cipherbomb.bytecode, await provider.getSigner());
Expand Down
13 changes: 13 additions & 0 deletions src/utils/music.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import music from '../assets/music.mp3';

const musicAudio = new Audio(music);
musicAudio.loop = true;
musicAudio.volume = 0.5;

export const playMusic = () => {
void musicAudio.play();
};

export const pauseMusic = () => {
void musicAudio.pause();
};

0 comments on commit 48ee45a

Please sign in to comment.