-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchess-platform-architecture.ts
More file actions
220 lines (192 loc) · 6.32 KB
/
chess-platform-architecture.ts
File metadata and controls
220 lines (192 loc) · 6.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Project Structure
chess-learning-platform/
├── src/
│ ├── modules/
│ │ ├── auth/
│ │ │ ├── auth.module.ts
│ │ │ ├── auth.service.ts
│ │ │ ├── auth.controller.ts
│ │ │ ├── strategies/
│ │ │ │ └── jwt.strategy.ts
│ │ │ └── guards/
│ │ │ ├── jwt-auth.guard.ts
│ │ │ └── roles.guard.ts
│ │ ├── users/
│ │ │ ├── users.module.ts
│ │ │ ├── users.service.ts
│ │ │ ├── users.controller.ts
│ │ │ └── dto/
│ │ ├── games/
│ │ │ ├── games.module.ts
│ │ │ ├── games.service.ts
│ │ │ ├── games.gateway.ts // WebSocket handler
│ │ │ └── games.controller.ts
│ │ ├── analysis/
│ │ │ ├── analysis.module.ts
│ │ │ ├── analysis.service.ts
│ │ │ ├── analysis.processor.ts // BullMQ worker
│ │ │ └── stockfish.service.ts
│ │ ├── lessons/
│ │ │ ├── lessons.module.ts
│ │ │ ├── lessons.service.ts
│ │ │ └── lessons.controller.ts
│ │ └── ai-assistant/
│ │ ├── ai-assistant.module.ts
│ │ ├── ai-assistant.service.ts
│ │ └── pattern-recognition.ts
│ ├── common/
│ │ ├── decorators/
│ │ ├── filters/
│ │ ├── interceptors/
│ │ └── pipes/
│ ├── database/
│ │ ├── prisma.service.ts
│ │ └── redis.service.ts
│ └── main.ts
├── prisma/
│ └── schema.prisma
├── test/
├── docker-compose.yml
└── package.json
// Main Application Bootstrap
// src/main.ts
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { RedisIoAdapter } from './adapters/redis-io.adapter';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
const configService = app.get(ConfigService);
// Enable CORS with specific origins
app.enableCors({
origin: configService.get('ALLOWED_ORIGINS')?.split(',') || ['http://localhost:3000'],
credentials: true,
});
// Global validation pipe with transformation
app.useGlobalPipes(new ValidationPipe({
transform: true,
whitelist: true,
forbidNonWhitelisted: true,
}));
// Redis adapter for WebSocket scaling
const redisIoAdapter = new RedisIoAdapter(app);
await redisIoAdapter.connectToRedis();
app.useWebSocketAdapter(redisIoAdapter);
// Swagger documentation
const config = new DocumentBuilder()
.setTitle('Chess Learning Platform API')
.setDescription('Real-time collaborative chess learning')
.setVersion('1.0')
.addBearerAuth()
.build();
const document = SwaggerModule.createDocument(app, config);
SwaggerModule.setup('api/docs', app, document);
await app.listen(configService.get('PORT') || 3000);
}
bootstrap();
// Prisma Schema
// prisma/schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
enum UserRole {
STUDENT
COACH
ADMIN
}
enum GameStatus {
ACTIVE
COMPLETED
ABANDONED
}
model User {
id String @id @default(cuid())
email String @unique
password String
name String
role UserRole
rating Int @default(1200)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
studentsGames Game[] @relation("StudentGames")
coachGames Game[] @relation("CoachGames")
lessons Lesson[]
progress Progress[]
@@index([email])
@@index([role])
}
model Game {
id String @id @default(cuid())
studentId String
coachId String
pgn String @db.Text
fen String // Current position
status GameStatus @default(ACTIVE)
startedAt DateTime @default(now())
endedAt DateTime?
// Relations
student User @relation("StudentGames", fields: [studentId], references: [id])
coach User @relation("CoachGames", fields: [coachId], references: [id])
moves Move[]
analysis Analysis?
lesson Lesson? @relation(fields: [lessonId], references: [id])
lessonId String?
@@index([studentId, coachId])
@@index([status])
}
model Move {
id String @id @default(cuid())
gameId String
moveNumber Int
notation String // e.g., "e4", "Nf3"
fen String // Position after move
evaluation Float? // Stockfish evaluation
annotation String? // Coach's comment
timestamp DateTime @default(now())
game Game @relation(fields: [gameId], references: [id], onDelete: Cascade)
@@unique([gameId, moveNumber])
@@index([gameId])
}
model Analysis {
id String @id @default(cuid())
gameId String @unique
blunders Json // Array of move numbers and severity
mistakes Json
inaccuracies Json
bestMoves Json
evaluation Float // Overall game quality (0-100)
createdAt DateTime @default(now())
game Game @relation(fields: [gameId], references: [id])
}
model Lesson {
id String @id @default(cuid())
title String
description String?
coachId String
topics String[] // e.g., ["opening", "tactics", "endgame"]
createdAt DateTime @default(now())
coach User @relation(fields: [coachId], references: [id])
games Game[]
progress Progress[]
@@index([coachId])
}
model Progress {
id String @id @default(cuid())
userId String
lessonId String
completionRate Float @default(0) // 0-100
weaknesses Json // Identified pattern weaknesses
strengths Json
lastActivityAt DateTime @default(now())
user User @relation(fields: [userId], references: [id])
lesson Lesson @relation(fields: [lessonId], references: [id])
@@unique([userId, lessonId])
@@index([userId])
}