Heartbeat server
This commit is contained in:
@@ -14,6 +14,7 @@
|
|||||||
--sheep-light: #7db0cf;
|
--sheep-light: #7db0cf;
|
||||||
--black: #1d3557;
|
--black: #1d3557;
|
||||||
--white: #ffffff;
|
--white: #ffffff;
|
||||||
|
--green: #10b981;
|
||||||
}
|
}
|
||||||
|
|
||||||
body {
|
body {
|
||||||
@@ -55,6 +56,12 @@
|
|||||||
gap: 10px;
|
gap: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.room-info {
|
||||||
|
display: flex;
|
||||||
|
align-items: center;
|
||||||
|
gap: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
.room-tag {
|
.room-tag {
|
||||||
font-size: 0.8rem;
|
font-size: 0.8rem;
|
||||||
font-weight: bold;
|
font-weight: bold;
|
||||||
@@ -64,6 +71,26 @@
|
|||||||
text-transform: uppercase;
|
text-transform: uppercase;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Connection Indicator */
|
||||||
|
.pulse {
|
||||||
|
width: 10px;
|
||||||
|
height: 10px;
|
||||||
|
border-radius: 50%;
|
||||||
|
background: #94a3b8;
|
||||||
|
display: inline-block;
|
||||||
|
transition: background 0.3s;
|
||||||
|
}
|
||||||
|
|
||||||
|
.online {
|
||||||
|
background: var(--green);
|
||||||
|
box-shadow: 0 0 8px var(--green);
|
||||||
|
}
|
||||||
|
|
||||||
|
.offline {
|
||||||
|
background: var(--wolf);
|
||||||
|
box-shadow: 0 0 8px var(--wolf);
|
||||||
|
}
|
||||||
|
|
||||||
.id-box {
|
.id-box {
|
||||||
font-size: 1.2rem;
|
font-size: 1.2rem;
|
||||||
font-weight: 900;
|
font-weight: 900;
|
||||||
@@ -87,7 +114,6 @@
|
|||||||
aspect-ratio: 600 / 420;
|
aspect-ratio: 600 / 420;
|
||||||
border: 3px solid var(--black);
|
border: 3px solid var(--black);
|
||||||
background: #fff;
|
background: #fff;
|
||||||
position: relative;
|
|
||||||
touch-action: manipulation;
|
touch-action: manipulation;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -106,13 +132,13 @@
|
|||||||
stroke: var(--black);
|
stroke: var(--black);
|
||||||
stroke-width: 2.5;
|
stroke-width: 2.5;
|
||||||
transition: all 0.15s ease;
|
transition: all 0.15s ease;
|
||||||
|
cursor: pointer;
|
||||||
}
|
}
|
||||||
|
|
||||||
.empty {
|
.empty {
|
||||||
fill: #fff;
|
fill: #fff;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Piece Colors */
|
|
||||||
.sheep {
|
.sheep {
|
||||||
fill: var(--sheep);
|
fill: var(--sheep);
|
||||||
}
|
}
|
||||||
@@ -121,7 +147,6 @@
|
|||||||
fill: var(--wolf);
|
fill: var(--wolf);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Subtle Select: Lightens the fill and adds a clean 4px border */
|
|
||||||
.wolf.selected {
|
.wolf.selected {
|
||||||
fill: var(--wolf-light);
|
fill: var(--wolf-light);
|
||||||
stroke-width: 4;
|
stroke-width: 4;
|
||||||
@@ -134,7 +159,7 @@
|
|||||||
|
|
||||||
.possible {
|
.possible {
|
||||||
fill: #fff;
|
fill: #fff;
|
||||||
stroke: #10b981;
|
stroke: var(--green);
|
||||||
stroke-width: 3;
|
stroke-width: 3;
|
||||||
stroke-dasharray: 4;
|
stroke-dasharray: 4;
|
||||||
}
|
}
|
||||||
@@ -176,8 +201,6 @@
|
|||||||
@media (max-width: 600px) {
|
@media (max-width: 600px) {
|
||||||
.main-card {
|
.main-card {
|
||||||
padding: 10px;
|
padding: 10px;
|
||||||
box-shadow: 4px 4px 0px var(--black);
|
|
||||||
gap: 10px;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.info-bar {
|
.info-bar {
|
||||||
@@ -193,10 +216,6 @@
|
|||||||
.controls {
|
.controls {
|
||||||
grid-template-columns: 1fr;
|
grid-template-columns: 1fr;
|
||||||
}
|
}
|
||||||
|
|
||||||
.id-box {
|
|
||||||
font-size: 1rem;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
</head>
|
</head>
|
||||||
@@ -205,17 +224,16 @@
|
|||||||
<div class="game-wrapper">
|
<div class="game-wrapper">
|
||||||
<div class="main-card">
|
<div class="main-card">
|
||||||
<div class="info-bar">
|
<div class="info-bar">
|
||||||
|
<div class="room-info">
|
||||||
<div>
|
<div>
|
||||||
<div class="room-tag">RAUM: <span id="codeDisplay">...</span></div>
|
<div class="room-tag">RAUM: <span id="codeDisplay">...</span> <span id="connCircle"
|
||||||
|
class="pulse"></span></div>
|
||||||
<div id="idBadge" class="id-box">...</div>
|
<div id="idBadge" class="id-box">...</div>
|
||||||
</div>
|
</div>
|
||||||
<div id="status" style="text-align: right; font-weight: 900; line-height: 1.2;">Laden...</div>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div id="status" style="text-align: right; font-weight: 900; line-height: 1.2;">Verbinde...</div>
|
||||||
<div class="board-container">
|
|
||||||
<svg viewBox="0 0 600 420" preserveAspectRatio="xMidYMid meet"></svg>
|
|
||||||
</div>
|
</div>
|
||||||
|
<div class="board-container"><svg viewBox="0 0 600 420" preserveAspectRatio="xMidYMid meet"></svg></div>
|
||||||
<div class="controls">
|
<div class="controls">
|
||||||
<button id="copyRoomBtn">Link</button>
|
<button id="copyRoomBtn">Link</button>
|
||||||
<button id="restartBtn">Swap & Reset</button>
|
<button id="restartBtn">Swap & Reset</button>
|
||||||
@@ -227,47 +245,53 @@
|
|||||||
<script>
|
<script>
|
||||||
let sessionId = localStorage.getItem("game_session_id") || (Math.random().toString(36).substring(2));
|
let sessionId = localStorage.getItem("game_session_id") || (Math.random().toString(36).substring(2));
|
||||||
localStorage.setItem("game_session_id", sessionId);
|
localStorage.setItem("game_session_id", sessionId);
|
||||||
|
|
||||||
const params = new URLSearchParams(window.location.search);
|
const params = new URLSearchParams(window.location.search);
|
||||||
let roomId = params.get("room") || prompt("Raumcode (leer für neu):");
|
let roomId = params.get("room");
|
||||||
|
let ws, myRole, state, selected = null;
|
||||||
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 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 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 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: [] };
|
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("connCircle").className = "pulse online";
|
||||||
|
};
|
||||||
|
|
||||||
ws.onmessage = e => {
|
ws.onmessage = e => {
|
||||||
const msg = JSON.parse(e.data);
|
const msg = JSON.parse(e.data);
|
||||||
if (msg.type === "init") {
|
if (msg.type === "init") {
|
||||||
document.getElementById("codeDisplay").innerText = msg.roomId;
|
roomId = msg.roomId;
|
||||||
if (!params.has("room")) window.history.replaceState({}, '', `?room=${msg.roomId}`);
|
document.getElementById("codeDisplay").innerText = roomId;
|
||||||
|
window.history.replaceState({}, '', `?room=${roomId}`);
|
||||||
}
|
}
|
||||||
if (msg.type === "state") {
|
if (msg.type === "state") {
|
||||||
state = msg.game;
|
state = msg.game; myRole = msg.yourRole;
|
||||||
myRole = msg.yourRole;
|
updateUI(); render();
|
||||||
updateUI();
|
|
||||||
render();
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
ws.onclose = () => {
|
||||||
|
document.getElementById("connCircle").className = "pulse offline";
|
||||||
|
setTimeout(connect, 2000);
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function updateUI() {
|
function updateUI() {
|
||||||
const badge = document.getElementById("idBadge");
|
const badge = document.getElementById("idBadge");
|
||||||
badge.innerText = myRole === "wolf" ? "WOLF" : "SCHAFE";
|
badge.innerText = myRole === "wolf" ? "WOLF" : "SCHAFE";
|
||||||
badge.className = `id-box is-${myRole}`;
|
badge.className = `id-box is-${myRole}`;
|
||||||
|
|
||||||
const statusEl = document.getElementById("status");
|
const statusEl = document.getElementById("status");
|
||||||
if (state.winner) {
|
if (state.winner) statusEl.innerHTML = `SIEG: ${state.winner.toUpperCase()}!`;
|
||||||
statusEl.innerHTML = `SIEG: ${state.winner === 'wolf' ? 'WOLF' : 'SCHAFE'}`;
|
else statusEl.innerHTML = `${state.turn === myRole ? 'DEIN ZUG' : 'WARTEN...'}<br><small>ZUG ${state.moveCount}/40</small>`;
|
||||||
} else {
|
|
||||||
statusEl.innerHTML = `${state.turn === myRole ? 'DEIN ZUG' : 'Gegner...'}<br><small>ZUG ${state.moveCount}/40</small>`;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function render() {
|
function render() {
|
||||||
|
const svg = document.querySelector("svg");
|
||||||
svg.innerHTML = "";
|
svg.innerHTML = "";
|
||||||
edges.forEach(([a, b]) => {
|
edges.forEach(([a, b]) => {
|
||||||
const l = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
const l = document.createElementNS("http://www.w3.org/2000/svg", "line");
|
||||||
@@ -280,34 +304,31 @@
|
|||||||
const [x, y] = pos[i];
|
const [x, y] = pos[i];
|
||||||
let type = state.wolf === i ? "wolf" : (state.sheep.includes(i) ? "sheep" : "empty");
|
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 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");
|
const g = document.createElementNS("http://www.w3.org/2000/svg", "g");
|
||||||
g.onclick = () => {
|
g.onclick = () => {
|
||||||
if (state.turn !== myRole || state.winner) return;
|
if (state.turn !== myRole || state.winner) return;
|
||||||
const isMine = (myRole === "wolf" && state.wolf === i) || (myRole === "sheep" && state.sheep.includes(i));
|
const isMine = (myRole === "wolf" && state.wolf === i) || (myRole === "sheep" && state.sheep.includes(i));
|
||||||
if (selected === null) { if (isMine) selected = 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; } }
|
else { if (isMine) selected = (selected === i ? null : i); else { if (ws.readyState === 1) ws.send(JSON.stringify({ from: selected, to: i })); selected = null; } }
|
||||||
render();
|
render();
|
||||||
};
|
};
|
||||||
|
|
||||||
const c = document.createElementNS("http://www.w3.org/2000/svg", "circle");
|
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("cx", x); c.setAttribute("cy", y); c.setAttribute("r", 25);
|
||||||
c.setAttribute("class", `node-circle ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''}`);
|
c.setAttribute("class", `node-circle ${type} ${selected === i ? 'selected' : ''} ${isPossible ? 'possible' : ''}`);
|
||||||
g.appendChild(c);
|
g.appendChild(c);
|
||||||
|
|
||||||
const t = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
const t = document.createElementNS("http://www.w3.org/2000/svg", "text");
|
||||||
t.setAttribute("x", x); t.setAttribute("y", y + 5);
|
t.setAttribute("x", x); t.setAttribute("y", y + 5); t.setAttribute("text-anchor", "middle");
|
||||||
t.setAttribute("text-anchor", "middle");
|
t.setAttribute("class", "node-label"); t.setAttribute("fill", (type === "empty" ? "black" : "white"));
|
||||||
t.setAttribute("class", "node-label");
|
|
||||||
t.setAttribute("fill", (type === "empty" ? "black" : "white"));
|
|
||||||
t.textContent = i; g.appendChild(t);
|
t.textContent = i; g.appendChild(t);
|
||||||
svg.appendChild(g);
|
svg.appendChild(g);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
document.getElementById("restartBtn").onclick = () => { selected = null; ws.send(JSON.stringify({ type: "restart" })); };
|
document.getElementById("restartBtn").onclick = () => { if (ws.readyState === 1) ws.send(JSON.stringify({ type: "restart" })); };
|
||||||
document.getElementById("copyRoomBtn").onclick = () => { navigator.clipboard.writeText(window.location.href); alert("Kopiert!"); };
|
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; } };
|
document.getElementById("exitBtn").onclick = () => { if (confirm("Beenden?")) { if (ws.readyState === 1) ws.send(JSON.stringify({ type: "leave" })); window.location.href = window.location.pathname; } };
|
||||||
|
|
||||||
|
connect();
|
||||||
</script>
|
</script>
|
||||||
</body>
|
</body>
|
||||||
|
|
||||||
|
|||||||
56
server.js
56
server.js
@@ -17,29 +17,36 @@ const rooms = {};
|
|||||||
function broadcast(roomId) {
|
function broadcast(roomId) {
|
||||||
const room = rooms[roomId];
|
const room = rooms[roomId];
|
||||||
if (!room) return;
|
if (!room) return;
|
||||||
|
|
||||||
room.players.forEach(p => {
|
room.players.forEach(p => {
|
||||||
if (p.ws && p.ws.readyState === WebSocket.OPEN) {
|
if (p.ws && p.ws.readyState === WebSocket.OPEN) {
|
||||||
p.ws.send(JSON.stringify({
|
p.ws.send(JSON.stringify({ type: "state", game: room.game, yourRole: p.role }));
|
||||||
type: "state",
|
|
||||||
game: room.game,
|
|
||||||
yourRole: p.role
|
|
||||||
}));
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Heartbeat: Check if clients are still alive every 30s
|
||||||
|
const interval = setInterval(() => {
|
||||||
|
wss.clients.forEach(ws => {
|
||||||
|
if (ws.isAlive === false) return ws.terminate();
|
||||||
|
ws.isAlive = false;
|
||||||
|
ws.ping();
|
||||||
|
});
|
||||||
|
}, 30000);
|
||||||
|
|
||||||
wss.on("connection", (ws, req) => {
|
wss.on("connection", (ws, req) => {
|
||||||
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
||||||
let roomId = urlParams.get("room");
|
let roomId = urlParams.get("room");
|
||||||
let sessionId = urlParams.get("sessionId");
|
let sessionId = urlParams.get("sessionId");
|
||||||
|
|
||||||
|
ws.isAlive = true;
|
||||||
|
ws.on('pong', () => ws.isAlive = true);
|
||||||
|
|
||||||
if (!roomId || !rooms[roomId]) {
|
if (!roomId || !rooms[roomId]) {
|
||||||
if (!roomId) roomId = randomBytes(3).toString("hex");
|
if (!roomId || roomId === "null") roomId = randomBytes(3).toString("hex");
|
||||||
rooms[roomId] = {
|
rooms[roomId] = {
|
||||||
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null },
|
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null },
|
||||||
players: [],
|
players: [],
|
||||||
nextFirstRole: "sheep" // Tracks who should be sheep next game
|
nextFirstRole: "sheep"
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +62,6 @@ wss.on("connection", (ws, req) => {
|
|||||||
if (player) {
|
if (player) {
|
||||||
player.ws = ws;
|
player.ws = ws;
|
||||||
} else {
|
} else {
|
||||||
// Assign role based on who is already there
|
|
||||||
const role = room.players.length === 0 ? room.nextFirstRole : (room.nextFirstRole === "sheep" ? "wolf" : "sheep");
|
const role = room.players.length === 0 ? room.nextFirstRole : (room.nextFirstRole === "sheep" ? "wolf" : "sheep");
|
||||||
player = { ws, role, sessionId };
|
player = { ws, role, sessionId };
|
||||||
room.players.push(player);
|
room.players.push(player);
|
||||||
@@ -66,31 +72,23 @@ wss.on("connection", (ws, req) => {
|
|||||||
|
|
||||||
ws.on("message", msg => {
|
ws.on("message", msg => {
|
||||||
let data = JSON.parse(msg);
|
let data = JSON.parse(msg);
|
||||||
|
|
||||||
if (data.type === "restart") {
|
if (data.type === "restart") {
|
||||||
// Swap roles for the next round
|
|
||||||
room.players.forEach(p => p.role = (p.role === "wolf" ? "sheep" : "wolf"));
|
room.players.forEach(p => p.role = (p.role === "wolf" ? "sheep" : "wolf"));
|
||||||
|
room.nextFirstRole = room.nextFirstRole === "sheep" ? "wolf" : "sheep";
|
||||||
room.game = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null };
|
room.game = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null };
|
||||||
broadcast(roomId);
|
broadcast(roomId);
|
||||||
} else if (data.type === "leave") {
|
} else if (data.type === "leave") {
|
||||||
room.players = room.players.filter(p => p.sessionId !== sessionId);
|
room.players = room.players.filter(p => p.sessionId !== sessionId);
|
||||||
if (room.players.length === 0) delete rooms[roomId];
|
if (room.players.length === 0) delete rooms[roomId];
|
||||||
} else {
|
} else if (data.from !== undefined && data.to !== undefined) {
|
||||||
handleMove(roomId, player.role, data);
|
handleMove(roomId, player.role, data);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
|
||||||
// Reuse the handleMove and checkWinConditions from previous scripts...
|
ws.on("close", () => {
|
||||||
function checkWinConditions(game) {
|
// We keep the player in the room for a while to allow reconnects
|
||||||
if (game.wolf === 0) return "wolf";
|
});
|
||||||
if (game.moveCount >= 40) return "wolf";
|
});
|
||||||
const canWolfMove = board_wolf[game.wolf].some(to => !game.sheep.includes(to));
|
|
||||||
const canSheepMove = game.sheep.some(from => board_sheep[from].some(to => to !== game.wolf && !game.sheep.includes(to)));
|
|
||||||
if (!canWolfMove) return "sheep";
|
|
||||||
if (!canSheepMove && game.turn === "sheep") return "wolf";
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
function handleMove(roomId, role, { from, to }) {
|
function handleMove(roomId, role, { from, to }) {
|
||||||
const room = rooms[roomId];
|
const room = rooms[roomId];
|
||||||
@@ -107,4 +105,14 @@ function handleMove(roomId, role, { from, to }) {
|
|||||||
broadcast(roomId);
|
broadcast(roomId);
|
||||||
}
|
}
|
||||||
|
|
||||||
server.listen(3030);
|
function checkWinConditions(game) {
|
||||||
|
if (game.wolf === 0) return "wolf";
|
||||||
|
if (game.moveCount >= 40) return "wolf";
|
||||||
|
const canWolfMove = board_wolf[game.wolf].some(to => !game.sheep.includes(to));
|
||||||
|
const canSheepMove = game.sheep.some(from => board_sheep[from].some(to => to !== game.wolf && !game.sheep.includes(to)));
|
||||||
|
if (!canWolfMove) return "sheep";
|
||||||
|
if (!canSheepMove && game.turn === "sheep") return "wolf";
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
server.listen(3030, () => console.log("Server running on port 3030"));
|
||||||
Reference in New Issue
Block a user