SyntaxSquad/DADProject#46 SyntaxSquad/DADProject#47 added sockets for game joining and card play
This commit is contained in:
@@ -2,6 +2,7 @@ class BiscaGame {
|
|||||||
constructor(id, type, player1, player2) {
|
constructor(id, type, player1, player2) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
|
|
||||||
this.deck = [];
|
this.deck = [];
|
||||||
this.trumpCard = null;
|
this.trumpCard = null;
|
||||||
this.trumpSuit = null;
|
this.trumpSuit = null;
|
||||||
@@ -14,6 +15,7 @@ class BiscaGame {
|
|||||||
|
|
||||||
this.currentTurn = null;
|
this.currentTurn = null;
|
||||||
this.status = "pending";
|
this.status = "pending";
|
||||||
|
this.startTime = null;
|
||||||
|
|
||||||
this.players = {
|
this.players = {
|
||||||
[player1.id]: {
|
[player1.id]: {
|
||||||
@@ -41,7 +43,10 @@ class BiscaGame {
|
|||||||
|
|
||||||
this.dealInitialCards();
|
this.dealInitialCards();
|
||||||
|
|
||||||
this.currentTurn = Object.keys(this.players)[0];
|
const playerIds = Object.keys(this.players);
|
||||||
|
this.currentTurn = playerIds.find((id) => id != 1) || playerIds[0];
|
||||||
|
|
||||||
|
this.startTime = Date.now();
|
||||||
this.status = "playing";
|
this.status = "playing";
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -49,6 +54,7 @@ class BiscaGame {
|
|||||||
const suits = ["c", "e", "o", "p"];
|
const suits = ["c", "e", "o", "p"];
|
||||||
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
|
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
|
||||||
let deck = [];
|
let deck = [];
|
||||||
|
|
||||||
for (const suit of suits) {
|
for (const suit of suits) {
|
||||||
for (const rank of ranks) {
|
for (const rank of ranks) {
|
||||||
deck.push({
|
deck.push({
|
||||||
@@ -60,7 +66,7 @@ class BiscaGame {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return deck.sort(() => Math.random() - 0.5); // Baralhar
|
return deck.sort(() => Math.random() - 0.5);
|
||||||
}
|
}
|
||||||
|
|
||||||
dealInitialCards() {
|
dealInitialCards() {
|
||||||
@@ -81,21 +87,30 @@ class BiscaGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
playCard(userId, cardId) {
|
playCard(userId, cardId) {
|
||||||
if (this.currentTurn != userId) return { error: "Não é a tua vez!" };
|
if (this.currentTurn != userId) return { error: "Not your turn!" };
|
||||||
|
|
||||||
const player = this.players[userId];
|
const player = this.players[userId];
|
||||||
const cardIndex = player.hand.findIndex((c) => c.id === cardId);
|
const cardIndex = player.hand.findIndex((c) => c.id === cardId);
|
||||||
|
|
||||||
if (cardIndex === -1) return { error: "Não tens essa carta!" };
|
if (cardIndex === -1) return { error: "You don't own that card!" };
|
||||||
|
|
||||||
const card = player.hand[cardIndex];
|
const card = player.hand[cardIndex];
|
||||||
|
|
||||||
player.hand.splice(cardIndex, 1); // Remove da mão (Server-side)
|
const deckEmpty = this.deck.length === 0 && this.trumpCard === null;
|
||||||
|
if (this.table.playerCard && deckEmpty) {
|
||||||
|
const suitToFollow = this.table.playerCard.suit;
|
||||||
|
const hasSuit = player.hand.some((c) => c.suit === suitToFollow);
|
||||||
|
if (hasSuit && card.suit !== suitToFollow) {
|
||||||
|
return { error: "You must follow the suit!" };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
player.hand.splice(cardIndex, 1);
|
||||||
|
|
||||||
if (!this.table.playerCard) {
|
if (!this.table.playerCard) {
|
||||||
this.table.playerCard = card;
|
this.table.playerCard = card;
|
||||||
this.table.firstPlayerId = userId;
|
this.table.firstPlayerId = userId;
|
||||||
this.currentTurn = this.getOpponentId(userId); // Passa a vez
|
this.currentTurn = this.getOpponentId(userId);
|
||||||
return { action: "next_turn" };
|
return { action: "next_turn" };
|
||||||
} else {
|
} else {
|
||||||
this.table.opponentCard = card;
|
this.table.opponentCard = card;
|
||||||
@@ -133,12 +148,43 @@ class BiscaGame {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const trickResult = { winnerId, points, cards: [card1, card2] };
|
const trickResult = { winnerId, points, cards: [card1, card2] };
|
||||||
|
|
||||||
this.table = { playerCard: null, opponentCard: null, firstPlayerId: null };
|
this.table = { playerCard: null, opponentCard: null, firstPlayerId: null };
|
||||||
|
|
||||||
if (this.players[winnerId].hand.length === 0) {
|
if (this.players[winnerId].hand.length === 0) {
|
||||||
this.status = "finished";
|
this.status = "finished";
|
||||||
return { action: "game_ended", trickResult, winnerId };
|
|
||||||
|
const pIds = Object.keys(this.players);
|
||||||
|
const p1Obj = this.players[pIds[0]];
|
||||||
|
const p2Obj = this.players[pIds[1]];
|
||||||
|
|
||||||
|
const totalTime = (Date.now() - this.startTime) / 1000;
|
||||||
|
|
||||||
|
let gameWinnerId = null;
|
||||||
|
let isDraw = false;
|
||||||
|
|
||||||
|
if (p1Obj.points > p2Obj.points) gameWinnerId = p1Obj.id;
|
||||||
|
else if (p2Obj.points > p1Obj.points) gameWinnerId = p2Obj.id;
|
||||||
|
else isDraw = true;
|
||||||
|
|
||||||
|
const finalWinner = isDraw ? null : gameWinnerId;
|
||||||
|
const finalLoser = isDraw
|
||||||
|
? null
|
||||||
|
: gameWinnerId == p1Obj.id
|
||||||
|
? p2Obj.id
|
||||||
|
: p1Obj.id;
|
||||||
|
|
||||||
|
return {
|
||||||
|
action: "game_ended",
|
||||||
|
trickResult,
|
||||||
|
winnerId: finalWinner,
|
||||||
|
loserId: finalLoser,
|
||||||
|
isDraw: isDraw,
|
||||||
|
player1_id: pIds[0],
|
||||||
|
player1_points: p1Obj.points,
|
||||||
|
player2_id: pIds[1],
|
||||||
|
player2_points: p2Obj.points,
|
||||||
|
totalTime: totalTime,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
return { action: "trick_resolved", trickResult };
|
return { action: "trick_resolved", trickResult };
|
||||||
@@ -146,7 +192,6 @@ class BiscaGame {
|
|||||||
|
|
||||||
getStateForPlayer(userId) {
|
getStateForPlayer(userId) {
|
||||||
const oppId = this.getOpponentId(userId);
|
const oppId = this.getOpponentId(userId);
|
||||||
|
|
||||||
return {
|
return {
|
||||||
id: this.id,
|
id: this.id,
|
||||||
trumpCard: this.trumpCard,
|
trumpCard: this.trumpCard,
|
||||||
@@ -154,10 +199,8 @@ class BiscaGame {
|
|||||||
deckCount: this.deck.length + (this.trumpCard ? 1 : 0),
|
deckCount: this.deck.length + (this.trumpCard ? 1 : 0),
|
||||||
currentTurn: this.currentTurn,
|
currentTurn: this.currentTurn,
|
||||||
table: this.table,
|
table: this.table,
|
||||||
|
|
||||||
hand: this.players[userId].hand,
|
hand: this.players[userId].hand,
|
||||||
points: this.players[userId].points,
|
points: this.players[userId].points,
|
||||||
|
|
||||||
opponent: {
|
opponent: {
|
||||||
points: this.players[oppId].points,
|
points: this.players[oppId].points,
|
||||||
cardCount: this.players[oppId].hand.length,
|
cardCount: this.players[oppId].hand.length,
|
||||||
|
|||||||
+76
-73
@@ -1,105 +1,115 @@
|
|||||||
const axios = require("axios");
|
import axios from "axios";
|
||||||
const { getGame, createGame } = require("../state/game");
|
import { getGame, createGame } from "../state/game.js";
|
||||||
const { getUser } = require("../state/connection");
|
import { getUser } from "../state/connection.js";
|
||||||
|
|
||||||
const API_URL = "http://localhost:8000/api/v1";
|
const API_URL = "http://localhost:8000/api/v1";
|
||||||
|
|
||||||
module.exports = (io, socket) => {
|
async function saveGameResult(gameId, result, token) {
|
||||||
// --- EVENTO: ENTRAR NO JOGO (Validado pela tabela GAMES) ---
|
try {
|
||||||
|
const payload = {
|
||||||
|
status: "Ended",
|
||||||
|
is_draw: result.isDraw,
|
||||||
|
total_time: result.totalTime,
|
||||||
|
player1_points: result.player1_points,
|
||||||
|
player2_points: result.player2_points,
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!result.isDraw && result.winnerId) {
|
||||||
|
payload.winner_user_id = result.winnerId;
|
||||||
|
payload.loser_user_id = result.loserId;
|
||||||
|
}
|
||||||
|
|
||||||
|
await axios.put(`${API_URL}/games/${gameId}`, payload, {
|
||||||
|
headers: { Authorization: token },
|
||||||
|
});
|
||||||
|
console.log(`[DB] Game ${gameId} saved.`);
|
||||||
|
} catch (error) {
|
||||||
|
console.error(`[DB Error] Game ${gameId}:`, error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default (io, socket) => {
|
||||||
socket.on("join-game", async ({ gameId }) => {
|
socket.on("join-game", async ({ gameId }) => {
|
||||||
const user = getUser(socket.id);
|
const user = getUser(socket.id);
|
||||||
|
|
||||||
if (!user) {
|
if (!user) {
|
||||||
socket.emit("error", { message: "Não autenticado." });
|
socket.emit("error", { message: "Not authenticated." });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
console.log(`[Socket] User ${user.id} a tentar entrar no Game ${gameId}`);
|
|
||||||
|
|
||||||
// 1. Verificar se o jogo já está na memória do Node
|
|
||||||
let game = getGame(gameId);
|
let game = getGame(gameId);
|
||||||
|
|
||||||
// 2. Se NÃO existe na memória, vamos buscá-lo ao Laravel
|
|
||||||
if (!game) {
|
if (!game) {
|
||||||
try {
|
try {
|
||||||
// Token para autenticação no Laravel
|
|
||||||
const token = socket.handshake.auth.token;
|
const token = socket.handshake.auth.token;
|
||||||
|
|
||||||
// CHAMADA À API GAMES
|
|
||||||
const response = await axios.get(`${API_URL}/games/${gameId}`, {
|
const response = await axios.get(`${API_URL}/games/${gameId}`, {
|
||||||
headers: { Authorization: token },
|
headers: { Authorization: token },
|
||||||
});
|
});
|
||||||
|
|
||||||
const gameData = response.data.data;
|
const gameData = response.data.data || response.data;
|
||||||
|
|
||||||
|
if (gameData.status === "Ended") {
|
||||||
|
socket.emit("error", { message: "Game already finished." });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Validação: O user pertence a este jogo?
|
|
||||||
if (
|
if (
|
||||||
gameData.player1_user_id != user.id &&
|
gameData.player1_user_id != user.id &&
|
||||||
gameData.player2_user_id != user.id
|
gameData.player2_user_id != user.id &&
|
||||||
|
gameData.player2_user_id !== 1
|
||||||
) {
|
) {
|
||||||
socket.emit("error", { message: "Não pertences a este jogo!" });
|
socket.emit("error", { message: "You are not in this game." });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Validação: O jogo já acabou?
|
const p1Name = gameData.player1?.name || "Player 1";
|
||||||
if (gameData.status === "Ended") {
|
const p2Name = gameData.player2?.name || "Player 2";
|
||||||
socket.emit("error", { message: "Este jogo já terminou." });
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log(`[Laravel] Game ${gameId} validado. A iniciar lógica...`);
|
|
||||||
|
|
||||||
// Preparar jogadores
|
|
||||||
// Nota: O endpoint 'games/{id}' deve retornar info básica dos users ou pelo menos os IDs.
|
|
||||||
// Se não vier o nome, usamos um placeholder ou o nome do user atual se coincidir.
|
|
||||||
|
|
||||||
const p1Name =
|
|
||||||
gameData.player1 && gameData.player1.name
|
|
||||||
? gameData.player1.name
|
|
||||||
: "Player 1";
|
|
||||||
const p2Name =
|
|
||||||
gameData.player2 && gameData.player2.name
|
|
||||||
? gameData.player2.name
|
|
||||||
: "Player 2";
|
|
||||||
|
|
||||||
const p1 = { id: gameData.player1_user_id, name: p1Name };
|
|
||||||
const p2 = { id: gameData.player2_user_id, name: p2Name };
|
|
||||||
|
|
||||||
// Tipo de jogo ('3' ou '9')
|
|
||||||
const type = gameData.type == "9" ? 9 : 3;
|
const type = gameData.type == "9" ? 9 : 3;
|
||||||
|
|
||||||
// CRIAR O JOGO NA MEMÓRIA
|
game = createGame(
|
||||||
game = createGame(gameId, type, p1, p2);
|
gameId,
|
||||||
} catch (error) {
|
type,
|
||||||
console.error(
|
{ id: gameData.player1_user_id, name: p1Name },
|
||||||
`[Erro Laravel] Falha ao buscar Game ${gameId}:`,
|
{ id: gameData.player2_user_id, name: p2Name }
|
||||||
error.message
|
|
||||||
);
|
);
|
||||||
// Dica: Se der 404, o jogo não existe na BD
|
} catch (error) {
|
||||||
socket.emit("error", { message: "Erro ao buscar dados do jogo." });
|
socket.emit("error", { message: "Failed to join game." });
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Juntar o socket à sala do jogo
|
const GHOST_ID = 1;
|
||||||
|
|
||||||
|
if (game && game.players) {
|
||||||
|
if (!game.players[user.id]) {
|
||||||
|
if (game.players[GHOST_ID]) {
|
||||||
|
delete game.players[GHOST_ID];
|
||||||
|
game.players[user.id] = {
|
||||||
|
id: user.id,
|
||||||
|
name: user.name,
|
||||||
|
hand: [],
|
||||||
|
points: 0,
|
||||||
|
tricks: 0,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
socket.join(`game_${gameId}`);
|
socket.join(`game_${gameId}`);
|
||||||
|
|
||||||
// 4. Enviar o estado do jogo ao jogador
|
if (game && game.players[user.id]) {
|
||||||
if (game.players[user.id]) {
|
socket.emit("game-state", game.getStateForPlayer(user.id));
|
||||||
// Envia apenas o que este jogador pode ver (esconde mão do adversário)
|
console.log(`--> Estado enviado para User ${user.id}`);
|
||||||
const gameState = game.getStateForPlayer(user.id);
|
} else {
|
||||||
socket.emit("game-state", gameState);
|
socket.emit("error", { message: "Error joining game state." });
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// --- EVENTO: JOGAR CARTA ---
|
|
||||||
socket.on("play-card", ({ gameId, cardId }) => {
|
socket.on("play-card", ({ gameId, cardId }) => {
|
||||||
const user = getUser(socket.id);
|
const user = getUser(socket.id);
|
||||||
const game = getGame(gameId);
|
const game = getGame(gameId);
|
||||||
|
|
||||||
if (!game || !user) return;
|
if (!game || !user) return;
|
||||||
|
|
||||||
// 1. Processar jogada
|
|
||||||
const result = game.playCard(user.id, cardId);
|
const result = game.playCard(user.id, cardId);
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
@@ -108,15 +118,10 @@ module.exports = (io, socket) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const roomName = `game_${gameId}`;
|
const roomName = `game_${gameId}`;
|
||||||
|
|
||||||
// 2. Atualizar estado visual (Solução rápida: Forçar refresh nos clientes)
|
|
||||||
// O ideal é iterar os sockets da sala e enviar 'game-state' personalizado a cada um
|
|
||||||
const socketsInRoom = io.sockets.adapter.rooms.get(roomName);
|
const socketsInRoom = io.sockets.adapter.rooms.get(roomName);
|
||||||
if (socketsInRoom) {
|
if (socketsInRoom) {
|
||||||
for (const socketId of socketsInRoom) {
|
for (const socketId of socketsInRoom) {
|
||||||
const s = io.sockets.sockets.get(socketId);
|
const s = io.sockets.sockets.get(socketId);
|
||||||
// Precisamos saber qual User ID pertence a este socket para enviar o estado certo
|
|
||||||
// Assumindo que o teu getUser(socketId) funciona:
|
|
||||||
const u = getUser(socketId);
|
const u = getUser(socketId);
|
||||||
if (u && game.players[u.id]) {
|
if (u && game.players[u.id]) {
|
||||||
s.emit("game-state", game.getStateForPlayer(u.id));
|
s.emit("game-state", game.getStateForPlayer(u.id));
|
||||||
@@ -124,20 +129,18 @@ module.exports = (io, socket) => {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 3. Eventos de Animação (Vaza / Fim de Jogo)
|
|
||||||
if (result.action === "trick_resolved") {
|
if (result.action === "trick_resolved") {
|
||||||
io.to(roomName).emit("trick-end", result.trickResult);
|
io.to(roomName).emit("trick-end", result.trickResult);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (result.action === "game_ended") {
|
if (result.action === "game_ended") {
|
||||||
io.to(roomName).emit("game-over", { winnerId: result.winnerId });
|
io.to(roomName).emit("game-over", {
|
||||||
|
winnerId: result.winnerId,
|
||||||
// AQUI: Salvar o resultado na BD 'GAMES'
|
isDraw: result.isDraw,
|
||||||
// axios.put(`${API_URL}/games/${gameId}`, {
|
p1Points: result.player1_points,
|
||||||
// status: 'Ended',
|
p2Points: result.player2_points,
|
||||||
// winner_user_id: result.winnerId,
|
});
|
||||||
// ...pontos...
|
saveGameResult(gameId, result, socket.handshake.auth.token);
|
||||||
// });
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
Generated
+259
@@ -9,6 +9,7 @@
|
|||||||
"version": "1.0.0",
|
"version": "1.0.0",
|
||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.13.2",
|
||||||
"nodemon": "^3.1.11",
|
"nodemon": "^3.1.11",
|
||||||
"socket.io": "^4.8.1"
|
"socket.io": "^4.8.1"
|
||||||
}
|
}
|
||||||
@@ -63,6 +64,23 @@
|
|||||||
"node": ">= 8"
|
"node": ">= 8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/asynckit": {
|
||||||
|
"version": "0.4.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/asynckit/-/asynckit-0.4.0.tgz",
|
||||||
|
"integrity": "sha512-Oei9OH4tRh0YqU3GxhX79dM/mwVgvbZJaSNaRk+bshkj0S5cfHcgYakreBjrHwatXKbz+IoIdYLxrKim2MjW0Q==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
|
"node_modules/axios": {
|
||||||
|
"version": "1.13.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/axios/-/axios-1.13.2.tgz",
|
||||||
|
"integrity": "sha512-VPk9ebNqPcy5lRGuSlKx752IlDatOjT9paPlm8A7yOuW2Fbvp4X3JznJtT4f0GzGLLiWE9W8onz51SqLYwzGaA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"follow-redirects": "^1.15.6",
|
||||||
|
"form-data": "^4.0.4",
|
||||||
|
"proxy-from-env": "^1.1.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/balanced-match": {
|
"node_modules/balanced-match": {
|
||||||
"version": "1.0.2",
|
"version": "1.0.2",
|
||||||
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
"resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.2.tgz",
|
||||||
@@ -112,6 +130,19 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/call-bind-apply-helpers": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/call-bind-apply-helpers/-/call-bind-apply-helpers-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-Sp1ablJ0ivDkSzjcaJdxEunN5/XvksFJ2sMBFfq6x0ryhQV/2b/KwFe21cMpmHtPOSij8K99/wSfoEuTObmuMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"function-bind": "^1.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/chokidar": {
|
"node_modules/chokidar": {
|
||||||
"version": "3.6.0",
|
"version": "3.6.0",
|
||||||
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
|
"resolved": "https://registry.npmjs.org/chokidar/-/chokidar-3.6.0.tgz",
|
||||||
@@ -136,6 +167,18 @@
|
|||||||
"fsevents": "~2.3.2"
|
"fsevents": "~2.3.2"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/combined-stream": {
|
||||||
|
"version": "1.0.8",
|
||||||
|
"resolved": "https://registry.npmjs.org/combined-stream/-/combined-stream-1.0.8.tgz",
|
||||||
|
"integrity": "sha512-FQN4MRfuJeHf7cBbBMJFXhKSDq+2kAArBlmRBvcvFE5BB1HZKXtSFASDhdlz9zOYwxh8lDdnvmMOe/+5cdoEdg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"delayed-stream": "~1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.8"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/concat-map": {
|
"node_modules/concat-map": {
|
||||||
"version": "0.0.1",
|
"version": "0.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz",
|
||||||
@@ -181,6 +224,29 @@
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/delayed-stream": {
|
||||||
|
"version": "1.0.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/delayed-stream/-/delayed-stream-1.0.0.tgz",
|
||||||
|
"integrity": "sha512-ZySD7Nf91aLB0RxL4KGrKHBXl7Eds1DAmEdcoVawXnLD7SDhpNgtuII2aAkg7a7QS41jxPSZ17p4VdGnMHk3MQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=0.4.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/dunder-proto": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/dunder-proto/-/dunder-proto-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-KIN/nDJBQRcXw0MLVhZE9iQHmG68qAVIBg9CqmUYjmQIhgij9U5MFvrqkUL5FbtyyzZuOeOt0zdeRe4UY7ct+A==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"call-bind-apply-helpers": "^1.0.1",
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"gopd": "^1.2.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/engine.io": {
|
"node_modules/engine.io": {
|
||||||
"version": "6.6.4",
|
"version": "6.6.4",
|
||||||
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.6.4.tgz",
|
"resolved": "https://registry.npmjs.org/engine.io/-/engine.io-6.6.4.tgz",
|
||||||
@@ -210,6 +276,51 @@
|
|||||||
"node": ">=10.0.0"
|
"node": ">=10.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/es-define-property": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-define-property/-/es-define-property-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-e3nRfgfUZ4rNGL232gUgX06QNyyez04KdjFrF+LTRoOXmrOgFKDg4BCdsjW8EnT69eqdYGmRpJwiPVYNrCaW3g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-errors": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-errors/-/es-errors-1.3.0.tgz",
|
||||||
|
"integrity": "sha512-Zf5H2Kxt2xjTvbJvP2ZWLEICxA6j+hAmMzIlypy4xcBg1vKVnx89Wy0GbS+kf5cwCVFFzdCFh2XSCFNULS6csw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-object-atoms": {
|
||||||
|
"version": "1.1.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-object-atoms/-/es-object-atoms-1.1.1.tgz",
|
||||||
|
"integrity": "sha512-FGgH2h8zKNim9ljj7dankFPcICIK9Cp5bm+c2gQSYePhpaG5+esrLODihIorn+Pe6FGJzWhXQotPv73jTaldXA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/es-set-tostringtag": {
|
||||||
|
"version": "2.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/es-set-tostringtag/-/es-set-tostringtag-2.1.0.tgz",
|
||||||
|
"integrity": "sha512-j6vWzfrGVfyXxge+O0x5sh6cvxAog0a/4Rdd2K36zCMV5eJ+/+tOAngRO8cODMNWbVRdVlmGZQL2YS3yR8bIUA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"get-intrinsic": "^1.2.6",
|
||||||
|
"has-tostringtag": "^1.0.2",
|
||||||
|
"hasown": "^2.0.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/fill-range": {
|
"node_modules/fill-range": {
|
||||||
"version": "7.1.1",
|
"version": "7.1.1",
|
||||||
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
|
"resolved": "https://registry.npmjs.org/fill-range/-/fill-range-7.1.1.tgz",
|
||||||
@@ -222,6 +333,42 @@
|
|||||||
"node": ">=8"
|
"node": ">=8"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/follow-redirects": {
|
||||||
|
"version": "1.15.11",
|
||||||
|
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.11.tgz",
|
||||||
|
"integrity": "sha512-deG2P0JfjrTxl50XGCDyfI97ZGVCxIpfKYmfyrQ54n5FO/0gfIES8C/Psl6kWVDolizcaaxZJnTS0QSMxvnsBQ==",
|
||||||
|
"funding": [
|
||||||
|
{
|
||||||
|
"type": "individual",
|
||||||
|
"url": "https://github.com/sponsors/RubenVerborgh"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">=4.0"
|
||||||
|
},
|
||||||
|
"peerDependenciesMeta": {
|
||||||
|
"debug": {
|
||||||
|
"optional": true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/form-data": {
|
||||||
|
"version": "4.0.5",
|
||||||
|
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.5.tgz",
|
||||||
|
"integrity": "sha512-8RipRLol37bNs2bhoV67fiTEvdTrbMUYcFTiy3+wuuOnUog2QBHCZWXDRijWQfAkhBj2Uf5UnVaiWwA5vdd82w==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"asynckit": "^0.4.0",
|
||||||
|
"combined-stream": "^1.0.8",
|
||||||
|
"es-set-tostringtag": "^2.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
|
"mime-types": "^2.1.12"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 6"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/fsevents": {
|
"node_modules/fsevents": {
|
||||||
"version": "2.3.3",
|
"version": "2.3.3",
|
||||||
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
"resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.3.tgz",
|
||||||
@@ -236,6 +383,52 @@
|
|||||||
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
"node": "^8.16.0 || ^10.6.0 || >=11.0.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/function-bind": {
|
||||||
|
"version": "1.1.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/function-bind/-/function-bind-1.1.2.tgz",
|
||||||
|
"integrity": "sha512-7XHNxH7qX9xG5mIwxkhumTox/MIRNcOgDrxWsMt2pAr23WHp6MrRlN7FBSFpCpr+oVO0F744iUgR82nJMfG2SA==",
|
||||||
|
"license": "MIT",
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/get-intrinsic": {
|
||||||
|
"version": "1.3.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/get-intrinsic/-/get-intrinsic-1.3.0.tgz",
|
||||||
|
"integrity": "sha512-9fSjSaos/fRIVIp+xSJlE6lfwhES7LNtKaCBIamHsjr2na1BiABJPo0mOjjz8GJDURarmCPGqaiVg5mfjb98CQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"call-bind-apply-helpers": "^1.0.2",
|
||||||
|
"es-define-property": "^1.0.1",
|
||||||
|
"es-errors": "^1.3.0",
|
||||||
|
"es-object-atoms": "^1.1.1",
|
||||||
|
"function-bind": "^1.1.2",
|
||||||
|
"get-proto": "^1.0.1",
|
||||||
|
"gopd": "^1.2.0",
|
||||||
|
"has-symbols": "^1.1.0",
|
||||||
|
"hasown": "^2.0.2",
|
||||||
|
"math-intrinsics": "^1.1.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/get-proto": {
|
||||||
|
"version": "1.0.1",
|
||||||
|
"resolved": "https://registry.npmjs.org/get-proto/-/get-proto-1.0.1.tgz",
|
||||||
|
"integrity": "sha512-sTSfBjoXBp89JvIKIefqw7U2CCebsc74kiY6awiGogKtoSGbgjYE/G/+l9sF3MWFPNc9IcoOC4ODfKHfxFmp0g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"dunder-proto": "^1.0.1",
|
||||||
|
"es-object-atoms": "^1.0.0"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/glob-parent": {
|
"node_modules/glob-parent": {
|
||||||
"version": "5.1.2",
|
"version": "5.1.2",
|
||||||
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
|
"resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-5.1.2.tgz",
|
||||||
@@ -248,6 +441,18 @@
|
|||||||
"node": ">= 6"
|
"node": ">= 6"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/gopd": {
|
||||||
|
"version": "1.2.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/gopd/-/gopd-1.2.0.tgz",
|
||||||
|
"integrity": "sha512-ZUKRh6/kUFoAiTAtTYPZJ3hw9wNxx+BIBOijnlG9PnrJsCcSjs1wyyD6vJpaYtgnzDrKYRSqf3OO6Rfa93xsRg==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/has-flag": {
|
"node_modules/has-flag": {
|
||||||
"version": "3.0.0",
|
"version": "3.0.0",
|
||||||
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
|
"resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz",
|
||||||
@@ -257,6 +462,45 @@
|
|||||||
"node": ">=4"
|
"node": ">=4"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/has-symbols": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/has-symbols/-/has-symbols-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-1cDNdwJ2Jaohmb3sg4OmKaMBwuC48sYni5HUw2DvsC8LjGTLK9h+eb1X6RyuOHe4hT0ULCW68iomhjUoKUqlPQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/has-tostringtag": {
|
||||||
|
"version": "1.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/has-tostringtag/-/has-tostringtag-1.0.2.tgz",
|
||||||
|
"integrity": "sha512-NqADB8VjPFLM2V0VvHUewwwsw0ZWBaIdgo+ieHtK3hasLz4qeCRjYcqfB6AQrBggRKppKF8L52/VqdVsO47Dlw==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"has-symbols": "^1.0.3"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
},
|
||||||
|
"funding": {
|
||||||
|
"url": "https://github.com/sponsors/ljharb"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"node_modules/hasown": {
|
||||||
|
"version": "2.0.2",
|
||||||
|
"resolved": "https://registry.npmjs.org/hasown/-/hasown-2.0.2.tgz",
|
||||||
|
"integrity": "sha512-0hJU9SCPvmMzIBdZFqNPXWa6dqh7WdH0cII9y+CyS8rG3nL48Bclra9HmKhVVUHyPWNH5Y7xDwAB7bfgSjkUMQ==",
|
||||||
|
"license": "MIT",
|
||||||
|
"dependencies": {
|
||||||
|
"function-bind": "^1.1.2"
|
||||||
|
},
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/ignore-by-default": {
|
"node_modules/ignore-by-default": {
|
||||||
"version": "1.0.1",
|
"version": "1.0.1",
|
||||||
"resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz",
|
"resolved": "https://registry.npmjs.org/ignore-by-default/-/ignore-by-default-1.0.1.tgz",
|
||||||
@@ -305,6 +549,15 @@
|
|||||||
"node": ">=0.12.0"
|
"node": ">=0.12.0"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/math-intrinsics": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/math-intrinsics/-/math-intrinsics-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-/IXtbwEk5HTPyEwyKX6hGkYXxM9nbj64B+ilVJnC/R6B0pH5G4V3b0pVbL7DBj4tkhBAppbQUlf6F6Xl9LHu1g==",
|
||||||
|
"license": "MIT",
|
||||||
|
"engines": {
|
||||||
|
"node": ">= 0.4"
|
||||||
|
}
|
||||||
|
},
|
||||||
"node_modules/mime-db": {
|
"node_modules/mime-db": {
|
||||||
"version": "1.52.0",
|
"version": "1.52.0",
|
||||||
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
"resolved": "https://registry.npmjs.org/mime-db/-/mime-db-1.52.0.tgz",
|
||||||
@@ -411,6 +664,12 @@
|
|||||||
"url": "https://github.com/sponsors/jonschlinkert"
|
"url": "https://github.com/sponsors/jonschlinkert"
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
"node_modules/proxy-from-env": {
|
||||||
|
"version": "1.1.0",
|
||||||
|
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
|
||||||
|
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg==",
|
||||||
|
"license": "MIT"
|
||||||
|
},
|
||||||
"node_modules/pstree.remy": {
|
"node_modules/pstree.remy": {
|
||||||
"version": "1.1.8",
|
"version": "1.1.8",
|
||||||
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
"resolved": "https://registry.npmjs.org/pstree.remy/-/pstree.remy-1.1.8.tgz",
|
||||||
|
|||||||
@@ -11,6 +11,7 @@
|
|||||||
"license": "ISC",
|
"license": "ISC",
|
||||||
"description": "",
|
"description": "",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
|
"axios": "^1.13.2",
|
||||||
"nodemon": "^3.1.11",
|
"nodemon": "^3.1.11",
|
||||||
"socket.io": "^4.8.1"
|
"socket.io": "^4.8.1"
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,19 +1,67 @@
|
|||||||
import { Server } from "socket.io";
|
import { Server } from "socket.io";
|
||||||
|
import axios from "axios";
|
||||||
import { handleConnectionEvents } from "./events/connection.js";
|
import { handleConnectionEvents } from "./events/connection.js";
|
||||||
|
import { addUser, removeUser } from "./state/connection.js";
|
||||||
|
import gameEvents from "./events/game.js";
|
||||||
|
|
||||||
export const server = {
|
export const server = {
|
||||||
io: null,
|
io: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const API_URL = "http://localhost:8000/api/v1";
|
||||||
|
|
||||||
export const serverStart = (port) => {
|
export const serverStart = (port) => {
|
||||||
server.io = new Server(port, {
|
server.io = new Server(port, {
|
||||||
cors: {
|
cors: {
|
||||||
origin: "*",
|
origin: "*",
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
server.io.use(async (socket, next) => {
|
||||||
|
const token = socket.handshake.auth.token;
|
||||||
|
|
||||||
|
if (!token) {
|
||||||
|
return next(new Error("Authentication error: No token provided"));
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
const response = await axios.get(`${API_URL}/users/me`, {
|
||||||
|
headers: { Authorization: token },
|
||||||
|
});
|
||||||
|
|
||||||
|
const user = response.data.data || response.data;
|
||||||
|
|
||||||
|
if (!user || !user.id) {
|
||||||
|
return next(new Error("Authentication error: User data not found"));
|
||||||
|
}
|
||||||
|
|
||||||
|
addUser(socket.id, user);
|
||||||
|
|
||||||
|
next();
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response) {
|
||||||
|
console.error(
|
||||||
|
"❌ Erro Laravel:",
|
||||||
|
error.response.status,
|
||||||
|
error.response.data
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
console.error("❌ Erro Rede:", error.message);
|
||||||
|
}
|
||||||
|
next(new Error("Authentication error: Invalid token"));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
server.io.on("connection", (socket) => {
|
server.io.on("connection", (socket) => {
|
||||||
console.log("New connection:", socket.id);
|
console.log("New connection:", socket.id);
|
||||||
|
|
||||||
|
gameEvents(server.io, socket);
|
||||||
|
|
||||||
handleConnectionEvents(server.io, socket);
|
handleConnectionEvents(server.io, socket);
|
||||||
|
|
||||||
|
socket.on("disconnect", () => {
|
||||||
|
removeUser(socket.id);
|
||||||
|
console.log("Disconnected:", socket.id);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -1,19 +1,17 @@
|
|||||||
const users = new Map();
|
const users = new Map();
|
||||||
|
|
||||||
export const addUser = (socket, user) => {
|
export const addUser = (socketId, userData) => {
|
||||||
users.set(socket.id, user);
|
users.set(socketId, userData);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const removeUser = (socketID) => {
|
export const getUser = (socketId) => {
|
||||||
const userToDelete = { ...users.get(socketID) };
|
return users.get(socketId);
|
||||||
users.delete(socketID);
|
|
||||||
return userToDelete;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUser = (socketID) => {
|
export const removeUser = (socketId) => {
|
||||||
return users.get(socketID);
|
users.delete(socketId);
|
||||||
};
|
};
|
||||||
|
|
||||||
export const getUserCount = () => {
|
export const getUserCount = () => {
|
||||||
return users.size;
|
return users.size;
|
||||||
};
|
};
|
||||||
+14
-34
@@ -1,49 +1,29 @@
|
|||||||
// websockets/state/game.js
|
const BiscaGame = require("../classes/BiscaGame");
|
||||||
|
|
||||||
// Importamos a classe que criaste no Passo 1
|
|
||||||
const BiscaGame = require('../classes/BiscaGame');
|
|
||||||
|
|
||||||
// Aqui guardamos todos os jogos a decorrer na memória RAM do servidor
|
|
||||||
// Chave: ID do jogo (matchId) -> Valor: Instância do Jogo (O objeto com o baralho, mãos, etc)
|
|
||||||
const activeGames = new Map();
|
const activeGames = new Map();
|
||||||
|
|
||||||
/**
|
|
||||||
* Cria um novo jogo, guarda-o na memória e retorna-o.
|
|
||||||
*/
|
|
||||||
const createGame = (id, type, player1, player2) => {
|
const createGame = (id, type, player1, player2) => {
|
||||||
// 1. Instanciar a classe (Criar o jogo com as regras)
|
const game = new BiscaGame(id, type, player1, player2);
|
||||||
const game = new BiscaGame(id, type, player1, player2);
|
|
||||||
|
|
||||||
// 2. Preparar o jogo (Baralhar, dar cartas iniciais)
|
|
||||||
game.init();
|
|
||||||
|
|
||||||
// 3. Guardar no "Armazém" para não se perder
|
game.init();
|
||||||
activeGames.set(id, game);
|
|
||||||
|
|
||||||
console.log(`[Game State] Jogo criado: ${id} (Tipo: ${type})`);
|
activeGames.set(id, game);
|
||||||
return game;
|
|
||||||
|
console.log(`[Game State] Game created: ${id} (Type: ${type})`);
|
||||||
|
return game;
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Busca um jogo existente pelo ID.
|
|
||||||
* Usado sempre que um jogador envia uma carta, para sabermos a qual jogo pertence.
|
|
||||||
*/
|
|
||||||
const getGame = (id) => {
|
const getGame = (id) => {
|
||||||
return activeGames.get(id);
|
return activeGames.get(id);
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
|
||||||
* Remove o jogo da memória.
|
|
||||||
* Usado quando o jogo acaba para libertar RAM.
|
|
||||||
*/
|
|
||||||
const removeGame = (id) => {
|
const removeGame = (id) => {
|
||||||
activeGames.delete(id);
|
activeGames.delete(id);
|
||||||
console.log(`[Game State] Jogo removido: ${id}`);
|
console.log(`[Game State] Game removed: ${id}`);
|
||||||
};
|
};
|
||||||
|
|
||||||
// Exportamos estas funções para serem usadas no ficheiro de eventos
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
createGame,
|
createGame,
|
||||||
getGame,
|
getGame,
|
||||||
removeGame
|
removeGame,
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user