Skip to content

Game data#73

Merged
jmtth merged 33 commits intocodastream:mainfrom
jmtth:game-data
Feb 24, 2026
Merged

Game data#73
jmtth merged 33 commits intocodastream:mainfrom
jmtth:game-data

Conversation

@jmtth
Copy link
Collaborator

@jmtth jmtth commented Feb 13, 2026

PR Draft sur la gestion des donnees du Game

Je fais une PR Draft pour vous tenir au courant de l'avancee de la base de donnee du Game

Important

Cette PR concerne :

  • lien frontend backend (creation des routes , controller et requetes sql)

Schema de la base dans game-service

rappel nous sommes en design pattern Database per service + Data replication

erDiagram

    TOURNAMENT {
        INTEGER id PK
        INTEGER creator_id
        TEXT status
        INTEGER created_at
    }

    MATCH {
        INTEGER id PK
        INTEGER tournament_id
        TEXT  sessionId
        INTEGER player1
        INTEGER player2
        INTEGER score_player1
        INTEGER score_player2
        INTEGER winner_id
        TEXT round
        INTEGER created_at
    }

    TOURNAMENT_PLAYER {
        INTEGER tournament_id PK
        INTEGER player_id PK
        INTEGER final_position
    }
    
    PLAYER {
        INTEGER id PK
        TEXT username
        TEXT avatar
        INTEGER updated_at
    }

 
    TOURNAMENT ||--o{ MATCH : contains
    TOURNAMENT ||--o{ TOURNAMENT_PLAYER : has_players
    TOURNAMENT }o--|| PLAYER : is
    MATCH }o--|| PLAYER : is
    TOURNAMENT_PLAYER }o--|| PLAYER : is 

Loading

Navigation dans le Tournoi

flowchart TD

%% Entry
A[Home / TournamentMenuPage.tsx]

A -->|Participate| B[TournamentsListPage.tsx]
A -->|Create| C[TournamentCreatePage.tsx]

C -->|POST /game/create-tournament| D[(Create tournament in DB)]
D -->|return tournamentId| E[TournamentPage.tsx]

B -->|Join| E

%% Tournament structure
E --> F{4 players ready?}

F -->|Yes| SF1[Semi-Final 1]
F -->|Yes| SF2[Semi-Final 2]

%% Semi Final 1
SF1 --> G1{Both players Start?}
G1 -->|Yes| M1[Play Match 1]
M1 -->|Save score in game.db| R1[Determine Winner/Loser 1]

%% Semi Final 2
SF2 --> G2{Both players Start?}
G2 -->|Yes| M2[Play Match 2]
M2 -->|Save score in game.db| R2[Determine Winner/Loser 2]

%% Propagation
R1 -->|Winner → Final P1| FP1[Final Slot P1]
R1 -->|Loser → Small Final P1| SP1[Small Final Slot P1]

R2 -->|Winner → Final P2| FP2[Final Slot P2]
R2 -->|Loser → Small Final P2| SP2[Small Final Slot P2]

%% Final
FP1 --> FINAL{Both players Start?}
FP2 --> FINAL

FINAL -->|Yes| FM[Play Final]
FM -->|Save score in game.db| FW[Determine Champion]

%% Small Final
SP1 --> SMALL{Both players Start?}
SP2 --> SMALL

SMALL -->|Yes| SM[Play Small Final]
SM -->|Save score in game.db| SW[Determine 3rd place]

%% Tournament completion
FW --> UPDATE[Update Tournament_player positions]
SW --> UPDATE

UPDATE --> END[Tournament Completed]

Loading

Services Game

Ajout du plugin custom Authplugin

  • permet de récupérer les informations du user

Ajout du plugin ioredis

permet decouplage des services et de ne pas faire une requette http pour avoir le username de chaque user

  • permet de récupérer le username et de mettre à jour la table player.
  • ajout d'un message dans le stream user-events dans le service user au moment de la création du profile.
await req.server.redis.xadd(
      'user.events',
      '*',
      'data',
      JSON.stringify({
        type: UserEventType.CREATED,
        id: profile.id,
        username: profile.username,
        timestamp: Date.now(),
      }),
    );
  • ajout d'un consumer dans le service game
await redis.xgroup('CREATE', STREAM, GROUP, '0', 'MKSTREAM');
const streams = await redis.xreadgroup(
        'GROUP',
        GROUP,
        CONSUMER,
        'BLOCK',
        5000,
        'COUNT',
        1,
        'STREAMS',
        STREAM,
        '>',
      );
const [[, messages]] = streams;
      for (const [id, fields] of messages) {
        await processMessage(app, redis, id, fields);
      }
  • il faut penser à ajouter ce consumer au hook Onready de Fastify.
fastify.addHook('onReady', async () => {
  startGameConsumer(fastify);
});

Modifications de certains composants

  • Input : ajout toggle button pour afficher masquer le password.

Routes Front(React) - Back(Fastify) du tournoi

info Front Back base de donnees
menu du tournoi /tournaments
liste des tournois /tournaments/list GET api/game/tournaments listTournament()
création d'un tournoi /tournaments/create POST api/game/create-tournament createTrounement()
un tournoi en particulier /tournaments/:id GET api/game/tournaments/:id showTournament()
rejoindre un tournoi /tournaments/:id Post api/game/tournaments/:id joinTournament()

