Skip to content

Commit

Permalink
Basic multiplayer
Browse files Browse the repository at this point in the history
  • Loading branch information
ProLoser committed Aug 31, 2024
1 parent f17ed50 commit 585c052
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/Friends/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Import FirebaseAuth and firebase.
import { useState, useCallback, useRef, useContext, ReactNode } from 'react';
import { useState, useCallback, useRef, useContext, ReactNode, useEffect } from 'react';
import type { ChangeEventHandler } from 'react';
import * as firebaseui from 'firebaseui';
import StyledFirebaseAuth from '../StyledFirebaseAuth';
Expand Down Expand Up @@ -57,6 +57,10 @@ export default function Friends({ show }: FriendsProps) {
setSearchResults(results)
}, [authUserSnapshot]);

useEffect(() => {
if (!show) setSearchResults([])
}, [show])

if (!show) return null;

const renderMatches: ReactNode[] = []
Expand Down
27 changes: 23 additions & 4 deletions src/Game/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import './Game.css';
import Dice from './Dice';
import Point from './Point';
import Piece from './Piece';
import Toolbar from '../Toolbar';
import { useCallback, useState, type DragEventHandler } from 'react';
import Toolbar from '../Toolbar'
import { useCallback, useContext, useEffect, useState, type DragEventHandler } from 'react';
import { GameContext } from '../MultiplayerContext';
import { AuthContext } from '../AuthContext';


// White = Positive, Black = Negative
Expand All @@ -24,6 +26,16 @@ export default function Game() {
const [board, setBoard] = useState(() => [...DEFAULT_BOARD])
const [dice, setDice] = useState(() => [rollDie(), rollDie()])
const [selected, setSelected] = useState<number|null>(null);
const game = useContext(GameContext);
const authUserSnapshot = useContext(AuthContext);

useEffect(() => {
if (game) {
setBoard(game.val().state)
} else {
setBoard([...DEFAULT_BOARD])
}
}, [game])

const onSelect = useCallback((position:number) => {
if (position === null || selected === position) {
Expand Down Expand Up @@ -83,8 +95,15 @@ export default function Game() {
nextBoard[from] -= Math.sign(offense)
}

setBoard([...nextBoard]);
}, [board])
setBoard(nextBoard);
if (game?.key) {
game.ref.child('moves').push({
player: authUserSnapshot?.val().uid,
move: `${from}/${to}`
})
game.ref.update({ state: nextBoard })
}
}, [board, game, authUserSnapshot])

const roll = useCallback(() => {
navigator.vibrate?.([50,50,60,30,90,20,110,10,150])
Expand Down
2 changes: 1 addition & 1 deletion src/MultiplayerContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ export const MultiplayerProvider = ({ children }: PropsWithChildren) => {
setMatch(await matchSnapshot.val())
}
},
}), [matches, authUser]);
}), [matches, authUser, users]);

// Synchronize Matches
useEffect(() => {
Expand Down

0 comments on commit 585c052

Please sign in to comment.