File size: 10,746 Bytes
bb258a2
 
 
 
 
 
43f0655
bb258a2
 
 
 
43f0655
bb258a2
 
 
 
 
 
 
 
 
 
 
43f0655
bb258a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f0655
 
 
 
 
bb258a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f0655
 
 
 
 
 
bb258a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f0655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb258a2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43f0655
 
 
 
 
 
 
 
 
 
bb258a2
 
 
 
 
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
document.addEventListener('DOMContentLoaded', function() {
    // Элементы DOM
    const roomForm = document.getElementById('roomForm');
    const usernameInput = document.getElementById('username');
    const roomInput = document.getElementById('room');
    const joinBtn = document.getElementById('joinBtn');
    const readyBtn = document.getElementById('readyBtn');
    const gameContainer = document.getElementById('gameContainer');
    const textDisplay = document.getElementById('textDisplay');
    const inputArea = document.getElementById('inputArea');
    const playersList = document.getElementById('playersList');
    const countdown = document.getElementById('countdown');
    
    // Переменные для игры
    let socket;
    let username = '';
    let room = '';
    let text = '';
    let startTime = null;
    let currentPosition = 0;
    let mistakes = 0;
    let totalChars = 0;
    let gameActive = false;
    let playerReady = false;
    
    // Инициализация Socket.IO
    function initSocket() {
        socket = io();
        
        // Обработчики событий Socket.IO
        socket.on('connect', () => {
            console.log('Connected to server');
        });
        
        socket.on('room_update', (data) => {
            updatePlayersList(data.players);
            
            // Если игра началась, но текст еще не отображен
            if (data.started && !gameActive) {
                startGame(data.text);
            }
        });
        
        socket.on('countdown_start', () => {
            // Начинаем обратный отсчет
            startCountdown();
        });
        
        socket.on('game_started', (data) => {
            startGame(data.text);
        });
        
        socket.on('progress_update', (players) => {
            updatePlayersProgress(players);
            
            // Проверяем, закончили ли все игроки
            let allFinished = true;
            for (const id in players) {
                if (!players[id].finished) {
                    allFinished = false;
                    break;
                }
            }
            
            // Если все закончили, показываем результаты
            if (allFinished) {
                showResults(players);
            }
        });
        
        socket.on('disconnect', () => {
            console.log('Disconnected from server');
        });
    }
    
    // Присоединение к комнате
    function joinRoom() {
        username = usernameInput.value.trim();
        room = roomInput.value.trim();
        
        if (!username || !room) {
            alert('Пожалуйста, введите имя пользователя и название комнаты');
            return;
        }
        
        socket.emit('join', {
            username: username,
            room: room
        });
        
        // Скрываем форму и показываем игровой контейнер
        roomForm.style.display = 'none';
        gameContainer.style.display = 'block';
    }
    
    // Обновление списка игроков
    function updatePlayersList(players) {
        playersList.innerHTML = '';
        
        for (const id in players) {
            const player = players[id];
            const playerItem = document.createElement('div');
            playerItem.className = 'player-item';
            playerItem.id = 'player-' + id;
            
            const playerName = document.createElement('div');
            playerName.className = 'player-name';
            playerName.textContent = player.username;
            
            // Добавляем индикатор готовности
            if (player.ready) {
                playerName.textContent += ' ✓';
                playerName.classList.add('player-ready');
            }
            
            const playerProgress = document.createElement('div');
            playerProgress.className = 'player-progress';
            
            const progressBar = document.createElement('div');
            progressBar.className = 'progress-bar';
            progressBar.style.width = player.progress + '%';
            
            const playerStats = document.createElement('div');
            playerStats.className = 'player-stats';
            playerStats.innerHTML = `
                <span>WPM: ${player.wpm || 0}</span>
                <span>Точность: ${player.accuracy || 0}%</span>
            `;
            
            playerProgress.appendChild(progressBar);
            playerItem.appendChild(playerName);
            playerItem.appendChild(playerProgress);
            playerItem.appendChild(playerStats);
            playersList.appendChild(playerItem);
        }
    }
    
    // Обновление прогресса игроков
    function updatePlayersProgress(players) {
        for (const id in players) {
            const player = players[id];
            const playerItem = document.getElementById('player-' + id);
            
            if (playerItem) {
                const progressBar = playerItem.querySelector('.progress-bar');
                progressBar.style.width = player.progress + '%';
                
                const playerStats = playerItem.querySelector('.player-stats');
                playerStats.innerHTML = `
                    <span>WPM: ${player.wpm || 0}</span>
                    <span>Точность: ${player.accuracy || 0}%</span>
                `;
            }
        }
    }
    
    // Обратный отсчет перед началом игры
    function startCountdown() {
        readyBtn.disabled = true;
        countdown.style.display = 'block';
        let count = 3;
        countdown.textContent = count;
        
        const countdownInterval = setInterval(() => {
            count--;
            countdown.textContent = count;
            
            if (count <= 0) {
                clearInterval(countdownInterval);
                countdown.style.display = 'none';
                socket.emit('start_game', { room: room });
            }
        }, 1000);
    }
    
    // Начало игры
    function startGame(textContent) {
        text = textContent;
        displayText();
        
        inputArea.value = '';
        inputArea.disabled = false;
        inputArea.focus();
        
        mistakes = 0;
        totalChars = 0;
        currentPosition = 0;
        
        startTime = new Date();
        gameActive = true;
    }
    
    // Отображение текста
    function displayText() {
        textDisplay.innerHTML = '';
        for (let i = 0; i < text.length; i++) {
            const span = document.createElement('span');
            span.textContent = text[i];
            textDisplay.appendChild(span);
        }
        // Выделяем первый символ как текущий
        if (textDisplay.firstChild) {
            textDisplay.firstChild.classList.add('current');
        }
    }
    
    // Показ результатов
    function showResults(players) {
        let winner = null;
        let maxWpm = 0;
        
        for (const id in players) {
            const player = players[id];
            if (player.wpm > maxWpm) {
                maxWpm = player.wpm;
                winner = player;
            }
        }
        
        if (winner) {
            alert(`Игра окончена! Победитель: ${winner.username} со скоростью ${winner.wpm} WPM и точностью ${winner.accuracy}%`);
        }
    }
    
    // Обработка ввода
    inputArea.addEventListener('input', function(e) {
        if (!gameActive) return;
        
        const inputValue = e.target.value;
        const currentChar = text[currentPosition];
        
        // Проверяем, правильно ли введен символ
        if (inputValue.charAt(inputValue.length - 1) === currentChar) {
            // Правильный символ
            const spans = textDisplay.querySelectorAll('span');
            spans[currentPosition].classList.remove('current');
            spans[currentPosition].classList.add('correct');
            
            currentPosition++;
            totalChars++;
            
            // Если есть следующий символ, делаем его текущим
            if (currentPosition < text.length) {
                spans[currentPosition].classList.add('current');
            } else {
                // Текст закончился, завершаем игру
                gameActive = false;
                inputArea.disabled = true;
            }
        } else {
            // Неправильный символ
            mistakes++;
            totalChars++;
        }
        
        // Обновляем статистику и отправляем на сервер
        updateStats();
    });
    
    // Обновление статистики
    function updateStats() {
        if (!gameActive && currentPosition < text.length) return;
        
        const currentTime = new Date();
        const timeElapsed = (currentTime - startTime) / 1000; // в секундах
        
        // Расчет WPM (слов в минуту)
        // Считаем, что среднее слово - 5 символов
        const wpm = Math.round((currentPosition / 5) / (timeElapsed / 60));
        
        // Расчет точности
        const accuracy = totalChars > 0 ? Math.round(((totalChars - mistakes) / totalChars) * 100) : 100;
        
        // Расчет прогресса
        const progress = Math.round((currentPosition / text.length) * 100);
        
        // Отправляем данные на сервер
        socket.emit('update_progress', {
            room: room,
            progress: progress,
            wpm: wpm,
            accuracy: accuracy
        });
    }
    
    // Обработчики кнопок
    joinBtn.addEventListener('click', function(e) {
        e.preventDefault();
        joinRoom();
    });
    
    readyBtn.addEventListener('click', function() {
        if (!playerReady) {
            playerReady = true;
            readyBtn.textContent = 'Готов!';
            readyBtn.classList.add('ready');
            
            socket.emit('player_ready', {
                room: room
            });
        }
    });
    
    // Инициализация
    initSocket();
});