GitHub Documentation

Help understanding the command pattern



  • Hi,

    Firstly, thanks for a great framework. I've been developing a multiplayer quiz using websockets in my spare time, and recently discovered Colyseus. I have decided to re-build my app using it as it ticks all the boxes and allows me to write less boilerplate code :)

    I have a couple of questions regarding the command pattern, and how to structure my application.

    Here's a link to the repo for reference: https://github.com/palmerhinchliffe/quickquiz-api-2/tree/develop

    Essentially it's a multiplayer quiz application that lets clients join a room and invite friends to compete in a quiz. The quiz questions are pulled from the Open Trivia DB API.

    I have been trying to implement the command pattern as per the best practices in the documentation, but I am struggling to understand how I can further modularize my code.

    I only have 1 room - the Quiz room. When the room receives a message from the client (e.g., a client joins, or answers a question, or initiates the start of the quiz, etc.) I dispatch the relevant command which in turn manipulates the player/quiz/room state, rather than the QuizRoom itself handling that logic.

    However, I can see that my QuizRoom class is getting bigger and bigger, and the documentation suggests that "...rooms should have as little code as possible..."

    The class is no-where near finished in terms of the messages it needs to receive and broadcast. Currently the client can join a room, set their name, set their 'ready status', and send messages to the room chat, but there are loads of other things the client and server need to communicate, such as the client notifying the server when a question has been answered (and the server responding with correct/incorrect), or the server notifying the client of the results of the quiz, or how many questions other players have answered correctly, etc. etc.

    Am I currently implementing the command pattern efficiently, and do I have to accept that my Quiz Room will have to hold a considerable amount of logic? In other words, do I have to declare all my this.onMessage() callbacks in this class itself, or is there a good way of modularizing this so the class doesn't contain 10's, or 100's of this.onMessage for every event.

    I am somewhat new to Node.js, and back-end programming generally, so please forgive me if I have made a giant conceptual misunderstanding with the way I have structured the project

    I appreciate my question may relate to design patterns generally, rather than Colyseus specifically, but I thought I would ask here anyway :)

    Thanks!


 

© 2021 Lucid Sight, Inc