hgjf
This commit is contained in:
parent
26725869d6
commit
dbe756c9fc
@ -399,7 +399,7 @@ function registerArenaHandlers(io, socket) {
|
|||||||
|
|
||||||
if (!io._arenaRooms) io._arenaRooms = new Map();
|
if (!io._arenaRooms) io._arenaRooms = new Map();
|
||||||
if (!io._arenaRooms.has(matchId))
|
if (!io._arenaRooms.has(matchId))
|
||||||
io._arenaRooms.set(matchId, { sockets: {}, names: {} });
|
io._arenaRooms.set(matchId, { sockets: {}, names: {}, boardCards: [] });
|
||||||
|
|
||||||
const room = io._arenaRooms.get(matchId);
|
const room = io._arenaRooms.get(matchId);
|
||||||
room.sockets[slot] = socket.id;
|
room.sockets[slot] = socket.id;
|
||||||
@ -419,6 +419,11 @@ function registerArenaHandlers(io, socket) {
|
|||||||
|
|
||||||
socket.join("arena_" + matchId);
|
socket.join("arena_" + matchId);
|
||||||
|
|
||||||
|
// Bereits gespielte Karten an neu verbundenen Spieler senden
|
||||||
|
if (room.boardCards?.length > 0) {
|
||||||
|
socket.emit("board_sync", { cards: room.boardCards });
|
||||||
|
}
|
||||||
|
|
||||||
const otherSlot = slot === "player1" ? "player2" : "player1";
|
const otherSlot = slot === "player1" ? "player2" : "player1";
|
||||||
if (room.sockets[otherSlot]) {
|
if (room.sockets[otherSlot]) {
|
||||||
console.log(
|
console.log(
|
||||||
@ -480,11 +485,29 @@ function registerArenaHandlers(io, socket) {
|
|||||||
console.log(`[1v1] Zug: ${slot} → ${nextSlot} | Match ${matchId}`);
|
console.log(`[1v1] Zug: ${slot} → ${nextSlot} | Match ${matchId}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* ── Karte gespielt → an Gegner weiterleiten ── */
|
/* ── Karte gespielt → direkt an Gegner-Socket senden ── */
|
||||||
socket.on("card_played", (data) => {
|
socket.on("card_played", (data) => {
|
||||||
const { matchId } = data;
|
const { matchId, slot } = data;
|
||||||
if (!matchId) return;
|
if (!matchId) return;
|
||||||
socket.to("arena_" + matchId).emit("card_played", data);
|
|
||||||
|
const opponentSlot = slot === "player1" ? "player2" : "player1";
|
||||||
|
const room = io._arenaRooms?.get(matchId);
|
||||||
|
|
||||||
|
// Direkt an Gegner-Socket senden (zuverlässiger als Room-Broadcast)
|
||||||
|
if (room?.sockets[opponentSlot]) {
|
||||||
|
io.to(room.sockets[opponentSlot]).emit("card_played", data);
|
||||||
|
} else {
|
||||||
|
// Fallback: Room-Broadcast
|
||||||
|
socket.to("arena_" + matchId).emit("card_played", data);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Karte im Server-State speichern (für Reconnect)
|
||||||
|
const roomState = io._arenaRooms?.get(matchId);
|
||||||
|
if (roomState) {
|
||||||
|
roomState.boardCards = roomState.boardCards || [];
|
||||||
|
roomState.boardCards.push(data);
|
||||||
|
}
|
||||||
|
|
||||||
console.log(`[1v1] card_played: ${data.card?.name} → ${data.boardSlot} | Match ${matchId}`);
|
console.log(`[1v1] card_played: ${data.card?.name} → ${data.boardSlot} | Match ${matchId}`);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
@ -864,6 +864,18 @@
|
|||||||
if (lockOverlay) lockOverlay.style.display = "flex";
|
if (lockOverlay) lockOverlay.style.display = "flex";
|
||||||
});
|
});
|
||||||
|
|
||||||
|
/* ── Board-Sync bei Reconnect ──────────────────────────── */
|
||||||
|
socket.on("board_sync", (data) => {
|
||||||
|
if (!data.cards) return;
|
||||||
|
data.cards.forEach((cardData) => {
|
||||||
|
const slotEl = document.getElementById(cardData.boardSlot);
|
||||||
|
if (!slotEl || boardState[cardData.boardSlot]) return; // bereits gesetzt
|
||||||
|
boardState[cardData.boardSlot] = cardData.card;
|
||||||
|
renderCardOnBoard(slotEl, cardData.card);
|
||||||
|
});
|
||||||
|
console.log("[1v1] Board sync:", data.cards.length, "Karten geladen");
|
||||||
|
});
|
||||||
|
|
||||||
/* ── Server: Zugwechsel ──────────────────────────────── */
|
/* ── Server: Zugwechsel ──────────────────────────────── */
|
||||||
let lastTurnChangeSlot = null;
|
let lastTurnChangeSlot = null;
|
||||||
socket.on("turn_change", (data) => {
|
socket.on("turn_change", (data) => {
|
||||||
@ -1347,12 +1359,26 @@
|
|||||||
});
|
});
|
||||||
|
|
||||||
/* ── Gegner hat Karte gespielt → auf Board anzeigen ── */
|
/* ── Gegner hat Karte gespielt → auf Board anzeigen ── */
|
||||||
|
const receivedCardEvents = new Set(); // Duplikat-Schutz
|
||||||
socket.on("card_played", (data) => {
|
socket.on("card_played", (data) => {
|
||||||
if (data.slot === mySlot) return; // eigene Aktion, bereits lokal gesetzt
|
// Eigene Aktionen ignorieren (bereits lokal gesetzt)
|
||||||
|
if (data.slot === mySlot) return;
|
||||||
|
|
||||||
|
// Duplikat-Schutz: dasselbe boardSlot-Event nicht doppelt verarbeiten
|
||||||
|
const eventKey = data.boardSlot + "_" + (data.card?.name || "");
|
||||||
|
if (receivedCardEvents.has(eventKey)) return;
|
||||||
|
receivedCardEvents.add(eventKey);
|
||||||
|
// Key nach 2s wieder freigeben (für spätere Karten auf demselben Slot)
|
||||||
|
setTimeout(() => receivedCardEvents.delete(eventKey), 2000);
|
||||||
|
|
||||||
const slotEl = document.getElementById(data.boardSlot);
|
const slotEl = document.getElementById(data.boardSlot);
|
||||||
if (!slotEl) return;
|
if (!slotEl) {
|
||||||
|
console.warn("[1v1] card_played: Slot nicht gefunden:", data.boardSlot);
|
||||||
|
return;
|
||||||
|
}
|
||||||
boardState[data.boardSlot] = data.card;
|
boardState[data.boardSlot] = data.card;
|
||||||
renderCardOnBoard(slotEl, data.card);
|
renderCardOnBoard(slotEl, data.card);
|
||||||
|
console.log("[1v1] Gegner Karte:", data.card?.name, "→", data.boardSlot);
|
||||||
});
|
});
|
||||||
|
|
||||||
/* Wenn Zug wechselt: draggable aktualisieren */
|
/* Wenn Zug wechselt: draggable aktualisieren */
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user