Skip to content

Comments

Feat: Player Profile Stats & Automated Favorite Tracking#779

Open
CapoMK25 wants to merge 3 commits intodevfrom
639-feature-request-player-object-should-include-info-that-is-shown-in-the-clientside-profile-page
Open

Feat: Player Profile Stats & Automated Favorite Tracking#779
CapoMK25 wants to merge 3 commits intodevfrom
639-feature-request-player-object-should-include-info-that-is-shown-in-the-clientside-profile-page

Conversation

@CapoMK25
Copy link
Collaborator

Brief description

This PR implements the requirements for tracking player class and character statistics, along with automated "Favorite" calculation. It also aligns the Player model with updated profile requirements (Carbon Footprint, Playstyle, and Clan Coins).

The core of this change moves the Service layer from returning raw Mongoose documents to sanitized plain objects. This ensures that calculated virtual fields (like favouriteClass) are stable and not stripped during a "DTO transformation".

Change list

Database & Schema (Player.schema.ts):

  • Added classStatistics and characterStatistics as Mongoose Maps to track gamesPlayed and wins.
  • Added carbonFootprint (Number), playstyle (String), and clanCoinsAccumulated (Number) fields.
  • Updated @Schema decorators (NestJS stuff) to ensure virtuals and getters are included during transformation.

Data Transfer Objects (Player.dto.ts, CreatePlayer.dto.ts):

  • Whitelisted statistics maps in CreatePlayerDto to prevent ValidationPipe from stripping incoming data.
  • Added favouriteClass and favouriteCharacter fields to PlayerDto to expose calculated results to the client.

Service Logic (Player.service.ts)

  • Implemented getFavourite() helper using a "Highest Games Played" algorithm.

  • Refactored getPlayerById to convert documents to plain objects via .toObject() before injecting.

  • Ensured data integrity by validating map existence before calculation to prevent undefined errors.

Test Suite Alignment (src/__tests__/...)

  • Updated Contract: Modified existing tests (notably getPlayerById.test.ts) to expect plain JavaScript objects instead of Mongoose documents.

  • Removed .toObject() calls: Since the Service now returns a plain object, internal test calls to .toObject() were removed to prevent TypeErrors.

  • Drift Protection: Adjusted timestamp expectations to handle the millisecond drift between local execution and database persistence.

Why the Tests were Updated

The tests were updated because the "Service Contract" itself had to change. To support the dynamic calculation of favorites, the service now performs the transformation to a plain object internally. This makes the API more robust for the frontend but required the test suite to stop treating the returned data as a Mongoose Document instance.

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

Labels

None yet

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Feature request: Player object should include info that is shown in the clientside profile page.

1 participant