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) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
|
||||
this.deck = [];
|
||||
this.trumpCard = null;
|
||||
this.trumpSuit = null;
|
||||
@@ -14,6 +15,7 @@ class BiscaGame {
|
||||
|
||||
this.currentTurn = null;
|
||||
this.status = "pending";
|
||||
this.startTime = null;
|
||||
|
||||
this.players = {
|
||||
[player1.id]: {
|
||||
@@ -41,7 +43,10 @@ class BiscaGame {
|
||||
|
||||
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";
|
||||
}
|
||||
|
||||
@@ -49,6 +54,7 @@ class BiscaGame {
|
||||
const suits = ["c", "e", "o", "p"];
|
||||
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
|
||||
let deck = [];
|
||||
|
||||
for (const suit of suits) {
|
||||
for (const rank of ranks) {
|
||||
deck.push({
|
||||
@@ -60,7 +66,7 @@ class BiscaGame {
|
||||
});
|
||||
}
|
||||
}
|
||||
return deck.sort(() => Math.random() - 0.5); // Baralhar
|
||||
return deck.sort(() => Math.random() - 0.5);
|
||||
}
|
||||
|
||||
dealInitialCards() {
|
||||
@@ -81,21 +87,30 @@ class BiscaGame {
|
||||
}
|
||||
|
||||
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 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];
|
||||
|
||||
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) {
|
||||
this.table.playerCard = card;
|
||||
this.table.firstPlayerId = userId;
|
||||
this.currentTurn = this.getOpponentId(userId); // Passa a vez
|
||||
this.currentTurn = this.getOpponentId(userId);
|
||||
return { action: "next_turn" };
|
||||
} else {
|
||||
this.table.opponentCard = card;
|
||||
@@ -133,12 +148,43 @@ class BiscaGame {
|
||||
}
|
||||
|
||||
const trickResult = { winnerId, points, cards: [card1, card2] };
|
||||
|
||||
this.table = { playerCard: null, opponentCard: null, firstPlayerId: null };
|
||||
|
||||
if (this.players[winnerId].hand.length === 0) {
|
||||
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 };
|
||||
@@ -146,7 +192,6 @@ class BiscaGame {
|
||||
|
||||
getStateForPlayer(userId) {
|
||||
const oppId = this.getOpponentId(userId);
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
trumpCard: this.trumpCard,
|
||||
@@ -154,10 +199,8 @@ class BiscaGame {
|
||||
deckCount: this.deck.length + (this.trumpCard ? 1 : 0),
|
||||
currentTurn: this.currentTurn,
|
||||
table: this.table,
|
||||
|
||||
hand: this.players[userId].hand,
|
||||
points: this.players[userId].points,
|
||||
|
||||
opponent: {
|
||||
points: this.players[oppId].points,
|
||||
cardCount: this.players[oppId].hand.length,
|
||||
|
||||
Reference in New Issue
Block a user