Added win detection
This commit is contained in:
@@ -3,34 +3,38 @@
|
|||||||
|
|
||||||
<head>
|
<head>
|
||||||
<meta charset="UTF-8">
|
<meta charset="UTF-8">
|
||||||
<title>Wolf & Schafe</title>
|
<title>Wolf & Schafe - Multiplayer</title>
|
||||||
<style>
|
<style>
|
||||||
body {
|
body {
|
||||||
font-family: Arial, sans-serif;
|
font-family: 'Segoe UI', Arial, sans-serif;
|
||||||
background: #f2f2f2;
|
background: #f0f2f5;
|
||||||
text-align: center;
|
text-align: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
#status {
|
#status {
|
||||||
font-size: 18px;
|
font-size: 20px;
|
||||||
margin-bottom: 15px;
|
font-weight: bold;
|
||||||
|
margin: 15px;
|
||||||
|
min-height: 50px;
|
||||||
}
|
}
|
||||||
|
|
||||||
svg {
|
svg {
|
||||||
background: white;
|
background: white;
|
||||||
border: 2px solid #ccc;
|
border-radius: 8px;
|
||||||
margin: auto;
|
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
|
||||||
display: block;
|
display: block;
|
||||||
|
margin: auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
.node {
|
.node {
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
stroke: #333;
|
stroke: #333;
|
||||||
stroke-width: 2;
|
stroke-width: 2;
|
||||||
|
transition: all 0.2s;
|
||||||
}
|
}
|
||||||
|
|
||||||
.empty {
|
.empty {
|
||||||
fill: #ddd;
|
fill: #ecf0f1;
|
||||||
}
|
}
|
||||||
|
|
||||||
.sheep {
|
.sheep {
|
||||||
@@ -42,157 +46,156 @@
|
|||||||
}
|
}
|
||||||
|
|
||||||
.selected {
|
.selected {
|
||||||
stroke: gold;
|
stroke: #f1c40f;
|
||||||
|
stroke-width: 6;
|
||||||
|
}
|
||||||
|
|
||||||
|
.possible {
|
||||||
|
stroke: #2ecc71;
|
||||||
stroke-width: 5;
|
stroke-width: 5;
|
||||||
|
stroke-dasharray: 5;
|
||||||
}
|
}
|
||||||
|
|
||||||
text {
|
text {
|
||||||
pointer-events: none;
|
pointer-events: none;
|
||||||
|
user-select: none;
|
||||||
}
|
}
|
||||||
|
|
||||||
button {
|
button {
|
||||||
margin: 5px;
|
margin: 10px;
|
||||||
padding: 8px 15px;
|
padding: 10px 20px;
|
||||||
font-size: 16px;
|
font-size: 14px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
border: none;
|
||||||
|
border-radius: 4px;
|
||||||
|
background: #2c3e50;
|
||||||
|
color: white;
|
||||||
|
}
|
||||||
|
|
||||||
|
button:hover {
|
||||||
|
background: #34495e;
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
|
|
||||||
<h2 id="role">Verbinde…</h2>
|
<h2 id="roleHeader">Verbinde...</h2>
|
||||||
<div id="status">–</div>
|
<div id="status">Warte auf Mitspieler...</div>
|
||||||
<svg width="600" height="420"></svg>
|
<svg width="600" height="420"></svg>
|
||||||
<button id="restartBtn">🔄 Spiel neu starten</button>
|
<br>
|
||||||
<button id="copyRoomBtn">📋 Raum-Link kopieren</button>
|
<button id="restartBtn">🔄 Neustart</button>
|
||||||
|
<button id="copyRoomBtn">📋 Link kopieren</button>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
// --- Room handling ---
|
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
let roomId = params.get("room"); // Use room from URL if exists
|
let roomId = params.get("room");
|
||||||
|
|
||||||
// Prompt user only if no room in URL
|
|
||||||
if (!roomId) {
|
if (!roomId) {
|
||||||
roomId = prompt("Gib den Raumcode ein oder leer lassen für neuen Raum:");
|
roomId = prompt("Raumcode eingeben (leer für neuen Raum):") || "";
|
||||||
if (!roomId) roomId = ""; // server will create new room
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- WebSocket connection ---
|
|
||||||
const protocol = location.protocol === "https:" ? "wss" : "ws";
|
const protocol = location.protocol === "https:" ? "wss" : "ws";
|
||||||
const ws = new WebSocket(`${protocol}://${location.host}?room=${roomId}`);
|
const ws = new WebSocket(`${protocol}://${location.host}?room=${roomId}`);
|
||||||
const svg = document.querySelector("svg");
|
const svg = document.querySelector("svg");
|
||||||
|
|
||||||
let role = null, state = null, selected = null;
|
let myRole = null, state = null, selected = null, currentRoomId = null;
|
||||||
|
|
||||||
// --- Board and positions ---
|
|
||||||
const board_wolf = {
|
|
||||||
0: [1, 2, 3], 1: [0, 2, 4, 5], 2: [0, 1, 3, 5], 3: [0, 2, 5, 6], 4: [1, 5, 7],
|
|
||||||
5: [1, 2, 3, 4, 6, 7, 8, 9], 6: [3, 5, 9], 7: [4, 5, 8, 10], 8: [5, 7, 9, 10],
|
|
||||||
9: [5, 6, 8, 10], 10: [7, 8, 9]
|
|
||||||
};
|
|
||||||
|
|
||||||
const board_sheep = {
|
|
||||||
0: [1, 2, 3], 1: [2, 4, 5], 2: [1, 3, 5], 3: [2, 5, 6], 4: [5, 7],
|
|
||||||
5: [4, 6, 7, 8, 9], 6: [5, 9], 7: [8, 10], 8: [7, 9, 10], 9: [8, 10], 10: []
|
|
||||||
};
|
|
||||||
|
|
||||||
|
const board_wolf = { 0: [1, 2, 3], 1: [0, 2, 4, 5], 2: [0, 1, 3, 5], 3: [0, 2, 5, 6], 4: [1, 5, 7], 5: [1, 2, 3, 4, 6, 7, 8, 9], 6: [3, 5, 9], 7: [4, 5, 8, 10], 8: [5, 7, 9, 10], 9: [5, 6, 8, 10], 10: [7, 8, 9] };
|
||||||
|
const board_sheep = { 0: [1, 2, 3], 1: [2, 4, 5], 2: [1, 3, 5], 3: [2, 5, 6], 4: [5, 7], 5: [4, 6, 7, 8, 9], 6: [5, 9], 7: [8, 10], 8: [7, 9, 10], 9: [8, 10], 10: [] };
|
||||||
const pos = { 0: [100, 210], 1: [200, 110], 2: [200, 210], 3: [200, 310], 4: [300, 110], 5: [300, 210], 6: [300, 310], 7: [400, 110], 8: [400, 210], 9: [400, 310], 10: [500, 210] };
|
const pos = { 0: [100, 210], 1: [200, 110], 2: [200, 210], 3: [200, 310], 4: [300, 110], 5: [300, 210], 6: [300, 310], 7: [400, 110], 8: [400, 210], 9: [400, 310], 10: [500, 210] };
|
||||||
const edges = [[0, 1], [0, 2], [0, 3], [1, 2], [2, 3], [1, 4], [2, 5], [3, 6], [4, 5], [5, 6], [4, 7], [5, 8], [6, 9], [7, 8], [8, 9], [5, 1], [5, 3], [5, 7], [5, 9], [7, 10], [8, 10], [9, 10]];
|
const edges = [[0, 1], [0, 2], [0, 3], [1, 2], [2, 3], [1, 4], [2, 5], [3, 6], [4, 5], [5, 6], [4, 7], [5, 8], [6, 9], [7, 8], [8, 9], [5, 1], [5, 3], [5, 7], [5, 9], [7, 10], [8, 10], [9, 10]];
|
||||||
|
|
||||||
// --- WebSocket events ---
|
|
||||||
ws.onmessage = e => {
|
ws.onmessage = e => {
|
||||||
const msg = JSON.parse(e.data);
|
const msg = JSON.parse(e.data);
|
||||||
if (msg.type === "role") {
|
if (msg.type === "role") {
|
||||||
role = msg.role;
|
myRole = msg.role;
|
||||||
roomId = msg.roomId;
|
currentRoomId = msg.roomId;
|
||||||
document.getElementById("role").innerText = "Du bist: " + role + " (Raum: " + roomId + ")";
|
document.getElementById("roleHeader").innerText = `Du bist: ${myRole.toUpperCase()} | Raum: ${currentRoomId}`;
|
||||||
}
|
}
|
||||||
if (msg.type === "state") { state = msg.game; render(); }
|
if (msg.type === "state") {
|
||||||
if (msg.type === "full") { alert("Raum ist voll."); }
|
state = msg.game;
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
if (msg.type === "full") alert("Dieser Raum ist leider voll!");
|
||||||
};
|
};
|
||||||
|
|
||||||
// --- Render ---
|
|
||||||
function render() {
|
function render() {
|
||||||
svg.innerHTML = "";
|
if (!state) return;
|
||||||
document.getElementById("status").innerText = state.turn === role ? "🟢 Du bist am Zug" : "⏳ Gegner ist am Zug";
|
svg.innerHTML = "";
|
||||||
|
|
||||||
// Draw Edges
|
// Update Status UI
|
||||||
edges.forEach(([a, b]) => {
|
const statusEl = document.getElementById("status");
|
||||||
const [x1, y1] = pos[a], [x2, y2] = pos[b];
|
if (state.winner) {
|
||||||
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
statusEl.innerHTML = `<span style="color:${state.winner === myRole ? 'green' : 'red'}">SPIEL ENDE: ${state.winner.toUpperCase()} GEWINNT!</span>`;
|
||||||
line.setAttribute("x1", x1); line.setAttribute("y1", y1);
|
} else {
|
||||||
line.setAttribute("x2", x2); line.setAttribute("y2", y2);
|
const turnText = state.turn === myRole ? "🟢 Dein Zug" : "⏳ Gegner zieht...";
|
||||||
line.setAttribute("stroke", "#aaa"); line.setAttribute("stroke-width", "4");
|
statusEl.innerHTML = `${turnText}<br><small>Zug: ${state.moveCount} / 20</small>`;
|
||||||
svg.appendChild(line);
|
|
||||||
});
|
|
||||||
|
|
||||||
// Draw Nodes
|
|
||||||
for (let i = 0; i <= 10; i++) {
|
|
||||||
const [x, y] = pos[i];
|
|
||||||
let cls = "empty";
|
|
||||||
if (state.wolf === i) cls = "wolf";
|
|
||||||
if (state.sheep.includes(i)) cls = "sheep";
|
|
||||||
|
|
||||||
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
|
||||||
circle.setAttribute("cx", x); circle.setAttribute("cy", y); circle.setAttribute("r", 26);
|
|
||||||
|
|
||||||
// --- HIGHLIGHT LOGIC ---
|
|
||||||
let isPossibleMove = false;
|
|
||||||
if (selected !== null) {
|
|
||||||
const allowed = (role === "wolf") ? board_wolf[selected] : board_sheep[selected];
|
|
||||||
const isTargetEmpty = (state.wolf !== i && !state.sheep.includes(i));
|
|
||||||
if (allowed?.includes(i) && isTargetEmpty) {
|
|
||||||
isPossibleMove = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
circle.setAttribute("class", "node " + cls + (selected === i ? " selected" : ""));
|
|
||||||
|
|
||||||
// Visual feedback for possible moves
|
|
||||||
if (isPossibleMove) {
|
|
||||||
circle.setAttribute("stroke", "lime");
|
|
||||||
circle.setAttribute("stroke-width", "6");
|
|
||||||
circle.setAttribute("stroke-dasharray", "4"); // Makes it look like a target
|
|
||||||
}
|
|
||||||
|
|
||||||
circle.onclick = () => handleClick(i);
|
|
||||||
svg.appendChild(circle);
|
|
||||||
|
|
||||||
// Add index numbers
|
|
||||||
const text = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
|
||||||
text.setAttribute("x", x); text.setAttribute("y", y + 6);
|
|
||||||
text.setAttribute("text-anchor", "middle");
|
|
||||||
text.setAttribute("fill", "white");
|
|
||||||
text.textContent = i;
|
|
||||||
svg.appendChild(text);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Handle clicks ---
|
// Draw Lines
|
||||||
function handleClick(i) {
|
edges.forEach(([a, b]) => {
|
||||||
if (!state || state.turn !== role) return;
|
const [x1, y1] = pos[a], [x2, y2] = pos[b];
|
||||||
const isMyPiece = (role === "wolf" && state.wolf === i) || (role === "sheep" && state.sheep.includes(i));
|
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
||||||
if (selected === null) { if (!isMyPiece) return; selected = i; render(); }
|
line.setAttribute("x1", x1); line.setAttribute("y1", y1);
|
||||||
else { ws.send(JSON.stringify({ from: selected, to: i })); selected = null; render(); }
|
line.setAttribute("x2", x2); line.setAttribute("y2", y2);
|
||||||
|
line.setAttribute("stroke", "#bdc3c7"); line.setAttribute("stroke-width", "3");
|
||||||
|
svg.appendChild(line);
|
||||||
|
});
|
||||||
|
|
||||||
|
// Draw Nodes
|
||||||
|
for (let i = 0; i <= 10; i++) {
|
||||||
|
const [x, y] = pos[i];
|
||||||
|
let type = "empty";
|
||||||
|
if (state.wolf === i) type = "wolf";
|
||||||
|
if (state.sheep.includes(i)) type = "sheep";
|
||||||
|
|
||||||
|
const isPossible = selected !== null &&
|
||||||
|
(myRole === "wolf" ? board_wolf[selected] : board_sheep[selected]).includes(i) &&
|
||||||
|
state.wolf !== i && !state.sheep.includes(i);
|
||||||
|
|
||||||
|
const circle = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
||||||
|
circle.setAttribute("cx", x); circle.setAttribute("cy", y); circle.setAttribute("r", 25);
|
||||||
|
circle.setAttribute("class", `node ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''}`);
|
||||||
|
|
||||||
|
circle.onclick = () => handleClick(i);
|
||||||
|
svg.appendChild(circle);
|
||||||
|
|
||||||
|
const txt = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
||||||
|
txt.setAttribute("x", x); txt.setAttribute("y", y + 5);
|
||||||
|
txt.setAttribute("text-anchor", "middle");
|
||||||
|
txt.setAttribute("fill", type === "empty" ? "#7f8c8d" : "white");
|
||||||
|
txt.textContent = i;
|
||||||
|
svg.appendChild(txt);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Restart button ---
|
function handleClick(i) {
|
||||||
document.getElementById("restartBtn").onclick = () => {
|
if (!state || state.turn !== myRole || state.winner) return;
|
||||||
ws.send(JSON.stringify({ type: "restart" }));
|
|
||||||
selected = null;
|
const isMyPiece = (myRole === "wolf" && state.wolf === i) ||
|
||||||
};
|
(myRole === "sheep" && state.sheep.includes(i));
|
||||||
|
|
||||||
|
if (selected === null) {
|
||||||
|
if (isMyPiece) { selected = i; render(); }
|
||||||
|
} else {
|
||||||
|
if (isMyPiece) {
|
||||||
|
selected = (selected === i) ? null : i; // Toggle selection
|
||||||
|
} else {
|
||||||
|
ws.send(JSON.stringify({ from: selected, to: i }));
|
||||||
|
selected = null;
|
||||||
|
}
|
||||||
|
render();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
document.getElementById("restartBtn").onclick = () => ws.send(JSON.stringify({ type: "restart" }));
|
||||||
|
|
||||||
// --- Copy room link button ---
|
|
||||||
document.getElementById("copyRoomBtn").onclick = () => {
|
document.getElementById("copyRoomBtn").onclick = () => {
|
||||||
if (!roomId) return;
|
const url = `${location.origin}${location.pathname}?room=${currentRoomId}`;
|
||||||
const url = `${location.origin}?room=${roomId}`;
|
navigator.clipboard.writeText(url).then(() => alert("Link kopiert!"));
|
||||||
navigator.clipboard.writeText(url)
|
|
||||||
.then(() => alert("Raum-Link kopiert!"))
|
|
||||||
.catch(() => alert("Kopieren fehlgeschlagen"));
|
|
||||||
};
|
};
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
</html>
|
</html>
|
||||||
178
server.js
178
server.js
@@ -9,126 +9,134 @@ app.use(express.static("public"));
|
|||||||
const server = http.createServer(app);
|
const server = http.createServer(app);
|
||||||
const wss = new WebSocket.Server({ server });
|
const wss = new WebSocket.Server({ server });
|
||||||
|
|
||||||
// Define both movement sets
|
// Movement logic: Wolf can move anywhere, Sheep only forward
|
||||||
const board_wolf = {
|
const board_wolf = {
|
||||||
0:[1,2,3], 1:[0,2,4,5], 2:[0,1,3,5], 3:[0,2,5,6], 4:[1,5,7],
|
0:[1,2,3], 1:[0,2,4,5], 2:[0,1,3,5], 3:[0,2,5,6], 4:[1,5,7],
|
||||||
5:[1,2,3,4,6,7,8,9], 6:[3,5,9], 7:[4,5,8,10], 8:[5,7,9,10],
|
5:[1,2,3,4,6,7,8,9], 6:[3,5,9], 7:[4,5,8,10], 8:[5,7,9,10],
|
||||||
9:[5,6,8,10], 10:[7,8,9]
|
9:[5,6,8,10], 10:[7,8,9]
|
||||||
};
|
};
|
||||||
|
|
||||||
const board_sheep = {
|
const board_sheep = {
|
||||||
0:[1,2,3], 1:[2,4,5], 2:[1,3,5], 3:[2,5,6], 4:[5,7],
|
0:[1,2,3], 1:[2,4,5], 2:[1,3,5], 3:[2,5,6], 4:[5,7],
|
||||||
5:[4,6,7,8,9], 6:[5,9], 7:[8,10], 8:[7,9,10], 9:[8,10], 10:[]
|
5:[4,6,7,8,9], 6:[5,9], 7:[8,10], 8:[7,9,10], 9:[8,10], 10:[]
|
||||||
};
|
};
|
||||||
|
|
||||||
// Rooms: roomId -> { game, players }
|
|
||||||
const rooms = {};
|
const rooms = {};
|
||||||
|
|
||||||
// Broadcast state to all players in a room
|
|
||||||
function broadcast(roomId) {
|
function broadcast(roomId) {
|
||||||
const room = rooms[roomId];
|
const room = rooms[roomId];
|
||||||
if (!room) return;
|
if (!room) return;
|
||||||
const msg = JSON.stringify({ type:"state", game: room.game });
|
const msg = JSON.stringify({ type: "state", game: room.game });
|
||||||
room.players.forEach(p => {
|
room.players.forEach(p => {
|
||||||
if(p.ws.readyState === WebSocket.OPEN) p.ws.send(msg);
|
if (p.ws.readyState === WebSocket.OPEN) p.ws.send(msg);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Select the correct allowed moves based on role
|
function checkWinConditions(game) {
|
||||||
function handleMove(roomId, role, {from, to}) {
|
// 1. Wolf reaches node 0
|
||||||
|
if (game.wolf === 0) return "wolf";
|
||||||
|
|
||||||
|
// 2. Turn limit (20 moves)
|
||||||
|
if (game.moveCount >= 20) return "wolf";
|
||||||
|
|
||||||
|
// Mobility Check helpers
|
||||||
|
const canWolfMove = board_wolf[game.wolf].some(to =>
|
||||||
|
!game.sheep.includes(to)
|
||||||
|
);
|
||||||
|
|
||||||
|
const canSheepMove = game.sheep.some(from =>
|
||||||
|
board_sheep[from].some(to => to !== game.wolf && !game.sheep.includes(to))
|
||||||
|
);
|
||||||
|
|
||||||
|
// 3. Wolf trapped
|
||||||
|
if (!canWolfMove) return "sheep";
|
||||||
|
|
||||||
|
// 4. Sheep cannot move
|
||||||
|
if (!canSheepMove && game.turn === "sheep") return "wolf";
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
function handleMove(roomId, role, { from, to }) {
|
||||||
const room = rooms[roomId];
|
const room = rooms[roomId];
|
||||||
if(!room) return;
|
if (!room || room.game.winner) return;
|
||||||
|
|
||||||
const game = room.game;
|
const game = room.game;
|
||||||
if(game.turn !== role) return;
|
if (game.turn !== role) return;
|
||||||
|
|
||||||
// Select the correct allowed moves based on role
|
const allowed = (role === "wolf") ? board_wolf[from] : board_sheep[from];
|
||||||
const allowedMoves = (role === "wolf") ? board_wolf[from] : board_sheep[from];
|
if (!allowed?.includes(to)) return;
|
||||||
|
if (game.sheep.includes(to) || game.wolf === to) return;
|
||||||
|
|
||||||
if(!allowedMoves?.includes(to)) return;
|
if (role === "wolf") {
|
||||||
if(game.sheep.includes(to) || game.wolf === to) return;
|
if (from !== game.wolf) return;
|
||||||
|
game.wolf = to;
|
||||||
if(role === "wolf") {
|
game.turn = "sheep";
|
||||||
if(from !== game.wolf) return;
|
|
||||||
game.wolf = to;
|
|
||||||
game.turn = "sheep";
|
|
||||||
} else {
|
} else {
|
||||||
const i = game.sheep.indexOf(from);
|
const i = game.sheep.indexOf(from);
|
||||||
if(i === -1) return;
|
if (i === -1) return;
|
||||||
game.sheep[i] = to;
|
game.sheep[i] = to;
|
||||||
game.turn = "wolf";
|
game.turn = "wolf";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
game.moveCount++;
|
||||||
|
game.winner = checkWinConditions(game);
|
||||||
broadcast(roomId);
|
broadcast(roomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Heartbeat
|
wss.on("connection", (ws, req) => {
|
||||||
function heartbeat() { this.isAlive = true; }
|
|
||||||
|
|
||||||
wss.on("connection", (ws, req) => { // Added 'req' parameter
|
|
||||||
ws.isAlive = true;
|
ws.isAlive = true;
|
||||||
ws.on("pong", heartbeat);
|
ws.on("pong", () => ws.isAlive = true);
|
||||||
|
|
||||||
// Use req.url to get the query string
|
|
||||||
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
||||||
let roomId = urlParams.get("room");
|
let roomId = urlParams.get("room");
|
||||||
|
|
||||||
// Logic for creating or joining
|
// Room creation/joining logic
|
||||||
if (!roomId || !rooms[roomId]) {
|
if (!roomId || !rooms[roomId]) {
|
||||||
// If no ID provided, or the provided ID doesn't exist, create a NEW one
|
if (!roomId) roomId = randomBytes(3).toString("hex");
|
||||||
// UNLESS you want people to be able to create specific room names:
|
|
||||||
if (!roomId) roomId = randomBytes(3).toString("hex");
|
|
||||||
|
|
||||||
if (!rooms[roomId]) {
|
|
||||||
rooms[roomId] = {
|
rooms[roomId] = {
|
||||||
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep" },
|
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null },
|
||||||
players: []
|
players: []
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const room = rooms[roomId];
|
const room = rooms[roomId];
|
||||||
|
if (room.players.length >= 2) {
|
||||||
// Reject if room full
|
ws.send(JSON.stringify({ type: "full" }));
|
||||||
if(room.players.length >= 2){
|
ws.close();
|
||||||
ws.send(JSON.stringify({type:"full"}));
|
return;
|
||||||
ws.close();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const role = room.players.length === 0 ? "sheep" : "wolf";
|
|
||||||
room.players.push({ ws, role, roomId });
|
|
||||||
|
|
||||||
// Send role + room info
|
|
||||||
ws.send(JSON.stringify({ type:"role", role, roomId }));
|
|
||||||
broadcast(roomId);
|
|
||||||
|
|
||||||
ws.on("message", msg=>{
|
|
||||||
let data;
|
|
||||||
try{ data = JSON.parse(msg); } catch{return;}
|
|
||||||
|
|
||||||
if(data.type==="restart"){
|
|
||||||
room.game = { wolf:5, sheep:[0,1,3], turn:"sheep" };
|
|
||||||
broadcast(roomId);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
handleMove(roomId, role, data);
|
const role = room.players.length === 0 ? "sheep" : "wolf";
|
||||||
});
|
room.players.push({ ws, role });
|
||||||
|
|
||||||
ws.on("close", ()=>{
|
ws.send(JSON.stringify({ type: "role", role, roomId }));
|
||||||
room.players = room.players.filter(p => p.ws !== ws);
|
broadcast(roomId);
|
||||||
if(room.players.length === 0) delete rooms[roomId]; // cleanup empty rooms
|
|
||||||
});
|
ws.on("message", msg => {
|
||||||
|
let data;
|
||||||
|
try { data = JSON.parse(msg); } catch { return; }
|
||||||
|
|
||||||
|
if (data.type === "restart") {
|
||||||
|
room.game = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null };
|
||||||
|
broadcast(roomId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
handleMove(roomId, role, data);
|
||||||
|
});
|
||||||
|
|
||||||
|
ws.on("close", () => {
|
||||||
|
room.players = room.players.filter(p => p.ws !== ws);
|
||||||
|
if (room.players.length === 0) delete rooms[roomId];
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Ping-pong to prevent idle disconnect
|
setInterval(() => {
|
||||||
const interval = setInterval(()=>{
|
wss.clients.forEach(ws => {
|
||||||
wss.clients.forEach(ws=>{
|
if (!ws.isAlive) return ws.terminate();
|
||||||
if(!ws.isAlive) return ws.terminate();
|
ws.isAlive = false;
|
||||||
ws.isAlive = false;
|
ws.ping();
|
||||||
ws.ping();
|
});
|
||||||
});
|
|
||||||
}, 30000);
|
}, 30000);
|
||||||
|
|
||||||
server.on("close", ()=>clearInterval(interval));
|
server.listen(3030, () => console.log("Server läuft auf Port 3030"));
|
||||||
server.listen(3030, ()=>console.log("Server läuft auf Port 3030"));
|
|
||||||
Reference in New Issue
Block a user