export function initSocial ({ socket, MessageType }) { const nametag = document.getElementById("nametag"); const inspect = document.getElementById("inspect-modal"); const inspectBackdrop = document.getElementById("inspect-backdrop"); const inspectName = document.getElementById("inspect-name"); const inspectAction = document.getElementById("inspect-action"); const inspectError = document.getElementById("inspect-error"); const friendsPanel = document.getElementById("friends-panel"); const friendsList = document.getElementById("friends-list"); const incomingList = document.getElementById("incoming-list"); const outgoingList = document.getElementById("outgoing-list"); let social = { friends: [], incoming: [], outgoing: [] }; let inspected = null; let open = false; document.getElementById("inspect-close").addEventListener("click", () => hideInspect()); inspectBackdrop.addEventListener("click", () => hideInspect()); document.getElementById("friends-toggle").addEventListener("click", () => { friendsPanel.hidden = !friendsPanel.hidden; }); function hideInspect () { open = false; inspected = null; inspect.hidden = true; inspectBackdrop.hidden = true; inspectError.textContent = ""; } function relation (characterId) { if (social.friends.some((entry) => entry.characterId === characterId)) { return "friends"; } if (social.incoming.some((entry) => entry.characterId === characterId)) { return "incoming"; } if (social.outgoing.some((entry) => entry.characterId === characterId)) { return "outgoing"; } return "none"; } function renderInspect () { if (!inspected) { return; } inspectName.textContent = `${inspected.firstName} ${inspected.lastName}`; inspectAction.replaceChildren(); const status = relation(inspected.characterId); if (status === "friends") { inspectAction.append(actionButton("Remove friend", () => { socket.send({ type: MessageType.friendRemove, characterId: inspected.characterId }); })); } else if (status === "incoming") { inspectAction.append( actionButton("Accept friend request", () => { socket.send({ type: MessageType.friendAccept, characterId: inspected.characterId }); }), actionButton("Decline", () => { socket.send({ type: MessageType.friendDecline, characterId: inspected.characterId }); }) ); } else if (status === "outgoing") { const pending = document.createElement("p"); pending.className = "hint"; pending.textContent = "Friend request sent."; inspectAction.append( pending, actionButton("Cancel request", () => { socket.send({ type: MessageType.friendDecline, characterId: inspected.characterId }); }) ); } else { inspectAction.append(actionButton("Send friend request", () => { socket.send({ type: MessageType.friendRequest, characterId: inspected.characterId }); })); } } function renderLists () { fillList(friendsList, social.friends, "No friends yet.", (entry) => { const row = namedRow(entry); row.append(actionButton("Remove", () => { socket.send({ type: MessageType.friendRemove, characterId: entry.characterId }); })); return row; }); fillList(incomingList, social.incoming, "No incoming requests.", (entry) => { const row = namedRow(entry); row.append( actionButton("Accept", () => { socket.send({ type: MessageType.friendAccept, characterId: entry.characterId }); }), actionButton("Decline", () => { socket.send({ type: MessageType.friendDecline, characterId: entry.characterId }); }) ); return row; }); fillList(outgoingList, social.outgoing, "No outgoing requests.", (entry) => { const row = namedRow(entry); row.append(actionButton("Cancel", () => { socket.send({ type: MessageType.friendDecline, characterId: entry.characterId }); })); return row; }); } return { isBlocking () { if (open) { return true; } const x = GameInput.mousePosition.x; const y = GameInput.mousePosition.y; for (const element of [document.getElementById("hud"), friendsPanel, inspect, inspectBackdrop]) { if (!element || element.hidden) { continue; } const box = element.getBoundingClientRect(); if (x >= box.left && x < box.right && y >= box.top && y < box.bottom) { return true; } } return false; }, inspectOpen: () => open, handleFriends (message) { social = { friends: message.friends ?? [], incoming: message.incoming ?? [], outgoing: message.outgoing ?? [] }; renderLists(); if (open) { renderInspect(); } }, inspect (entity) { if (!entity?.characterId) { return; } inspected = { characterId: entity.characterId, firstName: entity.firstName ?? "", lastName: entity.lastName ?? "" }; open = true; inspectError.textContent = ""; inspect.hidden = false; inspectBackdrop.hidden = false; renderInspect(); }, hideInspect, reset () { hideInspect(); friendsPanel.hidden = true; nametag.hidden = true; }, updateNametag (view, scene, localEntityId) { if (!view || !scene?.canvas) { nametag.hidden = true; return; } const canvas = scene.canvas; const rect = canvas.getBoundingClientRect(); const mouseX = GameInput.mousePosition.x - rect.left; const mouseY = GameInput.mousePosition.y - rect.top; const entity = view.entityAtScreen(mouseX, mouseY); if (!entity || entity.entityId === localEntityId || !entity.firstName) { nametag.hidden = true; canvas.style.cursor = ""; return; } const anchor = view.nametagAnchor(entity); if (!anchor) { nametag.hidden = true; return; } nametag.hidden = false; nametag.textContent = entity.firstName; nametag.style.left = `${rect.left + anchor.x}px`; nametag.style.top = `${rect.top + anchor.y - 6}px`; canvas.style.cursor = "pointer"; }, showError (message) { if (open) { inspectError.textContent = message; } } }; } function namedRow (entry) { const row = document.createElement("div"); row.className = "social-row"; const name = document.createElement("span"); name.textContent = `${entry.firstName} ${entry.lastName}`; row.append(name); return row; } function fillList (root, entries, emptyText, renderRow) { root.replaceChildren(); if (!entries.length) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = emptyText; root.append(empty); return; } for (const entry of entries) { root.append(renderRow(entry)); } } function actionButton (label, onClick) { const button = document.createElement("button"); button.type = "button"; button.textContent = label; button.addEventListener("click", onClick); return button; }