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

328 lines
11 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 | Rough Edition</title>
<style>
:root {
--bg: #e5e7eb;
--wolf: #ff3e3e;
--sheep: #3b82f6;
--black: #1a1a1a;
--surface: #ffffff;
}
body {
font-family: 'Courier New', Courier, monospace;
/* Rough, typewriter feel */
background-color: var(--bg);
color: var(--black);
margin: 0;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
/* Large Screen Scaling */
.game-wrapper {
width: 100%;
max-width: 900px;
/* Increased for big monitors */
padding: 20px;
box-sizing: border-box;
}
.main-card {
background: var(--surface);
border: 4px solid var(--black);
box-shadow: 12px 12px 0px var(--black);
/* Brutalist shadow */
padding: 2rem;
display: flex;
flex-direction: column;
gap: 20px;
}
/* Clear Identity Section */
.identity-banner {
display: flex;
align-items: center;
justify-content: space-between;
border: 3px solid var(--black);
padding: 15px;
background: #f3f4f6;
}
.id-box {
font-size: 1.5rem;
font-weight: 900;
padding: 10px 20px;
border: 3px solid var(--black);
text-transform: uppercase;
}
.is-wolf {
background: var(--wolf);
color: white;
}
.is-sheep {
background: var(--sheep);
color: white;
}
#status {
font-size: 1.2rem;
font-weight: bold;
text-align: right;
}
/* Board Scaling */
.board-container {
width: 100%;
aspect-ratio: 600 / 420;
background: #fff;
border: 3px solid var(--black);
cursor: crosshair;
}
svg {
width: 100%;
height: 100%;
}
/* Rough Elements */
.edge {
stroke: var(--black);
stroke-width: 4;
}
.node-circle {
stroke: var(--black);
stroke-width: 3;
transition: transform 0.1s;
}
.empty {
fill: #fff;
}
.sheep {
fill: var(--sheep);
}
.wolf {
fill: var(--wolf);
}
.selected {
stroke-width: 6;
stroke: #000;
fill: #fde047;
/* Yellow highlight for selection */
}
.possible {
fill: #fff;
stroke: #22c55e;
stroke-width: 4;
stroke-dasharray: 6;
}
.node-label {
font-family: Arial, sans-serif;
font-weight: 900;
pointer-events: none;
user-select: none;
}
/* Buttons */
.controls {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 15px;
}
button {
border: 3px solid var(--black);
background: white;
padding: 15px;
font-weight: 900;
cursor: pointer;
box-shadow: 4px 4px 0px var(--black);
text-transform: uppercase;
}
button:active {
box-shadow: 0px 0px 0px var(--black);
transform: translate(4px, 4px);
}
button#exitBtn {
background: #fda4af;
}
button#copyRoomBtn {
background: #bae6fd;
}
@media (max-width: 600px) {
.main-card {
padding: 10px;
border-width: 3px;
box-shadow: 6px 6px 0px var(--black);
}
.identity-banner {
flex-direction: column;
gap: 10px;
}
.controls {
grid-template-columns: 1fr;
}
}
</style>
</head>
<body>
<div class="game-wrapper">
<div class="main-card">
<div class="identity-banner">
<div>
<span style="font-size: 0.8rem; display: block;">DEINE ROLLE:</span>
<div id="idBadge" class="id-box">...</div>
</div>
<div id="status">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 Kopieren</button>
<button id="restartBtn">Neustart</button>
<button id="exitBtn">Verlassen</button>
</div>
</div>
</div>
<script>
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("Code (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;
const badge = document.getElementById("idBadge");
badge.innerText = myRole === "wolf" ? "DER WOLF" : "DIE SCHAFE";
badge.className = `id-box is-${myRole}`;
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) {
statusEl.innerHTML = `ENDE: ${state.winner.toUpperCase()} GEWINNT!`;
} else {
statusEl.innerHTML = (state.turn === myRole ? "DU BIST DRAN" : "WARTE...") + `<br><small>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.style.cursor = "pointer";
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", 26);
circle.setAttribute("class", `node-circle ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''}`);
group.appendChild(circle);
// Added Numbers back to nodes
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("class", "node-label");
txt.setAttribute("fill", (type === "empty" || selected === i) ? "black" : "white");
txt.textContent = i;
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>