Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#12
The logic for inviting and joining users to the room is implemented.
Backend:
Game mongo model:
players - array of players, it has an email and status ('invited', 'joined') of the player.
status - the status of the game. Statuses are to be considered, but for now: 'new' (the game just created), 'ready-to-start' (players limit reached and host can press start), 'in-progress' (the players are playing), 'finished'
New API routes:
/game/create - creates a new Game and adds the creator (host) into the players array right away.
/game/:gameId/join - adds player to players array. If the player already was there, just updated status to 'joined'
Socket.io
BE-user-joined event - subscribes the player to the room. The room id is the actual Game id. After that emits the event FE-user-joined to the room passing player's email, so the other players in the room can be notified.
I had an issue with socket.io as follows: the users keep reconnecting after 30 seconds, so they lose the room id. Solved this by specifying ping policies: pingInterval: 5000, pingTimeout: 10000. So the client keeps pinging the server every 5 seconds to know if it is still alive. This solved the problem. More about the issue: link
Frontend
GameContext
Manages the game state. Maybe we should use it managing lobby only (like LobbyContext.js), and create a separate for the actual game. Otherwise, the file is gonna be big...
socket.js util - opens the WebSocket connection to the backend and can be used across the whole app.
GameContext has FE-user-joined event subscription. It changes the players array and pops up the notification that the player is joined.