Matches almost done still refining
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
class BiscaGame {
|
||||
constructor(id, type, player1, player2) {
|
||||
constructor(id, type, player1, player2, dbId = null, onGameEnd = null) {
|
||||
this.id = id;
|
||||
this.dbId = dbId;
|
||||
this.type = type;
|
||||
this.onGameEnd = onGameEnd;
|
||||
|
||||
this.deck = [];
|
||||
this.trumpCard = null;
|
||||
@@ -17,7 +19,7 @@ class BiscaGame {
|
||||
};
|
||||
|
||||
this.currentTurn = null;
|
||||
this.status = 'pending';
|
||||
this.status = "pending";
|
||||
this.startTime = null;
|
||||
|
||||
this.players = {
|
||||
@@ -50,7 +52,7 @@ class BiscaGame {
|
||||
this.currentTurn = playerIds.find((id) => id != 1) || playerIds[0];
|
||||
|
||||
this.startTime = Date.now();
|
||||
this.status = 'playing';
|
||||
this.status = "playing";
|
||||
}
|
||||
|
||||
startTurnTimer(callback) {
|
||||
@@ -75,7 +77,7 @@ class BiscaGame {
|
||||
|
||||
handleTimeout() {
|
||||
console.log(
|
||||
`[Game ${this.id}] Timeout! Auto-playing for ${this.currentTurn}`,
|
||||
`[Game ${this.id}] Timeout! Auto-playing for ${this.currentTurn}`
|
||||
);
|
||||
|
||||
const lowestCard = this.getLowestCard(this.currentTurn);
|
||||
@@ -94,7 +96,7 @@ class BiscaGame {
|
||||
}
|
||||
|
||||
createDeck() {
|
||||
const suits = ['c', 'e', 'o', 'p'];
|
||||
const suits = ["c", "e", "o", "p"];
|
||||
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
|
||||
let deck = [];
|
||||
|
||||
@@ -130,7 +132,7 @@ class BiscaGame {
|
||||
}
|
||||
|
||||
playCard(userId, cardId) {
|
||||
if (this.currentTurn != userId) return { error: 'Not your turn!' };
|
||||
if (this.currentTurn != userId) return { error: "Not your turn!" };
|
||||
|
||||
const player = this.players[userId];
|
||||
const cardIndex = player.hand.findIndex((c) => c.id === cardId);
|
||||
@@ -144,7 +146,7 @@ class BiscaGame {
|
||||
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!' };
|
||||
return { error: "You must follow the suit!" };
|
||||
}
|
||||
}
|
||||
|
||||
@@ -156,7 +158,7 @@ class BiscaGame {
|
||||
this.table.playerCard = card;
|
||||
this.table.firstPlayerId = userId;
|
||||
this.currentTurn = this.getOpponentId(userId);
|
||||
return { action: 'next_turn' };
|
||||
return { action: "next_turn" };
|
||||
} else {
|
||||
this.table.opponentCard = card;
|
||||
return this.resolveTrick();
|
||||
@@ -196,7 +198,7 @@ class BiscaGame {
|
||||
this.table = { playerCard: null, opponentCard: null, firstPlayerId: null };
|
||||
|
||||
if (this.players[winnerId].hand.length === 0) {
|
||||
this.status = 'finished';
|
||||
this.status = "finished";
|
||||
this.stopTimer();
|
||||
|
||||
const pIds = Object.keys(this.players);
|
||||
@@ -216,11 +218,11 @@ class BiscaGame {
|
||||
const finalLoser = isDraw
|
||||
? null
|
||||
: gameWinnerId == p1Obj.id
|
||||
? p2Obj.id
|
||||
: p1Obj.id;
|
||||
? p2Obj.id
|
||||
: p1Obj.id;
|
||||
|
||||
return {
|
||||
action: 'game_ended',
|
||||
action: "game_ended",
|
||||
trickResult,
|
||||
winnerId: finalWinner,
|
||||
loserId: finalLoser,
|
||||
@@ -231,15 +233,20 @@ class BiscaGame {
|
||||
player2_points: p2Obj.points,
|
||||
totalTime: totalTime,
|
||||
};
|
||||
if (this.onGameEnd) {
|
||||
this.onGameEnd(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
return { action: 'trick_resolved', trickResult };
|
||||
return { action: "trick_resolved", trickResult };
|
||||
}
|
||||
|
||||
getStateForPlayer(userId) {
|
||||
const oppId = this.getOpponentId(userId);
|
||||
return {
|
||||
id: this.id,
|
||||
dbId: this.dbId,
|
||||
trumpCard: this.trumpCard,
|
||||
trumpSuit: this.trumpSuit,
|
||||
deckCount: this.deck.length + (this.trumpCard ? 1 : 0),
|
||||
|
||||
Reference in New Issue
Block a user