diff --git a/package.json b/package.json index 5b61813..f21493f 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "cookie-app", "private": true, - "version": "1.0.0", + "version": "1.0.1", "type": "module", "description": "Cookie...", "author": "Sebastian Jn ", diff --git a/socket/package.json b/socket/package.json index ea1dd0b..5c803e1 100644 --- a/socket/package.json +++ b/socket/package.json @@ -7,6 +7,7 @@ "license": "Apache-2.0", "author": "Sebastian Jn ", "dependencies": { + "chalk": "^5.4.1", "socket.io": "^4.8.1" }, "engines": { @@ -17,5 +18,9 @@ "type": "git", "url": "https://github.com/sebastianjnuwu/cookie.git" }, - "keywords": ["cookie-clicker", "socket", "real-time"] + "keywords": [ + "cookie-clicker", + "socket", + "real-time" + ] } diff --git a/socket/src/main.js b/socket/src/main.js index a092077..566dceb 100644 --- a/socket/src/main.js +++ b/socket/src/main.js @@ -1,208 +1,187 @@ import { Server } from "socket.io"; + +// Inicialização do servidor const io = new Server(); const ROOMS = {}; let roomIdCounter = 0; +/** + * Gera um identificador único para cada jogador. + * @returns {number} O identificador gerado. + */ function $generate_uuid() { roomIdCounter += 1; return roomIdCounter; } -// code +/** + * Gera um código de sala aleatório. + * @returns {string} O código gerado para a sala. + */ function $generate_code() { return Math.random().toString(36).substring(2, 8).toUpperCase(); } /** - * Event listener for a new connection from a client. + * Evento de conexão de um novo cliente. + * @param {Socket} socket - O objeto socket que representa a conexão do cliente. */ io.on("connection", (socket) => { + /** - * Handles room creation or joining a room. - * @param {Object} data - The data for the room. - * @param {string} data.room_code - The code of the room. - * @param {number} data.room_time - The time duration for the room in minutes. - * @param {string} data.room_player - The name of the player joining the room. + * Manipula a criação ou entrada de um jogador em uma sala. + * @param {Object} data - Dados para criação ou entrada na sala. + * @param {string} [data.room_code] - O código da sala. Se não fornecido, uma nova sala será criada. + * @param {number} data.room_time - A duração da sala em minutos. + * @param {string} data.room_player - O nome do jogador entrando na sala. + * @throws {Error} Se a sala não existir ou se o jogador já estiver na sala. */ socket.on("room", ({ room_code, room_time, room_player }) => { - // Generate a room code if not provided if (!room_code) { - const new_room_code = $generate_code(); // Ensure $generate_code is defined - room_code = new_room_code; + room_code = $generate_code(); ROOMS[room_code] = { code: room_code, date: new Date(), players: [], owner: room_player, - time: room_time || 11, // Default to 11 seconds if no time is provided + time: room_time || 11, state: "waiting", }; } - // Select the room by its code const room = ROOMS[room_code]; - // Check if the room exists if (!room) { - socket.emit("err_socket", { - message: `The room "${room_code}" doesn't exist!`, - }); + socket.emit("err_socket", { err_socket: "ROOM_NOT_FOUND" }); return; } - // Check if the room is in "waiting" state if (room.state !== "waiting") { - socket.emit("err_socket", { - message: `The room "${room_code}" is currently in "${room.state}" state.`, - }); + socket.emit("err_socket", { err_socket: "ROOM_STATE_ERROR" }); return; } - // Check if the player with the same nickname is already in the room if (room.players.find((player) => player.room_player === room_player)) { - socket.emit("err_socket", { - message: `A player with the nickname "${room_player}" already exists in the room.`, - }); + socket.emit("err_socket", { err_socket: "PLAYER_EXISTS" }); return; } - // Add the player to the room socket.join(room_code); - // Register the player in the room room.players.push({ - id: $generate_uuid(), // Ensure $generate_uuid is defined + id: $generate_uuid(), date: new Date(), socket: socket.id, - player_date: { - cookies: null, - }, + player_data: { cookies: null }, room_player, }); - // Send room update to all players io.to(room_code).emit("update_room", { room_player, room }); - - console.log( - `Player "${room_player}" joined room "${room_code}". Room state:`, - room, - ); + console.log(`Player "${room_player}" joined room "${room_code}". Room state:`, room); }); /** - * Handles player leaving a room. - * @param {Object} data - The data for leaving the room. - * @param {string} data.room_code - The code of the room. - * @param {string} data.room_player - The name of the player leaving the room. + * Manipula a saída de um jogador de uma sala. + * @param {Object} data - Dados para sair da sala. + * @param {string} data.room_code - O código da sala. + * @param {string} data.room_player - O nome do jogador que está saindo. + * @throws {Error} Se a sala ou o jogador não forem encontrados. */ socket.on("leave_room", ({ room_code, room_player }) => { const room = ROOMS[room_code]; - // Check if the room exists - if (!room) return; + if (!room) { + socket.emit("err_socket", { err_socket: "ROOM_NOT_FOUND" }); + return; + } - // Remove the player from the room - room.players = room.players.filter( - (player) => player.room_player !== room_player, - ); + room.players = room.players.filter(player => player.room_player !== room_player); - // Delete the room if it is empty if (room.players.length === 0) { delete ROOMS[room_code]; console.log(`Room ${room_code} has been deleted.`); } else if (room.owner === room_player) { - // Update the owner if necessary room.owner = room.players[0]?.room_player || null; console.log(`New owner of room ${room_code}: ${room.owner}`); } - // Remove the socket from the room socket.leave(room_code); - - // Send updated room state to all players io.to(room_code).emit("update_room", { room_player, room }); - console.log(`Socket ${socket.id} (${room_player}) left room ${room_code}`); + console.log(`Player ${room_player} (Socket ID: ${socket.id}) left room ${room_code}`); }); /** - * Handles player rejoining a room. - * @param {Object} data - The data for rejoining the room. - * @param {string} data.room_player - The name of the player rejoining the room. - * @param {string} data.room_code - The code of the room. + * Manipula o retorno de um jogador para uma sala. + * @param {Object} data - Dados para retornar à sala. + * @param {string} data.room_player - O nome do jogador retornando. + * @param {string} data.room_code - O código da sala. + * @throws {Error} Se a sala não for encontrada ou o jogador não estiver na sala. */ socket.on("rejoin_room", ({ room_player, room_code }) => { const room = ROOMS[room_code]; - if (!room) return; - if (room.state === "finished") return; - - // Update the socket ID for the player - for (const player of room.players) { - if (player.room_player === room_player) { - player.socket = socket.id; - break; // Exit the loop after updating the player's socket - } + if (!room) { + socket.emit("err_socket", { err_socket: "ROOM_NOT_FOUND" }); + return; } - // Player joins the room again - socket.join(room_code); + if (room.state === "finished") { + socket.emit("err_socket", { err_socket: "ROOM_STATE_ERROR" }); + return; + } - // Send updated room state to all players - io.to(room_code).emit("update_room", { room_player, room }); + const player = room.players.find(player => player.room_player === room_player); + if (player) { + player.socket = socket.id; + socket.join(room_code); + io.to(room_code).emit("update_room", { room_player, room }); + console.log(`Player "${room_player}" rejoined room "${room_code}".`); + } else { + socket.emit("err_socket", { err_socket: "PLAYER_NOT_FOUND" }); + } }); /** - * Starts the game in a room. - * @param {Object} data - The data for starting the game. - * @param {string} data.room_code - The code of the room. + * Inicia o jogo em uma sala. + * @param {Object} data - Dados para iniciar o jogo. + * @param {string} data.room_code - O código da sala. + * @throws {Error} Se a sala não existir. */ socket.on("start_game", ({ room_code }) => { const room = ROOMS[room_code]; - // Check if the room exists if (!room) { - socket.emit("err_socket", { - message: `Room ${room_code} not found.`, - }); + socket.emit("err_socket", { err_socket: "ROOM_NOT_FOUND" }); return; } - // Change the room state to "in_game" room.state = "in_game"; + let countdown = 3; - let countdown = 3; // Countdown for 3 seconds const countdownInterval = setInterval(() => { - // Send the current countdown to the room io.to(room_code).emit("count_down", { countdown }); if (countdown <= 0) { clearInterval(countdownInterval); - - // Start the game io.to(room_code).emit("game_start"); - let time_game = room.time * 1; // Convert time from minutes to seconds - + let time_game = room.time * 1; const gameInterval = setInterval(() => { - // Send the remaining game time to the room io.to(room_code).emit("timer", { time_game }); if (time_game <= 0) { clearInterval(gameInterval); - // End the game and generate the ranking const ranking = room.players - .sort((a, b) => b.player_date.cookies - a.player_date.cookies) // Sort by cookies + .sort((a, b) => b.player_data.cookies - a.player_data.cookies) .map((player, index) => ({ rank: index + 1, room_player: player.room_player, - cookies: player.player_date.cookies, + cookies: player.player_data.cookies, })); room.state = "finished"; - - // Send the game end event with the ranking io.to(room_code).emit("game_end", { ranking }); if (room.state === "finished") { @@ -210,56 +189,49 @@ io.on("connection", (socket) => { console.log(`Room ${room_code} has been deleted.`); } - console.log( - `Game in room ${room_code} finished! Ranking:`, - ranking, - ); - - return; // Prevent further execution + console.log(`Game in room "${room_code}" finished! Ranking:`, ranking); + return; } - time_game--; // Decrement the game time - }, 1000); // Update every second + time_game--; + }, 1000); } - countdown--; // Decrement the countdown - }, 1000); // Update every second + countdown--; + }, 1000); }); /** - * Updates the number of cookies for a player in the room. - * @param {Object} data - The data for updating cookies. - * @param {string} data.room_player - The name of the player. - * @param {string} data.room_code - The code of the room. - * @param {number} data.cookies - The number of cookies to update. + * Atualiza o número de cookies de um jogador na sala. + * @param {Object} data - Dados para atualizar os cookies. + * @param {string} data.room_player - O nome do jogador. + * @param {string} data.room_code - O código da sala. + * @param {number} data.cookies - O número de cookies a ser atualizado. + * @throws {Error} Se a sala ou o jogador não forem encontrados ou se o valor de cookies for inválido. */ socket.on("update_cookies", ({ room_player, room_code, cookies }) => { if (typeof cookies !== "number" || cookies < 0) { - console.error("Invalid cookie data received."); + socket.emit("err_socket", { err_socket: "INVALID_COOKIES" }); return; } const room = ROOMS[room_code]; if (!room) { - console.error(`Room ${room_code} not found.`); + socket.emit("err_socket", { err_socket: "ROOM_NOT_FOUND" }); return; } - const player = room.players.find( - (player) => player.room_player === room_player, - ); + const player = room.players.find(player => player.room_player === room_player); if (player) { - player.player_date.cookies = cookies; - console.log( - `Player ${room_player} in room ${room_code} updated cookies to ${cookies}.`, - ); + player.player_data.cookies = cookies; + console.log(`Player "${room_player}" in room "${room_code}" updated cookies to ${cookies}.`); } else { - console.error(`Player ${room_player} not found in room ${room_code}.`); + socket.emit("err_socket", { err_socket: "PLAYER_NOT_FOUND" }); } }); }); -// Start listening on port 3000 +// Inicia o servidor na porta 3000 io.listen(3000); diff --git a/www/src/locales/lang/en-US.ts b/www/src/locales/lang/en-US.ts index dc16830..7d4842c 100644 --- a/www/src/locales/lang/en-US.ts +++ b/www/src/locales/lang/en-US.ts @@ -1,48 +1,55 @@ // @lang/en-US const translation = { - general: { - cookies: "Cookies", - seconds: "Seconds", - timeLabel: "Time:", - start: "GO!", - message_now: "Now", - }, - splashScreen: { - splashClick: "Click...", - }, - menu: { - cookieTitle: "Cookie", - playButton: "Play", - settings: "Settings", - }, - ranking: { - rankingTitle: "Ranking", - roomCode: "Room Code:", - waiting: "Waiting...", - startButton: "Start", - leaveButton: "Leave", - }, - game: { - nicknameLabel: "Nickname", - gameOptionLabel: "Choose an option:", - randomRoom: "Join a random room", - createRoom: "Create a room", - joinRoom: "Join a room", - roomCodeLabel: "Room Code", - gameTimeLabel: "Game Time (in seconds)", - }, - room: { - no_room_player: "It seems you haven't set your nickname!", - no_room_time: "It seems you haven't set the room time!", - time_check: - "The room time must be greater than 10 seconds and less than or equal to 10 minutes!", - no_room_code: "It seems you haven't added the room code!", - no_connected: "It seems you've been disconnected from the Cookie game!", - }, - modal: { - cancel: "Cancel", - confirm: "Confirm", - }, + general: { + cookies: "Cookies", + seconds: "Seconds", + timeLabel: "Time:", + start: "GO!", + message_now: "Now" + }, + splashScreen: { + splashClick: "Click..." + }, + menu: { + cookieTitle: "Cookie", + playButton: "Play", + settings: "Settings" + }, + ranking: { + rankingTitle: "Ranking", + roomCode: "Room Code:", + waiting: "Waiting...", + startButton: "Start", + leaveButton: "Leave" + }, + game: { + nicknameLabel: "Nickname", + gameOptionLabel: "Choose an option:", + randomRoom: "Join a random room", + createRoom: "Create a room", + joinRoom: "Join a room", + roomCodeLabel: "Room Code", + gameTimeLabel: "Game Time (in seconds)" + }, + room: { + no_room_player: "It seems you haven't set your nickname!", + no_room_time: "It seems you haven't set the room time!", + time_check: + "The room time must be greater than 10 seconds and less than or equal to 10 minutes!", + no_room_code: "It seems you haven't added the room code!", + no_connected: "It seems you've been disconnected from the Cookie game!" + }, + modal: { + cancel: "Cancel", + confirm: "Confirm" + }, + err_message: { + ROOM_NOT_FOUND: "The room was not found!", + ROOM_STATE_ERROR: "The room is in the wrong state!", + PLAYER_EXISTS: "A player with this name already exists in the room.", + INVALID_COOKIES: "Invalid cookies data received.", + ROOM_CODE_NOT_FOUND: "The room code was not found." + } }; -export default translation; \ No newline at end of file +export default translation; diff --git a/www/src/locales/lang/es-ES.ts b/www/src/locales/lang/es-ES.ts index a5f7ea5..463fd5d 100644 --- a/www/src/locales/lang/es-ES.ts +++ b/www/src/locales/lang/es-ES.ts @@ -44,6 +44,13 @@ const translation = { cancel: "Cancelar", confirm: "Confirmar", }, + err_message: { + ROOM_NOT_FOUND: "¡La sala no fue encontrada!", + ROOM_STATE_ERROR: "¡La sala está en el estado incorrecto!", + PLAYER_EXISTS: "Ya existe un jugador con ese nombre en la sala.", + INVALID_COOKIES: "Datos de cookies inválidos recibidos.", + ROOM_CODE_NOT_FOUND: "No se encontró el código de la sala." +} }; export default translation; \ No newline at end of file diff --git a/www/src/locales/lang/pt-BR.ts b/www/src/locales/lang/pt-BR.ts index abed953..d2380b0 100644 --- a/www/src/locales/lang/pt-BR.ts +++ b/www/src/locales/lang/pt-BR.ts @@ -43,6 +43,13 @@ const translation = { cancel: "Cancelar", confirm: "Confirmar", }, + err_message: { + ROOM_NOT_FOUND: "A sala não foi encontrada!", + ROOM_STATE_ERROR: "A sala está no estado errado!", + PLAYER_EXISTS: "Já existe um jogador com esse nome na sala.", + INVALID_COOKIES: "Dados de cookies inválidos recebidos.", + ROOM_CODE_NOT_FOUND: "O código da sala não foi encontrado.", + } }; export default translation; \ No newline at end of file diff --git a/www/src/ts/game.ts b/www/src/ts/game.ts index a086f6a..80d7c54 100644 --- a/www/src/ts/game.ts +++ b/www/src/ts/game.ts @@ -178,8 +178,18 @@ $("#form_button").on("click", () => { }); // Handle socket errors -socket.on("err_socket", ({ message }: { message: string }) => { - showMessage(message); +socket.on("err_socket", ({ err_socket }: { err_socket: string }) => { + if (err_socket === 'ROOM_NOT_FOUND') { + showMessage(lang("err_message.ROOM_NOT_FOUND")); + } else if (err_socket === 'ROOM_STATE_ERROR') { + showMessage(lang("err_message.ROOM_STATE_ERROR")); + } else if (err_socket === 'PLAYER_EXISTS') { + showMessage(lang("err_message.PLAYER_EXISTS")); + } else if (err_socket === 'INVALID_COOKIES') { + showMessage(lang("err_message.INVALID_COOKIES")); + } else if (err_socket === 'ROOM_CODE_NOT_FOUND') { + showMessage(lang("err_message.ROOM_CODE_NOT_FOUND")); + } }); // Handle room updates