/
helpers.js
237 lines (218 loc) · 8.18 KB
/
helpers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import { socket } from "./init.js";
import { MODULE_NAME } from "./constants.js";
import { ModuleSettings } from "./settings.js";
export async function getActorDataFromDragEvent(event) {
try {
const data = TextEditor.getDragEventData(event);
switch (data.type) {
case "Actor":
const actor = await Actor.implementation.fromDropData(data);
if (actor) {
const data = {
name: actor.name || "",
img: actor.img || "",
};
return [data];
} else {
ui.notifications.error(game.i18n.localize("CHUD.errors.invalidActor"));
return null;
}
case "JournalEntry":
const entry = await JournalEntry.implementation.fromDropData(data);
const pages = entry.getEmbeddedCollection("JournalEntryPage").contents;
const conversationParticipants = [];
pages.forEach((page) => {
let participant;
switch (page.type) {
case "text":
if (page.flags["conversation-hud"]) {
// Handle text pages with the the CHUD flag
const pageType = page.flags["conversation-hud"].type;
if (pageType && pageType === "conversation") {
const participants = JSON.parse(page.text.content);
conversationParticipants.push(...participants);
}
} else if (page.flags["monks-enhanced-journal"]) {
// Handle text pages with the the MEJ flag
const pageType = page.flags["monks-enhanced-journal"].type;
if (pageType) {
switch (pageType) {
case "person":
case "picture":
case "organization":
case "loot":
case "place":
case "poi":
participant = {
name: page.name || "",
img: page.src || "",
};
conversationParticipants.push(participant);
default:
break;
}
}
} else {
// Fallback for older saved conversations that have no flag attached
// This is a hacky workaround
if (page.name === "Conversation Participants") {
// Add the new flag to the old conversation page
if (!page.flags["conversation-hud"]) {
page.flags["conversation-hud"] = { type: "conversation" };
}
const participants = JSON.parse(page.text.content);
conversationParticipants.push(...participants);
}
}
break;
case "image":
participant = {
name: page.image.caption || "",
img: page.src || "",
};
conversationParticipants.push(participant);
break;
case "person":
case "picture":
case "organization":
case "loot":
case "place":
case "poi":
participant = {
name: page.name || "",
img: page.src || "",
};
conversationParticipants.push(participant);
break;
default:
break;
}
});
if (conversationParticipants.length > 0) {
return conversationParticipants;
} else {
ui.notifications.warn(game.i18n.localize("CHUD.warnings.noParticipantDataFound"));
return null;
}
default:
ui.notifications.error(game.i18n.localize("CHUD.errors.typeNotSupported"));
return null;
}
} catch (e) {
ui.notifications.error(game.i18n.localize("CHUD.errors.generic"));
console.error(e);
return null;
}
}
export async function checkConversationDataAvailability(users) {
// let userId = users[0].id;
for (let i = 0; i < users.length; i++) {
const result = await socket.executeAsUser("getActiveConversation", users[i].id);
// Check to see if we have a result and it is an active conversation
if (result) {
if (result.conversationIsActive) {
// We found an active conversation
const userId = users[i].id;
return { result, userId };
}
}
}
}
export async function updateConversationControls() {
// Update the controls
const uiInterface = document.getElementById("interface");
const controls = document.getElementById("ui-conversation-controls");
if (controls) {
// Remove the old controls
uiInterface.removeChild(controls);
}
const conversationControls = await renderTemplate("modules/conversation-hud/templates/conversation_controls.hbs", {
isGM: game.user.isGM,
isMinimized: game.ConversationHud.conversationIsMinimized,
isVisible: game.ConversationHud.conversationIsVisible,
isSpeakingAs: game.ConversationHud.conversationIsSpeakingAs,
features: {
minimizeEnabled: game.settings.get(MODULE_NAME, ModuleSettings.enableMinimize),
speakAsEnabled: game.settings.get(MODULE_NAME, ModuleSettings.enableSpeakAs),
},
});
const updatedControls = document.createElement("section");
updatedControls.id = "ui-conversation-controls";
updatedControls.setAttribute("data-tooltip-direction", "LEFT");
updatedControls.innerHTML = conversationControls;
const uiRight = document.getElementById("ui-right");
uiRight.before(updatedControls);
}
export async function updateConversationLayout() {
// Update the layout
const conversationHud = document.getElementById("ui-conversation-hud");
if (game.ConversationHud.conversationIsMinimized) {
conversationHud.classList.add("minimized");
} else {
conversationHud.classList.remove("minimized");
}
if (game.ConversationHud.conversationIsVisible) {
const conversationBackground = document.getElementById("conversation-background");
if (game.ConversationHud.conversationIsMinimized) {
conversationBackground.classList.remove("visible");
} else {
conversationBackground.classList.add("visible");
}
}
}
export function handleOnClickContentLink(event, wrapped) {
event.preventDefault();
const currentTarget = event.currentTarget;
let document = null;
if (currentTarget.dataset.pack) {
return wrapped.bind(this)(event);
}
if (currentTarget.dataset.type !== "JournalEntry") {
return wrapped.bind(this)(event);
}
const collection = game.collections.get(currentTarget.dataset.type);
document = collection.get(currentTarget.dataset.id);
if (document?.flags?.core?.sheetClass === "conversation-entry-sheet.ConversationEntrySheet") {
if (!document.testUserPermission(game.user, "LIMITED")) {
ui.notifications.warn(game.i18n.localize("CHUD.errors.activateNoPerms"));
} else {
if (event.ctrlKey) {
if (game.ConversationHud) {
const pages = document.getEmbeddedCollection("JournalEntryPage").contents;
if (pages.length > 0) {
try {
const conversationData = JSON.parse(pages[0].text.content);
game.ConversationHud.startConversationFromData(conversationData);
} catch (error) {
if (error instanceof SyntaxError) {
ui.notifications.error(game.i18n.localize("CHUD.errors.failedToParse"));
} else {
ui.notifications.error(game.i18n.localize("CHUD.errors.genericSheetError"));
}
}
} else {
ui.notifications.error(game.i18n.localize("CHUD.errors.activateNoPages"));
}
} else {
ui.notifications.error(game.i18n.localize("CHUD.errors.activateNoInit"));
}
return;
}
}
}
return wrapped.bind(this)(event);
}
export function checkIfUserGM() {
if (!game.user.isGM) {
ui.notifications.error(game.i18n.localize("CHUD.errors.insufficientRights"));
return false;
}
return true;
}
export function checkIfConversationActive() {
if (!game.ConversationHud.conversationIsActive) {
ui.notifications.error(game.i18n.localize("CHUD.errors.noActiveConversation"));
return false;
}
return true;
}