BlueGit

timfa questward

Files

questward / client / src / play / social.js

social.js

social.js 6.7 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 getFriends ()
160 {
161 return social.friends ?? [];
162 },
163 inspect (entity)
164 {
165 if (!entity?.characterId)
166 { return; }
167 inspected = {
168 characterId: entity.characterId,
169 firstName: entity.firstName ?? "",
170 lastName: entity.lastName ?? ""
171 };
172 open = true;
173 inspectError.textContent = "";
174 inspect.hidden = false;
175 inspectBackdrop.hidden = false;
176 renderInspect();
177 },
178 hideInspect,
179 reset ()
180 {
181 hideInspect();
182 friendsPanel.hidden = true;
183 nametag.hidden = true;
184 },
185 updateNametag (view, scene, localEntityId)
186 {
187 if (!view || !scene?.canvas)
188 {
189 nametag.hidden = true;
190 return;
191 }
192 const canvas = scene.canvas;
193 const rect = canvas.getBoundingClientRect();
194 const mouseX = GameInput.mousePosition.x - rect.left;
195 const mouseY = GameInput.mousePosition.y - rect.top;
196 const entity = view.entityAtScreen(mouseX, mouseY);
197 if (!entity || entity.entityId === localEntityId || !entity.firstName)
198 {
199 nametag.hidden = true;
200 canvas.style.cursor = "";
201 return;
202 }
203 const anchor = view.nametagAnchor(entity);
204 if (!anchor)
205 {
206 nametag.hidden = true;
207 return;
208 }
209 nametag.hidden = false;
210 nametag.textContent = entity.firstName;
211 nametag.style.left = `${rect.left + anchor.x}px`;
212 nametag.style.top = `${rect.top + anchor.y - 6}px`;
213 canvas.style.cursor = "pointer";
214 },
215 showError (message)
216 {
217 if (open)
218 { inspectError.textContent = message; }
219 }
220 };
221}
222
223function namedRow (entry)
224{
225 const row = document.createElement("div");
226 row.className = "social-row";
227 const name = document.createElement("span");
228 name.textContent = `${entry.firstName} ${entry.lastName}`;
229 row.append(name);
230 return row;
231}
232
233function fillList (root, entries, emptyText, renderRow)
234{
235 root.replaceChildren();
236 if (!entries.length)
237 {
238 const empty = document.createElement("p");
239 empty.className = "hint";
240 empty.textContent = emptyText;
241 root.append(empty);
242 return;
243 }
244 for (const entry of entries)
245 { root.append(renderRow(entry)); }
246}
247
248function actionButton (label, onClick)
249{
250 const button = document.createElement("button");
251 button.type = "button";
252 button.textContent = label;
253 button.addEventListener("click", onClick);
254 return button;
255}