Skip to content

Commit

Permalink
Server: Route Handler For /event
Browse files Browse the repository at this point in the history
The getAllClients function has been added in eventReceipts.ts to return an array of client IDs.

The handleEvent function has been implemented in gameControllers.js to handle event propagation. This function retrieves all client IDs and schedules the event sending to all clients.

The routing in gameRoutes.js has been set up to handle the /events endpoint, including both GET and POST handlers.

Fixes shivansh-bhatnagar18#45
  • Loading branch information
sksmagr23 authored and kuv2707 committed Jun 7, 2024
1 parent 3c7dc8c commit 29ff99f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 3 deletions.
22 changes: 21 additions & 1 deletion backend/src/controllers/gameControllers.js
Original file line number Diff line number Diff line change
@@ -1 +1,21 @@
// Implement the handler for `/events` endpoint. Refer to ARCHITECTURE.md for implementation details.
import { getAllClients, scheduleSend } from '../eventRecipients';

async function handleEvent(req, res) {
const event = req.body;

const clientIds = getAllClients();

const eligibleClientIds = filterEligibleClients(clientIds);

eligibleClientIds.forEach((clientId) => {
scheduleSend(clientId, event);
});

res.status(200).send({ message: 'Event propagated to clients.' });
}

function filterEligibleClients(clientIds) {
return clientIds;
}

export default handleEvent;
4 changes: 4 additions & 0 deletions backend/src/eventRecipients.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ export function getClient(clientId: ClientId) {
return clients.get(clientId);
}

export function getAllClients(): ClientId[] {
return Array.from(clients.keys());
}

// eslint-disable-next-line @typescript-eslint/no-unused-vars
export function scheduleSend(clientId: ClientId, event: AppEvent) {
//todo: Enqueue the event for sending.
Expand Down
7 changes: 5 additions & 2 deletions backend/src/routes/gameRoutes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import express from 'express';
import { addClient } from '../eventRecipients';
import handleEvent from '../controllers/gameControllers';

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
router.post('/events', handleEvent);

export default router;

0 comments on commit 29ff99f

Please sign in to comment.