Update server.js

This commit is contained in:
pancakes-proxy 2025-03-11 10:49:45 -04:00 committed by GitHub
parent 337d8a3da4
commit d3d61b5284
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -1,19 +1,20 @@
const express = require('express');
const http = require('http');
const { Server } = require('socket.io');
const cookieParser = require('cookie-parser'); // Middleware for cookies
const app = express();
const server = http.createServer(app);
const io = new Server(server);
// Serve static files
// Serve static files and parse cookies
app.use(express.static('public'));
app.use(cookieParser());
// Chat state
let isChatLocked = false; // Lock state
let messageHistory = []; // Store messages
const users = {}; // { socketId: username }
const admins = {}; // { socketId: adminUsername }
// Serve admin panel
app.get('/admin', (req, res) => {
@ -24,57 +25,54 @@ app.get('/admin', (req, res) => {
io.on('connection', (socket) => {
console.log('User connected:', socket.id);
// Set a default username for regular users
// Set a default username
users[socket.id] = "Anonymous";
// Check if the user has the admin cookie
const cookies = socket.handshake.headers.cookie || '';
const isAdmin = cookies.includes('ADMINSERVERSERVICEPERMSEC3256');
// Handle username updates
socket.on('set username', (username) => {
users[socket.id] = username || "Anonymous";
console.log(`User ${socket.id} set their username to: ${users[socket.id]}`);
});
// Handle admin login
socket.on('admin login', (adminUsername) => {
admins[socket.id] = adminUsername; // Add admin to admin list
console.log(`Admin logged in: ${adminUsername}`);
});
// Handle chat messages
socket.on('chat message', (msg) => {
if (!isChatLocked || admins[socket.id]) { // Bypass lock for admins
const isAdmin = !!admins[socket.id];
const username = isAdmin ? `${admins[socket.id]} {admin}` : users[socket.id] || "Anonymous";
const formattedMessage = `${username}: ${msg}`;
messageHistory.push(formattedMessage); // Add to message history
if (!isChatLocked || isAdmin) { // Admins bypass lock
const username = users[socket.id] || "Anonymous";
const prefix = isAdmin ? `[Admin] ${username}` : username; // Add admin tag if applicable
const formattedMessage = `${prefix}: ${msg}`;
messageHistory.push(formattedMessage); // Save message history
io.emit('chat message', formattedMessage); // Broadcast to all clients
} else {
socket.emit('chat locked'); // Notify regular users if the chat is locked
}
});
// Handle admin-specific actions
// Handle admin actions
socket.on('admin lock', () => {
isChatLocked = true;
io.emit('chat lock status', isChatLocked); // Notify all clients
console.log('Chat has been locked by admin');
io.emit('chat lock status', isChatLocked);
console.log('Chat locked by admin');
});
socket.on('admin unlock', () => {
isChatLocked = false;
io.emit('chat lock status', isChatLocked); // Notify all clients
console.log('Chat has been unlocked by admin');
io.emit('chat lock status', isChatLocked);
console.log('Chat unlocked by admin');
});
socket.on('admin clear messages', () => {
messageHistory = []; // Clear message history
io.emit('chat history', messageHistory); // Notify all clients to clear chat
console.log('Chat messages have been cleared by admin');
messageHistory = [];
io.emit('chat history', messageHistory);
console.log('Messages cleared by admin');
});
socket.on('admin announcement', (announcement) => {
const adminMessage = `SERVERHOST (admin): ${announcement}`;
io.emit('chat message', adminMessage); // Broadcast announcement to all users
io.emit('chat message', adminMessage);
console.log('Announcement sent:', adminMessage);
});
@ -82,7 +80,6 @@ io.on('connection', (socket) => {
socket.on('disconnect', () => {
console.log('User disconnected:', socket.id);
delete users[socket.id];
delete admins[socket.id]; // Remove from admins if applicable
});
});