-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpackage.json
More file actions
162 lines (134 loc) · 4.17 KB
/
package.json
File metadata and controls
162 lines (134 loc) · 4.17 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
// backend/package.json
{
"name": "chessprofi-backend",
"version": "1.0.0",
"description": "ChessProfi - Платформа для обучения шахматам (Backend)",
"main": "src/index.js",
"scripts": {
"start": "node src/index.js",
"dev": "nodemon src/index.js",
"test": "echo \"Error: no test specified\" && exit 1"
},
"keywords": ["chess", "learning", "platform"],
"author": "ChessProfi Team",
"license": "MIT",
"dependencies": {
"express": "^4.18.2",
"mongoose": "^7.0.0",
"bcryptjs": "^2.4.3",
"jsonwebtoken": "^9.0.0",
"cors": "^2.8.5",
"socket.io": "^4.6.0",
"dotenv": "^16.0.3",
"chess.js": "^1.0.0-beta.6"
},
"devDependencies": {
"nodemon": "^2.0.20"
}
}
// backend/.env.example
# Переменные окружения для ChessProfi Backend
# Скопируйте этот файл в .env и заполните значения
# Порт сервера
PORT=5000
# MongoDB URI
MONGODB_URI=mongodb://localhost:27017/chessprofi
# JWT секретный ключ (используйте сложный случайный ключ в production)
JWT_SECRET=your-super-secret-jwt-key
# URL фронтенда для CORS
FRONTEND_URL=http://localhost:3000
// backend/src/routes/games.js
const express = require('express');
const Game = require('../models/Game');
const authMiddleware = require('../middleware/auth');
const router = express.Router();
// Получить все игры пользователя
router.get('/my-games', authMiddleware, async (req, res) => {
try {
const games = await Game.find({
$or: [
{ white: req.userId },
{ black: req.userId }
]
})
.populate('white', 'name rating')
.populate('black', 'name rating')
.sort('-startedAt')
.limit(20);
res.json(games);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Создать новую игру
router.post('/create', authMiddleware, async (req, res) => {
try {
const { opponentId } = req.body;
const game = new Game({
white: req.userId,
black: opponentId
});
await game.save();
await game.populate('white black', 'name rating');
res.status(201).json(game);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
// Получить игру по ID
router.get('/:id', authMiddleware, async (req, res) => {
try {
const game = await Game.findById(req.params.id)
.populate('white black', 'name rating');
if (!game) {
return res.status(404).json({ error: 'Игра не найдена' });
}
res.json(game);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;
// backend/src/middleware/auth.js
const jwt = require('jsonwebtoken');
module.exports = (req, res, next) => {
try {
const token = req.header('Authorization')?.replace('Bearer ', '');
if (!token) {
throw new Error();
}
const decoded = jwt.verify(token, process.env.JWT_SECRET || 'secret');
req.userId = decoded.userId;
req.userEmail = decoded.email;
req.userRole = decoded.role;
next();
} catch (error) {
res.status(401).json({ error: 'Пожалуйста, авторизуйтесь' });
}
};
// backend/src/routes/analysis.js
const express = require('express');
const { Chess } = require('chess.js');
const authMiddleware = require('../middleware/auth');
const router = express.Router();
// Анализ позиции
router.post('/position', authMiddleware, async (req, res) => {
try {
const { fen } = req.body;
const chess = new Chess(fen);
// Простой анализ (в реальном проекте здесь был бы Stockfish)
const analysis = {
isValid: chess.validate_fen(fen).valid,
isCheck: chess.isCheck(),
isCheckmate: chess.isCheckmate(),
isDraw: chess.isDraw(),
possibleMoves: chess.moves(),
turn: chess.turn(),
evaluation: Math.random() * 2 - 1 // Случайная оценка от -1 до 1
};
res.json(analysis);
} catch (error) {
res.status(500).json({ error: error.message });
}
});
module.exports = router;