Skip to content

Commit

Permalink
feat(version): v1.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianjnuwu committed Jan 6, 2025
1 parent 238b260 commit b010daa
Show file tree
Hide file tree
Showing 12 changed files with 216 additions and 150 deletions.
2 changes: 1 addition & 1 deletion biome.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@
"enabled": true
},
"files": {
"ignore": ["node_modules", "build", "dist", "android", "ios", "*.log"]
"ignore": ["node_modules", "www/src/js/modules", "build", "dist", "android", "ios", "*.log"]
}
}
2 changes: 2 additions & 0 deletions socket/package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"main": "src/main.js",
"type": "module",
"author": "Sebastian Jn <[email protected]>",
"dependencies": {
"socket.io": "^4.8.1"
}
Expand Down
142 changes: 91 additions & 51 deletions socket/src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@ io.on("connection", (socket) => {
date: new Date(),
socket: socket.id,
player_date: {
cps: 0,
cookies: 0
cookies: null
},
room_player
});
Expand Down Expand Up @@ -120,11 +119,14 @@ io.on("connection", (socket) => {
if (!room) return;

// trocar o socket id
room.players.forEach(x => {
if (x.room_player === room_player) {
x.socket = socket.id;
};
});
// Trocar o socket id
for (const player of room.players) {
if (player.room_player === room_player) {
player.socket = socket.id;
break; // Se só um jogador precisa ser atualizado, encerra o loop
}
}


// enter the code room
socket.join(room_code);
Expand All @@ -135,54 +137,92 @@ io.on("connection", (socket) => {
});

socket.on("start_game", ({ room_code }) => {

const room = ROOMS[room_code];
room.state = "in_game";

let countdown = 1; // Contagem regressiva de 10 segundos
const countdownInterval = setInterval(() => {
io.to(room_code).emit("count_down", { countdown });

if (countdown === 0) {
clearInterval(countdownInterval);

// Envia o início do jogo
io.to(room_code).emit("game_start");

// Inicia o temporizador do jogo
let time_game = room.time * 1; // Converte minutos em segundos (60 ta em 1 para teste)

const gameInterval = setInterval(() => {
time_game--;

// Atualiza o tempo restante
io.to(room_code).emit("timer", { time_game });

if (time_game === 0) {
clearInterval(gameInterval);

// Finaliza o jogo e envia o ranking
const ranking = room.players
.sort((a, b) => b.player_date.cookies - a.player_date.cookies) // Ordena por cookies
.map((player, index) => ({
rank: index + 1,
room_player: player.room_player,
cookies: player.player_date.cookies
}));

// Atualiza o estado da sala e envia o ranking
room.state = "finished";
io.to(room_code).emit("game_end", { ranking });

console.log(`Jogo na sala ${room_code} finalizado! Ranking:`, ranking);
}
}, 1000); // Atualiza a cada segundo
const room = ROOMS[room_code];

// Verifica se a sala existe
if (!room) {
socket.emit("err_socket", { message: `Sala ${room_code} não encontrada.` });
return;
}

// Altera o estado da sala para "in_game"
room.state = "in_game";

let countdown = 3; // Contagem regressiva de 10 segundos
const countdownInterval = setInterval(() => {
io.to(room_code).emit("count_down", { countdown });

if (countdown === 0) {
clearInterval(countdownInterval);

// Envia o evento de início do jogo
io.to(room_code).emit("game_start");

// Define o tempo de jogo em segundos
let time_game = room.time * 1; // Multiplica os minutos por 60 para converter para segundos

const gameInterval = setInterval(() => {
time_game--;

// Envia o tempo restante para a sala
io.to(room_code).emit("timer", { time_game });

if (time_game === 0) {
clearInterval(gameInterval);

// Finaliza o jogo e gera o ranking
const ranking = room.players
.sort((a, b) => b.player_date.cookies - a.player_date.cookies) // Ordena por cookies
.map((player, index) => ({
rank: index + 1,
room_player: player.room_player,
cookies: player.player_date.cookies,
}));

// Atualiza o estado da sala
room.state = "finished";

// Envia o evento de fim do jogo com o ranking
io.to(room_code).emit("game_end", { ranking });

console.log(`Jogo na sala ${room_code} finalizado! Ranking:`, ranking);
}
}, 1000); // Atualiza a cada segundo
}

countdown--;
}, 1000); // Atualiza a cada segundo
});

countdown--;
}, 1000); // Atualiza a cada segundo

socket.on("update_cookies", ({ room_player, room_code, cookies }) => {
// Valida se o número de cookies recebido é válido
if (typeof cookies !== "number" || cookies < 0) {
console.error("Dados inválidos recebidos no evento update_cookies.");
return;
}

// Busca a sala diretamente pelo código
const room = ROOMS[room_code];

if (!room) {
console.error(`Sala ${room_code} não encontrada.`);
return;
}

// Busca o jogador dentro da sala
const player = room.players.find((player) => player.room_player === room_player);

// Verifica se o jogador existe e atualiza os cookies
if (player) {
player.player_date.cookies = cookies;
console.log(`Jogador ${room_player} na sala ${room_code} atualizou cookies para ${cookies}.`);
} else {
console.error(`Jogador ${room_player} não encontrado na sala ${room_code}.`);
}
});


});

io.listen(3000);
13 changes: 8 additions & 5 deletions www/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
<title>Cookie</title>
<link rel="shortcut icon" href="./src/img/icon.webp" type="image/webp">
<link rel="stylesheet" href="./src/styles/css/ui.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/eruda/3.4.1/eruda.min.js" integrity="sha512-3RVqOZtMevFOLeXCp0/Wl7np/l3J3MMysaFDUhNh+hdKx+Wb0lMXuHwA6CZ/+4DfYZM01Om1as8g+mnTaQH9vA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
eruda.init();
</script>
<script type="module" src="./src/js/modules/socket.io.min.js"></script>
<script type="module" src="./src/js/modules/jquery.min.js"></script>
<script type="module" src="./src/js/main.js"></script>
<script type="module" src="./src/js/cookie/start.js"></script>
<script type="module" src="./main.ts"></script>
</head>

<!-- (body) -->
Expand Down Expand Up @@ -180,7 +183,7 @@ <h5 class="modal-title" id="room_modalLabel">
<!-- Tempo de jogo -->
<div class="mb-3" id="game_container">
<label for="gameTime" class="form-label">
<i class="fas fa-clock"></i> Tempo de Jogo (em minutos)
<i class="fas fa-clock"></i> Tempo de Jogo (em segundos)
</label>
<input type="number" class="form-control" id="room_time" name="gameTime" placeholder="Ex.: 10" min="1" max="1">
</div>
Expand All @@ -198,12 +201,12 @@ <h5 class="modal-title" id="room_modalLabel">
</div>
</div>
<!-- (room settings /) -->



<!--- (script) --->
<script type="module" src="./src/js/cookie/game.js" defer></script>
<script type="module" src="./main.ts"></script>
<script type="module" src="./src/js/modules/bootstrap.min.js"></script>
<!--- (script /) --->

</body>
<!-- (body /) -->

Expand Down
53 changes: 0 additions & 53 deletions www/src/js/cookie/game.js

This file was deleted.

Loading

0 comments on commit b010daa

Please sign in to comment.