Skip to content

Commit

Permalink
feat(version): v1.0.1
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianjnuwu committed Jan 15, 2025
1 parent 482abf1 commit 43c50d5
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div align="center">
<h1>Cookie Clicker</h1>
<img alt="icon" src="https://i.imgur.com/EOzKknx.png" width="40%" />
<img alt="icon" src="https://cookie-clicker-brasil.vercel.app/favicon.ico" width="40%" />
<p>
<b>🍪 Cookie Clicker Brasil</b> <i>is the best cookie clicking game you will find!</i>
</p>
Expand Down
47 changes: 47 additions & 0 deletions server/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,52 @@ io.on("connection", (socket: Socket) => {
);
});

/**
* Handles joining a random public room.
* Filters for available public rooms with space for more players.
* If no public rooms are available, an error is emitted.
* If the player is already in a room, an error is emitted.
* Otherwise, the player is added to a random room and the room is updated.
* @param {string} room_player - The name/identifier of the player.
*/
socket.on("join_random_room", ({ room_player }: { room_player: string }) => {
// Filters public rooms that are in "waiting" state and have less than 10 players
const availableRooms = Object.values(ROOMS).filter(
(room) => room.public && room.state === "waiting");

// If no available public rooms
if (availableRooms.length === 0) {
socket.emit("err_socket", { err_socket: "NO_PUBLIC_ROOMS_AVAILABLE" });
return;
}

// Randomly select a room from the available rooms
const randomRoom = availableRooms[Math.floor(Math.random() * availableRooms.length)];

// Check if the player is already in the selected room
if (randomRoom.players.find((player) => player.room_player === room_player)) {
socket.emit("err_socket", { err_socket: "PLAYER_EXISTS_IN_ROOM" });
return;
}

// Add the player to the selected room
socket.join(randomRoom.code);

randomRoom.players.push({
id: generateUuid(),
date: new Date(),
socket: socket.id,
player_data: { cookies: null },
room_player,
});

io.to(randomRoom.code).emit("update_room", { room_player, room: randomRoom });

console.log(`Player "${room_player}" joined random room "${randomRoom.code}". Room state:`, randomRoom);

});


/**
* Handles player rejoining a room.
* @param data - The data for rejoining the room.
Expand Down Expand Up @@ -273,6 +319,7 @@ io.on("connection", (socket: Socket) => {
socket.on("disconnect", () => {
console.log(`Client disconnected: ${socket.id}`);
});

});

// Starts the server on port 3000
Expand Down
4 changes: 2 additions & 2 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -182,8 +182,8 @@ <h5 class="modal-title" id="room_modalLabel">
</label>
<div>
<div class="form-check">
<input class="form-check-input" type="radio" name="option_game" id="randomRoom" value="random" disabled>
<label class="form-check-label" for="randomRoom">
<input class="form-check-input" type="radio" name="option_game" id="room_random" value="room_random">
<label class="form-check-label" for="room_random">
<i class="fas fa-random"></i>
<i18next i18next-id="game.randomRoom">Join a random room</i18next>
</label>
Expand Down
1 change: 1 addition & 0 deletions www/src/locales/lang/en-US.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const translation = {
ROOM_STATE_ERROR_FINISHED: "The match is over",
PLAYER_EXISTS: "A player with this name already exists in the room.",
INVALID_COOKIES: "Invalid cookies data received.",
NO_PUBLIC_ROOMS_AVAILABLE: "There are no public rooms available at the moment.",
ROOM_CODE_NOT_FOUND: "The room code was not found.",
},
};
Expand Down
1 change: 1 addition & 0 deletions www/src/locales/lang/pt-BR.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const translation = {
ROOM_STATE_ERROR_FINISHED: "A partida ja terminou",
PLAYER_EXISTS: "Já existe um jogador com esse nome na sala.",
INVALID_COOKIES: "Dados de cookies inválidos recebidos.",
NO_PUBLIC_ROOMS_AVAILABLE: "Não há salas públicas disponíveis no momento.",
ROOM_CODE_NOT_FOUND: "O código da sala não foi encontrado.",
},
};
Expand Down
2 changes: 1 addition & 1 deletion www/src/styles/scss/modal.scss
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@
.form-check-input {
width: 20px;
height: 20px;
border: none;
outline: none !important;
box-shadow: none;
border-radius: 50%;
border: 1.5px solid var(--text-color);
background-color: var(--layout);
transition: background-color 0.3s, border-color 0.3s, box-shadow 0.3s;
cursor: pointer;
Expand Down
17 changes: 17 additions & 0 deletions www/src/ts/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,20 @@ $("#form_button").on("click", () => {
`<i class="fas fa-exclamation-circle"></i> ${lang("room.no_room_player")}`,
);
}

if (option === "room_random") {

$room_modal.hide();

localStorage.setItem("name", roomPlayer);

socket.emit("join_random_room", {
room_player: roomPlayer,
});

return;

};

// Verificação do tempo só se a opção for "create"
if (option === "create") {
Expand Down Expand Up @@ -177,6 +191,7 @@ $("#form_button").on("click", () => {
room_time: roomTime,
room_player: roomPlayer,
});

});

// Handle socket errors
Expand All @@ -191,6 +206,8 @@ socket.on("err_socket", ({ err_socket }: { err_socket: string }) => {
showMessage(lang("err_message.PLAYER_EXISTS"));
} else if (err_socket === "INVALID_COOKIES") {
showMessage(lang("err_message.INVALID_COOKIES"));
} else if (err_socket === "NO_PUBLIC_ROOMS_AVAILABLE") {
showMessage(lang("err_message.NO_PUBLIC_ROOMS_AVAILABLE"));
} else if (err_socket === "ROOM_CODE_NOT_FOUND") {
showMessage(lang("err_message.ROOM_CODE_NOT_FOUND"));
}
Expand Down
4 changes: 4 additions & 0 deletions www/src/ts/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $(() => {
*/
$('input[name="option_game"]').on("change", () => {
// Clear the room code input field
const option = $('input[name="option_game"]:checked').val() as string;
const $room_code_input = $("#room_code") as JQuery<HTMLInputElement>;
const $code_container = $("#code_container");
const $game_container = $("#game_container");
Expand All @@ -35,6 +36,9 @@ $(() => {
if ($room_join_input.is(":checked")) {
$code_container.show();
$game_container.hide();
} else if (option === "room_random") {
$code_container.hide();
$game_container.hide();
} else {
$code_container.hide();
$game_container.show();
Expand Down

0 comments on commit 43c50d5

Please sign in to comment.