Caution

Changement route dans Game

  • /:sessionId -> /ws/:sessionId

BREAKING_CHANGES: add three table in game.db match, tournament,
tournament_player
add request for adding and udating database
next: add route and controller to interact with database

package-lock.json
srcs/.env.game.example
srcs/game/package.json
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/game/src/routes/game.routes.ts
srcs/game/src/types/game.dto.ts
newTournament: create a Tournament and send his id to the frontend
FastifyJWT: new interface for prehandler request
update(front): fakeCreateTournament -> CreatingTournament
Input: addc toggle button to sho/hide the password

package-lock.json
srcs/.env.game.example
srcs/game/package.json
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/game/src/pulgins/auth.plugin.ts
srcs/game/src/routes/game.routes.ts
srcs/game/src/server.ts
srcs/game/src/types/fastify.d.ts
srcs/nginx/src/components/atoms/Input.tsx
srcs/nginx/src/pages/TournamentCreatePage.tsx
feat(game-database): add new table player to have username
add sql request for listing tournaments

srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/users/src/app.ts
srcs/users/src/config/env.ts
srcs/users/src/plugins/ioredis.plugins.ts
jhervoch and others added 12 commits February 17, 2026 16:06


add to type in user.schema.ts
feat(game-redis): add ioredis plugins, consumer
fastify hook onReady StartGameConsumer
feat(game-db): add sql request for upsert user and deleted
feat(user-redis): add data to stream user.events in controller
feat(game): add envalid to config env

package-lock.json
srcs/docker-compose.yml
srcs/game/package.json
srcs/game/src/config/env.ts
srcs/game/src/core/game.consumer.ts
srcs/game/src/core/game.database.ts
srcs/game/src/plugins/auth.plugin.ts
srcs/game/src/plugins/ioredis.plugin.ts
srcs/game/src/server.ts
srcs/game/src/types/fastify.d.ts
srcs/game/tsconfig.json
srcs/shared/core/src/index.ts
srcs/shared/core/src/schemas/user.schema.ts
srcs/users/src/app.ts
srcs/users/src/controllers/profiles.controller.ts
srcs/users/src/types/fastify.d.ts
srcs/users/tsconfig.json
feat(game-tournament): add Tournament List page
add sql request for list tournament
fix(game): root route :sessionId intercept all get routes
change to ws/:sessionId

package-lock.json
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/game/src/routes/game.routes.ts
srcs/gateway/src/controllers/game.controller.ts
srcs/nginx/src/pages/TournamentsListPage.tsx
srcs/shared/core/src/index.ts
srcs/shared/core/src/schemas/game.schema.ts
feat(user-events): add event bus insteaed of calling redis.xadd directly
in controller
fix(gane-data): add player(creator) when creating a tournament

package-lock.json
srcs/game/src/core/game.consumer.ts
srcs/game/src/core/game.database.ts
srcs/shared/core/src/index.ts
srcs/shared/core/src/schemas/user.schema.ts
srcs/users/src/app.ts
srcs/users/src/controllers/profiles.controller.ts
srcs/users/src/events/redis.subscriber.ts
srcs/users/src/events/user.bus.ts
srcs/users/test/profiles.controller.test.ts
srcs/users/vite.config.mjs
srcs/users/src/plugins/ioredis.plugins.ts
srcs/blockchain/src/plugins/fastify-ioredis.ts
srcs/game/src/plugins/ioredis.plugin.ts
srcs/users/src/plugins/ioredis.plugins.ts
fix function to join a Tournament in backend

srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/game/src/routes/game.routes.ts
srcs/nginx/src/components/atoms/TournamentList.tsx
srcs/nginx/src/locales/en/common.json
srcs/nginx/src/locales/fr/common.json
srcs/nginx/src/locales/tf/common.json
srcs/nginx/src/pages/TournamentsListPage.tsx
srcs/users/src/plugins/ioredis.plugins.ts
feat(game-data-test): add python file for testing tournament managing

package-lock.json
srcs/game/src/controllers/game.controller.ts
srcs/tests/test_tournament.py
srcs/tests/test.py
srcs/tests/test_tournament.py
replace mock data by fetch daat from backend to load players in a
tournament
update event bus in user service to send avatarulr to game service to
avoid fetching in front end
update DTO in transcendence core

package-lock.json
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/game/src/routes/game.routes.ts
srcs/nginx/src/App.tsx
srcs/nginx/src/pages/TournamentPage.tsx
srcs/shared/core/src/index.ts
srcs/shared/core/src/schemas/game.schema.ts
srcs/shared/core/src/schemas/user.schema.ts
srcs/users/src/events/redis.subscriber.ts
@jmtth jmtth self-assigned this Feb 22, 2026
jmtth and others added 9 commits February 22, 2026 19:47
add avatar url in db game to avoid to o much fecth in client side
add event to update avatar url in redis when user change it

