Swap on reset
This commit is contained in:
109
server.js
109
server.js
@@ -9,67 +9,26 @@ app.use(express.static("public"));
|
||||
const server = http.createServer(app);
|
||||
const wss = new WebSocket.Server({ server });
|
||||
|
||||
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 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 rooms = {};
|
||||
|
||||
function broadcast(roomId) {
|
||||
const room = rooms[roomId];
|
||||
if (!room) return;
|
||||
const msg = JSON.stringify({ type: "state", game: room.game });
|
||||
|
||||
room.players.forEach(p => {
|
||||
if (p.ws && p.ws.readyState === WebSocket.OPEN) p.ws.send(msg);
|
||||
if (p.ws && p.ws.readyState === WebSocket.OPEN) {
|
||||
p.ws.send(JSON.stringify({
|
||||
type: "state",
|
||||
game: room.game,
|
||||
yourRole: p.role
|
||||
}));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
function handleMove(roomId, role, { from, to }) {
|
||||
const room = rooms[roomId];
|
||||
if (!room || room.game.winner) return;
|
||||
const game = room.game;
|
||||
if (game.turn !== role) return;
|
||||
|
||||
const allowed = (role === "wolf") ? board_wolf[from] : board_sheep[from];
|
||||
if (!allowed?.includes(to)) return;
|
||||
if (game.sheep.includes(to) || game.wolf === to) return;
|
||||
|
||||
if (role === "wolf") {
|
||||
if (from !== game.wolf) return;
|
||||
game.wolf = to;
|
||||
game.turn = "sheep";
|
||||
} else {
|
||||
const i = game.sheep.indexOf(from);
|
||||
if (i === -1) return;
|
||||
game.sheep[i] = to;
|
||||
game.turn = "wolf";
|
||||
}
|
||||
game.moveCount++;
|
||||
game.winner = checkWinConditions(game);
|
||||
broadcast(roomId);
|
||||
}
|
||||
|
||||
wss.on("connection", (ws, req) => {
|
||||
const urlParams = new URLSearchParams(req.url.split('?')[1]);
|
||||
let roomId = urlParams.get("room");
|
||||
@@ -79,7 +38,8 @@ wss.on("connection", (ws, req) => {
|
||||
if (!roomId) roomId = randomBytes(3).toString("hex");
|
||||
rooms[roomId] = {
|
||||
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null },
|
||||
players: []
|
||||
players: [],
|
||||
nextFirstRole: "sheep" // Tracks who should be sheep next game
|
||||
};
|
||||
}
|
||||
|
||||
@@ -93,35 +53,58 @@ wss.on("connection", (ws, req) => {
|
||||
}
|
||||
|
||||
if (player) {
|
||||
player.ws = ws; // Re-attach new socket to existing session
|
||||
player.ws = ws;
|
||||
} else {
|
||||
const role = room.players.length === 0 ? "sheep" : "wolf";
|
||||
// Assign role based on who is already there
|
||||
const role = room.players.length === 0 ? room.nextFirstRole : (room.nextFirstRole === "sheep" ? "wolf" : "sheep");
|
||||
player = { ws, role, sessionId };
|
||||
room.players.push(player);
|
||||
}
|
||||
|
||||
ws.send(JSON.stringify({ type: "role", role: player.role, roomId }));
|
||||
ws.send(JSON.stringify({ type: "init", role: player.role, roomId }));
|
||||
broadcast(roomId);
|
||||
|
||||
ws.on("message", msg => {
|
||||
let data;
|
||||
try { data = JSON.parse(msg); } catch { return; }
|
||||
let data = JSON.parse(msg);
|
||||
|
||||
if (data.type === "restart") {
|
||||
// Swap roles for the next round
|
||||
room.players.forEach(p => p.role = (p.role === "wolf" ? "sheep" : "wolf"));
|
||||
room.game = { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null };
|
||||
broadcast(roomId);
|
||||
} else if (data.type === "leave") {
|
||||
room.players = room.players.filter(p => p.sessionId !== sessionId);
|
||||
if (room.players.length === 0) delete rooms[roomId];
|
||||
ws.close();
|
||||
} else {
|
||||
handleMove(roomId, player.role, data);
|
||||
}
|
||||
});
|
||||
|
||||
ws.on("close", () => {
|
||||
// We don't remove the player here so they can reconnect on refresh
|
||||
});
|
||||
});
|
||||
|
||||
server.listen(3030, () => console.log("Server läuft auf Port 3030"));
|
||||
// Reuse the handleMove and checkWinConditions from previous scripts...
|
||||
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;
|
||||
}
|
||||
|
||||
function handleMove(roomId, role, { from, to }) {
|
||||
const room = rooms[roomId];
|
||||
if (!room || room.game.winner || room.game.turn !== role) return;
|
||||
const game = room.game;
|
||||
const allowed = (role === "wolf") ? board_wolf[from] : board_sheep[from];
|
||||
if (!allowed?.includes(to) || game.sheep.includes(to) || game.wolf === to) return;
|
||||
|
||||
if (role === "wolf") { game.wolf = to; game.turn = "sheep"; }
|
||||
else { game.sheep[game.sheep.indexOf(from)] = to; game.turn = "wolf"; }
|
||||
|
||||
game.moveCount++;
|
||||
game.winner = checkWinConditions(game);
|
||||
broadcast(roomId);
|
||||
}
|
||||
|
||||
server.listen(3030);
|
||||
Reference in New Issue
Block a user