How nikolaiunknown/tictactoe-server works
Purpose and Runtime
Spring Boot 3.5 / Java 21 backend for authenticated, real-time multiplayer Tic-Tac-Toe. It exposes REST for identity, game/history/profile operations and STOMP WebSocket for live play. Application bootstrap is src/main/java/com/tictactoe/server/ServerApplication.java; runtime settings, including game disconnect/rating parameters and JWT lifetime, are in src/main/resources/application.yaml.
Transport Entry Points
REST requests enter through src/main/java/com/tictactoe/server/controllers/AuthController.java, src/main/java/com/tictactoe/server/controllers/GameController.java, and src/main/java/com/tictactoe/server/controllers/PlayerController.java. Live client moves enter the WebSocket controller/handler pair at src/main/java/com/tictactoe/server/controllers/GameHandler.java and src/main/java/com/tictactoe/server/config/WebSocketConfig.java, using the documented /ws/connect endpoint and /app/game/move mapping. Separate global handlers normalize REST and WebSocket failures.
Authentication and Authorization Boundary
Spring Security protects HTTP and messaging traffic. JWT creation/validation is centered on src/main/java/com/tictactoe/server/security/JwtCore.java; HTTP bearer-token processing uses src/main/java/com/tictactoe/server/security/JwtTokenFilter.java, while STOMP connection/message authentication uses src/main/java/com/tictactoe/server/security/AuthenticationChannelInterceptor.java. Access and persisted refresh-token lifecycle flow through src/main/java/com/tictactoe/server/services/RefreshTokenService.java and src/main/java/com/tictactoe/server/models/RefreshToken.java.
Persistent Domain and Service Layer
MySQL-backed JPA entities represent players, games, and refresh tokens in src/main/java/com/tictactoe/server/models/Player.java, src/main/java/com/tictactoe/server/models/Game.java, and src/main/java/com/tictactoe/server/models/RefreshToken.java. Repositories isolate persistence; service implementations own business workflows: game creation/cancellation and history, player profiles/statistics/ELO, and token management. DTO/mapping boundaries prevent entities from being direct API contracts, notably src/main/java/com/tictactoe/server/mappers/GameMapper.java and src/main/java/com/tictactoe/server/mappers/PlayerMapper.java.
Live Game State and Move Processing
The in-memory gameplay/session layer is distinct from persisted game records. src/main/java/com/tictactoe/server/core/GameCore.java holds board/rule logic, src/main/java/com/tictactoe/server/core/GameSession.java represents an active match, and src/main/java/com/tictactoe/server/services/GameSessionService.java coordinates session lifecycle. A move flows from STOMP handler to src/main/java/com/tictactoe/server/services/GameMoveService.java, where participant, turn, board-cell, and game-status validation precede state mutation, result handling, and game/player persistence updates.
Matchmaking, Messaging, and Disconnect Lifecycle
Unstarted games/invitations and disconnected participants are managed independently by src/main/java/com/tictactoe/server/core/UnstartedGamesManager.java and src/main/java/com/tictactoe/server/core/DisconnectedPlayersManager.java. Outbound game/session/proposition messages are published through src/main/java/com/tictactoe/server/services/WebSocketMessagingService.java; message caching supports connection/session continuity via src/main/java/com/tictactoe/server/services/MessageCacheService.java. src/main/java/com/tictactoe/server/utils/WebSocketEventListener.java observes connection events, while src/main/java/com/tictactoe/server/utils/ScheduleTasks.java enforces delayed disconnect cancellation.
Infrastructure
Gradle builds and runs the service (build.gradle), and Dockerfile packages it for container deployment. Material external dependencies are MySQL via Spring Data JPA/Hibernate and client WebSocket/STOMP connections; there is no separate broker or cache service indicated by the repository.
Main entry points: GameMoveService.java Move service · AuthController.java REST controllers · WebSocketMessagingService.java WebSocket messaging · WebSocketConfig.java STOMP WebSocket
Components 20
Runtime
- Spring Boot app
src/main/java/com/tictactoe/server/ServerApplication.java
Transport
- REST controllers · Auth, game, and player endpoints.
src/main/java/com/tictactoe/server/controllers/AuthController.java - STOMP WebSocket · Exposes /ws/connect and application message routing.
src/main/java/com/tictactoe/server/config/WebSocketConfig.java - Game handler · Receives /app/game/move commands.
src/main/java/com/tictactoe/server/controllers/GameHandler.java
Security
- JWT core
src/main/java/com/tictactoe/server/security/JwtCore.java - Bearer filter
src/main/java/com/tictactoe/server/security/JwtTokenFilter.java - STOMP auth interceptor
src/main/java/com/tictactoe/server/security/AuthenticationChannelInterceptor.java - Refresh token service · Manages persisted refresh tokens.
src/main/java/com/tictactoe/server/services/RefreshTokenService.java
Domain & Persistence
- Game & player services · Creation, history, profiles, statistics, and ELO workflows.
- DTO mappers · Keeps JPA entities out of API contracts.
src/main/java/com/tictactoe/server/mappers/GameMapper.java - JPA domain entities · Players, games, and refresh tokens.
src/main/java/com/tictactoe/server/models/Game.java
Live Gameplay
- Game session service · Coordinates active match sessions.
src/main/java/com/tictactoe/server/services/GameSessionService.java - Game core · In-memory board and rule logic.
src/main/java/com/tictactoe/server/core/GameCore.java - Move service · Validates moves, mutates state, resolves results, and persists updates.
src/main/java/com/tictactoe/server/services/GameMoveService.java - Matchmaking managers · Tracks invitations and disconnected players.
src/main/java/com/tictactoe/server/core/UnstartedGamesManager.java - WebSocket messaging · Publishes game, session, and proposition events.
src/main/java/com/tictactoe/server/services/WebSocketMessagingService.java - Message cache
src/main/java/com/tictactoe/server/services/MessageCacheService.java - Connection lifecycle · Observes disconnects and cancels games after delay.
src/main/java/com/tictactoe/server/utils/WebSocketEventListener.java
Infrastructure
- MySQL · Spring Data JPA/Hibernate persistence store.
- Gradle & container build
build.gradle
Connections 24
- Spring Boot app → REST controllers: starts
- Spring Boot app → STOMP WebSocket: configures
- REST controllers → Bearer filter: secured by
- Bearer filter → JWT core: validates bearer JWT
- STOMP WebSocket → STOMP auth interceptor: intercepts messages
- STOMP auth interceptor → JWT core: authenticates JWT
- REST controllers → Refresh token service: issues/refreshes tokens
- Refresh token service → JPA domain entities: manages token entity
- REST controllers → Game & player services: invokes
- Game & player services → DTO mappers: maps DTOs
- Game & player services → JPA domain entities: reads/writes
- JPA domain entities → MySQL: persisted in
- STOMP WebSocket → Game handler: routes move commands
- Game handler → Move service: submits move
- Move service → Game session service: loads active session
- Move service → Game core: validates and applies
- Move service → Game & player services: persists results and ratings
- Move service → WebSocket messaging: publishes outcome
- Game session service → WebSocket messaging: publishes session events
- Matchmaking managers → WebSocket messaging: publishes invitations
- WebSocket messaging → Message cache: caches messages
- Connection lifecycle → Matchmaking managers: handles disconnects
- Connection lifecycle → Game session service: cancels delayed matches
- Gradle & container build → Spring Boot app: builds and packages