package-lock.json
srcs/game/src/core/game.database.ts
srcs/users/src/controllers/profiles.controller.ts
srcs/users/src/events/redis.subscriber.ts
update(game-front): Tournament page has only one StartButton
feat(front-data): add event user update to have avatarurl in game database
update(font-game): load avatar in playercapsule
update(locales): update translation files
fix(game-data): don't return error when joining a tournament you have
alredy joined

docs/redis.md
package-lock.json
srcs/game/src/core/game.database.ts
srcs/nginx/src/components/atoms/CircleButtonSimple.tsx
srcs/nginx/src/components/atoms/MatchNode.tsx
srcs/nginx/src/components/atoms/StartButton.tsx
srcs/nginx/src/components/molecules/TournamentBracket.tsx
srcs/nginx/src/locales/en/common.json
srcs/nginx/src/locales/fr/common.json
srcs/nginx/src/locales/tf/common.json
srcs/nginx/src/pages/TournamentMenuPage.tsx
srcs/nginx/src/pages/TournamentPage.tsx
srcs/nginx/src/pages/TournamentsListPage.tsx
srcs/users/src/controllers/profiles.controller.ts
srcs/users/src/events/redis.subscriber.ts
srcs/users/src/services/profiles.service.ts
srcs/users/src/utils/mappers.ts
srcs/users/test/profiles.controller.test.ts
srcs/users/test/profiles.service.test.ts
srcs/nginx/src/components/atoms/MatchNode.tsx
srcs/nginx/src/components/molecules/TournamentBracket.tsx
srcs/nginx/src/pages/TournamentCreatePage.tsx
srcs/nginx/src/pages/TournamentPage.tsx
srcs/nginx/src/pages/TournamentsListPage.tsx
.github/workflows/ci.yml
cookies.txt
srcs/game/src/controllers/game.controller.ts
srcs/tests/test_tournament.py
@jmtth jmtth marked this pull request as ready for review February 23, 2026 14:17
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR implements tournament data management for the game service using a "Database per service + Data replication" pattern. The main goal is to establish backend-frontend communication through REST API routes and integrate Redis streams for inter-service communication.

Changes:

  • Added SQLite database schema for tournaments, matches, players, and tournament_players
  • Implemented Redis streams consumer in game service to sync user data from user service
  • Created REST API routes for tournament CRUD operations with authentication
  • Integrated frontend React components to consume tournament APIs with polling
  • Added authentication plugin to game service using JWT
  • Added functional tests for tournament creation and joining

Reviewed changes

Copilot reviewed 49 out of 53 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
srcs/game/src/core/game.database.ts SQLite database operations for tournaments and players
srcs/game/src/core/game.consumer.ts Redis streams consumer for user event synchronization
srcs/game/src/plugins/auth.plugin.ts JWT authentication plugin for game service
srcs/game/src/plugins/ioredis.plugin.ts Redis client plugin with graceful shutdown
srcs/game/src/controllers/game.controller.ts Tournament REST API endpoints
srcs/game/src/routes/game.routes.ts Route definitions with authentication middleware
srcs/users/src/events/redis.subscriber.ts User event publisher to Redis streams
srcs/users/src/plugins/ioredis.plugins.ts Redis plugin for user service
srcs/nginx/src/pages/TournamentsListPage.tsx Tournament list UI with polling
srcs/nginx/src/pages/TournamentPage.tsx Tournament detail page with real-time updates
srcs/tests/test_tournament.py Functional tests for tournament endpoints
cookies.txt Security risk: Contains JWT token that should not be committed
docs/redis.md Documentation for Redis streams testing commands

jhervoch added 2 commits February 23, 2026 16:17
package-lock.json.orig
cookies.txt
Copy link
Collaborator

@rom98759 rom98759 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tres bien, petit fix a faire et c'est parfait

jmtth and others added 7 commits February 24, 2026 11:13
package-lock.json
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.database.ts
srcs/nginx/package.json
srcs/nginx/src/pages/TournamentPage.tsx
srcs/nginx/src/pages/TournamentsListPage.tsx
srcs/shared/core/src/errors/error-catalog.ts
srcs/shared/core/src/errors/error-types.ts
srcs/shared/core/src/logging/logging.ts
BREAKING CHANGES  Apperror db.transaction to avoid race condition

package-lock.json
srcs/.env.game.example
srcs/game/src/controllers/game.controller.ts
srcs/game/src/core/game.consumer.ts
srcs/game/src/core/game.database.ts
srcs/game/src/plugins/auth.plugin.ts
srcs/game/src/routes/game.routes.ts
srcs/game/src/server.ts
srcs/game/src/types/fastify.d.ts
srcs/shared/core/src/errors/error-catalog.ts
srcs/tests/test_tournament.py
fix(env): no need jwt env anymore

srcs/game/src/config/env.ts
srcs/game/src/controllers/game.controller.ts
srcs/game/src/controllers/game.controller.ts
srcs/nginx/src/router/TournamentGuard.tsx
srcs/nginx/src/router/TournamentRoutes.tsx
Copy link
Collaborator

@rom98759 rom98759 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Parfait

@jmtth jmtth merged commit 8413406 into codastream:main Feb 24, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants