Files
WolfSheepGame/public/index.html

437 lines
14 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 | Online</title>
<style>
:root {
--bg: #f0f2f5;
--wolf: #e63946;
--wolf-light: #ff707a;
--sheep: #457b9d;
--sheep-light: #8ecae6;
--black: #1d3557;
--white: #ffffff;
--green: #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;
}
/* Connection Dot */
.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;
}
svg {
width: 100%;
height: auto;
display: block;
}
.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;
}
#overlay {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.92);
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;
}
/* Toast Popup Styles */
#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>
<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>
<button class="btn btn-green" style="font-size: 1.1rem;" onclick="createRoom()">+ NEUES SPIEL</button>
<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 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>
<svg viewBox="0 0 600 420"></svg>
</div>
<div class="controls">
<button class="btn" id="copyBtn">Link Kopieren</button>
<button class="btn" onclick="ws.send(JSON.stringify({type:'restart'}))">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 ws, myRole, state, 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: [] };
function connect() {
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.onclose = () => document.getElementById("dot").classList.remove("conn-online");
ws.onmessage = e => {
const msg = JSON.parse(e.data);
if (msg.type === "room_list") updateRoomList(msg.rooms);
if (msg.type === "init") {
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") {
state = msg.game; myRole = msg.yourRole;
document.getElementById("overlay").className = msg.onlineCount < 2 ? "" : "hidden";
render();
}
};
}
function updateRoomList(rooms) {
const list = document.getElementById("roomList");
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() {
const id = Math.random().toString(36).substring(7);
window.location.search = `?room=${id}`;
}
function showToast() {
const toast = document.getElementById("toast");
toast.className = "show";
setTimeout(() => { toast.className = ""; }, 3000);
}
document.getElementById("copyBtn").onclick = async () => {
const url = window.location.href;
try {
await navigator.clipboard.writeText(url);
showToast();
} catch (err) {
const t = document.createElement("textarea");
t.value = url;
document.body.appendChild(t);
t.select();
document.execCommand('copy');
document.body.removeChild(t);
showToast();
}
};
function render() {
const svg = document.querySelector("svg");
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);
});
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.toUpperCase()}!`;
else statusEl.innerHTML = `${state.turn === myRole ? 'DEIN ZUG' : 'WARTEN...'}<br><small>ZUG ${state.moveCount}/40</small>`;
for (let i = 0; i <= 10; i++) {
const [x, y] = pos[i];
const isWolf = state.wolf === i;
const isSheep = state.sheep.includes(i);
const type = isWolf ? "wolf" : (isSheep ? "sheep" : "empty");
const isPossible = selected !== null && (myRole === "wolf" ? bWolf[selected] : bSheep[selected]).includes(i) && !isWolf && !isSheep;
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
g.onclick = () => {
if (state.turn !== myRole || state.winner) return;
const isMine = (myRole === "wolf" && isWolf) || (myRole === "sheep" && isSheep);
if (isMine) selected = i;
else if (isPossible) { ws.send(JSON.stringify({ from: selected, to: i })); selected = null; }
else 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 + 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);
}
}
connect();
</script>
</body>
</html>