BlueGit

timfa questward

Files

questward / client / src / play / social.js

social.js

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