SyntaxSquad/DADProject#46 SyntaxSquad/DADProject#47 Added initial setup for websockets
This commit is contained in:
@@ -0,0 +1,192 @@
|
||||
class BiscaGame {
|
||||
constructor(id, type, player1, player2) {
|
||||
this.id = id;
|
||||
this.type = type;
|
||||
this.deck = [];
|
||||
this.trumpCard = null;
|
||||
this.trumpSuit = null;
|
||||
|
||||
this.table = {
|
||||
playerCard: null,
|
||||
opponentCard: null,
|
||||
firstPlayerId: null,
|
||||
};
|
||||
|
||||
this.currentTurn = null;
|
||||
this.status = "pending";
|
||||
|
||||
this.players = {
|
||||
[player1.id]: {
|
||||
id: player1.id,
|
||||
name: player1.name,
|
||||
hand: [],
|
||||
points: 0,
|
||||
tricks: 0,
|
||||
},
|
||||
[player2.id]: {
|
||||
id: player2.id,
|
||||
name: player2.name,
|
||||
hand: [],
|
||||
points: 0,
|
||||
tricks: 0,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
init() {
|
||||
this.deck = this.createDeck();
|
||||
const lastCard = this.deck.pop();
|
||||
this.trumpCard = lastCard;
|
||||
this.trumpSuit = lastCard.suit;
|
||||
|
||||
this.dealInitialCards();
|
||||
|
||||
this.currentTurn = Object.keys(this.players)[0];
|
||||
this.status = "playing";
|
||||
}
|
||||
|
||||
createDeck() {
|
||||
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({
|
||||
id: `${suit}-${rank}`,
|
||||
suit,
|
||||
rank,
|
||||
strength: this.getStrength(rank),
|
||||
points: this.getPoints(rank),
|
||||
});
|
||||
}
|
||||
}
|
||||
return deck.sort(() => Math.random() - 0.5); // Baralhar
|
||||
}
|
||||
|
||||
dealInitialCards() {
|
||||
const cardsToDeal = this.type == 9 ? 9 : 3;
|
||||
const pIds = Object.keys(this.players);
|
||||
for (let i = 0; i < cardsToDeal; i++) {
|
||||
pIds.forEach((id) => this.drawCard(id));
|
||||
}
|
||||
}
|
||||
|
||||
drawCard(userId) {
|
||||
if (this.deck.length > 0) {
|
||||
this.players[userId].hand.push(this.deck.pop());
|
||||
} else if (this.trumpCard) {
|
||||
this.players[userId].hand.push(this.trumpCard);
|
||||
this.trumpCard = null;
|
||||
}
|
||||
}
|
||||
|
||||
playCard(userId, cardId) {
|
||||
if (this.currentTurn != userId) return { error: "Não é a tua vez!" };
|
||||
|
||||
const player = this.players[userId];
|
||||
const cardIndex = player.hand.findIndex((c) => c.id === cardId);
|
||||
|
||||
if (cardIndex === -1) return { error: "Não tens essa carta!" };
|
||||
|
||||
const card = player.hand[cardIndex];
|
||||
|
||||
player.hand.splice(cardIndex, 1); // Remove da mão (Server-side)
|
||||
|
||||
if (!this.table.playerCard) {
|
||||
this.table.playerCard = card;
|
||||
this.table.firstPlayerId = userId;
|
||||
this.currentTurn = this.getOpponentId(userId); // Passa a vez
|
||||
return { action: "next_turn" };
|
||||
} else {
|
||||
this.table.opponentCard = card;
|
||||
return this.resolveTrick();
|
||||
}
|
||||
}
|
||||
|
||||
resolveTrick() {
|
||||
const p1Id = this.table.firstPlayerId;
|
||||
const p2Id = this.getOpponentId(p1Id);
|
||||
const card1 = this.table.playerCard;
|
||||
const card2 = this.table.opponentCard;
|
||||
|
||||
let winnerId = p2Id;
|
||||
let p1Wins = false;
|
||||
|
||||
if (card2.suit === this.trumpSuit && card1.suit !== this.trumpSuit)
|
||||
p1Wins = false;
|
||||
else if (card1.suit === this.trumpSuit && card2.suit !== this.trumpSuit)
|
||||
p1Wins = true;
|
||||
else if (card1.suit === card2.suit)
|
||||
p1Wins = card1.strength > card2.strength;
|
||||
else p1Wins = true;
|
||||
|
||||
winnerId = p1Wins ? p1Id : p2Id;
|
||||
|
||||
const points = card1.points + card2.points;
|
||||
this.players[winnerId].points += points;
|
||||
this.players[winnerId].tricks += 1;
|
||||
this.currentTurn = winnerId;
|
||||
|
||||
if (this.type == 3) {
|
||||
this.drawCard(winnerId);
|
||||
this.drawCard(this.getOpponentId(winnerId));
|
||||
}
|
||||
|
||||
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 };
|
||||
}
|
||||
|
||||
return { action: "trick_resolved", trickResult };
|
||||
}
|
||||
|
||||
getStateForPlayer(userId) {
|
||||
const oppId = this.getOpponentId(userId);
|
||||
|
||||
return {
|
||||
id: this.id,
|
||||
trumpCard: this.trumpCard,
|
||||
trumpSuit: this.trumpSuit,
|
||||
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,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
getOpponentId(id) {
|
||||
return Object.keys(this.players).find((pid) => pid != id);
|
||||
}
|
||||
getPoints(r) {
|
||||
const p = { 1: 11, 7: 10, 13: 4, 11: 3, 12: 2 };
|
||||
return p[r] || 0;
|
||||
}
|
||||
getStrength(r) {
|
||||
const s = {
|
||||
1: 10,
|
||||
7: 9,
|
||||
13: 8,
|
||||
11: 7,
|
||||
12: 6,
|
||||
6: 5,
|
||||
5: 4,
|
||||
4: 3,
|
||||
3: 2,
|
||||
2: 1,
|
||||
};
|
||||
return s[r] || 0;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BiscaGame;
|
||||
Reference in New Issue
Block a user