first
This commit is contained in:
		
						commit
						11b062b959
					
				
							
								
								
									
										47
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										47
									
								
								.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,47 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					.DS_Store
 | 
				
			||||||
 | 
					Thumbs.db
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*.log
 | 
				
			||||||
 | 
					npm-debug.log*
 | 
				
			||||||
 | 
					yarn-debug.log*
 | 
				
			||||||
 | 
					yarn-error.log*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					node_modules/
 | 
				
			||||||
 | 
					package-lock.json
 | 
				
			||||||
 | 
					.pnpm-debug.log
 | 
				
			||||||
 | 
					.npmrc
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					build/
 | 
				
			||||||
 | 
					dist/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*.env
 | 
				
			||||||
 | 
					docker-compose.override.yml
 | 
				
			||||||
 | 
					docker-compose*.log
 | 
				
			||||||
 | 
					.dockerignore
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.vscode/
 | 
				
			||||||
 | 
					.idea/
 | 
				
			||||||
 | 
					*.sw?
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.DS_Store
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*~
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					*.lnk
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					tmp/
 | 
				
			||||||
 | 
					temp/
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.cache/
 | 
				
			||||||
 | 
					.history/
 | 
				
			||||||
							
								
								
									
										20
									
								
								backend/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								backend/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,20 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					FROM node:14-alpine
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					WORKDIR /app
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					COPY package*.json ./
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					RUN npm install
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					COPY . .
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					EXPOSE 4000
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					CMD ["npm", "start"]
 | 
				
			||||||
							
								
								
									
										199
									
								
								backend/index.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										199
									
								
								backend/index.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,199 @@
 | 
				
			|||||||
 | 
					const express = require('express');
 | 
				
			||||||
 | 
					const cors = require('cors');
 | 
				
			||||||
 | 
					const mysql = require('mysql2/promise');
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const app = express();
 | 
				
			||||||
 | 
					app.use(cors());
 | 
				
			||||||
 | 
					app.use(express.json());
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					(async () => {
 | 
				
			||||||
 | 
					  console.log("Initializing database connection...");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const pool = await mysql.createPool({
 | 
				
			||||||
 | 
					      host: 'host.docker.internal',
 | 
				
			||||||
 | 
					      user: 'root',
 | 
				
			||||||
 | 
					      password: 'somepassword',
 | 
				
			||||||
 | 
					      database: 'battleship',
 | 
				
			||||||
 | 
					      waitForConnections: true,
 | 
				
			||||||
 | 
					      connectionLimit: 10,
 | 
				
			||||||
 | 
					      queueLimit: 0
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log("Successfully connected to MySQL!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const conn = await pool.getConnection();
 | 
				
			||||||
 | 
					    const [rows] = await conn.query("SELECT VERSION() AS mysql_version");
 | 
				
			||||||
 | 
					    console.log(`MySQL Version: ${rows[0].mysql_version}`);
 | 
				
			||||||
 | 
					    conn.release();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    console.log("Checking or creating tables...");
 | 
				
			||||||
 | 
					    await pool.execute(`
 | 
				
			||||||
 | 
					      CREATE TABLE IF NOT EXISTS games (
 | 
				
			||||||
 | 
					        id INT AUTO_INCREMENT PRIMARY KEY,
 | 
				
			||||||
 | 
					        player1 VARCHAR(255),
 | 
				
			||||||
 | 
					        player2 VARCHAR(255),
 | 
				
			||||||
 | 
					        player1_ships JSON,
 | 
				
			||||||
 | 
					        player2_ships JSON,
 | 
				
			||||||
 | 
					        current_turn INT DEFAULT 1,
 | 
				
			||||||
 | 
					        game_state JSON NOT NULL
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					    `);
 | 
				
			||||||
 | 
					    console.log("Table 'games' is ready!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    await pool.execute(`
 | 
				
			||||||
 | 
					      CREATE TABLE IF NOT EXISTS game_results (
 | 
				
			||||||
 | 
					        id INT AUTO_INCREMENT PRIMARY KEY,
 | 
				
			||||||
 | 
					        game_id INT,
 | 
				
			||||||
 | 
					        winner VARCHAR(255),
 | 
				
			||||||
 | 
					        loser VARCHAR(255),
 | 
				
			||||||
 | 
					        moves INT
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					    `);
 | 
				
			||||||
 | 
					    console.log("Table 'game_results' is ready!");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					app.get('/api/results', async (req, res) => {
 | 
				
			||||||
 | 
					  try {
 | 
				
			||||||
 | 
					    const [results] = await pool.execute('SELECT * FROM game_results ORDER BY id DESC');
 | 
				
			||||||
 | 
					    res.json({ results });
 | 
				
			||||||
 | 
					  } catch (err) {
 | 
				
			||||||
 | 
					    console.error("Error fetching results:", err);
 | 
				
			||||||
 | 
					    res.status(500).json({ error: "Database error" });
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    app.post('/api/new-game', async (req, res) => {
 | 
				
			||||||
 | 
					      const { player1, player2 } = req.body;
 | 
				
			||||||
 | 
					      if (!player1 || !player2) {
 | 
				
			||||||
 | 
					        console.warn("Missing player names in request.");
 | 
				
			||||||
 | 
					        return res.status(400).json({ error: 'Player names required' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      const gameState = {
 | 
				
			||||||
 | 
					        boardSize: 5,
 | 
				
			||||||
 | 
					        hits: [],
 | 
				
			||||||
 | 
					        misses: [],
 | 
				
			||||||
 | 
					        gameOver: false
 | 
				
			||||||
 | 
					      };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        console.log(`Creating new game for ${player1} vs ${player2}...`);
 | 
				
			||||||
 | 
					        const [result] = await pool.execute(
 | 
				
			||||||
 | 
					          'INSERT INTO games (player1, player2, game_state) VALUES (?, ?, ?)',
 | 
				
			||||||
 | 
					          [player1, player2, JSON.stringify(gameState)]
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					        console.log(`Game created with ID: ${result.insertId}`);
 | 
				
			||||||
 | 
					        res.json({ gameId: result.insertId, message: 'Game created' });
 | 
				
			||||||
 | 
					      } catch (err) {
 | 
				
			||||||
 | 
					        console.error('Error creating game:', err);
 | 
				
			||||||
 | 
					        res.status(500).json({ error: 'Database error' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    app.post('/api/place-ships', async (req, res) => {
 | 
				
			||||||
 | 
					      const { gameId, player, ships } = req.body;
 | 
				
			||||||
 | 
					      if (!gameId || !player || !ships) {
 | 
				
			||||||
 | 
					        console.warn("Missing parameters for placing ships.");
 | 
				
			||||||
 | 
					        return res.status(400).json({ error: 'Missing parameters' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        console.log(`Placing ships for player ${player} in game ID: ${gameId}...`);
 | 
				
			||||||
 | 
					        const [rows] = await pool.execute('SELECT * FROM games WHERE id = ?', [gameId]);
 | 
				
			||||||
 | 
					        if (rows.length === 0) {
 | 
				
			||||||
 | 
					          console.warn(`Game with ID ${gameId} not found.`);
 | 
				
			||||||
 | 
					          return res.status(404).json({ error: 'Game not found' });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let updateField = player === 1 ? 'player1_ships' : 'player2_ships';
 | 
				
			||||||
 | 
					        await pool.execute(`UPDATE games SET ${updateField} = ? WHERE id = ?`, [JSON.stringify(ships), gameId]);
 | 
				
			||||||
 | 
					        console.log(`Ships placed for player ${player} in game ID: ${gameId}`);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res.json({ message: `Ships placed for player ${player}` });
 | 
				
			||||||
 | 
					      } catch (err) {
 | 
				
			||||||
 | 
					        console.error('Error placing ships:', err);
 | 
				
			||||||
 | 
					        res.status(500).json({ error: 'Database error' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    // 🎯 Атака
 | 
				
			||||||
 | 
					    app.post('/api/attack', async (req, res) => {
 | 
				
			||||||
 | 
					      const { gameId, row, col, player } = req.body;
 | 
				
			||||||
 | 
					      if (!gameId || typeof row === 'undefined' || typeof col === 'undefined' || !player) {
 | 
				
			||||||
 | 
					        console.warn("Missing attack parameters in request.");
 | 
				
			||||||
 | 
					        return res.status(400).json({ error: 'Missing parameters' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        console.log(`Player ${player} attacks (${row}, ${col}) in game ID: ${gameId}`);
 | 
				
			||||||
 | 
					        const [rows] = await pool.execute('SELECT * FROM games WHERE id = ?', [gameId]);
 | 
				
			||||||
 | 
					        if (rows.length === 0) {
 | 
				
			||||||
 | 
					          console.warn(`Game with ID ${gameId} not found.`);
 | 
				
			||||||
 | 
					          return res.status(404).json({ error: 'Game not found' });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        let game = rows[0];
 | 
				
			||||||
 | 
					        let state = JSON.parse(game.game_state);
 | 
				
			||||||
 | 
					        let opponentShips = player === 1 ? JSON.parse(game.player2_ships || "[]") : JSON.parse(game.player1_ships || "[]");
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        const hit = opponentShips.some(ship => ship.row === row && ship.col === col);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        if (hit) {
 | 
				
			||||||
 | 
					          console.log(`HIT! Player ${player} hit an opponent's ship at (${row}, ${col})`);
 | 
				
			||||||
 | 
					          state.hits.push({ row, col });
 | 
				
			||||||
 | 
					          if (state.hits.length === opponentShips.length) {
 | 
				
			||||||
 | 
					            state.gameOver = true;
 | 
				
			||||||
 | 
					            console.log(`Player ${player} has won the game!`);
 | 
				
			||||||
 | 
					          }
 | 
				
			||||||
 | 
					        } else {
 | 
				
			||||||
 | 
					          console.log(`MISS! Player ${player} attacked (${row}, ${col}) but missed.`);
 | 
				
			||||||
 | 
					          state.misses.push({ row, col });
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        state.current_turn = player === 1 ? 2 : 1;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        await pool.execute('UPDATE games SET game_state = ? WHERE id = ?', [JSON.stringify(state), gameId]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        res.json({ hit, state });
 | 
				
			||||||
 | 
					      } catch (err) {
 | 
				
			||||||
 | 
					        console.error("Attack error:", err);
 | 
				
			||||||
 | 
					        res.status(500).json({ error: 'Database error' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    app.post('/api/save-result', async (req, res) => {
 | 
				
			||||||
 | 
					      const { gameId, winner, loser, moves } = req.body;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      if (!gameId || !winner || !loser || !moves) {
 | 
				
			||||||
 | 
					        console.warn("Missing game result parameters.");
 | 
				
			||||||
 | 
					        return res.status(400).json({ error: 'Missing parameters' });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      try {
 | 
				
			||||||
 | 
					        console.log(`Saving result: Winner - ${winner}, Loser - ${loser}, Moves - ${moves}`);
 | 
				
			||||||
 | 
					        await pool.execute(
 | 
				
			||||||
 | 
					          `INSERT INTO game_results (game_id, winner, loser, moves) VALUES (?, ?, ?, ?)`,
 | 
				
			||||||
 | 
					          [gameId, winner, loser, moves]
 | 
				
			||||||
 | 
					        );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        console.log(`Game result saved for game ID ${gameId}`);
 | 
				
			||||||
 | 
					        res.json({ message: "Game result saved" });
 | 
				
			||||||
 | 
					      } catch (err) {
 | 
				
			||||||
 | 
					        console.error("Error saving game result:", err);
 | 
				
			||||||
 | 
					        res.status(500).json({ error: "Database error" });
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const port = 4000;
 | 
				
			||||||
 | 
					    app.listen(port, () => {
 | 
				
			||||||
 | 
					      console.log(`Backend started on port ${port}`);
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  } catch (err) {
 | 
				
			||||||
 | 
					    console.error("Database connection failed!", err);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					})();
 | 
				
			||||||
							
								
								
									
										18
									
								
								backend/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										18
									
								
								backend/package.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,18 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					    "name": "battleship-backend",
 | 
				
			||||||
 | 
					    "version": "1.0.0",
 | 
				
			||||||
 | 
					    "description": "Backend for Battleship game",
 | 
				
			||||||
 | 
					    "main": "index.js",
 | 
				
			||||||
 | 
					    "scripts": {
 | 
				
			||||||
 | 
					        "start": "node index.js"
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    "keywords": [],
 | 
				
			||||||
 | 
					    "author": "",
 | 
				
			||||||
 | 
					    "license": "MIT",
 | 
				
			||||||
 | 
					    "dependencies": {
 | 
				
			||||||
 | 
					        "cors": "^2.8.5",
 | 
				
			||||||
 | 
					        "express": "^4.18.2",
 | 
				
			||||||
 | 
					        "mysql2": "^3.12.0",
 | 
				
			||||||
 | 
					        "pg": "^8.9.0"
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										32
									
								
								docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										32
									
								
								docker-compose.yml
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,32 @@
 | 
				
			|||||||
 | 
					version: '3.8'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					services:
 | 
				
			||||||
 | 
					  backend:
 | 
				
			||||||
 | 
					    build: ./backend
 | 
				
			||||||
 | 
					    container_name: battleship_backend
 | 
				
			||||||
 | 
					    restart: unless-stopped
 | 
				
			||||||
 | 
					    environment:
 | 
				
			||||||
 | 
					      - DB_HOST=battleship-mysql
 | 
				
			||||||
 | 
					      - DB_PORT=3306
 | 
				
			||||||
 | 
					      - DB_USER=root
 | 
				
			||||||
 | 
					      - DB_PASSWORD=somepassword
 | 
				
			||||||
 | 
					      - DB_NAME=battleship
 | 
				
			||||||
 | 
					    ports:
 | 
				
			||||||
 | 
					      - "4000:4000"
 | 
				
			||||||
 | 
					    networks:
 | 
				
			||||||
 | 
					      - app-net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  frontend:
 | 
				
			||||||
 | 
					    build: ./frontend
 | 
				
			||||||
 | 
					    container_name: battleship_frontend
 | 
				
			||||||
 | 
					    restart: unless-stopped
 | 
				
			||||||
 | 
					    depends_on:
 | 
				
			||||||
 | 
					      - backend
 | 
				
			||||||
 | 
					    ports:
 | 
				
			||||||
 | 
					      - "3001:80"
 | 
				
			||||||
 | 
					    networks:
 | 
				
			||||||
 | 
					      - app-net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					networks:
 | 
				
			||||||
 | 
					  app-net:
 | 
				
			||||||
 | 
					    external: true
 | 
				
			||||||
							
								
								
									
										24
									
								
								frontend/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								frontend/.gitignore
									
									
									
									
										vendored
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					# Logs
 | 
				
			||||||
 | 
					logs
 | 
				
			||||||
 | 
					*.log
 | 
				
			||||||
 | 
					npm-debug.log*
 | 
				
			||||||
 | 
					yarn-debug.log*
 | 
				
			||||||
 | 
					yarn-error.log*
 | 
				
			||||||
 | 
					pnpm-debug.log*
 | 
				
			||||||
 | 
					lerna-debug.log*
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					node_modules
 | 
				
			||||||
 | 
					dist
 | 
				
			||||||
 | 
					dist-ssr
 | 
				
			||||||
 | 
					*.local
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Editor directories and files
 | 
				
			||||||
 | 
					.vscode/*
 | 
				
			||||||
 | 
					!.vscode/extensions.json
 | 
				
			||||||
 | 
					.idea
 | 
				
			||||||
 | 
					.DS_Store
 | 
				
			||||||
 | 
					*.suo
 | 
				
			||||||
 | 
					*.ntvs*
 | 
				
			||||||
 | 
					*.njsproj
 | 
				
			||||||
 | 
					*.sln
 | 
				
			||||||
 | 
					*.sw?
 | 
				
			||||||
							
								
								
									
										28
									
								
								frontend/Dockerfile
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								frontend/Dockerfile
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,28 @@
 | 
				
			|||||||
 | 
					# --- Build stage ---
 | 
				
			||||||
 | 
					    FROM node:18-alpine AS build
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    WORKDIR /app
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Копируем package.json и package-lock.json (если есть)
 | 
				
			||||||
 | 
					    COPY package*.json ./
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Устанавливаем зависимости
 | 
				
			||||||
 | 
					    RUN npm install
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Копируем исходный код
 | 
				
			||||||
 | 
					    COPY . .
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Собираем проект (Vite по умолчанию генерирует папку dist)
 | 
				
			||||||
 | 
					    RUN npm run build
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # --- Production stage ---
 | 
				
			||||||
 | 
					    FROM nginx:alpine
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Копируем собранный проект в nginx
 | 
				
			||||||
 | 
					    COPY --from=build /app/dist /usr/share/nginx/html
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    # Открываем порт 80 для nginx
 | 
				
			||||||
 | 
					    EXPOSE 80
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
 | 
					    CMD ["nginx", "-g", "daemon off;"]
 | 
				
			||||||
 | 
					    
 | 
				
			||||||
							
								
								
									
										12
									
								
								frontend/README.md
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								frontend/README.md
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					# React + Vite
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					This template provides a minimal setup to get React working in Vite with HMR and some ESLint rules.
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					Currently, two official plugins are available:
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					- [@vitejs/plugin-react](https://github.com/vitejs/vite-plugin-react/blob/main/packages/plugin-react/README.md) uses [Babel](https://babeljs.io/) for Fast Refresh
 | 
				
			||||||
 | 
					- [@vitejs/plugin-react-swc](https://github.com/vitejs/vite-plugin-react-swc) uses [SWC](https://swc.rs/) for Fast Refresh
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					## Expanding the ESLint configuration
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					If you are developing a production application, we recommend using TypeScript and enable type-aware lint rules. Check out the [TS template](https://github.com/vitejs/vite/tree/main/packages/create-vite/template-react-ts) to integrate TypeScript and [`typescript-eslint`](https://typescript-eslint.io) in your project.
 | 
				
			||||||
							
								
								
									
										33
									
								
								frontend/eslint.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										33
									
								
								frontend/eslint.config.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,33 @@
 | 
				
			|||||||
 | 
					import js from '@eslint/js'
 | 
				
			||||||
 | 
					import globals from 'globals'
 | 
				
			||||||
 | 
					import reactHooks from 'eslint-plugin-react-hooks'
 | 
				
			||||||
 | 
					import reactRefresh from 'eslint-plugin-react-refresh'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default [
 | 
				
			||||||
 | 
					  { ignores: ['dist'] },
 | 
				
			||||||
 | 
					  {
 | 
				
			||||||
 | 
					    files: ['**/*.{js,jsx}'],
 | 
				
			||||||
 | 
					    languageOptions: {
 | 
				
			||||||
 | 
					      ecmaVersion: 2020,
 | 
				
			||||||
 | 
					      globals: globals.browser,
 | 
				
			||||||
 | 
					      parserOptions: {
 | 
				
			||||||
 | 
					        ecmaVersion: 'latest',
 | 
				
			||||||
 | 
					        ecmaFeatures: { jsx: true },
 | 
				
			||||||
 | 
					        sourceType: 'module',
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    plugins: {
 | 
				
			||||||
 | 
					      'react-hooks': reactHooks,
 | 
				
			||||||
 | 
					      'react-refresh': reactRefresh,
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					    rules: {
 | 
				
			||||||
 | 
					      ...js.configs.recommended.rules,
 | 
				
			||||||
 | 
					      ...reactHooks.configs.recommended.rules,
 | 
				
			||||||
 | 
					      'no-unused-vars': ['error', { varsIgnorePattern: '^[A-Z_]' }],
 | 
				
			||||||
 | 
					      'react-refresh/only-export-components': [
 | 
				
			||||||
 | 
					        'warn',
 | 
				
			||||||
 | 
					        { allowConstantExport: true },
 | 
				
			||||||
 | 
					      ],
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					]
 | 
				
			||||||
							
								
								
									
										13
									
								
								frontend/index.html
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										13
									
								
								frontend/index.html
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,13 @@
 | 
				
			|||||||
 | 
					<!doctype html>
 | 
				
			||||||
 | 
					<html lang="en">
 | 
				
			||||||
 | 
					  <head>
 | 
				
			||||||
 | 
					    <meta charset="UTF-8" />
 | 
				
			||||||
 | 
					    <link rel="icon" type="image/svg+xml" href="/vite.svg" />
 | 
				
			||||||
 | 
					    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
 | 
				
			||||||
 | 
					    <title>Vite + React</title>
 | 
				
			||||||
 | 
					  </head>
 | 
				
			||||||
 | 
					  <body>
 | 
				
			||||||
 | 
					    <div id="root"></div>
 | 
				
			||||||
 | 
					    <script type="module" src="/src/main.jsx"></script>
 | 
				
			||||||
 | 
					  </body>
 | 
				
			||||||
 | 
					</html>
 | 
				
			||||||
							
								
								
									
										27
									
								
								frontend/package.json
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										27
									
								
								frontend/package.json
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,27 @@
 | 
				
			|||||||
 | 
					{
 | 
				
			||||||
 | 
					  "name": "frontend",
 | 
				
			||||||
 | 
					  "private": true,
 | 
				
			||||||
 | 
					  "version": "0.0.0",
 | 
				
			||||||
 | 
					  "type": "module",
 | 
				
			||||||
 | 
					  "scripts": {
 | 
				
			||||||
 | 
					    "dev": "vite",
 | 
				
			||||||
 | 
					    "build": "vite build",
 | 
				
			||||||
 | 
					    "lint": "eslint .",
 | 
				
			||||||
 | 
					    "preview": "vite preview"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "dependencies": {
 | 
				
			||||||
 | 
					    "react": "^19.0.0",
 | 
				
			||||||
 | 
					    "react-dom": "^19.0.0"
 | 
				
			||||||
 | 
					  },
 | 
				
			||||||
 | 
					  "devDependencies": {
 | 
				
			||||||
 | 
					    "@eslint/js": "^9.21.0",
 | 
				
			||||||
 | 
					    "@types/react": "^19.0.10",
 | 
				
			||||||
 | 
					    "@types/react-dom": "^19.0.4",
 | 
				
			||||||
 | 
					    "@vitejs/plugin-react": "^4.3.4",
 | 
				
			||||||
 | 
					    "eslint": "^9.21.0",
 | 
				
			||||||
 | 
					    "eslint-plugin-react-hooks": "^5.1.0",
 | 
				
			||||||
 | 
					    "eslint-plugin-react-refresh": "^0.4.19",
 | 
				
			||||||
 | 
					    "globals": "^15.15.0",
 | 
				
			||||||
 | 
					    "vite": "^6.2.0"
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										1
									
								
								frontend/public/vite.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								frontend/public/vite.svg
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="31.88" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 257"><defs><linearGradient id="IconifyId1813088fe1fbc01fb466" x1="-.828%" x2="57.636%" y1="7.652%" y2="78.411%"><stop offset="0%" stop-color="#41D1FF"></stop><stop offset="100%" stop-color="#BD34FE"></stop></linearGradient><linearGradient id="IconifyId1813088fe1fbc01fb467" x1="43.376%" x2="50.316%" y1="2.242%" y2="89.03%"><stop offset="0%" stop-color="#FFEA83"></stop><stop offset="8.333%" stop-color="#FFDD35"></stop><stop offset="100%" stop-color="#FFA800"></stop></linearGradient></defs><path fill="url(#IconifyId1813088fe1fbc01fb466)" d="M255.153 37.938L134.897 252.976c-2.483 4.44-8.862 4.466-11.382.048L.875 37.958c-2.746-4.814 1.371-10.646 6.827-9.67l120.385 21.517a6.537 6.537 0 0 0 2.322-.004l117.867-21.483c5.438-.991 9.574 4.796 6.877 9.62Z"></path><path fill="url(#IconifyId1813088fe1fbc01fb467)" d="M185.432.063L96.44 17.501a3.268 3.268 0 0 0-2.634 3.014l-5.474 92.456a3.268 3.268 0 0 0 3.997 3.378l24.777-5.718c2.318-.535 4.413 1.507 3.936 3.838l-7.361 36.047c-.495 2.426 1.782 4.5 4.151 3.78l15.304-4.649c2.372-.72 4.652 1.36 4.15 3.788l-11.698 56.621c-.732 3.542 3.979 5.473 5.943 2.437l1.313-2.028l72.516-144.72c1.215-2.423-.88-5.186-3.54-4.672l-25.505 4.922c-2.396.462-4.435-1.77-3.759-4.114l16.646-57.705c.677-2.35-1.37-4.583-3.769-4.113Z"></path></svg>
 | 
				
			||||||
| 
		 After Width: | Height: | Size: 1.5 KiB  | 
							
								
								
									
										42
									
								
								frontend/src/App.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										42
									
								
								frontend/src/App.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,42 @@
 | 
				
			|||||||
 | 
					.app-container {
 | 
				
			||||||
 | 
					  font-family: 'Arial', sans-serif;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  padding: 20px;
 | 
				
			||||||
 | 
					  background-color: #f0f8ff;
 | 
				
			||||||
 | 
					  min-height: 100vh;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.game-controls {
 | 
				
			||||||
 | 
					  margin: 20px 0;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.btn {
 | 
				
			||||||
 | 
					  padding: 10px 20px;
 | 
				
			||||||
 | 
					  font-size: 16px;
 | 
				
			||||||
 | 
					  background-color: #0077cc;
 | 
				
			||||||
 | 
					  color: white;
 | 
				
			||||||
 | 
					  border: none;
 | 
				
			||||||
 | 
					  border-radius: 5px;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  transition: background-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.btn:hover {
 | 
				
			||||||
 | 
					  background-color: #005fa3;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.game-board {
 | 
				
			||||||
 | 
					  margin-top: 20px;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.game-message {
 | 
				
			||||||
 | 
					  margin-top: 20px;
 | 
				
			||||||
 | 
					  font-size: 18px;
 | 
				
			||||||
 | 
					  color: #333;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.game-over {
 | 
				
			||||||
 | 
					  font-size: 24px;
 | 
				
			||||||
 | 
					  color: red;
 | 
				
			||||||
 | 
					  margin-top: 10px;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										107
									
								
								frontend/src/App.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										107
									
								
								frontend/src/App.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,107 @@
 | 
				
			|||||||
 | 
					import React, { useState } from 'react';
 | 
				
			||||||
 | 
					import PlayerForm from './components/PlayerForm';
 | 
				
			||||||
 | 
					import PlacementBoard from './components/PlacementBoard';
 | 
				
			||||||
 | 
					import GameBoard from './components/GameBoard';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function App() {
 | 
				
			||||||
 | 
					  const [step, setStep] = useState(0);
 | 
				
			||||||
 | 
					  const [gameId, setGameId] = useState(null);
 | 
				
			||||||
 | 
					  const [player1Name, setPlayer1Name] = useState('');
 | 
				
			||||||
 | 
					  const [player2Name, setPlayer2Name] = useState('');
 | 
				
			||||||
 | 
					  const [player1Ships, setPlayer1Ships] = useState([]);
 | 
				
			||||||
 | 
					  const [player2Ships, setPlayer2Ships] = useState([]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handlePlayersSubmit = async (p1, p2) => {
 | 
				
			||||||
 | 
					    setPlayer1Name(p1);
 | 
				
			||||||
 | 
					    setPlayer2Name(p2);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      const response = await fetch('http://localhost:4000/api/new-game', {
 | 
				
			||||||
 | 
					        method: 'POST',
 | 
				
			||||||
 | 
					        headers: { 'Content-Type': 'application/json' },
 | 
				
			||||||
 | 
					        body: JSON.stringify({ player1: p1, player2: p2 })
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const data = await response.json();
 | 
				
			||||||
 | 
					      if (data.gameId) {
 | 
				
			||||||
 | 
					        setGameId(data.gameId);
 | 
				
			||||||
 | 
					        setStep(1);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        console.error('Game creation error:', data);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      console.error('Network error:', error);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handlePlacementDone = async (ships) => {
 | 
				
			||||||
 | 
					    if (!gameId) {
 | 
				
			||||||
 | 
					      console.error('Error: gameId is missing!');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      const response = await fetch('http://localhost:4000/api/place-ships', {
 | 
				
			||||||
 | 
					        method: 'POST',
 | 
				
			||||||
 | 
					        headers: { 'Content-Type': 'application/json' },
 | 
				
			||||||
 | 
					        body: JSON.stringify({ gameId, player: step, ships })
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      const data = await response.json();
 | 
				
			||||||
 | 
					      if (data.message) {
 | 
				
			||||||
 | 
					        console.log(`Ships of player ${step} saved on server.`);
 | 
				
			||||||
 | 
					        if (step === 1) {
 | 
				
			||||||
 | 
					          setPlayer1Ships(ships);
 | 
				
			||||||
 | 
					          setStep(2);
 | 
				
			||||||
 | 
					        } else if (step === 2) {
 | 
				
			||||||
 | 
					          setPlayer2Ships(ships);
 | 
				
			||||||
 | 
					          setStep(3);
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        console.error('Error saving ships:', data);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
					      console.error('Network error when saving ships:', error);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  // Функция для сброса состояния и начала новой игры
 | 
				
			||||||
 | 
					  const resetGame = () => {
 | 
				
			||||||
 | 
					    setStep(0);
 | 
				
			||||||
 | 
					    setGameId(null);
 | 
				
			||||||
 | 
					    setPlayer1Name('');
 | 
				
			||||||
 | 
					    setPlayer2Name('');
 | 
				
			||||||
 | 
					    setPlayer1Ships([]);
 | 
				
			||||||
 | 
					    setPlayer2Ships([]);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <div className="app-container">
 | 
				
			||||||
 | 
					      {step === 0 && <PlayerForm onSubmit={handlePlayersSubmit} />}
 | 
				
			||||||
 | 
					      {step === 1 && (
 | 
				
			||||||
 | 
					        <PlacementBoard
 | 
				
			||||||
 | 
					          playerName={player1Name}
 | 
				
			||||||
 | 
					          onPlacementDone={handlePlacementDone}
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					      )}
 | 
				
			||||||
 | 
					      {step === 2 && (
 | 
				
			||||||
 | 
					        <PlacementBoard
 | 
				
			||||||
 | 
					          playerName={player2Name}
 | 
				
			||||||
 | 
					          onPlacementDone={handlePlacementDone}
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					      )}
 | 
				
			||||||
 | 
					      {step === 3 && (
 | 
				
			||||||
 | 
					        <GameBoard
 | 
				
			||||||
 | 
					          gameId={gameId}
 | 
				
			||||||
 | 
					          player1Name={player1Name}
 | 
				
			||||||
 | 
					          player2Name={player2Name}
 | 
				
			||||||
 | 
					          player1Ships={player1Ships}
 | 
				
			||||||
 | 
					          player2Ships={player2Ships}
 | 
				
			||||||
 | 
					          onNewGame={resetGame} 
 | 
				
			||||||
 | 
					        />
 | 
				
			||||||
 | 
					      )}
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default App;
 | 
				
			||||||
							
								
								
									
										1
									
								
								frontend/src/assets/react.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										1
									
								
								frontend/src/assets/react.svg
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1 @@
 | 
				
			|||||||
 | 
					<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" aria-hidden="true" role="img" class="iconify iconify--logos" width="35.93" height="32" preserveAspectRatio="xMidYMid meet" viewBox="0 0 256 228"><path fill="#00D8FF" d="M210.483 73.824a171.49 171.49 0 0 0-8.24-2.597c.465-1.9.893-3.777 1.273-5.621c6.238-30.281 2.16-54.676-11.769-62.708c-13.355-7.7-35.196.329-57.254 19.526a171.23 171.23 0 0 0-6.375 5.848a155.866 155.866 0 0 0-4.241-3.917C100.759 3.829 77.587-4.822 63.673 3.233C50.33 10.957 46.379 33.89 51.995 62.588a170.974 170.974 0 0 0 1.892 8.48c-3.28.932-6.445 1.924-9.474 2.98C17.309 83.498 0 98.307 0 113.668c0 15.865 18.582 31.778 46.812 41.427a145.52 145.52 0 0 0 6.921 2.165a167.467 167.467 0 0 0-2.01 9.138c-5.354 28.2-1.173 50.591 12.134 58.266c13.744 7.926 36.812-.22 59.273-19.855a145.567 145.567 0 0 0 5.342-4.923a168.064 168.064 0 0 0 6.92 6.314c21.758 18.722 43.246 26.282 56.54 18.586c13.731-7.949 18.194-32.003 12.4-61.268a145.016 145.016 0 0 0-1.535-6.842c1.62-.48 3.21-.974 4.76-1.488c29.348-9.723 48.443-25.443 48.443-41.52c0-15.417-17.868-30.326-45.517-39.844Zm-6.365 70.984c-1.4.463-2.836.91-4.3 1.345c-3.24-10.257-7.612-21.163-12.963-32.432c5.106-11 9.31-21.767 12.459-31.957c2.619.758 5.16 1.557 7.61 2.4c23.69 8.156 38.14 20.213 38.14 29.504c0 9.896-15.606 22.743-40.946 31.14Zm-10.514 20.834c2.562 12.94 2.927 24.64 1.23 33.787c-1.524 8.219-4.59 13.698-8.382 15.893c-8.067 4.67-25.32-1.4-43.927-17.412a156.726 156.726 0 0 1-6.437-5.87c7.214-7.889 14.423-17.06 21.459-27.246c12.376-1.098 24.068-2.894 34.671-5.345a134.17 134.17 0 0 1 1.386 6.193ZM87.276 214.515c-7.882 2.783-14.16 2.863-17.955.675c-8.075-4.657-11.432-22.636-6.853-46.752a156.923 156.923 0 0 1 1.869-8.499c10.486 2.32 22.093 3.988 34.498 4.994c7.084 9.967 14.501 19.128 21.976 27.15a134.668 134.668 0 0 1-4.877 4.492c-9.933 8.682-19.886 14.842-28.658 17.94ZM50.35 144.747c-12.483-4.267-22.792-9.812-29.858-15.863c-6.35-5.437-9.555-10.836-9.555-15.216c0-9.322 13.897-21.212 37.076-29.293c2.813-.98 5.757-1.905 8.812-2.773c3.204 10.42 7.406 21.315 12.477 32.332c-5.137 11.18-9.399 22.249-12.634 32.792a134.718 134.718 0 0 1-6.318-1.979Zm12.378-84.26c-4.811-24.587-1.616-43.134 6.425-47.789c8.564-4.958 27.502 2.111 47.463 19.835a144.318 144.318 0 0 1 3.841 3.545c-7.438 7.987-14.787 17.08-21.808 26.988c-12.04 1.116-23.565 2.908-34.161 5.309a160.342 160.342 0 0 1-1.76-7.887Zm110.427 27.268a347.8 347.8 0 0 0-7.785-12.803c8.168 1.033 15.994 2.404 23.343 4.08c-2.206 7.072-4.956 14.465-8.193 22.045a381.151 381.151 0 0 0-7.365-13.322Zm-45.032-43.861c5.044 5.465 10.096 11.566 15.065 18.186a322.04 322.04 0 0 0-30.257-.006c4.974-6.559 10.069-12.652 15.192-18.18ZM82.802 87.83a323.167 323.167 0 0 0-7.227 13.238c-3.184-7.553-5.909-14.98-8.134-22.152c7.304-1.634 15.093-2.97 23.209-3.984a321.524 321.524 0 0 0-7.848 12.897Zm8.081 65.352c-8.385-.936-16.291-2.203-23.593-3.793c2.26-7.3 5.045-14.885 8.298-22.6a321.187 321.187 0 0 0 7.257 13.246c2.594 4.48 5.28 8.868 8.038 13.147Zm37.542 31.03c-5.184-5.592-10.354-11.779-15.403-18.433c4.902.192 9.899.29 14.978.29c5.218 0 10.376-.117 15.453-.343c-4.985 6.774-10.018 12.97-15.028 18.486Zm52.198-57.817c3.422 7.8 6.306 15.345 8.596 22.52c-7.422 1.694-15.436 3.058-23.88 4.071a382.417 382.417 0 0 0 7.859-13.026a347.403 347.403 0 0 0 7.425-13.565Zm-16.898 8.101a358.557 358.557 0 0 1-12.281 19.815a329.4 329.4 0 0 1-23.444.823c-7.967 0-15.716-.248-23.178-.732a310.202 310.202 0 0 1-12.513-19.846h.001a307.41 307.41 0 0 1-10.923-20.627a310.278 310.278 0 0 1 10.89-20.637l-.001.001a307.318 307.318 0 0 1 12.413-19.761c7.613-.576 15.42-.876 23.31-.876H128c7.926 0 15.743.303 23.354.883a329.357 329.357 0 0 1 12.335 19.695a358.489 358.489 0 0 1 11.036 20.54a329.472 329.472 0 0 1-11 20.722Zm22.56-122.124c8.572 4.944 11.906 24.881 6.52 51.026c-.344 1.668-.73 3.367-1.15 5.09c-10.622-2.452-22.155-4.275-34.23-5.408c-7.034-10.017-14.323-19.124-21.64-27.008a160.789 160.789 0 0 1 5.888-5.4c18.9-16.447 36.564-22.941 44.612-18.3ZM128 90.808c12.625 0 22.86 10.235 22.86 22.86s-10.235 22.86-22.86 22.86s-22.86-10.235-22.86-22.86s10.235-22.86 22.86-22.86Z"></path></svg>
 | 
				
			||||||
| 
		 After Width: | Height: | Size: 4.0 KiB  | 
							
								
								
									
										9
									
								
								frontend/src/components/Board.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								frontend/src/components/Board.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					.board {
 | 
				
			||||||
 | 
					    display: inline-block;
 | 
				
			||||||
 | 
					    border: 2px solid #333;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .board-row {
 | 
				
			||||||
 | 
					    display: flex;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
							
								
								
									
										46
									
								
								frontend/src/components/Board.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								frontend/src/components/Board.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,46 @@
 | 
				
			|||||||
 | 
					import React from 'react';
 | 
				
			||||||
 | 
					import Cell from './Cell';
 | 
				
			||||||
 | 
					import './Board.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function Board({
 | 
				
			||||||
 | 
					  boardSize,
 | 
				
			||||||
 | 
					  ships = [],
 | 
				
			||||||
 | 
					  hits = [],
 | 
				
			||||||
 | 
					  misses = [],
 | 
				
			||||||
 | 
					  sunkShips = [],
 | 
				
			||||||
 | 
					  onCellClick,
 | 
				
			||||||
 | 
					  hideShips = false
 | 
				
			||||||
 | 
					}) {
 | 
				
			||||||
 | 
					  const rows = Array.from({ length: boardSize }, (_, i) => i);
 | 
				
			||||||
 | 
					  const cols = Array.from({ length: boardSize }, (_, i) => i);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <div className="board">
 | 
				
			||||||
 | 
					      {rows.map((r) => (
 | 
				
			||||||
 | 
					        <div key={r} className="board-row">
 | 
				
			||||||
 | 
					          {cols.map((c) => {
 | 
				
			||||||
 | 
					            const hasShip = ships.some((s) => s.row === r && s.col === c);
 | 
				
			||||||
 | 
					            const isHit = hits.some((h) => h.row === r && h.col === c);
 | 
				
			||||||
 | 
					            const isMiss = misses.some((m) => m.row === r && m.col === c);
 | 
				
			||||||
 | 
					            const isSunk = sunkShips.some((s) => s.row === r && s.col === c);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return (
 | 
				
			||||||
 | 
					              <Cell
 | 
				
			||||||
 | 
					                key={`${r}-${c}`}
 | 
				
			||||||
 | 
					                row={r}
 | 
				
			||||||
 | 
					                col={c}
 | 
				
			||||||
 | 
					                hasShip={hasShip && !hideShips}
 | 
				
			||||||
 | 
					                isHit={isHit}
 | 
				
			||||||
 | 
					                isMiss={isMiss}
 | 
				
			||||||
 | 
					                isSunk={isSunk}
 | 
				
			||||||
 | 
					                onClick={onCellClick}
 | 
				
			||||||
 | 
					              />
 | 
				
			||||||
 | 
					            );
 | 
				
			||||||
 | 
					          })}
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					      ))}
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default Board;
 | 
				
			||||||
							
								
								
									
										24
									
								
								frontend/src/components/Cell.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								frontend/src/components/Cell.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					.cell {
 | 
				
			||||||
 | 
					    width: 30px;
 | 
				
			||||||
 | 
					    height: 30px;
 | 
				
			||||||
 | 
					    border: 1px solid black;
 | 
				
			||||||
 | 
					    display: inline-block;
 | 
				
			||||||
 | 
					    background-color: white;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .cell.hit {
 | 
				
			||||||
 | 
					    background-color: red !important;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .cell.miss {
 | 
				
			||||||
 | 
					    background-color: green !important;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .cell.sunk {
 | 
				
			||||||
 | 
					    background-color: black !important;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .cell.has-ship {
 | 
				
			||||||
 | 
					    background-color: blue; 
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
							
								
								
									
										24
									
								
								frontend/src/components/Cell.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										24
									
								
								frontend/src/components/Cell.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,24 @@
 | 
				
			|||||||
 | 
					import React from 'react';
 | 
				
			||||||
 | 
					import './Cell.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function Cell({ row, col, hasShip, isHit, isMiss, isSunk, onClick }) {
 | 
				
			||||||
 | 
					  let className = 'cell';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  if (isSunk) {
 | 
				
			||||||
 | 
					    className += ' sunk';
 | 
				
			||||||
 | 
					  } else if (isHit) {
 | 
				
			||||||
 | 
					    className += ' hit';
 | 
				
			||||||
 | 
					  } else if (isMiss) {
 | 
				
			||||||
 | 
					    className += ' miss';
 | 
				
			||||||
 | 
					  } else if (hasShip) {
 | 
				
			||||||
 | 
					    className += ' has-ship';
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleClick = () => {
 | 
				
			||||||
 | 
					    if (onClick) onClick(row, col);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return <div className={className} onClick={handleClick} />;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default Cell;
 | 
				
			||||||
							
								
								
									
										66
									
								
								frontend/src/components/GameBoard.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										66
									
								
								frontend/src/components/GameBoard.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,66 @@
 | 
				
			|||||||
 | 
					.game-board {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					  margin: 2rem auto;
 | 
				
			||||||
 | 
					  max-width: 1000px;
 | 
				
			||||||
 | 
					  padding: 2rem;
 | 
				
			||||||
 | 
					  background-color: #ffffff;
 | 
				
			||||||
 | 
					  border-radius: 12px;
 | 
				
			||||||
 | 
					  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
 | 
				
			||||||
 | 
					  font-family: 'Arial', sans-serif;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.turn-title {
 | 
				
			||||||
 | 
					  margin-bottom: 1.5rem;
 | 
				
			||||||
 | 
					  font-size: 2rem;
 | 
				
			||||||
 | 
					  color: #333;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.winner-section {
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  margin-bottom: 1.5rem;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.winner-section h2 {
 | 
				
			||||||
 | 
					  font-size: 2rem;
 | 
				
			||||||
 | 
					  color: #28a745;
 | 
				
			||||||
 | 
					  margin-bottom: 1rem;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.new-game-button {
 | 
				
			||||||
 | 
					  padding: 1rem 2rem;
 | 
				
			||||||
 | 
					  font-size: 1.3rem;
 | 
				
			||||||
 | 
					  border: none;
 | 
				
			||||||
 | 
					  border-radius: 8px;
 | 
				
			||||||
 | 
					  background-color: #007bff;
 | 
				
			||||||
 | 
					  color: #fff;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  transition: background-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.new-game-button:hover {
 | 
				
			||||||
 | 
					  background-color: #0056b3;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.boards-container {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-wrap: wrap;
 | 
				
			||||||
 | 
					  gap: 2rem;
 | 
				
			||||||
 | 
					  justify-content: center;
 | 
				
			||||||
 | 
					  width: 100%;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.board-wrapper {
 | 
				
			||||||
 | 
					  flex: 1;
 | 
				
			||||||
 | 
					  min-width: 300px;
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.board-wrapper h3 {
 | 
				
			||||||
 | 
					  margin-bottom: 1rem;
 | 
				
			||||||
 | 
					  font-size: 1.5rem;
 | 
				
			||||||
 | 
					  color: #007bff;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										174
									
								
								frontend/src/components/GameBoard.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										174
									
								
								frontend/src/components/GameBoard.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,174 @@
 | 
				
			|||||||
 | 
					import React, { useState, useEffect } from 'react';
 | 
				
			||||||
 | 
					import Board from './Board';
 | 
				
			||||||
 | 
					import './GameBoard.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function checkSunkShip(ships, hits) {
 | 
				
			||||||
 | 
					  const sunkShipsArrays = ships.filter((ship) =>
 | 
				
			||||||
 | 
					    ship.every((cell) =>
 | 
				
			||||||
 | 
					      hits.some((hit) => hit.row === cell.row && hit.col === cell.col)
 | 
				
			||||||
 | 
					    )
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  sunkShipsArrays.forEach((ship) => {
 | 
				
			||||||
 | 
					    const size = ship.length;
 | 
				
			||||||
 | 
					    if (size === 4) {
 | 
				
			||||||
 | 
					      console.log('Battleship (4 cells) is sunk!');
 | 
				
			||||||
 | 
					    } else if (size === 3) {
 | 
				
			||||||
 | 
					      console.log('Cruiser (3 cells) is sunk!');
 | 
				
			||||||
 | 
					    } else if (size === 2) {
 | 
				
			||||||
 | 
					      console.log('Destroyer (2 cells) is sunk!');
 | 
				
			||||||
 | 
					    } else if (size === 1) {
 | 
				
			||||||
 | 
					      console.log('Submarine (1 cell) is sunk!');
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  });
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return sunkShipsArrays.flat();
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function GameBoard({ gameId, player1Name, player2Name, player1Ships, player2Ships, onNewGame }) {
 | 
				
			||||||
 | 
					  const [currentPlayer, setCurrentPlayer] = useState(1);
 | 
				
			||||||
 | 
					  const [hitsP1, setHitsP1] = useState([]);
 | 
				
			||||||
 | 
					  const [missesP1, setMissesP1] = useState([]);
 | 
				
			||||||
 | 
					  const [hitsP2, setHitsP2] = useState([]);
 | 
				
			||||||
 | 
					  const [missesP2, setMissesP2] = useState([]);
 | 
				
			||||||
 | 
					  const [sunkShipsP1, setSunkShipsP1] = useState([]);
 | 
				
			||||||
 | 
					  const [sunkShipsP2, setSunkShipsP2] = useState([]);
 | 
				
			||||||
 | 
					  const [winner, setWinner] = useState(null);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    console.log('player1Ships:', JSON.stringify(player1Ships, null, 2));
 | 
				
			||||||
 | 
					    console.log('player2Ships:', JSON.stringify(player2Ships, null, 2));
 | 
				
			||||||
 | 
					  }, []);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const saveGameResult = async (winnerName, loserName) => {
 | 
				
			||||||
 | 
					    try {
 | 
				
			||||||
 | 
					      const resp = await fetch('http://localhost:4000/api/save-result', {
 | 
				
			||||||
 | 
					        method: 'POST',
 | 
				
			||||||
 | 
					        headers: { 'Content-Type': 'application/json' },
 | 
				
			||||||
 | 
					        body: JSON.stringify({
 | 
				
			||||||
 | 
					          gameId,
 | 
				
			||||||
 | 
					          winner: winnerName,
 | 
				
			||||||
 | 
					          loser: loserName,
 | 
				
			||||||
 | 
					          moves: hitsP1.length + hitsP2.length
 | 
				
			||||||
 | 
					        })
 | 
				
			||||||
 | 
					      });
 | 
				
			||||||
 | 
					      const data = await resp.json();
 | 
				
			||||||
 | 
					      console.log('Game result saved:', data);
 | 
				
			||||||
 | 
					    } catch (err) {
 | 
				
			||||||
 | 
					      console.error('Error saving game result:', err);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    const totalCellsP1 = player1Ships.flat().length;
 | 
				
			||||||
 | 
					    const totalCellsP2 = player2Ships.flat().length;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (hitsP1.length === totalCellsP2) {
 | 
				
			||||||
 | 
					      setWinner(player1Name);
 | 
				
			||||||
 | 
					      saveGameResult(player1Name, player2Name);
 | 
				
			||||||
 | 
					    } else if (hitsP2.length === totalCellsP1) {
 | 
				
			||||||
 | 
					      setWinner(player2Name);
 | 
				
			||||||
 | 
					      saveGameResult(player2Name, player1Name);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  }, [hitsP1, hitsP2, player1Ships, player2Ships, player1Name, player2Name]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  useEffect(() => {
 | 
				
			||||||
 | 
					    const sunkForP1 = checkSunkShip(player1Ships, hitsP2);
 | 
				
			||||||
 | 
					    const sunkForP2 = checkSunkShip(player2Ships, hitsP1);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    setSunkShipsP1(sunkForP1);
 | 
				
			||||||
 | 
					    setSunkShipsP2(sunkForP2);
 | 
				
			||||||
 | 
					  }, [hitsP1, hitsP2, player1Ships, player2Ships]);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleAttack = (row, col, opponent) => {
 | 
				
			||||||
 | 
					    if (winner) return;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let alreadyAttacked;
 | 
				
			||||||
 | 
					    if (opponent === 2) {
 | 
				
			||||||
 | 
					      alreadyAttacked = [...hitsP1, ...missesP1].some(
 | 
				
			||||||
 | 
					        (cell) => cell.row === row && cell.col === col
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      alreadyAttacked = [...hitsP2, ...missesP2].some(
 | 
				
			||||||
 | 
					        (cell) => cell.row === row && cell.col === col
 | 
				
			||||||
 | 
					      );
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (alreadyAttacked) {
 | 
				
			||||||
 | 
					      alert('You have already attacked this cell!');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    let isHit = false;
 | 
				
			||||||
 | 
					    if (opponent === 2) {
 | 
				
			||||||
 | 
					      isHit = player2Ships.flat().some((cell) => cell.row === row && cell.col === col);
 | 
				
			||||||
 | 
					      if (isHit) {
 | 
				
			||||||
 | 
					        setHitsP1([...hitsP1, { row, col }]); 
 | 
				
			||||||
 | 
					        console.log(`Player1 hit a ship at [${row},${col}] on Player2's board`);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        setMissesP1([...missesP1, { row, col }]);
 | 
				
			||||||
 | 
					        console.log(`Player1 missed at [${row},${col}] on Player2's board`);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    } else {
 | 
				
			||||||
 | 
					      isHit = player1Ships.flat().some((cell) => cell.row === row && cell.col === col);
 | 
				
			||||||
 | 
					      if (isHit) {
 | 
				
			||||||
 | 
					        setHitsP2([...hitsP2, { row, col }]);
 | 
				
			||||||
 | 
					        console.log(`Player2 hit a ship at [${row},${col}] on Player1's board`);
 | 
				
			||||||
 | 
					      } else {
 | 
				
			||||||
 | 
					        setMissesP2([...missesP2, { row, col }]);
 | 
				
			||||||
 | 
					        console.log(`Player2 missed at [${row},${col}] on Player1's board`);
 | 
				
			||||||
 | 
					      }
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (!isHit) {
 | 
				
			||||||
 | 
					      setCurrentPlayer(currentPlayer === 1 ? 2 : 1);
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <div className="game-board">
 | 
				
			||||||
 | 
					      {winner ? (
 | 
				
			||||||
 | 
					        <div className="winner-section">
 | 
				
			||||||
 | 
					          <h2>Winner: {winner}!</h2>
 | 
				
			||||||
 | 
					          <button className="new-game-button" onClick={onNewGame}>Start New Game</button>
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					      ) : (
 | 
				
			||||||
 | 
					        <h2 className="turn-title">Current turn: {currentPlayer === 1 ? player1Name : player2Name}</h2>
 | 
				
			||||||
 | 
					      )}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      <div className="boards-container">
 | 
				
			||||||
 | 
					        <div className="board-wrapper">
 | 
				
			||||||
 | 
					          <h3>{player1Name}'s Board</h3>
 | 
				
			||||||
 | 
					          <Board
 | 
				
			||||||
 | 
					            boardSize={10}
 | 
				
			||||||
 | 
					            ships={player1Ships.flat()}
 | 
				
			||||||
 | 
					            hits={hitsP2}
 | 
				
			||||||
 | 
					            misses={missesP2}
 | 
				
			||||||
 | 
					            sunkShips={sunkShipsP1}
 | 
				
			||||||
 | 
					            onCellClick={
 | 
				
			||||||
 | 
					              currentPlayer === 2 ? (r, c) => handleAttack(r, c, 1) : null
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            hideShips={true}
 | 
				
			||||||
 | 
					          />
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        <div className="board-wrapper">
 | 
				
			||||||
 | 
					          <h3>{player2Name}'s Board</h3>
 | 
				
			||||||
 | 
					          <Board
 | 
				
			||||||
 | 
					            boardSize={10}
 | 
				
			||||||
 | 
					            ships={player2Ships.flat()}
 | 
				
			||||||
 | 
					            hits={hitsP1}
 | 
				
			||||||
 | 
					            misses={missesP1}
 | 
				
			||||||
 | 
					            sunkShips={sunkShipsP2}
 | 
				
			||||||
 | 
					            onCellClick={
 | 
				
			||||||
 | 
					              currentPlayer === 1 ? (r, c) => handleAttack(r, c, 2) : null
 | 
				
			||||||
 | 
					            }
 | 
				
			||||||
 | 
					            hideShips={true}
 | 
				
			||||||
 | 
					          />
 | 
				
			||||||
 | 
					        </div>
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default GameBoard;
 | 
				
			||||||
							
								
								
									
										12
									
								
								frontend/src/components/Header.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								frontend/src/components/Header.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					.header {
 | 
				
			||||||
 | 
					    background-color: #0077cc;
 | 
				
			||||||
 | 
					    color: white;
 | 
				
			||||||
 | 
					    padding: 20px 0;
 | 
				
			||||||
 | 
					    margin-bottom: 20px;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					  .header h1 {
 | 
				
			||||||
 | 
					    margin: 0;
 | 
				
			||||||
 | 
					    font-size: 2.5em;
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
							
								
								
									
										12
									
								
								frontend/src/components/Header.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										12
									
								
								frontend/src/components/Header.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,12 @@
 | 
				
			|||||||
 | 
					import React from 'react';
 | 
				
			||||||
 | 
					import './Header.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const Header = ({ title }) => {
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <header className="header">
 | 
				
			||||||
 | 
					      <h1>{title}</h1>
 | 
				
			||||||
 | 
					    </header>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default Header;
 | 
				
			||||||
							
								
								
									
										52
									
								
								frontend/src/components/PlacementBoard.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								frontend/src/components/PlacementBoard.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,52 @@
 | 
				
			|||||||
 | 
					.placement-board {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					  margin: 2rem auto;
 | 
				
			||||||
 | 
					  max-width: 800px;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  background: #ffffff;
 | 
				
			||||||
 | 
					  padding: 2rem;
 | 
				
			||||||
 | 
					  border-radius: 12px;
 | 
				
			||||||
 | 
					  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.placement-board h2 {
 | 
				
			||||||
 | 
					  margin-bottom: 1.5rem;
 | 
				
			||||||
 | 
					  font-size: 2rem;
 | 
				
			||||||
 | 
					  color: #333;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.ship-label {
 | 
				
			||||||
 | 
					  font-size: 1.2rem;
 | 
				
			||||||
 | 
					  margin-bottom: 0.5rem;
 | 
				
			||||||
 | 
					  color: #555;
 | 
				
			||||||
 | 
					  display: block;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.ship-select {
 | 
				
			||||||
 | 
					  font-size: 1.2rem;
 | 
				
			||||||
 | 
					  padding: 0.6rem;
 | 
				
			||||||
 | 
					  margin-bottom: 1.5rem;
 | 
				
			||||||
 | 
					  border: 1px solid #ccc;
 | 
				
			||||||
 | 
					  border-radius: 6px;
 | 
				
			||||||
 | 
					  width: 100%;
 | 
				
			||||||
 | 
					  max-width: 300px;
 | 
				
			||||||
 | 
					  background-color: #f9f9f9;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.finish-button {
 | 
				
			||||||
 | 
					  margin-top: 1.5rem;
 | 
				
			||||||
 | 
					  padding: 0.8rem 1.5rem;
 | 
				
			||||||
 | 
					  font-size: 1.2rem;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  background-color: #28a745;
 | 
				
			||||||
 | 
					  color: #fff;
 | 
				
			||||||
 | 
					  border: none;
 | 
				
			||||||
 | 
					  border-radius: 6px;
 | 
				
			||||||
 | 
					  transition: background-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.finish-button:hover {
 | 
				
			||||||
 | 
					  background-color: #218838;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										90
									
								
								frontend/src/components/PlacementBoard.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										90
									
								
								frontend/src/components/PlacementBoard.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,90 @@
 | 
				
			|||||||
 | 
					import React, { useState } from 'react';
 | 
				
			||||||
 | 
					import Board from './Board';
 | 
				
			||||||
 | 
					import './PlacementBoard.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					const SHIPS_LIMIT = {
 | 
				
			||||||
 | 
					  'Battleship (4 cells)': { size: 4, max: 1 },
 | 
				
			||||||
 | 
					  'Cruiser (3 cells)': { size: 3, max: 2 },
 | 
				
			||||||
 | 
					  'Destroyer (2 cells)': { size: 2, max: 3 },
 | 
				
			||||||
 | 
					  'Submarine (1 cell)': { size: 1, max: 4 }
 | 
				
			||||||
 | 
					};
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function PlacementBoard({ playerName, onPlacementDone }) {
 | 
				
			||||||
 | 
					  const [ships, setShips] = useState([]);
 | 
				
			||||||
 | 
					  const [selectedShipType, setSelectedShipType] = useState('Battleship (4 cells)');
 | 
				
			||||||
 | 
					  const [shipCounts, setShipCounts] = useState({});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleCellClick = (row, col) => {
 | 
				
			||||||
 | 
					    const shipData = SHIPS_LIMIT[selectedShipType];
 | 
				
			||||||
 | 
					    if (!shipData) {
 | 
				
			||||||
 | 
					      alert('Invalid ship type!');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const currentCount = shipCounts[selectedShipType] || 0;
 | 
				
			||||||
 | 
					    if (currentCount >= shipData.max) {
 | 
				
			||||||
 | 
					      alert(`You can only place ${shipData.max} ${selectedShipType}!`);
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    if (col + shipData.size > 10) {
 | 
				
			||||||
 | 
					      alert('Ship is out of bounds!');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    const newShip = [];
 | 
				
			||||||
 | 
					    for (let i = 0; i < shipData.size; i++) {
 | 
				
			||||||
 | 
					      newShip.push({ row, col: col + i });
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    setShips([...ships, newShip]);
 | 
				
			||||||
 | 
					    setShipCounts({
 | 
				
			||||||
 | 
					      ...shipCounts,
 | 
				
			||||||
 | 
					      [selectedShipType]: currentCount + 1
 | 
				
			||||||
 | 
					    });
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleDone = () => {
 | 
				
			||||||
 | 
					    if (
 | 
				
			||||||
 | 
					      Object.keys(SHIPS_LIMIT).some(
 | 
				
			||||||
 | 
					        (type) => (shipCounts[type] || 0) < SHIPS_LIMIT[type].max
 | 
				
			||||||
 | 
					      )
 | 
				
			||||||
 | 
					    ) {
 | 
				
			||||||
 | 
					      alert('You must place all required ships!');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    onPlacementDone(ships);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <div className="placement-board">
 | 
				
			||||||
 | 
					      <h2>Ship Placement: {playerName}</h2>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      <label className="ship-label">Select ship type:</label>
 | 
				
			||||||
 | 
					      <select
 | 
				
			||||||
 | 
					        className="ship-select"
 | 
				
			||||||
 | 
					        onChange={(e) => setSelectedShipType(e.target.value)}
 | 
				
			||||||
 | 
					        value={selectedShipType}
 | 
				
			||||||
 | 
					      >
 | 
				
			||||||
 | 
					        {Object.keys(SHIPS_LIMIT).map((ship, index) => (
 | 
				
			||||||
 | 
					          <option key={index} value={ship}>
 | 
				
			||||||
 | 
					            {ship} ({shipCounts[ship] || 0}/{SHIPS_LIMIT[ship].max})
 | 
				
			||||||
 | 
					          </option>
 | 
				
			||||||
 | 
					        ))}
 | 
				
			||||||
 | 
					      </select>
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      <Board
 | 
				
			||||||
 | 
					        boardSize={10}
 | 
				
			||||||
 | 
					        ships={ships.flat()}
 | 
				
			||||||
 | 
					        onCellClick={handleCellClick}
 | 
				
			||||||
 | 
					        hideShips={false}
 | 
				
			||||||
 | 
					      />
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					      <button className="finish-button" onClick={handleDone}>
 | 
				
			||||||
 | 
					        Finish Placement
 | 
				
			||||||
 | 
					      </button>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default PlacementBoard;
 | 
				
			||||||
							
								
								
									
										123
									
								
								frontend/src/components/PlayerForm.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										123
									
								
								frontend/src/components/PlayerForm.css
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,123 @@
 | 
				
			|||||||
 | 
					.player-form-container {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  align-items: center;
 | 
				
			||||||
 | 
					  padding: 2rem;
 | 
				
			||||||
 | 
					  background: #f7f9fc;
 | 
				
			||||||
 | 
					  min-height: 100vh;
 | 
				
			||||||
 | 
					  font-family: 'Arial', sans-serif;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.player-form-card,
 | 
				
			||||||
 | 
					.results-card {
 | 
				
			||||||
 | 
					  background: #ffffff;
 | 
				
			||||||
 | 
					  border-radius: 8px;
 | 
				
			||||||
 | 
					  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
 | 
				
			||||||
 | 
					  width: 400px;
 | 
				
			||||||
 | 
					  margin-bottom: 2rem;
 | 
				
			||||||
 | 
					  padding: 2rem;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.form-title {
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  margin-bottom: 1.5rem;
 | 
				
			||||||
 | 
					  font-size: 1.8rem;
 | 
				
			||||||
 | 
					  color: #333;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.player-form {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.form-group {
 | 
				
			||||||
 | 
					  display: flex;
 | 
				
			||||||
 | 
					  flex-direction: column;
 | 
				
			||||||
 | 
					  margin-bottom: 1.2rem;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.form-group label {
 | 
				
			||||||
 | 
					  margin-bottom: 0.5rem;
 | 
				
			||||||
 | 
					  font-size: 1.1rem;
 | 
				
			||||||
 | 
					  color: #555;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.form-group input {
 | 
				
			||||||
 | 
					  padding: 0.8rem;
 | 
				
			||||||
 | 
					  font-size: 1.1rem;
 | 
				
			||||||
 | 
					  border: 1px solid #ccc;
 | 
				
			||||||
 | 
					  border-radius: 4px;
 | 
				
			||||||
 | 
					  transition: border-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.form-group input:focus {
 | 
				
			||||||
 | 
					  border-color: #007bff;
 | 
				
			||||||
 | 
					  outline: none;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.submit-button {
 | 
				
			||||||
 | 
					  padding: 0.8rem;
 | 
				
			||||||
 | 
					  font-size: 1.2rem;
 | 
				
			||||||
 | 
					  background-color: #007bff;
 | 
				
			||||||
 | 
					  color: #fff;
 | 
				
			||||||
 | 
					  border: none;
 | 
				
			||||||
 | 
					  border-radius: 4px;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  transition: background-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.submit-button:hover {
 | 
				
			||||||
 | 
					  background-color: #0056b3;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.results-section {
 | 
				
			||||||
 | 
					  width: 400px;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.toggle-results-button {
 | 
				
			||||||
 | 
					  padding: 0.8rem;
 | 
				
			||||||
 | 
					  font-size: 1.2rem;
 | 
				
			||||||
 | 
					  background-color: #28a745;
 | 
				
			||||||
 | 
					  color: #fff;
 | 
				
			||||||
 | 
					  border: none;
 | 
				
			||||||
 | 
					  border-radius: 4px;
 | 
				
			||||||
 | 
					  cursor: pointer;
 | 
				
			||||||
 | 
					  margin-bottom: 1rem;
 | 
				
			||||||
 | 
					  transition: background-color 0.3s;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.toggle-results-button:hover {
 | 
				
			||||||
 | 
					  background-color: #218838;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.results-title {
 | 
				
			||||||
 | 
					  font-size: 1.6rem;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  margin-bottom: 1rem;
 | 
				
			||||||
 | 
					  color: #333;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.results-table {
 | 
				
			||||||
 | 
					  width: 100%;
 | 
				
			||||||
 | 
					  border-collapse: collapse;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.results-table th,
 | 
				
			||||||
 | 
					.results-table td {
 | 
				
			||||||
 | 
					  border: 1px solid #ddd;
 | 
				
			||||||
 | 
					  padding: 0.8rem;
 | 
				
			||||||
 | 
					  font-size: 1.1rem;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.results-table th {
 | 
				
			||||||
 | 
					  background-color: #007bff;
 | 
				
			||||||
 | 
					  color: #fff;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					.no-results {
 | 
				
			||||||
 | 
					  font-size: 1.1rem;
 | 
				
			||||||
 | 
					  text-align: center;
 | 
				
			||||||
 | 
					  color: #777;
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
							
								
								
									
										104
									
								
								frontend/src/components/PlayerForm.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										104
									
								
								frontend/src/components/PlayerForm.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,104 @@
 | 
				
			|||||||
 | 
					import React, { useState } from 'react';
 | 
				
			||||||
 | 
					import './PlayerForm.css';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					function PlayerForm({ onSubmit }) {
 | 
				
			||||||
 | 
					  const [p1, setP1] = useState('');
 | 
				
			||||||
 | 
					  const [p2, setP2] = useState('');
 | 
				
			||||||
 | 
					  const [results, setResults] = useState([]);
 | 
				
			||||||
 | 
					  const [showResults, setShowResults] = useState(false);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleSubmit = (e) => {
 | 
				
			||||||
 | 
					    e.preventDefault();
 | 
				
			||||||
 | 
					    if (p1.trim() === '' || p2.trim() === '') {
 | 
				
			||||||
 | 
					      alert('Please enter both player names!');
 | 
				
			||||||
 | 
					      console.log('Error: Missing player names');
 | 
				
			||||||
 | 
					      return;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    console.log('Starting new game with players:', p1, p2);
 | 
				
			||||||
 | 
					    onSubmit(p1, p2);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const fetchResults = () => {
 | 
				
			||||||
 | 
					    console.log('Fetching previous game results...');
 | 
				
			||||||
 | 
					    fetch('http://localhost:4000/api/results')
 | 
				
			||||||
 | 
					      .then((resp) => resp.json())
 | 
				
			||||||
 | 
					      .then((data) => {
 | 
				
			||||||
 | 
					        console.log('Fetched results:', data.results);
 | 
				
			||||||
 | 
					        setResults(data.results);
 | 
				
			||||||
 | 
					      })
 | 
				
			||||||
 | 
					      .catch((err) => console.error('Error fetching results:', err));
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  const handleToggleResults = () => {
 | 
				
			||||||
 | 
					    if (!showResults) {
 | 
				
			||||||
 | 
					      fetchResults();
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					    setShowResults(!showResults);
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					  return (
 | 
				
			||||||
 | 
					    <div className="player-form-container">
 | 
				
			||||||
 | 
					      <div className="player-form-card">
 | 
				
			||||||
 | 
					        <h2 className="form-title">Enter Player Names</h2>
 | 
				
			||||||
 | 
					        <form onSubmit={handleSubmit} className="player-form">
 | 
				
			||||||
 | 
					          <div className="form-group">
 | 
				
			||||||
 | 
					            <label>Player 1:</label>
 | 
				
			||||||
 | 
					            <input
 | 
				
			||||||
 | 
					              type="text"
 | 
				
			||||||
 | 
					              value={p1}
 | 
				
			||||||
 | 
					              onChange={(e) => setP1(e.target.value)}
 | 
				
			||||||
 | 
					              placeholder="Enter Player 1 name"
 | 
				
			||||||
 | 
					              required
 | 
				
			||||||
 | 
					            />
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					          <div className="form-group">
 | 
				
			||||||
 | 
					            <label>Player 2:</label>
 | 
				
			||||||
 | 
					            <input
 | 
				
			||||||
 | 
					              type="text"
 | 
				
			||||||
 | 
					              value={p2}
 | 
				
			||||||
 | 
					              onChange={(e) => setP2(e.target.value)}
 | 
				
			||||||
 | 
					              placeholder="Enter Player 2 name"
 | 
				
			||||||
 | 
					              required
 | 
				
			||||||
 | 
					            />
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					          <button type="submit" className="submit-button">Start Game</button>
 | 
				
			||||||
 | 
					        </form>
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					      
 | 
				
			||||||
 | 
					      <div className="results-section">
 | 
				
			||||||
 | 
					        <button className="toggle-results-button" onClick={handleToggleResults}>
 | 
				
			||||||
 | 
					          {showResults ? 'Hide Results' : 'Show Results'}
 | 
				
			||||||
 | 
					        </button>
 | 
				
			||||||
 | 
					        {showResults && (
 | 
				
			||||||
 | 
					          <div className="results-card">
 | 
				
			||||||
 | 
					            <h3 className="results-title">Previous Game Results</h3>
 | 
				
			||||||
 | 
					            {results.length > 0 ? (
 | 
				
			||||||
 | 
					              <table className="results-table">
 | 
				
			||||||
 | 
					                <thead>
 | 
				
			||||||
 | 
					                  <tr>
 | 
				
			||||||
 | 
					                    <th>Winner</th>
 | 
				
			||||||
 | 
					                    <th>Loser</th>
 | 
				
			||||||
 | 
					                    <th>Shots</th>
 | 
				
			||||||
 | 
					                  </tr>
 | 
				
			||||||
 | 
					                </thead>
 | 
				
			||||||
 | 
					                <tbody>
 | 
				
			||||||
 | 
					                  {results.map((result, index) => (
 | 
				
			||||||
 | 
					                    <tr key={index}>
 | 
				
			||||||
 | 
					                      <td>{result.winner}</td>
 | 
				
			||||||
 | 
					                      <td>{result.loser}</td>
 | 
				
			||||||
 | 
					                      <td>{result.moves}</td>
 | 
				
			||||||
 | 
					                    </tr>
 | 
				
			||||||
 | 
					                  ))}
 | 
				
			||||||
 | 
					                </tbody>
 | 
				
			||||||
 | 
					              </table>
 | 
				
			||||||
 | 
					            ) : (
 | 
				
			||||||
 | 
					              <p className="no-results">No previous results available.</p>
 | 
				
			||||||
 | 
					            )}
 | 
				
			||||||
 | 
					          </div>
 | 
				
			||||||
 | 
					        )}
 | 
				
			||||||
 | 
					      </div>
 | 
				
			||||||
 | 
					    </div>
 | 
				
			||||||
 | 
					  );
 | 
				
			||||||
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					export default PlayerForm;
 | 
				
			||||||
							
								
								
									
										0
									
								
								frontend/src/index.css
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										0
									
								
								frontend/src/index.css
									
									
									
									
									
										Normal file
									
								
							
							
								
								
									
										10
									
								
								frontend/src/main.jsx
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								frontend/src/main.jsx
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					import { StrictMode } from 'react'
 | 
				
			||||||
 | 
					import { createRoot } from 'react-dom/client'
 | 
				
			||||||
 | 
					import './index.css'
 | 
				
			||||||
 | 
					import App from './App.jsx'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					createRoot(document.getElementById('root')).render(
 | 
				
			||||||
 | 
					  <StrictMode>
 | 
				
			||||||
 | 
					    <App />
 | 
				
			||||||
 | 
					  </StrictMode>,
 | 
				
			||||||
 | 
					)
 | 
				
			||||||
							
								
								
									
										7
									
								
								frontend/vite.config.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										7
									
								
								frontend/vite.config.js
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,7 @@
 | 
				
			|||||||
 | 
					import { defineConfig } from 'vite'
 | 
				
			||||||
 | 
					import react from '@vitejs/plugin-react'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// https://vite.dev/config/
 | 
				
			||||||
 | 
					export default defineConfig({
 | 
				
			||||||
 | 
					  plugins: [react()],
 | 
				
			||||||
 | 
					})
 | 
				
			||||||
							
								
								
									
										34
									
								
								prepare-app.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										34
									
								
								prepare-app.sh
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,34 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Preparing app..."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					docker network inspect app-net >/dev/null 2>&1 || docker network create app-net
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if ! docker image inspect mysql:8.0.30 >/dev/null 2>&1; then
 | 
				
			||||||
 | 
					    echo "MySQL image not found locally. Pulling image..."
 | 
				
			||||||
 | 
					    docker pull mysql:8.0.30
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					MYSQL_CONTAINER=$(docker ps -aq -f name=battleship-mysql)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					if [ -n "$MYSQL_CONTAINER" ]; then
 | 
				
			||||||
 | 
					  
 | 
				
			||||||
 | 
					    if [ "$(docker ps -q -f name=battleship-mysql)" ]; then
 | 
				
			||||||
 | 
					        echo "MySQL container is already running."
 | 
				
			||||||
 | 
					    else
 | 
				
			||||||
 | 
					        echo "Starting existing MySQL container..."
 | 
				
			||||||
 | 
					        docker start battleship-mysql
 | 
				
			||||||
 | 
					        docker network connect app-net battleship-mysql || true
 | 
				
			||||||
 | 
					    fi
 | 
				
			||||||
 | 
					else
 | 
				
			||||||
 | 
					    echo "Creating and starting MySQL container..."
 | 
				
			||||||
 | 
					    docker run --network app-net --name battleship-mysql \
 | 
				
			||||||
 | 
					      -e MYSQL_ROOT_PASSWORD=somepassword \
 | 
				
			||||||
 | 
					      -e MYSQL_DATABASE=battleship \
 | 
				
			||||||
 | 
					      -p 3306:3306 \
 | 
				
			||||||
 | 
					      -d mysql:8.0.30
 | 
				
			||||||
 | 
					fi
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					docker-compose build
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Preparation done."
 | 
				
			||||||
							
								
								
									
										10
									
								
								remove-app.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										10
									
								
								remove-app.sh
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,10 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Removing app..."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					docker-compose down -v --rmi local
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					# Если нужно удалить базу данных, можно удалить контейнер MySQL отдельно:
 | 
				
			||||||
 | 
					docker rm -f battleship-mysql
 | 
				
			||||||
 | 
					docker volume rm db_data
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "App removed completely."
 | 
				
			||||||
							
								
								
									
										9
									
								
								start-app.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								start-app.sh
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Starting app..."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					docker-compose up -d
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "App is running."
 | 
				
			||||||
 | 
					echo "Frontend is available at: http://localhost:3001"
 | 
				
			||||||
 | 
					echo "Backend is available at: http://localhost:4000"
 | 
				
			||||||
 | 
					docker-compose ps
 | 
				
			||||||
							
								
								
									
										6
									
								
								stop-app.sh
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										6
									
								
								stop-app.sh
									
									
									
									
									
										Normal file
									
								
							@ -0,0 +1,6 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
 | 
					echo "Stopping app..."
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					docker-compose stop
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					echo "App stopped. You can run ./start-app.sh to resume."
 | 
				
			||||||
		Loading…
	
		Reference in New Issue
	
	Block a user