Files
WolfSheepGame/public/index.html
2026-02-10 18:59:21 +01:00

689 lines
24 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: #f0f2f5;
--wolf: #e63946;
--wolf-light: #ff707a;
--sheep: #457b9d;
--sheep-light: #8ecae6;
--black: #1d3557;
--white: #ffffff;
--green: #10b981;
--arrow-primary: #f59e0b;
--arrow-secondary: #10b981;
}
body {
font-family: 'Courier New', monospace;
background: var(--bg);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 10px;
box-sizing: border-box;
}
.main-card {
background: white;
border: 4px solid var(--black);
box-shadow: 8px 8px 0px var(--black);
padding: 1.5rem;
width: 100%;
max-width: 650px;
display: flex;
flex-direction: column;
gap: 15px;
position: relative;
}
.room-item {
display: flex;
justify-content: space-between;
padding: 12px;
border: 3px solid var(--black);
margin-bottom: 10px;
cursor: pointer;
font-weight: 900;
background: #fff;
box-shadow: 4px 4px 0px var(--black);
}
.room-item:hover {
background: #f8fafc;
transform: translate(-2px, -2px);
box-shadow: 6px 6px 0px var(--black);
}
.hidden {
display: none !important;
}
.info-bar {
display: flex;
justify-content: space-between;
align-items: center;
border: 3px solid var(--black);
padding: 10px;
background: #f8fafc;
}
.id-box {
font-size: 1.2rem;
font-weight: 900;
padding: 8px 16px;
border: 3px solid var(--black);
text-transform: uppercase;
}
.is-wolf {
background: var(--wolf);
color: white;
}
.is-sheep {
background: var(--sheep);
color: white;
}
.conn-dot {
display: inline-block;
width: 8px;
height: 8px;
background: #ccc;
border-radius: 50%;
margin-right: 5px;
}
.conn-online {
background: var(--green);
box-shadow: 0 0 5px var(--green);
animation: pulse 2s infinite;
}
@keyframes pulse {
0% {
opacity: 1;
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
}
}
.board-wrapper {
position: relative;
width: 100%;
border: 3px solid var(--black);
background: #fff;
overflow: hidden;
}
svg {
width: 100%;
height: auto;
display: block;
user-select: none;
-webkit-user-select: none;
}
.edge {
stroke: var(--black);
stroke-width: 3;
}
.node-circle {
stroke: var(--black);
stroke-width: 2.5;
transition: 0.1s;
cursor: pointer;
}
.wolf {
fill: var(--wolf);
}
.sheep {
fill: var(--sheep);
}
.empty {
fill: #fff;
}
.wolf.selected {
fill: var(--wolf-light) !important;
stroke-width: 4;
}
.sheep.selected {
fill: var(--sheep-light) !important;
stroke-width: 4;
}
.possible {
fill: #fff;
stroke: var(--green);
stroke-width: 2.5;
stroke-dasharray: 4;
}
.arrow {
pointer-events: none;
stroke-width: 4;
stroke-linecap: round;
opacity: 1;
}
.arrow-pri {
stroke: var(--arrow-primary);
}
.arrow-sec {
stroke: var(--arrow-secondary);
}
#head-pri {
fill: var(--arrow-primary);
}
#head-sec {
fill: var(--arrow-secondary);
}
#victory-banner {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: rgba(255, 255, 255, 0.95);
border: 5px solid var(--black);
padding: 20px;
z-index: 20;
text-align: center;
box-shadow: 8px 8px 0px var(--black);
pointer-events: none;
width: 70%;
}
#victory-banner h2 {
margin: 0;
font-size: 1.8rem;
text-transform: uppercase;
}
.winner-glow {
animation: winner-pulse 1.5s infinite;
}
@keyframes winner-pulse {
0% {
filter: drop-shadow(0 0 2px #ffd700);
stroke-width: 2.5;
}
50% {
filter: drop-shadow(0 0 12px #ffd700);
stroke-width: 6;
}
100% {
filter: drop-shadow(0 0 2px #ffd700);
stroke-width: 2.5;
}
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.9);
z-index: 10;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
text-align: center;
font-weight: 900;
}
.controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
.btn {
border: 3px solid var(--black);
background: white;
padding: 12px 5px;
font-weight: 900;
cursor: pointer;
box-shadow: 4px 4px 0px var(--black);
text-transform: uppercase;
font-size: 0.8rem;
}
.btn:active {
box-shadow: 0px 0px 0px;
transform: translate(2px, 2px);
}
.btn-green {
background: var(--green) !important;
color: white;
}
.restart-pulse {
animation: btn-pulse 1.5s infinite;
background: var(--green) !important;
color: white;
}
@keyframes btn-pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.05);
}
100% {
transform: scale(1);
}
}
#toast {
visibility: hidden;
min-width: 200px;
background-color: var(--black);
color: var(--white);
text-align: center;
padding: 12px;
position: fixed;
z-index: 100;
left: 50%;
bottom: 30px;
transform: translateX(-50%);
font-weight: 900;
box-shadow: 6px 6px 0px var(--green);
border: 2px solid var(--white);
}
#toast.show {
visibility: visible;
animation: fadein 0.5s, fadeout 0.5s 2.5s;
}
@keyframes fadein {
from {
bottom: 0;
opacity: 0;
}
to {
bottom: 30px;
opacity: 1;
}
}
@keyframes fadeout {
from {
bottom: 30px;
opacity: 1;
}
to {
bottom: 0;
opacity: 0;
}
}
</style>
</head>
<body oncontextmenu="return false;">
<div id="lobby" class="main-card">
<h1 style="text-align: center; margin: 0; border-bottom: 4px solid var(--black); padding-bottom: 10px;">WOLF &
SCHAFE</h1>
<div style="display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin-top: 10px;">
<button class="btn btn-green" onclick="createRoom()">+ ONLINE SPIEL</button>
<button class="btn" onclick="startLocal()">+ LOKAL SPIELEN</button>
</div>
<h3 style="margin: 15px 0 5px 0;">Offene Räume:</h3>
<div id="roomList">Suche Räume...</div>
</div>
<div id="game" class="main-card hidden">
<div class="info-bar">
<div>
<div id="roomInfo" style="font-size: 0.7rem; font-weight: 900; display: flex; align-items: center;">
<span id="dot" class="conn-dot"></span> RAUM: <span id="roomCode">...</span>
</div>
<div id="idBadge" class="id-box">...</div>
</div>
<div id="status" style="text-align: right; font-weight: 900; line-height: 1.2;">...</div>
</div>
<div class="board-wrapper">
<div id="overlay" class="hidden">
<div>WARTEN AUF GEGNER...</div>
<p style="font-size: 0.7rem; margin-top: 10px; padding: 0 10px;">Link kopieren und Freund einladen!</p>
</div>
<div id="victory-banner" class="hidden">
<h2 id="winner-text">GEWONNEN!</h2>
<p style="margin: 5px 0 0 0; font-size: 0.8rem;">SPIEL BEENDET</p>
</div>
<svg viewBox="0 0 600 420">
<defs>
<marker id="arrowhead-pri" markerWidth="6" markerHeight="4" refX="5" refY="2" orient="auto">
<polygon points="0 0, 6 2, 0 4" id="head-pri" />
</marker>
<marker id="arrowhead-sec" markerWidth="6" markerHeight="4" refX="5" refY="2" orient="auto">
<polygon points="0 0, 6 2, 0 4" id="head-sec" />
</marker>
</defs>
</svg>
</div>
<div class="controls">
<button class="btn" id="copyBtn">Kopieren</button>
<button class="btn" id="restartBtn">Restart</button>
<button class="btn" onclick="location.href='/'">Lobby</button>
</div>
</div>
<div id="toast">Link kopiert!</div>
<script>
let sessionId = localStorage.getItem("game_session_id") || Math.random().toString(36).substring(2);
localStorage.setItem("game_session_id", sessionId);
const params = new URLSearchParams(window.location.search);
let roomId = params.get("room");
let isLocal = params.get("mode") === "local";
let ws, myRole, selected = null;
let state = null;
let history = [];
let historyIndex = -1;
let plannedArrows = [];
let dragStartNode = null;
let dragStartTime = 0;
let rightClickStart = null;
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]];
const bWolf = { 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 bSheep = { 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: [] };
function initLocalState() {
const startState = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null, lastMove: null };
history = [];
updateHistory(startState, true);
document.getElementById("lobby").classList.add("hidden");
document.getElementById("game").classList.remove("hidden");
document.getElementById("roomInfo").style.visibility = "hidden";
document.getElementById("copyBtn").disabled = true;
document.getElementById("copyBtn").style.opacity = "0.5";
}
function updateHistory(newState, forceJump = false) {
const isAtEnd = historyIndex === history.length - 1;
if (newState.moveCount === 0) {
history = [JSON.parse(JSON.stringify(newState))];
plannedArrows = [];
} else if (!state || newState.moveCount > state.moveCount) {
history.push(JSON.parse(JSON.stringify(newState)));
}
state = newState;
if (isAtEnd || forceJump) historyIndex = history.length - 1;
render();
}
function connect() {
if (isLocal) return initLocalState();
const protocol = location.protocol === "https:" ? "wss" : "ws";
ws = new WebSocket(`${protocol}://${location.host}?room=${roomId}&sessionId=${sessionId}`);
ws.onopen = () => document.getElementById("dot").classList.add("conn-online");
ws.onmessage = e => {
const msg = JSON.parse(e.data);
if (msg.type === "room_list") updateRoomList(msg.rooms);
if (msg.type === "init") {
myRole = msg.role;
roomId = msg.roomId;
document.getElementById("roomCode").innerText = roomId.toUpperCase();
window.history.replaceState({}, '', `?room=${roomId}`);
document.getElementById("lobby").classList.add("hidden");
document.getElementById("game").classList.remove("hidden");
}
if (msg.type === "state") {
myRole = msg.yourRole;
document.getElementById("overlay").className = msg.onlineCount < 2 ? "" : "hidden";
updateHistory(msg.game, msg.game.moveCount === 0);
}
};
}
window.addEventListener("keydown", (e) => {
if (history.length === 0) return;
switch (e.key) {
case "ArrowLeft": if (historyIndex > 0) historyIndex--; break;
case "ArrowRight": if (historyIndex < history.length - 1) historyIndex++; break;
case "ArrowUp": historyIndex = history.length - 1; break;
case "ArrowDown": historyIndex = 0; break;
default: return;
}
selected = null;
render();
});
function handleLocalMove(from, to) {
const baseState = history[historyIndex];
const allowed = (baseState.turn === "wolf") ? bWolf[from] : bSheep[from];
if (!allowed?.includes(to) || baseState.sheep.includes(to) || baseState.wolf === to) return;
const nextState = JSON.parse(JSON.stringify(baseState));
if (nextState.turn === "wolf") { nextState.wolf = to; nextState.turn = "sheep"; }
else { nextState.sheep[nextState.sheep.indexOf(from)] = to; nextState.turn = "wolf"; }
nextState.moveCount++;
nextState.winner = checkWinConditions(nextState);
history = history.slice(0, historyIndex + 1);
plannedArrows = [];
updateHistory(nextState, true);
}
function checkWinConditions(g) {
if (g.wolf === 0) return "wolf";
if (g.moveCount >= 40) return "wolf";
const canWolfMove = bWolf[g.wolf].some(to => !g.sheep.includes(to));
if (!canWolfMove) return "sheep";
const canAnySheepMove = g.sheep.some(sPos => {
return bSheep[sPos].some(to => to !== g.wolf && !g.sheep.includes(to));
});
if (!canAnySheepMove) return "wolf";
return null;
}
function toggleArrow(from, to, type) {
if (from === to || !bWolf[from].includes(to)) return;
const idx = plannedArrows.findIndex(a => a.from === from && a.to === to && a.type === type);
if (idx > -1) plannedArrows.splice(idx, 1);
else plannedArrows.push({ from, to, type });
render();
}
function render() {
const viewState = history[historyIndex];
if (!viewState) return;
const isViewingHistory = historyIndex < history.length - 1;
const svg = document.querySelector("svg");
const defs = svg.querySelector("defs");
svg.innerHTML = "";
svg.appendChild(defs);
edges.forEach(([a, b]) => {
const l = document.createElementNS("http://www.w3.org/2000/svg", "line");
l.setAttribute("x1", pos[a][0]); l.setAttribute("y1", pos[a][1]);
l.setAttribute("x2", pos[b][0]); l.setAttribute("y2", pos[b][1]);
l.setAttribute("class", "edge"); svg.appendChild(l);
});
const effectiveRole = isLocal ? viewState.turn : myRole;
const badge = document.getElementById("idBadge");
badge.innerText = effectiveRole === "wolf" ? "WOLF" : "SCHAFE";
badge.className = `id-box is-${effectiveRole}`;
const statusEl = document.getElementById("status");
const banner = document.getElementById("victory-banner");
const restartBtn = document.getElementById("restartBtn");
if (viewState.winner) {
banner.classList.remove("hidden");
const winMsg = viewState.winner === "wolf" ? "WOLF GEWINNT!" : "SCHAFE GEWINNEN!";
document.getElementById("winner-text").innerText = winMsg;
document.getElementById("winner-text").style.color = viewState.winner === "wolf" ? "var(--wolf)" : "var(--sheep)";
statusEl.innerHTML = "ENDE";
restartBtn.classList.add("restart-pulse");
} else {
banner.classList.add("hidden");
restartBtn.classList.remove("restart-pulse");
let label = (isLocal || viewState.turn === myRole) ? 'DEIN ZUG' : 'WARTEN...';
if (isViewingHistory) label = "HISTORIE";
statusEl.innerHTML = `${label}<br><small>ZUG ${viewState.moveCount}/40</small>`;
}
for (let i = 0; i <= 10; i++) {
const [x, y] = pos[i];
const isWolf = viewState.wolf === i;
const isSheep = viewState.sheep.includes(i);
const type = isWolf ? "wolf" : (isSheep ? "sheep" : "empty");
const isWinnerPiece = (viewState.winner === "wolf" && isWolf) || (viewState.winner === "sheep" && isSheep);
const canInteract = isLocal || (!isViewingHistory && state.turn === myRole);
const isPossible = canInteract && selected !== null && (viewState.turn === "wolf" ? bWolf[selected] : bSheep[selected]).includes(i) && !isWolf && !isSheep;
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.onmousedown = (e) => {
if (e.button === 0) {
dragStartNode = i;
dragStartTime = Date.now();
}
if (e.button === 2) rightClickStart = i;
};
g.onmouseup = (e) => {
if (e.button === 0 && dragStartNode !== null) {
const duration = Date.now() - dragStartTime;
if (dragStartNode === i && duration < 250) {
if (viewState.winner || (!isLocal && isViewingHistory)) return;
if (!isLocal && state.turn !== myRole) return;
const isMine = (viewState.turn === "wolf" && isWolf) || (viewState.turn === "sheep" && isSheep);
if (isMine) selected = i;
else if (isPossible) {
plannedArrows = [];
if (isLocal) handleLocalMove(selected, i);
else ws.send(JSON.stringify({ from: selected, to: i }));
selected = null;
} else selected = null;
render();
} else if (dragStartNode !== i) {
toggleArrow(dragStartNode, i, 'sec');
}
dragStartNode = null;
}
if (e.button === 2 && rightClickStart !== null) {
if (rightClickStart !== i) toggleArrow(rightClickStart, i, 'pri');
else plannedArrows = [];
rightClickStart = null;
render();
}
};
const c = document.createElementNS("http://www.w3.org/2000/svg", "circle");
c.setAttribute("cx", x); c.setAttribute("cy", y); c.setAttribute("r", 25);
c.setAttribute("class", `node-circle ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''} ${isWinnerPiece ? 'winner-glow' : ''}`);
if (isViewingHistory) c.style.strokeDasharray = "4";
g.appendChild(c);
const t = document.createElementNS("http://www.w3.org/2000/svg", "text");
t.setAttribute("x", x); t.setAttribute("y", y + 6); t.setAttribute("text-anchor", "middle");
t.setAttribute("style", "pointer-events:none; font-weight:900; font-family:sans-serif; font-size: 18px;");
t.setAttribute("fill", (type === "empty") ? "#000" : "#fff");
t.textContent = i; g.appendChild(t);
svg.appendChild(g);
}
plannedArrows.forEach(arrow => {
const s = pos[arrow.from]; const e = pos[arrow.to];
const dx = e[0] - s[0]; const dy = e[1] - s[1];
const angle = Math.atan2(dy, dx);
const x1 = s[0] + Math.cos(angle) * 28; const y1 = s[1] + Math.sin(angle) * 28;
const x2 = e[0] - Math.cos(angle) * 32; const y2 = e[1] - Math.sin(angle) * 32;
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("class", `arrow ${arrow.type === 'pri' ? 'arrow-pri' : 'arrow-sec'}`);
line.setAttribute("marker-end", `url(#arrowhead-${arrow.type})`);
svg.appendChild(line);
});
}
function updateRoomList(rooms) {
const list = document.getElementById("roomList");
if (!list) return;
list.innerHTML = rooms.length === 0 ? "<p style='text-align:center'>Keine aktiven Räume.</p>" : "";
rooms.forEach(r => {
const div = document.createElement("div");
div.className = "room-item";
div.innerHTML = `<span>RAUM: ${r.id.toUpperCase()}</span> <span>${r.players}/2</span>`;
div.onclick = () => { window.location.search = `?room=${r.id}`; };
list.appendChild(div);
});
}
function createRoom() { window.location.search = `?room=${Math.random().toString(36).substring(7)}`; }
function startLocal() { window.location.search = `?mode=local`; }
document.getElementById("restartBtn").onclick = () => {
plannedArrows = [];
if (isLocal) initLocalState();
else ws.send(JSON.stringify({ type: 'restart' }));
};
document.getElementById("copyBtn").onclick = async () => {
const url = window.location.origin + window.location.pathname + `?room=${roomId}`;
try { await navigator.clipboard.writeText(url); showToast(); } catch (err) { }
};
function showToast() {
const t = document.getElementById("toast");
t.className = "show";
setTimeout(() => { t.className = ""; }, 3000);
}
connect();
</script>
</body>
</html>