Refactor game-player relationship to initialize p0 and p1 at game creation #4
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.
Issue number #11
Relevant issue number
Please check the following
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
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.
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.
Passed player0Id and player1Id when calling the createGame function.
Ensured proper error handling for scenarios where the gameName is missing.
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.