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

305 lines
10 KiB
HTML

<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Wolf & Schafe</title>
<style>
:root {
--bg: #f8fafc;
--card-bg: #ffffff;
--wolf-color: #ef4444;
--sheep-color: #3b82f6;
--empty-color: #f1f5f9;
--border-color: #e2e8f0;
--text-main: #1e293b;
--text-sub: #64748b;
--accent: #f59e0b;
}
body {
font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
background-color: var(--bg);
color: var(--text-main);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
touch-action: manipulation;
}
.game-card {
background: var(--card-bg);
width: 95%;
max-width: 600px;
padding: 1.5rem;
border-radius: 16px;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
border: 1px solid var(--border-color);
text-align: center;
}
header {
margin-bottom: 1rem;
}
h2 {
margin: 0;
font-size: 1.1rem;
color: var(--text-sub);
font-weight: 500;
}
#status {
font-size: 1.25rem;
font-weight: 700;
margin: 0.5rem 0;
min-height: 3rem;
line-height: 1.2;
}
.board-container {
width: 100%;
aspect-ratio: 600 / 420;
position: relative;
}
svg {
width: 100%;
height: 100%;
display: block;
}
/* SVG Elements */
.edge {
stroke: var(--border-color);
stroke-width: 3;
stroke-linecap: round;
}
.node-circle {
transition: r 0.2s ease, stroke-width 0.2s ease;
stroke: var(--border-color);
stroke-width: 1.5;
}
.empty {
fill: var(--empty-color);
}
.sheep {
fill: var(--sheep-color);
stroke: rgba(0, 0, 0, 0.1);
}
.wolf {
fill: var(--wolf-color);
stroke: rgba(0, 0, 0, 0.1);
}
.selected {
stroke: var(--accent);
stroke-width: 5;
}
.possible {
fill: #dcfce7;
stroke: #22c55e;
stroke-dasharray: 4;
r: 20;
/* Slightly smaller for indicator */
}
/* Responsive Controls */
.controls {
margin-top: 1.5rem;
display: grid;
grid-template-columns: repeat(2, 1fr);
gap: 10px;
}
button {
padding: 12px;
border-radius: 10px;
border: 1px solid var(--border-color);
background: white;
color: var(--text-main);
font-weight: 600;
cursor: pointer;
font-size: 0.9rem;
display: flex;
align-items: center;
justify-content: center;
gap: 6px;
}
button:active {
background: #f1f5f9;
transform: scale(0.98);
}
#exitBtn {
grid-column: span 2;
background: #fff1f2;
color: #be123c;
border-color: #fecdd3;
}
#copyRoomBtn {
background: #f0f9ff;
color: #0369a1;
border-color: #bae6fd;
}
@media (max-width: 480px) {
.game-card {
padding: 1rem;
border-radius: 0;
width: 100%;
min-height: 100vh;
border: none;
}
#status {
font-size: 1.1rem;
}
}
</style>
</head>
<body>
<div class="game-card">
<header>
<h2 id="roleHeader">Verbinde...</h2>
<div id="status">Warte auf Mitspieler...</div>
</header>
<div class="board-container">
<svg viewBox="0 0 600 420" preserveAspectRatio="xMidYMid meet"></svg>
</div>
<div class="controls">
<button id="copyRoomBtn">📋 Link</button>
<button id="restartBtn">🔄 Reset</button>
<button id="exitBtn">🚪 Raum verlassen</button>
</div>
</div>
<script>
// Session and Room Setup
let sessionId = localStorage.getItem("game_session_id") || (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 neu):") || "";
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: [80, 210], 1: [180, 110], 2: [180, 210], 3: [180, 310], 4: [280, 110], 5: [280, 210], 6: [280, 310], 7: [380, 110], 8: [380, 210], 9: [380, 310], 10: [480, 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 = `${myRole.toUpperCase()} • RAUM ${currentRoomId}`;
if (!params.has("room")) window.history.replaceState({}, '', `?room=${currentRoomId}`);
}
if (msg.type === "state") { state = msg.game; render(); }
};
function render() {
if (!state) return;
svg.innerHTML = "";
const statusEl = document.getElementById("status");
if (state.winner) {
const winColor = state.winner === myRole ? "#059669" : "#dc2626";
statusEl.innerHTML = `<span style="color:${winColor}">${state.winner === myRole ? '🏆 Sieg!' : 'Spiel beendet'}</span>`;
} else {
const turnText = state.turn === myRole ? `<span style="color:var(--text-main)">Du bist dran</span>` : `<span style="color:var(--text-sub)">Gegner zieht...</span>`;
statusEl.innerHTML = `${turnText}<br><small style="font-weight:400; font-size: 0.8rem; color: var(--text-sub)">Zug ${state.moveCount}/40</small>`;
}
edges.forEach(([a, b]) => {
const line = document.createElementNS("http://www.w3.org/2000/svg", "line");
line.setAttribute("x1", pos[a][0]); line.setAttribute("y1", pos[a][1]);
line.setAttribute("x2", pos[b][0]); line.setAttribute("y2", pos[b][1]);
line.setAttribute("class", "edge");
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 group = document.createElementNS("http://www.w3.org/2000/svg", "g");
group.onclick = () => handleClick(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-circle ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''}`);
group.appendChild(circle);
if (type !== "empty") {
const txt = document.createElementNS("http://www.w3.org/2000/svg", "text");
txt.setAttribute("x", x); txt.setAttribute("y", y + 6);
txt.setAttribute("text-anchor", "middle");
txt.setAttribute("fill", "white");
txt.setAttribute("font-weight", "bold");
txt.textContent = type === "wolf" ? "W" : "S";
group.appendChild(txt);
}
svg.appendChild(group);
}
}
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 = () => {
navigator.clipboard.writeText(window.location.href).then(() => alert("Link kopiert!"));
};
document.getElementById("exitBtn").onclick = () => {
if (confirm("Raum verlassen?")) {
ws.send(JSON.stringify({ type: "leave" }));
window.location.href = window.location.pathname;
}
};
</script>
</body>
</html>