Skip to content

Commit

Permalink
Merge branch 'shivansh-bhatnagar18:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
PrathamX595 authored Jun 1, 2024
2 parents 927e8ec + 44927c8 commit 5f89ce4
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 2 deletions.
12 changes: 11 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,16 @@ Given that you have already forked the repository and set it up locally:
- Addressing reviews on existing PRs is more important than creating new PRs. Please be responsive to the feedback and make the necessary updates.
- Create a new branch for each issue you are working on. This will help you keep your changes isolated and make it easier to manage multiple PRs. The branch should be created from upstream/master, and only after fetching the latest changes from the main branch from upstream first.

## How to make a good Pull Request
- Make sure your PR is solving only one issue. If you are solving multiple issues, create separate PRs for each issue.
- Make sure your PR is up-to-date with the main branch. If there are any conflicts, resolve them before opening the PR.
- See [code review](#code-review) section for more details on how to address review comments.
- Write a coherent pull request description linking to the issue you are solving and the approach and notable decisions you made while solving the issue.
- The Pull Request should pass all the checks before it can be merged. The checks include:
- ESLint checks (There should be no eslint errors at least in the files you have modified.)
- Prettier checks
- Unit tests

## Common Git Operations you may need to perform

During contribution, you will often need to rewrite commit history or sync your branch with the main branch.
Expand Down Expand Up @@ -157,7 +167,7 @@ This will open VSCode whenever you run a command that requires a text editor, li

All contributions go through a code review process to ensure the quality and maintainability of the codebase. During the review, maintainers may provide feedback or request changes to your code. Please be responsive to the feedback and make the necessary updates. There may be multiple rounds of review before your changes are approved.

When you open a pull request, you can request a review from the maintainers. You can also request a review after making changes in resopnse to feedback. The requested reviewer may then review the PR themselves or delegate to another maintainer.
When you open a pull request, you can request a review from the maintainers. You can also request a review after making changes in response to feedback. The requested reviewer may then review the PR themselves or delegate it to another maintainer. When requesting a review, make sure that your PR doesn't have merge conflicts. If it does, resolve the conflicts before requesting a review.

Your PR will be merged only after the maintainers approve it. Different areas of codebase are handled by different maintainers.

Expand Down
3 changes: 2 additions & 1 deletion backend/.prettierrc
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"singleQuote": true,
"trailingComma": "es5",
"tabWidth": 4,
"semi": true
"semi": true,
"endOfLine": "lf"
}
18 changes: 18 additions & 0 deletions backend/eventRecipients.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// this module is responsible for handling the clients currently connected to the server.
// It stores the clients in a Map object, where the key is the client's user_id and the value is the client's http response object.

import { Response } from 'express';

const clients = new Map<string, Response>();

export function addClient(userId: string, res: Response) {
clients.set(userId, res);
}

export function removeClient(userId: string) {
clients.delete(userId);
}

export function getClient(userId: string) {
return clients.get(userId);
}
10 changes: 10 additions & 0 deletions backend/routes/gameRoutes.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import express from 'express';
import { addClient } from '../eventRecipients';
const router = express.Router();

router.get('/events', (req, res) => {
addClient('user_id', res);
});

// the post handler should retrieve the game the user is currently in, and update the game state.
// The request body contains the event data, as described in ARCHITECTURE.md
31 changes: 31 additions & 0 deletions backend/src/types.d.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
// We declare those types which are used throughout the application here.
// For types that are used only in one file, we can declare them in that file itself.

type CardType = 'number' | 'special' | 'wild';

type CardColor = 'red' | 'blue' | 'green' | 'yellow';
Expand All @@ -18,3 +21,31 @@ type Player = {
id: string;
cards: UNOCard[];
};

type EventResult = {
type: 'SUCCESS' | 'ERROR';
message: string;
};

declare global {
type GameEngine = import('./engine').GameEngine;
}

type GameEventType = 'DRAW_CARD' | 'THROW_CARD';

type GameEvent =
| {
type: 'DRAW_CARD';
playerId: string;
data: {
card: UNOCard;
};
}
| {
type: 'THROW_CARD';
playerId: string;
data: {
card: UNOCard;
};
};
//todo: Add more events
5 changes: 5 additions & 0 deletions backend/uno-game-engine/engine.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { getShuffledCardDeck } from './deck';
import { handleEvent } from './gameEvents';

const NUM_CARDS_PER_PLAYER = 7;

Expand Down Expand Up @@ -52,4 +53,8 @@ export class GameEngine {
.find((p: Player) => p.id === player.id)
.cards.push(this.cardDeck.pop());
}
dispatchEvent(event: GameEvent) {
// handle different types of events based on event.type
handleEvent(this, event);
}
}
22 changes: 22 additions & 0 deletions backend/uno-game-engine/gameEvents.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// this module houses the handlers for various game events.

import { type GameEngine } from './engine';

type GameEventHandler = (game: GameEngine, event: GameEvent) => EventResult;

const map = new Map<GameEventType, GameEventHandler>();

export function registerEventHandler(
eventType: GameEventType,
handler: GameEventHandler
) {
map.set(eventType, handler);
}

export function handleEvent(game: GameEngine, event: GameEvent): EventResult {
const handler = map.get(event.type);
if (!handler) {
return { type: 'ERROR', message: 'Invalid event type' };
}
return handler(game, event);
}

0 comments on commit 5f89ce4

Please sign in to comment.