From ec1fea47344c84dd5856e0513549b179d5969c18 Mon Sep 17 00:00:00 2001 From: PrathamX595 Date: Sun, 2 Jun 2024 12:59:29 +0530 Subject: [PATCH] Frontend: added error page an error page was added which will be rendered whenever an unknown route will be accessed Fixes: #21 --- backend/{uno-game-engine => src}/types.d.ts | 5 +++++ .../uno-game-engine/{engine.js => engine.ts} | 18 ++++++++++++++---- frontend/src/pages/Error.tsx | 15 +++++++++++---- package-lock.json | 6 ++++++ 4 files changed, 36 insertions(+), 8 deletions(-) rename backend/{uno-game-engine => src}/types.d.ts (87%) rename backend/uno-game-engine/{engine.js => engine.ts} (74%) create mode 100644 package-lock.json diff --git a/backend/uno-game-engine/types.d.ts b/backend/src/types.d.ts similarity index 87% rename from backend/uno-game-engine/types.d.ts rename to backend/src/types.d.ts index 7c281f2..832d640 100644 --- a/backend/uno-game-engine/types.d.ts +++ b/backend/src/types.d.ts @@ -13,3 +13,8 @@ type UNOCard = { value: CardValue; id: undefined; }; + +type Player = { + id: string; + cards: UNOCard[]; +}; diff --git a/backend/uno-game-engine/engine.js b/backend/uno-game-engine/engine.ts similarity index 74% rename from backend/uno-game-engine/engine.js rename to backend/uno-game-engine/engine.ts index cac5e3a..2af035d 100644 --- a/backend/uno-game-engine/engine.js +++ b/backend/uno-game-engine/engine.ts @@ -1,7 +1,17 @@ import { getShuffledCardDeck } from './deck'; const NUM_CARDS_PER_PLAYER = 7; + export class GameEngine { + cardDeck: UNOCard[]; + thrownCards: UNOCard[]; + players: Player[]; + currentPlayerIndex: number; + lastThrownCard: UNOCard | null; + currentColor: number; + direction: number; + status: 'READY' | 'STARTED'; + constructor() { this.cardDeck = getShuffledCardDeck(); this.thrownCards = []; @@ -18,12 +28,12 @@ export class GameEngine { throw new Error('Not enough cards to distribute'); } - this.players = this.players.map((player) => { + this.players = this.players.map((player: Player) => { player.cards = this.cardDeck.splice(0, NUM_CARDS_PER_PLAYER); return player; }); } - addPlayer(player) { + addPlayer(player: Player) { this.players.push(player); } startGame() { @@ -36,10 +46,10 @@ export class GameEngine { this.currentPlayerIndex = (this.currentPlayerIndex + this.direction) % this.players.length; } - drawCardFromDeck(player) { + drawCardFromDeck(player: Player) { //todo: Handle the case when the deck is empty and we have to move the thrown cards back to the deck this.players - .find((p) => p.id === player.id) + .find((p: Player) => p.id === player.id) .cards.push(this.cardDeck.pop()); } } diff --git a/frontend/src/pages/Error.tsx b/frontend/src/pages/Error.tsx index 7a93fd7..607e46e 100644 --- a/frontend/src/pages/Error.tsx +++ b/frontend/src/pages/Error.tsx @@ -2,10 +2,17 @@ import { Link } from 'react-router-dom'; function Error() { return ( -
- This page does not exist! - Go back to homepage -
+ <> +
+
+

404

+
+
Oops! Looks like there's an error
+
+ Back to Home +
+
+ ); } diff --git a/package-lock.json b/package-lock.json new file mode 100644 index 0000000..f887bf7 --- /dev/null +++ b/package-lock.json @@ -0,0 +1,6 @@ +{ + "name": "multiplayer-uno", + "lockfileVersion": 3, + "requires": true, + "packages": {} +}