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

Refactor game-player relationship to initialize p0 and p1 at game creation #4

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

Conversation

rmmahmou
Copy link
Collaborator

@rmmahmou rmmahmou commented Dec 10, 2024

Issue number #11

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

This pull request aims to resolve issue #11
Overview
This pull request addresses the first section of the stretch task, focusing on refactoring the relationship between games and players. Specifically, it ensures that the p0 and p1 fields are set when the game starts, rather than when it finishes. This refactor eliminates reliance on a foreign key from users to games and introduces a more explicit structure for player management.

Changes Implemented

  1. Updated the Game Schema:
    Added p0 and p1 fields to explicitly store the IDs of the two players participating in a game.
    Introduced a customToJSON method to dynamically generate the players array based on p0 and p1 for compatibility and cleaner data representation.
Screen Shot 2024-12-10 at 1 32 17 PM
  1. Refactored the createGame Function:
    Modified the createGame function in customGameHook.js to accept player0Id and player1Id as parameters.
    Ensured that these IDs are set to p0 and p1 when a game is created.
Screen Shot 2024-12-10 at 1 33 30 PM
  1. Updated the create.js Controller:
    Passed player0Id and player1Id when calling the createGame function.
    Ensured proper error handling for scenarios where the gameName is missing.
Screen Shot 2024-12-10 at 1 34 35 PM

Testing and Validation
Verified that:
p0 and p1 fields are set correctly during game creation.
The players virtual array is dynamically populated and included in API responses.
Existing functionality remains unaffected by these changes.

Impact
This refactor:

Simplifies and clarifies the relationship between games and players.
Aligns the backend data model with the requirements of [Issue cuttle-cards#505], preparing for future enhancements.
Improves maintainability by eliminating indirect relationships and replacing them with explicit player references.

@rmmahmou rmmahmou self-assigned this Dec 10, 2024
@rmmahmou rmmahmou added enhancement New feature or request Backend labels Dec 10, 2024
@rmmahmou rmmahmou linked an issue Dec 10, 2024 that may be closed by this pull request
@Hakaabi Hakaabi added this to the Sprint 5 - Stretch Task milestone Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor Game-Player Relationship
2 participants