Skip to content

Commit

Permalink
Making the uno button functioning
Browse files Browse the repository at this point in the history
Set uno button to trigger Announce_Uno Event

 Also added a handler for this event

Fixes : #159
  • Loading branch information
Abhishek committed Jun 25, 2024
1 parent 015214c commit 75992ee
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
36 changes: 35 additions & 1 deletion frontend/src/clientDispatch.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { GameEvent, GameEventTypes } from '../../backend/src/types';
import { APIPlayer, GameEvent, GameEventTypes } from '../../backend/src/types';
import { GameState } from './contexts/GameContext';
import assert from 'assert';

Expand All @@ -21,6 +21,9 @@ export const clientDispatch = (
case GameEventTypes.THROW_CARD:
newGameState = handleThrowCard(gameState, event);
break;
case GameEventTypes.ANNOUNCE_UNO:
newGameState = handleAnnounceUno(gameState, event);
break;
case GameEventTypes.STATE_SYNC:
newGameState = handleStateSync(gameState, event);
break;
Expand Down Expand Up @@ -125,3 +128,34 @@ const handleStateSync = (gameState: GameState, event: GameEvent): GameState => {
runningEvents: data.runningEvents,
};
};

const handleAnnounceUno = (
gameState: GameState,
event: GameEvent
): GameState => {
if (event.type !== GameEventTypes.ANNOUNCE_UNO) {
throw new Error(
`Invalid event type for handleStateSync: ${event.type}`
);
}
const { playerId } = event;
const player: APIPlayer = gameState.players.find(
(p) => p.id == playerId
) as APIPlayer;
const gamePlayer = {
id: player.id,
cards: player.cards.map((cardId) => {
const card = gameState.cardDeck.find((c) => c.id === cardId);
if (!card)
throw new Error(`Card with id ${cardId} not found in deck`);
return card;
}),
};
return {
...gameState,
runningEvents: {
hasAnnouncedUNO: gamePlayer,
vulnerableToUNO: null,
},
};
};
8 changes: 8 additions & 0 deletions frontend/src/pages/Game.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Button from '../library/button';
import { useModal } from '../library/modal/ModalContext';
import CopyButton from '../library/copyButton';
import Chatbox from '../library/chatbox/Chatbox';
import * as channel from '../channel';
import { GameEventTypes } from '../../../backend/src/types';

function Game() {
const { gameState } = useGameContext();
Expand All @@ -14,6 +16,11 @@ function Game() {
}
// eslint-disable-next-line
}, [gameState?.players.length, gameState.id]);
const announceGame = () => {
channel.triggerEvent({
type: GameEventTypes.ANNOUNCE_UNO,
});
};

function GamePropertiesModal() {
return (
Expand Down Expand Up @@ -133,6 +140,7 @@ function Game() {
className="border-2 active:bg-red-600 mt-4 hover:bg-purple-900"
backgroundColor="bg-purple-800"
buttonSize="w-18 h-10"
onClick={announceGame}
>
UNO
</Button>
Expand Down

0 comments on commit 75992ee

Please sign in to comment.