import { mountLayerPreview } from "./characterPreview.js"; const FRIEND_NAME_COLOR = "#e8c84a"; export function initSocial ({ socket, MessageType, resolveSpriteUrl = null, resolveImage = null }) { const nametag = document.getElementById("nametag"); const inspect = document.getElementById("inspect-modal"); const inspectBackdrop = document.getElementById("inspect-backdrop"); const inspectName = document.getElementById("inspect-name"); const inspectBounty = document.getElementById("inspect-bounty"); 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; /** @type {number|null} expanded friend account in the panel */ let expandedAccountId = null; let localAccountId = null; let localCharacterId = null; document.getElementById("inspect-close").addEventListener("click", () => hideInspect()); inspectBackdrop.addEventListener("click", () => hideInspect()); document.getElementById("friends-toggle").addEventListener("click", () => { friendsPanel.hidden = !friendsPanel.hidden; }); const friendsToggle = document.getElementById("friends-toggle"); function syncFriendsAlert () { const hasIncoming = (social.incoming ?? []).length > 0; friendsToggle?.classList.toggle("has-incoming", hasIncoming); if (friendsToggle) { friendsToggle.title = hasIncoming ? "Friends — pending requests" : "Friends"; } } function hideInspect () { open = false; inspected = null; inspect.hidden = true; inspectBackdrop.hidden = true; inspectError.textContent = ""; if (inspectBounty) { inspectBounty.hidden = true; inspectBounty.textContent = ""; } } function friendAccountOfCharacter (characterId) { const id = characterId | 0; for (const account of social.friends ?? []) { if ((account.characters ?? []).some((entry) => entry.characterId === id)) { return account; } } return null; } function isFriendCharacter (characterId, accountId = null) { const id = characterId | 0; if (!id) { return false; } if (localAccountId && accountId && (accountId | 0) === (localAccountId | 0) && id !== (localCharacterId | 0)) { return true; } return !!friendAccountOfCharacter(id); } function relation (characterId) { if (isFriendCharacter(characterId)) { return "friends"; } const account = accountHoldingCharacter(characterId, social.incoming); if (account) { return "incoming"; } if (accountHoldingCharacter(characterId, social.outgoing)) { return "outgoing"; } return "none"; } function accountHoldingCharacter (characterId, accounts) { const id = characterId | 0; return (accounts ?? []).find((account) => (account.characters ?? []).some((entry) => entry.characterId === id)) ?? null; } function renderInspect () { if (!inspected) { return; } inspectName.textContent = `${inspected.firstName} ${inspected.lastName}`; inspectName.style.color = isFriendCharacter(inspected.characterId, inspected.accountId) ? FRIEND_NAME_COLOR : ""; inspectAction.replaceChildren(); const status = relation(inspected.characterId); const friendAccount = friendAccountOfCharacter(inspected.characterId); if (status === "friends") { inspectAction.append(actionButton("Remove friend", () => { socket.send({ type: MessageType.friendRemove, accountId: friendAccount?.accountId, characterId: inspected.characterId }); })); } else if (status === "incoming") { const incoming = accountHoldingCharacter(inspected.characterId, social.incoming); inspectAction.append( actionButton("Accept friend request", () => { socket.send({ type: MessageType.friendAccept, accountId: incoming?.accountId, characterId: inspected.characterId }); }), actionButton("Decline", () => { socket.send({ type: MessageType.friendDecline, accountId: incoming?.accountId, characterId: inspected.characterId }); }) ); } else if (status === "outgoing") { const outgoing = accountHoldingCharacter(inspected.characterId, social.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, accountId: outgoing?.accountId, 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.", (account) => accountRow(account, { expanded: expandedAccountId === account.accountId, onToggle () { expandedAccountId = expandedAccountId === account.accountId ? null : account.accountId; renderLists(); }, onRemove () { socket.send({ type: MessageType.friendRemove, accountId: account.accountId }); } })); fillList(incomingList, social.incoming, "No incoming requests.", (account) => accountRequestRow(account, { onAccept () { socket.send({ type: MessageType.friendAccept, accountId: account.accountId }); }, onDecline () { socket.send({ type: MessageType.friendDecline, accountId: account.accountId }); } })); fillList(outgoingList, social.outgoing, "No outgoing requests.", (account) => accountRequestRow(account, { onDecline () { socket.send({ type: MessageType.friendDecline, accountId: account.accountId }); }, declineLabel: "Cancel" })); } function accountRow (account, { expanded, onToggle, onRemove }) { const wrap = document.createElement("div"); wrap.className = "friend-account"; const header = document.createElement("div"); header.className = "social-row friend-account-header"; const status = document.createElement("span"); status.className = `friend-status-dot ${account.online ? "online" : "offline"}`; status.title = account.online ? "Online" : "Offline"; status.setAttribute("aria-label", account.online ? "Online" : "Offline"); const nameCol = document.createElement("div"); nameCol.className = "friend-account-copy"; const nameBtn = document.createElement("button"); nameBtn.type = "button"; nameBtn.className = "friend-account-name"; nameBtn.textContent = account.username || `Account ${account.accountId}`; nameBtn.addEventListener("click", onToggle); nameCol.append(nameBtn); if (account.online && account.playingAs) { const playing = document.createElement("span"); playing.className = "friend-playing-as"; const who = `${account.playingAs.firstName ?? ""} ${account.playingAs.lastName ?? ""}`.trim(); playing.textContent = who ? `Playing as ${who}` : "Online"; nameCol.append(playing); } header.append(status, nameCol, actionButton("Remove", onRemove)); wrap.append(header); if (expanded) { const list = document.createElement("div"); list.className = "friend-character-list"; for (const character of account.characters ?? []) { list.append(characterPreviewRow(character)); } if (!(account.characters ?? []).length) { const empty = document.createElement("p"); empty.className = "hint"; empty.textContent = "No characters."; list.append(empty); } wrap.append(list); } return wrap; } function accountRequestRow (account, { onAccept = null, onDecline, declineLabel = "Decline" }) { const row = document.createElement("div"); row.className = "social-row"; const name = document.createElement("span"); name.textContent = account.username || `Account ${account.accountId}`; row.append(name); if (onAccept) { row.append(actionButton("Accept", onAccept)); } row.append(actionButton(declineLabel, onDecline)); return row; } function characterPreviewRow (entry) { const row = document.createElement("div"); row.className = "friend-character-row"; row.append(characterPreview(entry.layers), (() => { const name = document.createElement("span"); name.textContent = `${entry.firstName} ${entry.lastName}`.trim(); return name; })()); return row; } function characterPreview (layers) { return mountLayerPreview(layers, { resolveImage, resolveSpriteUrl, size: 48, className: "char-preview friend-char-preview" }); } 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 ?? [] }; if ( expandedAccountId != null && !(social.friends ?? []).some((account) => account.accountId === expandedAccountId) ) { expandedAccountId = null; } renderLists(); syncFriendsAlert(); if (open) { renderInspect(); } }, /** Flat character list for pigeon send targets. */ getFriends () { const out = []; for (const account of social.friends ?? []) { for (const character of account.characters ?? []) { out.push(character); } } return out; }, isFriendCharacter, setLocalIdentity ({ accountId = null, characterId = null } = {}) { localAccountId = accountId | 0 || null; localCharacterId = characterId | 0 || null; }, inspect (entity) { if (!entity?.characterId) { return; } inspected = { characterId: entity.characterId, accountId: entity.accountId ?? null, firstName: entity.firstName ?? "", lastName: entity.lastName ?? "" }; open = true; inspectError.textContent = ""; if (inspectBounty) { inspectBounty.hidden = true; inspectBounty.textContent = ""; } inspect.hidden = false; inspectBackdrop.hidden = false; renderInspect(); socket.send({ type: MessageType.getCharacterBounty, characterId: entity.characterId }); }, handleCharacterBounty (message) { if (!open || !inspected || !inspectBounty) { return; } const target = message.target; if (!target || target.characterId !== inspected.characterId) { return; } const amount = target.amountCopper | 0; if (amount <= 0) { inspectBounty.hidden = true; inspectBounty.textContent = ""; return; } inspectBounty.hidden = false; inspectBounty.textContent = `Bounty: ${target.amountLabel ?? amount}`; }, hideInspect, closeOverlays () { hideInspect(); friendsPanel.hidden = true; }, reset () { hideInspect(); friendsPanel.hidden = true; nametag.hidden = true; social = { friends: [], incoming: [], outgoing: [] }; syncFriendsAlert(); }, 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; const friend = entity.characterId && isFriendCharacter(entity.characterId, entity.accountId); nametag.classList.toggle("friend-name", !!friend); nametag.style.color = friend ? FRIEND_NAME_COLOR : ""; 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 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; }