Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring api/controllers/game/draw.js to extract the game logic to helper methods #16

Open
wants to merge 19 commits into
base: main
Choose a base branch
from

Conversation

Hakaabi
Copy link

@Hakaabi Hakaabi commented Dec 10, 2024

Issue number

Relevant issue number

Please check the following

  • Do the tests still pass? (see Run the Tests)
  • Is the code formatted properly? (see Linting (Formatting))
  • For New Features:
    • Have tests been added to cover any new features or fixes?
    • Has the documentation been updated accordingly?

Please describe additional details for testing this change


Description:
with this pull request I aimed to introduce significant enhancement and refactoring to the backend game logic for better modularity, reusability, and maintainability according to the description written in the original issue cuttle-cards#505 in the of the original repository (cuttle-cards#505). Additionally, I have implemented unit tests h to validate the functionality of the refactored helper methods, ensuring style consistency with the existing test units.

Modification made to the files:

-Code Refactoring:
I extracted the omplex game logic from api/controllers/game/draw.js into and turned them into helper methods under the api/helpers/game-logic/ folder.
In this folder I created modular helper functions:

  1. validateDrawConditions.js, which handles validation logic for drawing a card.
  2. updateGameStateAfterDraw.js, which updates game state after a card is drawn.
  3. publishGameState.j, which publishes game state changes to connected clients.
  4. errorHandling.js, which manages error responses and logging.
    the was done with the aim of Improving readability, reusability, and for separation of concerns across the backend logic.

Lastly, Unit Testing:
I wrote a test suite located in tests/unit/specs/gameLogicHelpers.spec.js to validate each helper method:
validateDrawConditions, to ensure proper validation for various game scenarios.
updateGameStateAfterDraw, this confirms accurate updates to the game state.
publishGameState, validates correct broadcasting of game state changes.
errorHandling, Tests appropriate error response handling.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor Backend Endpoints to Use Helper Functions
1 participant