ravithejads commited on
Commit
75cfd02
·
verified ·
1 Parent(s): 74553d1

Delete room_game.js

Browse files
Files changed (1) hide show
  1. room_game.js +0 -371
room_game.js DELETED
@@ -1,371 +0,0 @@
1
- class RoomTicTacToeGame {
2
- constructor() {
3
- this.currentRoomId = null;
4
- this.roomData = null;
5
- this.cells = document.querySelectorAll('.cell');
6
- this.gameStatus = document.getElementById('gameStatus');
7
- this.roomInfo = document.getElementById('roomInfo');
8
- this.markdownContent = document.getElementById('markdownContent');
9
- this.chatMessages = document.getElementById('chatMessages');
10
- this.chatInput = document.getElementById('chatInput');
11
- this.sendBtn = document.getElementById('sendBtn');
12
- this.gameArea = document.getElementById('gameArea');
13
- this.noRoom = document.getElementById('noRoom');
14
-
15
- this.initGame();
16
- }
17
-
18
- initGame() {
19
- this.cells.forEach((cell, index) => {
20
- cell.addEventListener('click', () => this.handleCellClick(index));
21
- });
22
-
23
- // Update room state every 2 seconds if in a room
24
- setInterval(() => {
25
- if (this.currentRoomId) {
26
- this.refreshRoomState();
27
- }
28
- }, 2000);
29
- }
30
-
31
- async createNewRoom() {
32
- try {
33
- const response = await fetch('/rooms', {
34
- method: 'POST',
35
- headers: {
36
- 'Content-Type': 'application/json'
37
- }
38
- });
39
-
40
- if (!response.ok) {
41
- throw new Error(`HTTP error! status: ${response.status}`);
42
- }
43
-
44
- const data = await response.json();
45
- this.joinRoomById(data.room_id);
46
-
47
- } catch (error) {
48
- console.error('Failed to create room:', error);
49
- this.gameStatus.textContent = "Failed to create room. Try again.";
50
- }
51
- }
52
-
53
- async joinRoom() {
54
- const roomId = document.getElementById('roomIdInput').value.trim();
55
- if (!roomId) {
56
- this.gameStatus.textContent = "Please enter a room ID";
57
- return;
58
- }
59
-
60
- await this.joinRoomById(roomId);
61
- }
62
-
63
- async joinRoomById(roomId) {
64
- try {
65
- const response = await fetch(`/rooms/${roomId}`);
66
-
67
- if (!response.ok) {
68
- throw new Error(`HTTP error! status: ${response.status}`);
69
- }
70
-
71
- const data = await response.json();
72
- this.currentRoomId = roomId;
73
- this.roomData = data.room_data;
74
-
75
- // Clear the input
76
- document.getElementById('roomIdInput').value = '';
77
-
78
- // Show game area and enable chat
79
- this.gameArea.style.display = 'block';
80
- this.noRoom.style.display = 'none';
81
- this.chatInput.disabled = false;
82
- this.sendBtn.disabled = false;
83
- this.chatInput.placeholder = "Type a message...";
84
-
85
- // Update display
86
- this.updateDisplay();
87
- this.updateMarkdown(data.markdown);
88
- this.loadChatHistory();
89
-
90
- this.gameStatus.textContent = `Joined room ${roomId}!`;
91
-
92
- } catch (error) {
93
- console.error('Failed to join room:', error);
94
- this.gameStatus.textContent = `Failed to join room ${roomId}. Check the room ID.`;
95
- }
96
- }
97
-
98
- leaveRoom() {
99
- this.currentRoomId = null;
100
- this.roomData = null;
101
-
102
- // Hide game area and disable chat
103
- this.gameArea.style.display = 'none';
104
- this.noRoom.style.display = 'block';
105
- this.chatInput.disabled = true;
106
- this.sendBtn.disabled = true;
107
- this.chatInput.placeholder = "Join a room first...";
108
-
109
- // Clear display
110
- this.clearBoard();
111
- this.gameStatus.textContent = "Create or join a room to start playing!";
112
- this.updateRoomInfo();
113
- this.markdownContent.textContent = "Select or create a room to see the markdown representation...";
114
-
115
- // Clear chat
116
- this.chatMessages.innerHTML = `
117
- <div class="message ai">
118
- <div class="message-sender">System:</div>
119
- <div>Create or join a room to start chatting with Mistral AI!</div>
120
- </div>
121
- `;
122
- }
123
-
124
- async refreshRoomState() {
125
- if (!this.currentRoomId) return;
126
-
127
- try {
128
- const response = await fetch(`/rooms/${this.currentRoomId}`);
129
-
130
- if (!response.ok) {
131
- if (response.status === 404) {
132
- this.gameStatus.textContent = "Room no longer exists!";
133
- this.leaveRoom();
134
- return;
135
- }
136
- throw new Error(`HTTP error! status: ${response.status}`);
137
- }
138
-
139
- const data = await response.json();
140
- this.roomData = data.room_data;
141
- this.updateDisplay();
142
- this.updateMarkdown(data.markdown);
143
-
144
- } catch (error) {
145
- console.error('Failed to refresh room:', error);
146
- }
147
- }
148
-
149
- async handleCellClick(index) {
150
- if (!this.currentRoomId || !this.roomData) {
151
- this.gameStatus.textContent = "Join a room first!";
152
- return;
153
- }
154
-
155
- if (this.roomData.game_status !== 'active' ||
156
- this.roomData.board[index] !== '' ||
157
- this.roomData.current_player !== 'X') {
158
- return;
159
- }
160
-
161
- this.gameStatus.textContent = "Making your move...";
162
-
163
- try {
164
- const response = await fetch(`/rooms/${this.currentRoomId}/move`, {
165
- method: 'POST',
166
- headers: {
167
- 'Content-Type': 'application/json'
168
- },
169
- body: JSON.stringify({
170
- position: index
171
- })
172
- });
173
-
174
- if (!response.ok) {
175
- throw new Error(`HTTP error! status: ${response.status}`);
176
- }
177
-
178
- const data = await response.json();
179
- this.roomData = data.room_data;
180
- this.updateDisplay();
181
- this.updateMarkdown(data.markdown);
182
- this.loadChatHistory(); // Reload chat to get AI's move message
183
-
184
- if (this.roomData.game_status === 'active') {
185
- this.gameStatus.textContent = "Mistral is thinking...";
186
- setTimeout(() => {
187
- if (this.roomData.current_player === 'X') {
188
- this.gameStatus.textContent = "Your turn! Click a square.";
189
- }
190
- }, 1000);
191
- }
192
-
193
- } catch (error) {
194
- console.error('Move failed:', error);
195
- this.gameStatus.textContent = "Move failed. Try again.";
196
- }
197
- }
198
-
199
- async sendChatMessage() {
200
- if (!this.currentRoomId) {
201
- return;
202
- }
203
-
204
- const message = this.chatInput.value.trim();
205
- if (!message) return;
206
-
207
- this.chatInput.value = '';
208
-
209
- try {
210
- const response = await fetch(`/rooms/${this.currentRoomId}/chat`, {
211
- method: 'POST',
212
- headers: {
213
- 'Content-Type': 'application/json'
214
- },
215
- body: JSON.stringify({
216
- message: message
217
- })
218
- });
219
-
220
- if (!response.ok) {
221
- throw new Error(`HTTP error! status: ${response.status}`);
222
- }
223
-
224
- const data = await response.json();
225
- this.roomData = data.room_data;
226
- this.updateMarkdown(data.markdown);
227
- this.loadChatHistory();
228
-
229
- } catch (error) {
230
- console.error('Chat failed:', error);
231
- this.addChatMessage("Failed to send message", 'system');
232
- }
233
- }
234
-
235
- updateDisplay() {
236
- if (!this.roomData) return;
237
-
238
- // Update board
239
- this.roomData.board.forEach((cell, index) => {
240
- this.cells[index].textContent = cell;
241
- this.cells[index].className = 'cell';
242
- if (cell) {
243
- this.cells[index].classList.add(cell.toLowerCase());
244
- }
245
- });
246
-
247
- // Update game status
248
- if (this.roomData.game_status === 'won') {
249
- const winner = this.roomData.winner === 'X' ? 'You' : 'Mistral AI';
250
- this.gameStatus.textContent = `🎉 ${winner} won!`;
251
- } else if (this.roomData.game_status === 'draw') {
252
- this.gameStatus.textContent = "🤝 It's a draw!";
253
- } else if (this.roomData.current_player === 'X') {
254
- this.gameStatus.textContent = "Your turn! Click a square.";
255
- } else {
256
- this.gameStatus.textContent = "Mistral's turn...";
257
- }
258
-
259
- this.updateRoomInfo();
260
- }
261
-
262
- updateRoomInfo() {
263
- if (!this.roomData || !this.currentRoomId) {
264
- this.roomInfo.innerHTML = `
265
- <div>Status: No room selected</div>
266
- <div>Room ID: -</div>
267
- <div>Game Status: -</div>
268
- <div>Your Turn: -</div>
269
- `;
270
- return;
271
- }
272
-
273
- const isYourTurn = this.roomData.current_player === 'X' && this.roomData.game_status === 'active';
274
- this.roomInfo.innerHTML = `
275
- <div>Status: Connected</div>
276
- <div>Room ID: ${this.currentRoomId}</div>
277
- <div>Game Status: ${this.roomData.game_status}</div>
278
- <div>Your Turn: ${isYourTurn ? 'Yes' : 'No'}</div>
279
- <div>Moves: ${this.roomData.moves_count}/9</div>
280
- `;
281
- }
282
-
283
- updateMarkdown(markdown) {
284
- if (markdown) {
285
- this.markdownContent.textContent = markdown;
286
- }
287
- }
288
-
289
- loadChatHistory() {
290
- if (!this.roomData || !this.roomData.chat_history) return;
291
-
292
- this.chatMessages.innerHTML = '';
293
-
294
- this.roomData.chat_history.forEach(msg => {
295
- this.addChatMessage(msg.message, msg.sender);
296
- });
297
- }
298
-
299
- addChatMessage(message, sender) {
300
- const messageDiv = document.createElement('div');
301
- messageDiv.className = `message ${sender}`;
302
-
303
- const senderDiv = document.createElement('div');
304
- senderDiv.className = 'message-sender';
305
-
306
- let senderName;
307
- if (sender === 'user') senderName = 'You:';
308
- else if (sender === 'ai') senderName = 'Mistral AI:';
309
- else senderName = 'System:';
310
-
311
- senderDiv.textContent = senderName;
312
-
313
- const contentDiv = document.createElement('div');
314
- contentDiv.textContent = message;
315
-
316
- messageDiv.appendChild(senderDiv);
317
- messageDiv.appendChild(contentDiv);
318
- this.chatMessages.appendChild(messageDiv);
319
-
320
- // Scroll to bottom
321
- this.chatMessages.scrollTop = this.chatMessages.scrollHeight;
322
- }
323
-
324
- clearBoard() {
325
- this.cells.forEach(cell => {
326
- cell.textContent = '';
327
- cell.className = 'cell';
328
- });
329
- }
330
-
331
- async resetGame() {
332
- if (!this.currentRoomId) return;
333
-
334
- // Create a new room instead of resetting current one
335
- await this.createNewRoom();
336
- }
337
- }
338
-
339
- // Global functions for HTML onclick events
340
- let game;
341
-
342
- function createNewRoom() {
343
- game.createNewRoom();
344
- }
345
-
346
- function joinRoom() {
347
- game.joinRoom();
348
- }
349
-
350
- function leaveRoom() {
351
- game.leaveRoom();
352
- }
353
-
354
- function sendChatMessage() {
355
- game.sendChatMessage();
356
- }
357
-
358
- function handleEnter(event) {
359
- if (event.key === 'Enter') {
360
- sendChatMessage();
361
- }
362
- }
363
-
364
- function resetGame() {
365
- game.resetGame();
366
- }
367
-
368
- // Initialize game when page loads
369
- document.addEventListener('DOMContentLoaded', () => {
370
- game = new RoomTicTacToeGame();
371
- });