This commit is contained in:
cay 2026-04-12 11:27:10 +01:00
parent c3a1ddfc85
commit 0e23a4ec57
2 changed files with 29 additions and 14 deletions

View File

@ -188,16 +188,16 @@ function startReadyTimer(io, matchId) {
if (io._arenaTimers.has(matchId)) return;
let remaining = READY_TIMEOUT;
io.to("arena_" + matchId).emit("ready_timer", { remaining });
emitToMatch(io, matchId, "ready_timer", { remaining });
const interval = setInterval(() => {
remaining--;
io.to("arena_" + matchId).emit("ready_timer", { remaining });
emitToMatch(io, matchId, "ready_timer", { remaining });
if (remaining <= 0) {
clearInterval(interval);
io._arenaTimers.delete(matchId);
console.log(`[1v1] Match ${matchId} abgebrochen Zeit abgelaufen.`);
io.to("arena_" + matchId).emit("match_cancelled", {
emitToMatch(io, matchId, "match_cancelled", {
reason: "timeout",
message: "Zeit abgelaufen.",
});
@ -359,16 +359,30 @@ function registerTeamModeHandlers(io, socket, mode) {
});
}
/* ── Helper: Event an beide Spieler senden (Room-Broadcast) ── */
function emitToMatch(io, matchId, event, data) {
io.to("arena_" + matchId).emit(event, data);
/* ── Hilfsfunktionen: Direkte Socket-Zustellung ── */
function getRoom(io, matchId) {
return io._arenaRooms?.get(matchId) || null;
}
// Sendet an beide Spieler über ihre gespeicherten Socket-IDs
function emitToMatch(io, matchId, event, data) {
const room = getRoom(io, matchId);
if (!room) { console.warn(`[emitToMatch] Kein Room für ${matchId}`); return; }
["player1", "player2"].forEach((slot) => {
if (room.sockets[slot]) {
io.to(room.sockets[slot]).emit(event, data);
console.log(`[emitToMatch] ${event}${slot} (${room.sockets[slot]})`);
}
});
}
// Sendet an den Gegner des Senders
function emitToOpponent(io, matchId, senderSlot, event, data) {
const opponentSlot = senderSlot === "player1" ? "player2" : "player1";
const room = io._arenaRooms?.get(matchId);
if (room?.sockets[opponentSlot]) {
io.to(room.sockets[opponentSlot]).emit(event, data);
const room = getRoom(io, matchId);
if (!room) return;
const oppSlot = senderSlot === "player1" ? "player2" : "player1";
if (room.sockets[oppSlot]) {
io.to(room.sockets[oppSlot]).emit(event, data);
}
}
@ -465,10 +479,10 @@ function registerArenaHandlers(io, socket) {
if (!io._arenaReady.has(matchId)) io._arenaReady.set(matchId, new Set());
const readySet = io._arenaReady.get(matchId);
readySet.add(slot);
io.to("arena_" + matchId).emit("ready_status", {
readyCount: readySet.size,
readySlots: Array.from(readySet),
});
const readyData = { readyCount: readySet.size, readySlots: Array.from(readySet) };
// Direkt an beide Spieler senden (zuverlässiger als Room-Broadcast)
emitToMatch(io, matchId, "ready_status", readyData);
console.log(`[1v1] ready_status: ${readySet.size}/2 bereit | Match ${matchId}`);
if (readySet.size >= 2) {
stopReadyTimer(io, matchId);
io._arenaReady.delete(matchId);

View File

@ -930,6 +930,7 @@
});
socket.on("ready_status", (data) => {
console.log("[1v1] ready_status empfangen:", data);
const pip1 = document.getElementById("pip-player1");
const pip2 = document.getElementById("pip-player2");
if (data.readySlots) {