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

Fixed the functionality of START_GAME Event #172

Merged
merged 2 commits into from
Jun 28, 2024
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
8 changes: 8 additions & 0 deletions backend/src/controllers/eventControllers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,14 @@ export async function handleAppEvent(req: AuthRequest, res: Response) {
event,
game.players.map((player) => player.id)
);
// Emit state synchronization event
if (event.type === 'DRAW_CARD' || event.type === 'START_GAME') {
const stateSyncEvent = await makeStateSyncEvent(game);
propagateEventToClients(
stateSyncEvent,
game.players.map((player) => player.id)
);
}
}
} else {
// it is a message event etc
Expand Down
13 changes: 13 additions & 0 deletions frontend/src/clientDispatch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export const clientDispatch = (
case GameEventTypes.STATE_SYNC:
newGameState = handleStateSync(gameState, event);
break;
case GameEventTypes.START_GAME:
newGameState = handleStartGame(gameState, event);
break;

default:
throw new Error(`Unhandled event type: ${event}`);
}
Expand Down Expand Up @@ -159,3 +163,12 @@ const handleAnnounceUno = (
},
};
};

const handleStartGame = (gameState: GameState, event: GameEvent): GameState => {
if (event.type !== GameEventTypes.START_GAME) {
throw new Error(
`Invalid event type for handleStateSync: ${event.type}`
);
}
return { ...gameState, status: 'STARTED' };
};
15 changes: 13 additions & 2 deletions frontend/src/pages/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import { IoSettings } from 'react-icons/io5';
import { useNavigate } from 'react-router-dom';
import { triggerEvent } from '../channel';
import { useAuth } from '../contexts/AuthContext';
import { useToast } from '../library/toast/toast-context';

function Game() {
const { gameState } = useGameContext();
const toast = useToast();
const { getUser } = useAuth();
const navigate = useNavigate();
const [FirstUser, setFirstUser] = useState(true);
Expand Down Expand Up @@ -81,7 +83,7 @@ function Game() {
useEffect(() => {
modal.show(<GamePropertiesModal />, 'large', [], false);
// eslint-disable-next-line
}, []); // todo add the required dependencies
}, [gameState.players.length, gameState.id]); // todo add the required dependencies
Copy link
Collaborator

Choose a reason for hiding this comment

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

Including id here is necessary as previously explained. As long as the Game component is mounted, the game's id wont change.

This change will cause a UX inconvenience as previously mentioned, but we can ignore that for now.

Copy link
Author

Choose a reason for hiding this comment

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

Without this , for joining players game id is not visible on the modal , so i included it , for now, i guess, we can keep it like this and fix this in upcoming prs.

const drawCard = () => {
channel.triggerEvent({
type: GameEventTypes.DRAW_CARD,
Expand All @@ -102,8 +104,17 @@ function Game() {
}

function handleStartGame() {
if (gameState.players.length < 2) {
return toast.open({
message: {
heading: 'Error',
content: 'Not Enough players to Start Game',
},
color: 'error',
});
}
triggerEvent({
type: GameEventTypes.LEAVE_GAME,
type: GameEventTypes.START_GAME,
data: null,
});
setFirstUser(false);
Expand Down
Loading