| 1 | export function initSocial ({ socket, MessageType }) |
| 2 | { |
| 3 | const nametag = document.getElementById("nametag"); |
| 4 | const inspect = document.getElementById("inspect-modal"); |
| 5 | const inspectBackdrop = document.getElementById("inspect-backdrop"); |
| 6 | const inspectName = document.getElementById("inspect-name"); |
| 7 | const inspectBounty = document.getElementById("inspect-bounty"); |
| 8 | const inspectAction = document.getElementById("inspect-action"); |
| 9 | const inspectError = document.getElementById("inspect-error"); |
| 10 | const friendsPanel = document.getElementById("friends-panel"); |
| 11 | const friendsList = document.getElementById("friends-list"); |
| 12 | const incomingList = document.getElementById("incoming-list"); |
| 13 | const outgoingList = document.getElementById("outgoing-list"); |
| 14 | let social = { friends: [], incoming: [], outgoing: [] }; |
| 15 | let inspected = null; |
| 16 | let open = false; |
| 17 | |
| 18 | document.getElementById("inspect-close").addEventListener("click", () => hideInspect()); |
| 19 | inspectBackdrop.addEventListener("click", () => hideInspect()); |
| 20 | document.getElementById("friends-toggle").addEventListener("click", () => |
| 21 | { |
| 22 | friendsPanel.hidden = !friendsPanel.hidden; |
| 23 | }); |
| 24 | |
| 25 | function hideInspect () |
| 26 | { |
| 27 | open = false; |
| 28 | inspected = null; |
| 29 | inspect.hidden = true; |
| 30 | inspectBackdrop.hidden = true; |
| 31 | inspectError.textContent = ""; |
| 32 | if (inspectBounty) |
| 33 | { |
| 34 | inspectBounty.hidden = true; |
| 35 | inspectBounty.textContent = ""; |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | function relation (characterId) |
| 40 | { |
| 41 | if (social.friends.some((entry) => entry.characterId === characterId)) |
| 42 | { return "friends"; } |
| 43 | if (social.incoming.some((entry) => entry.characterId === characterId)) |
| 44 | { return "incoming"; } |
| 45 | if (social.outgoing.some((entry) => entry.characterId === characterId)) |
| 46 | { return "outgoing"; } |
| 47 | return "none"; |
| 48 | } |
| 49 | |
| 50 | function renderInspect () |
| 51 | { |
| 52 | if (!inspected) |
| 53 | { return; } |
| 54 | inspectName.textContent = `${inspected.firstName} ${inspected.lastName}`; |
| 55 | inspectAction.replaceChildren(); |
| 56 | const status = relation(inspected.characterId); |
| 57 | if (status === "friends") |
| 58 | { |
| 59 | inspectAction.append(actionButton("Remove friend", () => |
| 60 | { |
| 61 | socket.send({ type: MessageType.friendRemove, characterId: inspected.characterId }); |
| 62 | })); |
| 63 | } |
| 64 | else if (status === "incoming") |
| 65 | { |
| 66 | inspectAction.append( |
| 67 | actionButton("Accept friend request", () => |
| 68 | { |
| 69 | socket.send({ type: MessageType.friendAccept, characterId: inspected.characterId }); |
| 70 | }), |
| 71 | actionButton("Decline", () => |
| 72 | { |
| 73 | socket.send({ type: MessageType.friendDecline, characterId: inspected.characterId }); |
| 74 | }) |
| 75 | ); |
| 76 | } |
| 77 | else if (status === "outgoing") |
| 78 | { |
| 79 | const pending = document.createElement("p"); |
| 80 | pending.className = "hint"; |
| 81 | pending.textContent = "Friend request sent."; |
| 82 | inspectAction.append( |
| 83 | pending, |
| 84 | actionButton("Cancel request", () => |
| 85 | { |
| 86 | socket.send({ type: MessageType.friendDecline, characterId: inspected.characterId }); |
| 87 | }) |
| 88 | ); |
| 89 | } |
| 90 | else |
| 91 | { |
| 92 | inspectAction.append(actionButton("Send friend request", () => |
| 93 | { |
| 94 | socket.send({ type: MessageType.friendRequest, characterId: inspected.characterId }); |
| 95 | })); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | function renderLists () |
| 100 | { |
| 101 | fillList(friendsList, social.friends, "No friends yet.", (entry) => |
| 102 | { |
| 103 | const row = namedRow(entry); |
| 104 | row.append(actionButton("Remove", () => |
| 105 | { |
| 106 | socket.send({ type: MessageType.friendRemove, characterId: entry.characterId }); |
| 107 | })); |
| 108 | return row; |
| 109 | }); |
| 110 | fillList(incomingList, social.incoming, "No incoming requests.", (entry) => |
| 111 | { |
| 112 | const row = namedRow(entry); |
| 113 | row.append( |
| 114 | actionButton("Accept", () => |
| 115 | { |
| 116 | socket.send({ type: MessageType.friendAccept, characterId: entry.characterId }); |
| 117 | }), |
| 118 | actionButton("Decline", () => |
| 119 | { |
| 120 | socket.send({ type: MessageType.friendDecline, characterId: entry.characterId }); |
| 121 | }) |
| 122 | ); |
| 123 | return row; |
| 124 | }); |
| 125 | fillList(outgoingList, social.outgoing, "No outgoing requests.", (entry) => |
| 126 | { |
| 127 | const row = namedRow(entry); |
| 128 | row.append(actionButton("Cancel", () => |
| 129 | { |
| 130 | socket.send({ type: MessageType.friendDecline, characterId: entry.characterId }); |
| 131 | })); |
| 132 | return row; |
| 133 | }); |
| 134 | } |
| 135 | |
| 136 | return { |
| 137 | isBlocking () |
| 138 | { |
| 139 | if (open) |
| 140 | { return true; } |
| 141 | const x = GameInput.mousePosition.x; |
| 142 | const y = GameInput.mousePosition.y; |
| 143 | for (const element of [document.getElementById("hud"), friendsPanel, inspect, inspectBackdrop]) |
| 144 | { |
| 145 | if (!element || element.hidden) |
| 146 | { continue; } |
| 147 | const box = element.getBoundingClientRect(); |
| 148 | if (x >= box.left && x < box.right && y >= box.top && y < box.bottom) |
| 149 | { return true; } |
| 150 | } |
| 151 | return false; |
| 152 | }, |
| 153 | inspectOpen: () => open, |
| 154 | handleFriends (message) |
| 155 | { |
| 156 | social = { |
| 157 | friends: message.friends ?? [], |
| 158 | incoming: message.incoming ?? [], |
| 159 | outgoing: message.outgoing ?? [] |
| 160 | }; |
| 161 | renderLists(); |
| 162 | if (open) |
| 163 | { renderInspect(); } |
| 164 | }, |
| 165 | getFriends () |
| 166 | { |
| 167 | return social.friends ?? []; |
| 168 | }, |
| 169 | inspect (entity) |
| 170 | { |
| 171 | if (!entity?.characterId) |
| 172 | { return; } |
| 173 | inspected = { |
| 174 | characterId: entity.characterId, |
| 175 | firstName: entity.firstName ?? "", |
| 176 | lastName: entity.lastName ?? "" |
| 177 | }; |
| 178 | open = true; |
| 179 | inspectError.textContent = ""; |
| 180 | if (inspectBounty) |
| 181 | { |
| 182 | inspectBounty.hidden = true; |
| 183 | inspectBounty.textContent = ""; |
| 184 | } |
| 185 | inspect.hidden = false; |
| 186 | inspectBackdrop.hidden = false; |
| 187 | renderInspect(); |
| 188 | socket.send({ |
| 189 | type: MessageType.getCharacterBounty, |
| 190 | characterId: entity.characterId |
| 191 | }); |
| 192 | }, |
| 193 | handleCharacterBounty (message) |
| 194 | { |
| 195 | if (!open || !inspected || !inspectBounty) |
| 196 | { return; } |
| 197 | const target = message.target; |
| 198 | if (!target || target.characterId !== inspected.characterId) |
| 199 | { return; } |
| 200 | const amount = target.amountCopper | 0; |
| 201 | if (amount <= 0) |
| 202 | { |
| 203 | inspectBounty.hidden = true; |
| 204 | inspectBounty.textContent = ""; |
| 205 | return; |
| 206 | } |
| 207 | inspectBounty.hidden = false; |
| 208 | inspectBounty.textContent = `Bounty: ${target.amountLabel ?? amount}`; |
| 209 | }, |
| 210 | hideInspect, |
| 211 | reset () |
| 212 | { |
| 213 | hideInspect(); |
| 214 | friendsPanel.hidden = true; |
| 215 | nametag.hidden = true; |
| 216 | }, |
| 217 | updateNametag (view, scene, localEntityId) |
| 218 | { |
| 219 | if (!view || !scene?.canvas) |
| 220 | { |
| 221 | nametag.hidden = true; |
| 222 | return; |
| 223 | } |
| 224 | const canvas = scene.canvas; |
| 225 | const rect = canvas.getBoundingClientRect(); |
| 226 | const mouseX = GameInput.mousePosition.x - rect.left; |
| 227 | const mouseY = GameInput.mousePosition.y - rect.top; |
| 228 | const entity = view.entityAtScreen(mouseX, mouseY); |
| 229 | if (!entity || entity.entityId === localEntityId || !entity.firstName) |
| 230 | { |
| 231 | nametag.hidden = true; |
| 232 | canvas.style.cursor = ""; |
| 233 | return; |
| 234 | } |
| 235 | const anchor = view.nametagAnchor(entity); |
| 236 | if (!anchor) |
| 237 | { |
| 238 | nametag.hidden = true; |
| 239 | return; |
| 240 | } |
| 241 | nametag.hidden = false; |
| 242 | nametag.textContent = entity.firstName; |
| 243 | nametag.style.left = `${rect.left + anchor.x}px`; |
| 244 | nametag.style.top = `${rect.top + anchor.y - 6}px`; |
| 245 | canvas.style.cursor = "pointer"; |
| 246 | }, |
| 247 | showError (message) |
| 248 | { |
| 249 | if (open) |
| 250 | { inspectError.textContent = message; } |
| 251 | } |
| 252 | }; |
| 253 | } |
| 254 | |
| 255 | function namedRow (entry) |
| 256 | { |
| 257 | const row = document.createElement("div"); |
| 258 | row.className = "social-row"; |
| 259 | const name = document.createElement("span"); |
| 260 | name.textContent = `${entry.firstName} ${entry.lastName}`; |
| 261 | row.append(name); |
| 262 | return row; |
| 263 | } |
| 264 | |
| 265 | function fillList (root, entries, emptyText, renderRow) |
| 266 | { |
| 267 | root.replaceChildren(); |
| 268 | if (!entries.length) |
| 269 | { |
| 270 | const empty = document.createElement("p"); |
| 271 | empty.className = "hint"; |
| 272 | empty.textContent = emptyText; |
| 273 | root.append(empty); |
| 274 | return; |
| 275 | } |
| 276 | for (const entry of entries) |
| 277 | { root.append(renderRow(entry)); } |
| 278 | } |
| 279 | |
| 280 | function actionButton (label, onClick) |
| 281 | { |
| 282 | const button = document.createElement("button"); |
| 283 | button.type = "button"; |
| 284 | button.textContent = label; |
| 285 | button.addEventListener("click", onClick); |
| 286 | return button; |
| 287 | } |