-
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdark-whispers.js
154 lines (140 loc) · 6.91 KB
/
dark-whispers.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
formDarkWhispers()
async function formDarkWhispers () {
// Non-GMs are not permitted to send Dark Whispers
if (!game.user.isGM) {
return ui.notifications.error(game.i18n.localize("GMTOOLKIT.Message.DarkWhispers.NoPermission"))
}
// Setup: determine group of actors to be whispered to
const group = game.gmtoolkit.utility.getGroup(game.settings.get("wfrp4e-gm-toolkit", "defaultGroupDarkWhispers")).filter(g => g.type === "character")
const targeted = game.gmtoolkit.utility.getGroup(game.settings.get("wfrp4e-gm-toolkit", "defaultGroupDarkWhispers"), { interaction: "targeted" }).filter(g => g.type === "character")
// Setup: exit with notice if there are no player-assigned characters
if (!group) {
return ui.notifications.error(game.i18n.localize("GMTOOLKIT.Message.DarkWhispers.NoEligibleCharacters"))
}
// Setup: exit with notice if there are no player-assigned characters with Corruption
if (!group.some(g => g.system?.status?.corruption?.value > 0)) {
return ui.notifications.error(game.i18n.localize("GMTOOLKIT.Message.DarkWhispers.NoEligibleCharacters"))
}
// Setup dialog content
// Build list of characters to select via dialog
const characterList = []
group.forEach(g => {
characterList.push({
actorId: g?.actor?.id || g.id,
name: g?.actor?.name || g.name,
corruption: {
value: g.system?.status?.corruption?.value,
max: g.system?.status?.corruption?.max
},
assignedUser: game.users.players.filter(p => p.character === g)[0],
owners: game.users.players.filter(p => p.id in g.ownership),
targeted: targeted.includes(g)
})
})
// Build dialog content
let checkOptions = ""
characterList.forEach(actor => {
const canWhisperTo = (actor.corruption.value)
? `enabled title="${game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.HasCorruption")}"`
: `disabled title="${game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.NoCorruption")}"`
const checked = (actor.targeted && actor.corruption.value) ? "checked" : ""
const playerOwners = actor.owners.map(m => m.name).join(", ")
checkOptions += `
<div class="form-group">
<input type="checkbox" id="${actor.actorId}" name="${actor.actorId}" value="${actor.name}" ${canWhisperTo} ${checked}>
<label for="${actor.actorId}" title="${game.i18n.format("GMTOOLKIT.Dialog.DarkWhispers.PlayerTooltip", { assignedUser: actor.assignedUser?.name || game.i18n.localize("GMTOOLKIT.Dialog.None"), playerOwners: playerOwners })}"> <strong>${actor.name}</strong> (${actor.assignedUser?.name || game.i18n.localize("GMTOOLKIT.Dialog.NotAssigned")})</label>
<label for="${actor.actorId}"> ${actor.corruption.value} / ${actor.corruption.max} ${game.i18n.localize("NAME.Corruption")} </label>
</div>
`
})
// Construct and show form
const darkwhisper = (game.tables.getName(game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.Title")))
? await game.tables.getName(game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.Title")).draw(
{ displayChat: false }
).then(w => w.results[0].text)
: game.i18n.format("GMTOOLKIT.Dialog.DarkWhispers.ImportTable")
const dialogContent = `
<div class="form-group ">
<label for="targets">${game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.WhisperTargets")} </label>
</div>
${checkOptions}
<div class="form-group message">
<label for="message">${game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.WhisperMessage")}</label>
</div>
<div class="form-group">
<textarea id="message" name="message" rows="4" cols="50">${darkwhisper}</textarea>
</div>
<div class="form-group">
<input type="checkbox" id="sendToOwners" name="sendToOwners">
<label for="sendToOwners">${game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.SendToOwners")}</label>
</div>
`
new Dialog({
title: game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.Title"),
content: dialogContent,
buttons: {
cancel: {
label: game.i18n.localize("GMTOOLKIT.Dialog.Cancel"),
callback: () => abortWhisper()
},
whisper: {
label: game.i18n.localize("GMTOOLKIT.Dialog.DarkWhispers.SendWhisper"),
callback: html => sendDarkWhispers(html, characterList, html.find('[name="sendToOwners"]')[0].checked)
}
}
}).render(true)
}
function sendDarkWhispers (html, characterList, sendToOwners) {
// Build list of selected players ids for whispers target
const characterTargets = []
const playerRecipients = []
for ( const character of characterList ) {
if (html.find(`[name="${character.actorId}"]`)[0].checked) {
characterTargets.push(character.name)
sendToOwners
? playerRecipients.push(...character.owners.map(m => m.id))
: playerRecipients.push(character.assignedUser?.id)
}
}
// Check for whisper message
darkwhisper = html.find('[name="message"]')[0].value
// Abort if no whisper or character is selected
if (playerRecipients.filter(p => p === undefined).length
=== playerRecipients.length
|| !playerRecipients.length || !darkwhisper
) return abortWhisper()
// Construct and send message to whisper targets
// Build the translation string based on the setting
const messageTemplate = `GMTOOLKIT.Settings.DarkWhispers.message.${game.settings.get("wfrp4e-gm-toolkit", "messageDarkWhispers")}`
// Parse the translated message
const whisperMessage = `${game.i18n.format(messageTemplate, { message: darkwhisper })}`
// Add response buttons for chat card. data- attributes are used by listener.
const responseButtons = `
<span class="chat-card-button-area">
<a class="chat-card-button darkwhisper-button" data-button="actOnWhisper" data-ask="${game.i18n.format(darkwhisper)}">${game.i18n.localize("GMTOOLKIT.Message.DarkWhispers.Accept")}</a>
<a class="chat-card-button darkwhisper-button" data-button="denyDarkGods" data-ask="${game.i18n.format(darkwhisper)}">${game.i18n.localize("GMTOOLKIT.Message.DarkWhispers.Reject")}</a>
</span>
`
// Post the message
ChatMessage.create({
content: whisperMessage + responseButtons,
whisper: playerRecipients,
flavor: characterTargets.join(", ")
})
}
function abortWhisper () {
return ui.notifications.warn(
game.i18n.format("GMTOOLKIT.Message.DarkWhispers.WhisperAborted", { currentUser: game.user.name }),
{ console: false }
)
}
/* ==========
* MACRO: Send Dark Whispers
* VERSION: 0.9.5
* UPDATED: 2022-08-14
* DESCRIPTION: Open a dialog to send a Dark Whisper (WFRP p183) to one or more selected player character(s).
* TIP: Only player-assigned or player-owned characters with Corruption can be sent a Dark Whisper.
* TIP: The placeholder whisper is drawn from the Dark Whispers table. Change this for different random whispers.
* TIP: The whisper can be edited in the dialog, regardless of what is pre-filled from the Dark Whispers table.
* TIP: Actor tokens that are targeted in a scene are pre-selected in the Send Dark Whisper dialog.
========== */