forked from unsoluble/fvtt-macros
-
Notifications
You must be signed in to change notification settings - Fork 0
/
inspiration-cards.js
146 lines (127 loc) · 4.44 KB
/
inspiration-cards.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
const deckTable = game.tables.getName('Inspiration Deck');
const packName = 'shared-compendiums.inspiration-cards';
const actors = ['Barnabus Truesheel', 'Lachish', 'Natriel Adonai', 'Owlgar', 'Phingo Underfoot'];
const DistributeItems = true;
const DeleteExisting = true;
// Open the div, set the BG styles.
let messageBody = `<div style='
text-align: center;
line-height: 1.5em;
border-radius: .8em;
box-shadow: 0 8px 12px rgba(0, 0, 0, 0.5);
padding: 10px 0 14px 0;
margin: 15px;
background: url(assets/Inspiration/inspiration-card-bg-nebula.webp);
background-size: cover;
'>`;
// Reset the rolls array, which emulates Draw Without Replacement.
let rolls = [];
(async () => {
for (const actor of actors) {
// If desired, delete any existing inspiration cards from the actors.
if (DeleteExisting === true) {
let existingCards = game.actors.getName(actor).items.filter((item) => {
if (item.data.data.source == 'Inspiration Deck') {
return true;
}
});
//collect IDs to output (and echo what we are deleting)
const deleteIds = existingCards.map( (card) => {
console.log(`Deleting ${card.name} from ${actor}`);
return card.id;
});
//delete these inspiration card IDs
await game.actors.getName(actor).deleteEmbeddedDocuments("Item", deleteIds);
// Draw a card for each actor.
await doDraw(actor);
}
}
// Close the BG div at the end.
messageBody += '</div>';
let chatData = {
user: game.user._id,
speaker: ChatMessage.getSpeaker({ alias: 'Inspiration!' }),
content: messageBody,
};
ChatMessage.create(chatData, {});
})();
async function doDraw(actor) {
let roll = '';
do {
//future proof against 0.9 async rolls
roll = await(new Roll(deckTable.data.formula).evaluate({async:true}));
} while (rolls.includes(roll.total));
rolls.push(roll.total);
const result = deckTable.getResultsForRoll(roll.total)[0];
const destinationActor = game.actors.getName(actor);
let item = await searchItem({ key: packName, name: result.data.text });
let entity = await findItemInCompendium({ keys: packName, name: item.name, id: item._id });
messageBody +=
`<div style='
color: #efefe0;
margin-top: 5px;
text-shadow: 1px 1px 3px rgba(0, 0, 0, 0.8);
'>` +
actor +
` gets</div><a
class='entity-link'
data-id='` +
item._id +
`' data-pack='` +
packName +
`' style='
border: none;
background: none;'>
<img style='
width: 80%;
margin-top: 2px;
border: none;
'src=` +
buildImageURL(item.name) +
'></a><br>';
if (DistributeItems) await destinationActor.createEmbeddedDocuments("Item", [entity.data]);
}
// These search and find functions I'm sure could be consolidated/simplified,
// but they work as-is so I ain't touching them.
async function searchItem({ key, name = '' }) {
let pack = game.packs.get(key);
let pack_index = pack.index.length > 1 ? pack.index : await pack.getIndex();
let item_index = pack_index.find((i) => i.name.toLowerCase() === name.toLowerCase());
if (item_index)
return {
comp_link: `@Compendium[${key}.${item_index._id}]{${item_index.name}}`,
img_link: `<img src="${item_index.img}" width="25" height="25">`,
key: key,
img: item_index.img,
name: item_index.name,
_id: item_index._id,
};
return undefined;
}
async function findItemInCompendium({ keys = [], name = '', id = '' }) {
keys = keys instanceof Array ? keys : [keys];
for (let key of keys) {
let pack = game.packs.get(key);
let itemID = id ?? (await pack.getIndex()).find((i) => i.name === name)?._id;
if (itemID) return await pack.getDocument(itemID);
}
}
function buildImageURL(name) {
let theURL = '/assets/Inspiration/standard/' + string_to_slug(name) + '.webp';
return theURL;
}
function string_to_slug(str) {
str = str.replace(/^\s+|\s+$/g, ''); // trim
str = str.toLowerCase();
// remove accents, swap ñ for n, etc
var from = 'àáäâèéëêìíïîòóöôùúüûñç·/_,:;';
var to = 'aaaaeeeeiiiioooouuuunc------';
for (var i = 0, l = from.length; i < l; i++) {
str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i));
}
str = str
.replace(/[^a-z0-9 -]/g, '') // remove invalid chars
.replace(/\s+/g, '-') // collapse whitespace and replace by -
.replace(/-+/g, '-'); // collapse dashes
return str;
}