forked from shivansh-bhatnagar18/multiplayer-uno
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend: Complete the event propagation mechanism.
- A client can send a polling get request at game/events. If there are events for the client, they are flushed immediately. - A client can post a game event at game/events. The active game of the client is fetched, and the event is dispatched on the game. The change is then enqueued to be sent to all the current players' clients.
- Loading branch information
Showing
6 changed files
with
66 additions
and
31 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import { Response } from 'express'; | ||
import { enqueueForSend } from '../eventRecipients'; | ||
import { AuthRequest } from '../middlewares/authMiddleware'; | ||
import { retrieveGame } from '../gameStore'; | ||
|
||
export async function handleGameEvent(req: AuthRequest, res: Response) { | ||
const event = req.body; | ||
const activeGameId = req.user.activeGameId; | ||
if (!activeGameId) { | ||
res.status(404).send({ | ||
message: 'User is not actively playing any game', | ||
}); | ||
return; | ||
} | ||
const game = retrieveGame(activeGameId); | ||
if (!game) { | ||
res.status(404).send({ message: 'Game not found' }); | ||
return; | ||
} | ||
//todo: When game data is retrieved from database, it is not an instance of GameEngine | ||
// so we would need to convert it to an instance of GameEngine | ||
const result = game.dispatchEvent(event); | ||
if (result.type === 'ERROR') { | ||
res.status(400).send({ message: result.message }); | ||
return; | ||
} else { | ||
// the game state after a successful event is propagated to all clients | ||
// we can choose to relay the event received, so that the clients apply the event | ||
// to their local game state, but that would be an extra implementation burden. | ||
// Instead, we can just send the new game state to the clients. | ||
// todo: send updated game state rather than event | ||
for (const player of game.players) { | ||
enqueueForSend(player.id, event); | ||
} | ||
res.status(200).send({ message: 'Event propagated to clients.' }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,18 @@ | ||
import express from 'express'; | ||
import { addClient } from '../eventRecipients'; | ||
import handleEvent from '../controllers/gameControllers'; | ||
import { handleGameEvent } from '../controllers/gameControllers'; | ||
import { AuthRequest, verifyToken } from '../middlewares/authMiddleware'; | ||
|
||
const router = express.Router(); | ||
router.use(verifyToken); | ||
|
||
router.get('/events', (req, res) => { | ||
addClient('user_id', res); | ||
router.get('/events', function (req: AuthRequest, res) { | ||
const clientId = req.user.id as string; | ||
// note: we might need to use a different client id if the user is allowed to have multiple clients | ||
// ie, the user is allowed to play multiple games on multiple devices at the same time | ||
addClient(clientId, res); | ||
}); | ||
|
||
router.post('/events', handleEvent); | ||
router.post('/events', handleGameEvent); | ||
|
||
export default router; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters