Removed unecessary comments

This commit is contained in:
2026-02-20 20:14:42 +01:00
parent 44694abad2
commit 099a441745

View File

@@ -1,6 +1,6 @@
const express = require("express"); const express = require("express");
const http = require("http"); const http = require("http");
const net = require("net"); // Added for Bot Support const net = require("net");
const WebSocket = require("ws"); const WebSocket = require("ws");
const app = express(); const app = express();
@@ -9,11 +9,9 @@ app.use(express.static("public"));
const server = http.createServer(app); const server = http.createServer(app);
const wss = new WebSocket.Server({ server }); const wss = new WebSocket.Server({ server });
// Board Logic
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_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_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: [] };
// Direction Map for Bots
const pos_coords = { 0: [0,1], 1: [1,0], 2: [1,1], 3: [1,2], 4: [2,0], 5: [2,1], 6: [2,2], 7: [3,0], 8: [3,1], 9: [3,2], 10: [4,1] }; const pos_coords = { 0: [0,1], 1: [1,0], 2: [1,1], 3: [1,2], 4: [2,0], 5: [2,1], 6: [2,2], 7: [3,0], 8: [3,1], 9: [3,2], 10: [4,1] };
const dir_map = { "n": [0,-1], "s": [0,1], "o": [1,0], "w": [-1,0], "no": [1,-1], "nw": [-1,-1], "so": [1,1], "sw": [-1,1] }; const dir_map = { "n": [0,-1], "s": [0,1], "o": [1,0], "w": [-1,0], "no": [1,-1], "nw": [-1,-1], "so": [1,1], "sw": [-1,1] };
@@ -25,7 +23,6 @@ function getDir(from, to) {
const rooms = {}; const rooms = {};
// --- BOT TCP SERVER (Port 3031) ---
const botServer = net.createServer((socket) => { const botServer = net.createServer((socket) => {
let roomId = `BOT_${Math.random().toString(36).substring(7)}`; let roomId = `BOT_${Math.random().toString(36).substring(7)}`;
let botName = "UnknownBot"; let botName = "UnknownBot";
@@ -35,10 +32,8 @@ const botServer = net.createServer((socket) => {
socket.on("data", (data) => { socket.on("data", (data) => {
const msg = data.toString().trim(); const msg = data.toString().trim();
// Initial Handshake
if (!rooms[roomId]) { if (!rooms[roomId]) {
botName = msg.split(" ").pop(); botName = msg.split(" ").pop();
// socket.write(`#welcome ${botName}\n`);
rooms[roomId] = { rooms[roomId] = {
game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null, lastMove: null }, game: { wolf: 5, sheep: [0, 1, 3], turn: "sheep", moveCount: 0, winner: null, lastMove: null },
@@ -61,13 +56,11 @@ const botServer = net.createServer((socket) => {
const targetDir = msg.toLowerCase(); const targetDir = msg.toLowerCase();
to = board_wolf[from].find(t => getDir(from, t) === targetDir); to = board_wolf[from].find(t => getDir(from, t) === targetDir);
} else { } else {
// FIX: Parse the first character as the sheep index (1-3) const sheepIndex = parseInt(msg[0]) - 1;
const sheepIndex = parseInt(msg[0]) - 1; // Convert 1-3 to 0-2 array index
const targetDir = msg.substring(1).toLowerCase(); const targetDir = msg.substring(1).toLowerCase();
// Ensure the index is valid (0, 1, or 2)
if (game.sheep[sheepIndex] !== undefined) { if (game.sheep[sheepIndex] !== undefined) {
from = game.sheep[sheepIndex]; // Get board position (e.g., 6) from = game.sheep[sheepIndex];
to = board_sheep[from].find(t => getDir(from, t) === targetDir); to = board_sheep[from].find(t => getDir(from, t) === targetDir);
} }
} }
@@ -86,7 +79,7 @@ const botServer = net.createServer((socket) => {
function swapRoles(room) { function swapRoles(room) {
room.players.forEach(p => { room.players.forEach(p => {
p.role = (p.role === "wolf") ? "sheep" : "wolf"; p.role = (p.role === "wolf") ? "sheep" : "wolf";
if (p.isBot) p.botInitialized = false; // Reset bot flag for new role msg if (p.isBot) p.botInitialized = false;
}); });
} }
@@ -97,20 +90,17 @@ function sendBotState(roomId) {
const game = room.game; const game = room.game;
// 1. Handle Game End
if (game.winner) { if (game.winner) {
const result = (game.winner === bot.role) ? "you win" : "you loose"; const result = (game.winner === bot.role) ? "you win" : "you loose";
bot.socket.write(`#gameends ${result}\n`); bot.socket.write(`#gameends ${result}\n`);
return; return;
} }
// 2. Send Role (Only once at start of game/restart)
if (!bot.botInitialized) { if (!bot.botInitialized) {
bot.socket.write(`#role ${bot.role === "wolf" ? "WOLF" : "SCHAF"}\n`); bot.socket.write(`#role ${bot.role === "wolf" ? "WOLF" : "SCHAF"}\n`);
bot.botInitialized = true; bot.botInitialized = true;
} }
// 3. Send Turn Data
if (game.turn === bot.role) { if (game.turn === bot.role) {
bot.socket.write(`#pos_opponent ${bot.role === "wolf" ? game.sheep.join(" ") : game.wolf}\n`); bot.socket.write(`#pos_opponent ${bot.role === "wolf" ? game.sheep.join(" ") : game.wolf}\n`);
bot.socket.write(`#pos_you ${bot.role === "wolf" ? game.wolf : game.sheep.join(" ")}\n`); bot.socket.write(`#pos_you ${bot.role === "wolf" ? game.wolf : game.sheep.join(" ")}\n`);
@@ -131,8 +121,6 @@ function sendBotState(roomId) {
} }
} }
// --- EXISTING LOGIC UPDATED ---
function broadcastRoomList() { function broadcastRoomList() {
const list = JSON.stringify({ type: "room_list", rooms: getRoomList() }); const list = JSON.stringify({ type: "room_list", rooms: getRoomList() });
wss.clients.forEach(client => { wss.clients.forEach(client => {
@@ -183,7 +171,6 @@ function checkWinConditions(game) {
return null; return null;
} }
// WebSocket connection logic remains mostly same, just ensure it adds human to bot room
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");
@@ -203,7 +190,7 @@ wss.on("connection", (ws, req) => {
const room = rooms[roomId]; const room = rooms[roomId];
ws.roomId = roomId; ws.roomId = roomId;
ws.sessionId = sessionId; // Store on socket for easy lookup during close ws.sessionId = sessionId;
let player = room.players.find(p => p.sessionId === sessionId); let player = room.players.find(p => p.sessionId === sessionId);
@@ -225,23 +212,18 @@ wss.on("connection", (ws, req) => {
broadcastState(roomId); broadcastState(roomId);
if (room.players.some(p => p.isBot)) sendBotState(roomId); if (room.players.some(p => p.isBot)) sendBotState(roomId);
// --- HANDLE DISCONNECT ---
ws.on("close", () => { ws.on("close", () => {
const currentRoom = rooms[ws.roomId]; const currentRoom = rooms[ws.roomId];
if (!currentRoom) return; if (!currentRoom) return;
// Remove the player from the room array
currentRoom.players = currentRoom.players.filter(p => p.sessionId !== ws.sessionId); currentRoom.players = currentRoom.players.filter(p => p.sessionId !== ws.sessionId);
// If the room is now empty (or only contains a bot), delete it
const humanRemaining = currentRoom.players.some(p => !p.isBot); const humanRemaining = currentRoom.players.some(p => !p.isBot);
if (currentRoom.players.length === 0 || (!humanRemaining && currentRoom.players.some(p => p.isBot))) { if (currentRoom.players.length === 0 || (!humanRemaining && currentRoom.players.some(p => p.isBot))) {
// If it was a bot room, close the bot socket before deleting
const bot = currentRoom.players.find(p => p.isBot); const bot = currentRoom.players.find(p => p.isBot);
if (bot && bot.socket) bot.socket.destroy(); if (bot && bot.socket) bot.socket.destroy();
delete rooms[ws.roomId]; delete rooms[ws.roomId];
} else { } else {
// Room still has someone, notify them
broadcastState(ws.roomId); broadcastState(ws.roomId);
} }