@@ -6,6 +6,9 @@ class BiscaGame {
|
||||
this.deck = [];
|
||||
this.trumpCard = null;
|
||||
this.trumpSuit = null;
|
||||
this.timer = null;
|
||||
this.turnDeadline = null;
|
||||
this.timeoutCallback = null;
|
||||
|
||||
this.table = {
|
||||
playerCard: null,
|
||||
@@ -50,6 +53,44 @@ class BiscaGame {
|
||||
this.status = "playing";
|
||||
}
|
||||
|
||||
startTurnTimer(callback) {
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
|
||||
this.timeoutCallback = callback;
|
||||
const DURATION_MS = 20000;
|
||||
this.turnDeadline = Date.now() + DURATION_MS;
|
||||
|
||||
console.log(`[Game ${this.id}] Timer started for User ${this.currentTurn}`);
|
||||
|
||||
this.timer = setTimeout(() => {
|
||||
this.handleTimeout();
|
||||
}, DURATION_MS);
|
||||
}
|
||||
|
||||
stopTimer() {
|
||||
if (this.timer) clearTimeout(this.timer);
|
||||
this.timer = null;
|
||||
this.turnDeadline = null;
|
||||
}
|
||||
|
||||
handleTimeout() {
|
||||
console.log(`[Game ${this.id}] Timeout! Auto-playing for ${this.currentTurn}`);
|
||||
|
||||
const lowestCard = this.getLowestCard(this.currentTurn);
|
||||
|
||||
if (lowestCard && this.timeoutCallback) {
|
||||
this.timeoutCallback(this.currentTurn, lowestCard.id);
|
||||
}
|
||||
}
|
||||
|
||||
getLowestCard(userId) {
|
||||
const hand = this.players[userId].hand;
|
||||
if (!hand || hand.length === 0) return null;
|
||||
|
||||
const sortedHand = [...hand].sort((a, b) => a.strength - b.strength);
|
||||
return sortedHand[0];
|
||||
}
|
||||
|
||||
createDeck() {
|
||||
const suits = ["c", "e", "o", "p"];
|
||||
const ranks = [1, 2, 3, 4, 5, 6, 7, 11, 12, 13];
|
||||
@@ -105,6 +146,8 @@ class BiscaGame {
|
||||
}
|
||||
}
|
||||
|
||||
this.stopTimer();
|
||||
|
||||
player.hand.splice(cardIndex, 1);
|
||||
|
||||
if (!this.table.playerCard) {
|
||||
@@ -152,6 +195,7 @@ class BiscaGame {
|
||||
|
||||
if (this.players[winnerId].hand.length === 0) {
|
||||
this.status = "finished";
|
||||
this.stopTimer();
|
||||
|
||||
const pIds = Object.keys(this.players);
|
||||
const p1Obj = this.players[pIds[0]];
|
||||
@@ -205,6 +249,7 @@ class BiscaGame {
|
||||
points: this.players[oppId].points,
|
||||
cardCount: this.players[oppId].hand.length,
|
||||
},
|
||||
turnDeadline: this.turnDeadline,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user