Files
WolfSheepGame/public/index.html
2026-02-09 22:13:02 +01:00

213 lines
7.8 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>Wolf & Schafe - Multiplayer</title>
<style>
body {
font-family: 'Segoe UI', Arial, sans-serif;
background: #f0f2f5;
text-align: center;
}
#status {
font-size: 20px;
font-weight: bold;
margin: 15px;
min-height: 50px;
}
svg {
background: white;
border-radius: 8px;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
display: block;
margin: auto;
}
.node {
cursor: pointer;
stroke: #333;
stroke-width: 2;
transition: all 0.2s;
}
.empty {
fill: #ecf0f1;
}
.sheep {
fill: #3498db;
}
.wolf {
fill: #e74c3c;
}
.selected {
stroke: #f1c40f;
stroke-width: 6;
}
.possible {
stroke: #2ecc71;
stroke-width: 5;
stroke-dasharray: 5;
}
text {
pointer-events: none;
user-select: none;
}
button {
margin: 10px;
padding: 10px 20px;
font-size: 14px;
cursor: pointer;
border: none;
border-radius: 4px;
background: #2c3e50;
color: white;
}
button:hover {
background: #34495e;
}
#exitBtn {
background: #e74c3c;
}
#exitBtn:hover {
background: #c0392b;
}
</style>
</head>
<body>
<h2 id="roleHeader">Verbinde...</h2>
<div id="status">Warte auf Mitspieler...</div>
<svg width="600" height="420"></svg>
<br>
<button id="restartBtn">🔄 Neustart</button>
<button id="copyRoomBtn">📋 Link kopieren</button>
<button id="exitBtn">🚪 Raum verlassen</button>
<script>
// Session Management
let sessionId = localStorage.getItem("game_session_id");
if (!sessionId) {
sessionId = Math.random().toString(36).substring(2) + Date.now().toString(36);
localStorage.setItem("game_session_id", sessionId);
}
const params = new URLSearchParams(window.location.search);
let roomId = params.get("room");
if (!roomId) {
roomId = prompt("Raumcode eingeben (leer für neuen Raum):") || "";
}
const protocol = location.protocol === "https:" ? "wss" : "ws";
const ws = new WebSocket(`${protocol}://${location.host}?room=${roomId}&sessionId=${sessionId}`);
const svg = document.querySelector("svg");
let myRole = null, state = null, selected = null, currentRoomId = null;
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 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]];
ws.onmessage = e => {
const msg = JSON.parse(e.data);
if (msg.type === "role") {
myRole = msg.role;
currentRoomId = msg.roomId;
document.getElementById("roleHeader").innerText = `Du bist: ${myRole.toUpperCase()} | Raum: ${currentRoomId}`;
if (!params.has("room")) {
window.history.replaceState({}, '', `?room=${currentRoomId}`);
}
}
if (msg.type === "state") { state = msg.game; render(); }
if (msg.type === "full") alert("Dieser Raum ist leider voll!");
};
function render() {
if (!state) return;
svg.innerHTML = "";
const statusEl = document.getElementById("status");
if (state.winner) {
statusEl.innerHTML = `<span style="color:${state.winner === myRole ? 'green' : 'red'}">SPIEL ENDE: ${state.winner.toUpperCase()} GEWINNT!</span>`;
} else {
const turnText = state.turn === myRole ? "🟢 Dein Zug" : "⏳ Gegner zieht...";
statusEl.innerHTML = `${turnText}<br><small>Zug: ${state.moveCount} / 40</small>`;
}
edges.forEach(([a, b]) => {
const [x1, y1] = pos[a], [x2, y2] = pos[b];
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
line.setAttribute("x1", x1); line.setAttribute("y1", y1); line.setAttribute("x2", x2); line.setAttribute("y2", y2);
line.setAttribute("stroke", "#bdc3c7"); line.setAttribute("stroke-width", "3");
svg.appendChild(line);
});
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);
}
}
function handleClick(i) {
if (!state || state.turn !== myRole || state.winner) return;
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;
} else {
ws.send(JSON.stringify({ from: selected, to: i }));
selected = null;
}
render();
}
}
document.getElementById("restartBtn").onclick = () => ws.send(JSON.stringify({ type: "restart" }));
document.getElementById("copyRoomBtn").onclick = () => {
const url = `${location.origin}${location.pathname}?room=${currentRoomId}`;
navigator.clipboard.writeText(url).then(() => alert("Link kopiert!"));
};
document.getElementById("exitBtn").onclick = () => {
if (confirm("Möchtest du den Raum verlassen?")) {
ws.send(JSON.stringify({ type: "leave" }));
window.location.href = window.location.pathname;
}
};
</script>
</body>
</html>