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

314 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 | Pro</title>
<style>
:root {
--bg: #f0f2f5;
--wolf: #e63946;
--wolf-light: #ff6b78;
--sheep: #457b9d;
--sheep-light: #7db0cf;
--black: #1d3557;
--white: #ffffff;
}
body {
font-family: 'Courier New', Courier, monospace;
background: var(--bg);
color: var(--black);
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
margin: 0;
padding: 10px;
box-sizing: border-box;
}
.game-wrapper {
width: 100%;
max-width: 800px;
}
.main-card {
background: var(--white);
border: 4px solid var(--black);
box-shadow: 8px 8px 0px var(--black);
padding: 1.5rem;
display: flex;
flex-direction: column;
gap: 15px;
}
.info-bar {
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: space-between;
border: 3px solid var(--black);
padding: 10px;
background: #f8fafc;
gap: 10px;
}
.room-tag {
font-size: 0.8rem;
font-weight: bold;
background: var(--black);
color: white;
padding: 4px 8px;
text-transform: uppercase;
}
.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;
}
.board-container {
width: 100%;
aspect-ratio: 600 / 420;
border: 3px solid var(--black);
background: #fff;
position: relative;
touch-action: manipulation;
}
svg {
width: 100%;
height: 100%;
display: block;
}
.edge {
stroke: var(--black);
stroke-width: 3;
}
.node-circle {
stroke: var(--black);
stroke-width: 2.5;
transition: all 0.15s ease;
}
.empty {
fill: #fff;
}
/* Piece Colors */
.sheep {
fill: var(--sheep);
}
.wolf {
fill: var(--wolf);
}
/* Subtle Select: Lightens the fill and adds a clean 4px border */
.wolf.selected {
fill: var(--wolf-light);
stroke-width: 4;
}
.sheep.selected {
fill: var(--sheep-light);
stroke-width: 4;
}
.possible {
fill: #fff;
stroke: #10b981;
stroke-width: 3;
stroke-dasharray: 4;
}
.node-label {
font-family: sans-serif;
font-size: 14px;
font-weight: 900;
pointer-events: none;
}
.controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 10px;
}
button {
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;
}
button:active {
box-shadow: 0px 0px 0px var(--black);
transform: translate(2px, 2px);
}
#exitBtn {
background: #fee2e2;
color: #991b1b;
}
@media (max-width: 600px) {
.main-card {
padding: 10px;
box-shadow: 4px 4px 0px var(--black);
gap: 10px;
}
.info-bar {
flex-direction: column;
align-items: stretch;
text-align: center;
}
#status {
text-align: center !important;
}
.controls {
grid-template-columns: 1fr;
}
.id-box {
font-size: 1rem;
}
}
</style>
</head>
<body>
<div class="game-wrapper">
<div class="main-card">
<div class="info-bar">
<div>
<div class="room-tag">RAUM: <span id="codeDisplay">...</span></div>
<div id="idBadge" class="id-box">...</div>
</div>
<div id="status" style="text-align: right; font-weight: 900; line-height: 1.2;">Laden...</div>
</div>
<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">Swap & Reset</button>
<button id="exitBtn">Beenden</button>
</div>
</div>
</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") || prompt("Raumcode (leer für neu):");
const ws = new WebSocket(`${location.protocol === "https:" ? "wss" : "ws"}://${location.host}?room=${roomId}&sessionId=${sessionId}`);
const svg = document.querySelector("svg");
let myRole = null, state = null, selected = 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: [] };
ws.onmessage = e => {
const msg = JSON.parse(e.data);
if (msg.type === "init") {
document.getElementById("codeDisplay").innerText = msg.roomId;
if (!params.has("room")) window.history.replaceState({}, '', `?room=${msg.roomId}`);
}
if (msg.type === "state") {
state = msg.game;
myRole = msg.yourRole;
updateUI();
render();
}
};
function updateUI() {
const badge = document.getElementById("idBadge");
badge.innerText = myRole === "wolf" ? "WOLF" : "SCHAFE";
badge.className = `id-box is-${myRole}`;
const statusEl = document.getElementById("status");
if (state.winner) {
statusEl.innerHTML = `SIEG: ${state.winner === 'wolf' ? 'WOLF' : 'SCHAFE'}`;
} else {
statusEl.innerHTML = `${state.turn === myRole ? 'DEIN ZUG' : 'Gegner...'}<br><small>ZUG ${state.moveCount}/40</small>`;
}
}
function render() {
svg.innerHTML = "";
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);
});
for (let i = 0; i <= 10; i++) {
const [x, y] = pos[i];
let type = state.wolf === i ? "wolf" : (state.sheep.includes(i) ? "sheep" : "empty");
const isPossible = selected !== null && (myRole === "wolf" ? bWolf[selected] : bSheep[selected]).includes(i) && state.wolf !== i && !state.sheep.includes(i);
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.onclick = () => {
if (state.turn !== myRole || state.winner) return;
const isMine = (myRole === "wolf" && state.wolf === i) || (myRole === "sheep" && state.sheep.includes(i));
if (selected === null) { if (isMine) selected = i; }
else { if (isMine) selected = (selected === i ? null : i); else { ws.send(JSON.stringify({ from: selected, to: i })); selected = 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' : ''}`);
g.appendChild(c);
const t = document.createElementNS("http://www.w3.org/2000/svg", "text");
t.setAttribute("x", x); t.setAttribute("y", y + 5);
t.setAttribute("text-anchor", "middle");
t.setAttribute("class", "node-label");
t.setAttribute("fill", (type === "empty" ? "black" : "white"));
t.textContent = i; g.appendChild(t);
svg.appendChild(g);
}
}
document.getElementById("restartBtn").onclick = () => { selected = null; ws.send(JSON.stringify({ type: "restart" })); };
document.getElementById("copyRoomBtn").onclick = () => { navigator.clipboard.writeText(window.location.href); alert("Kopiert!"); };
document.getElementById("exitBtn").onclick = () => { if (confirm("Beenden?")) { ws.send(JSON.stringify({ type: "leave" })); window.location.href = window.location.pathname; } };
</script>
</body>
</html>