srztjrtz
This commit is contained in:
parent
8d9f890337
commit
09a119f68b
@ -246,45 +246,6 @@ body {
|
||||
color: #7ec8ff;
|
||||
}
|
||||
|
||||
/* ── Online-User-Liste ──────────────────────────── */
|
||||
|
||||
#online-users {
|
||||
position: fixed;
|
||||
left: 20px;
|
||||
bottom: 340px;
|
||||
width: 350px;
|
||||
background: linear-gradient(#2b2115, #1a140d);
|
||||
border: 3px solid #8b6a3c;
|
||||
color: #f3e6c5;
|
||||
font-family: serif;
|
||||
overflow-y: auto;
|
||||
}
|
||||
|
||||
.online-header {
|
||||
background: #3a2a17;
|
||||
padding: 5px;
|
||||
text-align: center;
|
||||
border-bottom: 2px solid #8b6a3c;
|
||||
}
|
||||
|
||||
#online-list div {
|
||||
padding: 4px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
#online-list div:hover {
|
||||
background: #5b4325;
|
||||
}
|
||||
|
||||
.online-dot {
|
||||
display: inline-block;
|
||||
width: 8px;
|
||||
height: 8px;
|
||||
background: #3cff3c;
|
||||
border-radius: 50%;
|
||||
margin-right: 6px;
|
||||
box-shadow: 0 0 6px #3cff3c;
|
||||
}
|
||||
|
||||
/* ── Music Control ──────────────────────────────── */
|
||||
|
||||
|
||||
161
sockets/chat.js
161
sockets/chat.js
@ -1,89 +1,88 @@
|
||||
/* ============================================================
|
||||
sockets/chat.js
|
||||
Alle Socket-Events rund um Chat, Whisper & Online-Status
|
||||
============================================================ */
|
||||
const socket = io();
|
||||
window._socket = socket; // Globale Referenz für arena.js und andere Module
|
||||
const username =
|
||||
typeof window.playerName === "object"
|
||||
? window.playerName.name
|
||||
: window.playerName;
|
||||
|
||||
const db = require("../database/database");
|
||||
socket.emit("register", username);
|
||||
const chatTitle = document.getElementById("chat-title");
|
||||
|
||||
const onlineUsers = {}; // ingameName → socketId
|
||||
let channel = "global";
|
||||
|
||||
function registerChatHandlers(io, socket) {
|
||||
const chatBox = document.getElementById("chat-messages");
|
||||
|
||||
/* ── Registrierung ── */
|
||||
socket.on("register", async (username) => {
|
||||
const [rows] = await db.query(
|
||||
"SELECT ingame_name FROM accounts WHERE username = ?",
|
||||
[username],
|
||||
);
|
||||
let history = {
|
||||
global: [],
|
||||
guild: [],
|
||||
private: [],
|
||||
};
|
||||
|
||||
if (!rows.length) return;
|
||||
function updateChat() {
|
||||
chatBox.value = history[channel].join("\n");
|
||||
|
||||
const ingameName = rows[0].ingame_name;
|
||||
socket.user = ingameName;
|
||||
onlineUsers[ingameName] = socket.id;
|
||||
io.emit("onlineUsers", Object.keys(onlineUsers));
|
||||
});
|
||||
|
||||
/* ── Chat-Nachrichten ── */
|
||||
socket.on("chatMessage", (data) => {
|
||||
if (data.channel === "global") {
|
||||
io.emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "global",
|
||||
});
|
||||
}
|
||||
|
||||
if (data.channel === "guild") {
|
||||
io.to("guild_" + data.guild).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "guild",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* ── Flüstern ── */
|
||||
socket.on("whisper", (data) => {
|
||||
const targetSocket = onlineUsers[data.to];
|
||||
|
||||
if (!targetSocket) {
|
||||
socket.emit("systemMessage", { message: data.to + " ist offline" });
|
||||
return;
|
||||
}
|
||||
|
||||
io.to(targetSocket).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
|
||||
socket.emit("chatMessage", {
|
||||
user: "(an " + data.to + ")",
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
});
|
||||
|
||||
/* ── Private Nachricht ── */
|
||||
socket.on("privateMessage", (data) => {
|
||||
const target = onlineUsers[data.to];
|
||||
if (target) {
|
||||
io.to(target).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* ── Disconnect: aus Online-Liste entfernen ── */
|
||||
socket.on("disconnect", () => {
|
||||
if (socket.user) {
|
||||
delete onlineUsers[socket.user];
|
||||
io.emit("onlineUsers", Object.keys(onlineUsers));
|
||||
}
|
||||
});
|
||||
chatBox.scrollTop = chatBox.scrollHeight;
|
||||
}
|
||||
|
||||
module.exports = { registerChatHandlers };
|
||||
document.querySelectorAll(".chat-channels button").forEach((btn) => {
|
||||
btn.onclick = () => {
|
||||
document
|
||||
.querySelectorAll(".chat-channels button")
|
||||
.forEach((b) => b.classList.remove("active"));
|
||||
|
||||
btn.classList.add("active");
|
||||
|
||||
channel = btn.dataset.channel;
|
||||
|
||||
// Titel ändern
|
||||
if (channel === "global") chatTitle.innerText = "Globalchat";
|
||||
if (channel === "guild") chatTitle.innerText = "Gildenchat";
|
||||
if (channel === "private") chatTitle.innerText = "Privatchat";
|
||||
|
||||
updateChat();
|
||||
};
|
||||
});
|
||||
|
||||
document.getElementById("chat-send").onclick = () => {
|
||||
const text = document.getElementById("chat-text").value;
|
||||
|
||||
if (!text) return;
|
||||
|
||||
if (text.startsWith("/w ")) {
|
||||
const parts = text.split(" ");
|
||||
|
||||
const target = parts[1];
|
||||
|
||||
const message = parts.slice(2).join(" ");
|
||||
|
||||
socket.emit("whisper", {
|
||||
to: target,
|
||||
message: message,
|
||||
});
|
||||
} else {
|
||||
socket.emit("chatMessage", {
|
||||
channel: channel,
|
||||
message: text,
|
||||
});
|
||||
}
|
||||
|
||||
document.getElementById("chat-text").value = "";
|
||||
};
|
||||
|
||||
socket.on("chatMessage", (data) => {
|
||||
const line = `${data.user}: ${data.message}`;
|
||||
|
||||
history[data.channel].push(line);
|
||||
|
||||
if (history[data.channel].length > 50) history[data.channel].shift();
|
||||
|
||||
if (data.channel === channel) updateChat();
|
||||
});
|
||||
|
||||
socket.on("systemMessage", (data) => {
|
||||
const line = "[System] " + data.message;
|
||||
|
||||
history[channel].push(line);
|
||||
|
||||
updateChat();
|
||||
});
|
||||
|
||||
87
sockets/sockets_chat.js
Normal file
87
sockets/sockets_chat.js
Normal file
@ -0,0 +1,87 @@
|
||||
/* ============================================================
|
||||
sockets/chat.js
|
||||
Alle Socket-Events rund um Chat, Whisper & Online-Status
|
||||
============================================================ */
|
||||
|
||||
const db = require("../database/database");
|
||||
|
||||
const onlineUsers = {}; // ingameName → socketId (wird für Whisper benötigt)
|
||||
|
||||
function registerChatHandlers(io, socket) {
|
||||
|
||||
/* ── Registrierung ── */
|
||||
socket.on("register", async (username) => {
|
||||
const [rows] = await db.query(
|
||||
"SELECT ingame_name FROM accounts WHERE username = ?",
|
||||
[username],
|
||||
);
|
||||
|
||||
if (!rows.length) return;
|
||||
|
||||
const ingameName = rows[0].ingame_name;
|
||||
socket.user = ingameName;
|
||||
onlineUsers[ingameName] = socket.id;
|
||||
});
|
||||
|
||||
/* ── Chat-Nachrichten ── */
|
||||
socket.on("chatMessage", (data) => {
|
||||
if (data.channel === "global") {
|
||||
io.emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "global",
|
||||
});
|
||||
}
|
||||
|
||||
if (data.channel === "guild") {
|
||||
io.to("guild_" + data.guild).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "guild",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* ── Flüstern ── */
|
||||
socket.on("whisper", (data) => {
|
||||
const targetSocket = onlineUsers[data.to];
|
||||
|
||||
if (!targetSocket) {
|
||||
socket.emit("systemMessage", { message: data.to + " ist offline" });
|
||||
return;
|
||||
}
|
||||
|
||||
io.to(targetSocket).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
|
||||
socket.emit("chatMessage", {
|
||||
user: "(an " + data.to + ")",
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
});
|
||||
|
||||
/* ── Private Nachricht ── */
|
||||
socket.on("privateMessage", (data) => {
|
||||
const target = onlineUsers[data.to];
|
||||
if (target) {
|
||||
io.to(target).emit("chatMessage", {
|
||||
user: socket.user,
|
||||
message: data.message,
|
||||
channel: "private",
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
/* ── Disconnect ── */
|
||||
socket.on("disconnect", () => {
|
||||
if (socket.user) {
|
||||
delete onlineUsers[socket.user];
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
module.exports = { registerChatHandlers };
|
||||
@ -605,12 +605,6 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="online-users">
|
||||
<div class="online-header">Spieler Online</div>
|
||||
|
||||
<div id="online-list"></div>
|
||||
</div>
|
||||
|
||||
<div id="game-chat">
|
||||
<div class="chat-header" id="chat-title">Globalchat</div>
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user