Skip to content

Commit 3d22ff2

Browse files
committed
Send rendered rolls from DDB to DOM too and avoid duplicates
debounces the fallback rendered roll so if a real one arrives within 500ms, it can override the fallback avoiding sending double rendered rolls when DDB renders it after a failure to find a VTT
1 parent b1759db commit 3d22ff2

File tree

4 files changed

+46
-4
lines changed

4 files changed

+46
-4
lines changed

src/common/roll_renderer.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -380,6 +380,9 @@ class Beyond20RollRenderer {
380380
this._displayer.sendMessage(request, title, html, character, request.whisper, play_sound, source, attributes, description, json_attack_rolls, roll_info, json_damage_rolls, json_total_damages, open)
381381
} else if (canPostHTML) {
382382
this._displayer.postHTML(request, title, html, character, request.whisper, play_sound, source, attributes, description, json_attack_rolls, roll_info, json_damage_rolls, json_total_damages, open);
383+
if (this._displayer.sendMessageToDOM) {
384+
this._displayer.sendMessageToDOM(request, title, html, character, request.whisper, play_sound, source, attributes, description, json_attack_rolls, roll_info, json_damage_rolls, json_total_damages, open)
385+
}
383386
}
384387

385388
if (attack_rolls.length > 0) {

src/common/utils.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,17 @@ function initializeAlertify() {
281281

282282
}
283283

284+
const bouncedFallbackRenders = {};
285+
function simpleHash(input) {
286+
let hash = 0;
287+
for (let i = 0; i < input.length; i++) {
288+
const char = input.charCodeAt(i);
289+
hash = (hash << 5) - hash + char;
290+
hash |= 0; // Convert to 32-bit integer
291+
}
292+
return hash;
293+
}
294+
284295
function forwardMessageToDOM(request) {
285296
if (request.action == "hp-update") {
286297
sendCustomEvent("UpdateHP", [request, request.character.name, request.character.hp, request.character["max-hp"], request.character["temp-hp"]]);
@@ -294,6 +305,30 @@ function forwardMessageToDOM(request) {
294305
// Requires roll_renderer to be set (currently in generic-site and ddb pages)
295306
roll_renderer.handleRollRequest(request);
296307
} else if (request.action == "rendered-roll") {
297-
sendCustomEvent("RenderedRoll", [request]);
308+
// Hash the original request to be able to match it with the rendered one in case of fallback
309+
// But don't use the whole request since it can change by the time we render it (original-whisper is added)
310+
const reqHash = simpleHash(JSON.stringify({
311+
action: request.request.action,
312+
type: request.request.type,
313+
character: request.request.character,
314+
roll: request.request.roll,
315+
name: request.request.name,
316+
ability: request.request.ability,
317+
modifier: request.request.modifier,
318+
description: request.request.description
319+
}));
320+
if (request.rendered === "fallback") {
321+
// This is a fallback render, if we're sending it from DDB, we might end up with
322+
// a double render, so bounce this one for 500ms to let the real render happen if it's
323+
// going to, then override the fallback render if we do.
324+
bouncedFallbackRenders[reqHash] = setTimeout(() => {
325+
delete bouncedFallbackRenders[reqHash];
326+
sendCustomEvent("RenderedRoll", [request]);
327+
}, 500);
328+
} else {
329+
clearTimeout(bouncedFallbackRenders[reqHash]);
330+
delete bouncedFallbackRenders[reqHash];
331+
sendCustomEvent("RenderedRoll", [request]);
332+
}
298333
}
299334
}

src/dndbeyond/base/dice.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class DNDBDisplayer {
3636
});
3737
}
3838

39-
async sendMessage(request, title, html, character, whisper, play_sound, source, attributes, description, attack_rolls, roll_info, damage_rolls, total_damages, open) {
39+
async sendMessageToDOM(request, title, html, character, whisper, play_sound, source, attributes, description, attack_rolls, roll_info, damage_rolls, total_damages, open) {
4040
const req = {
4141
action: "rendered-roll",
4242
request,
@@ -54,9 +54,13 @@ class DNDBDisplayer {
5454
total_damages,
5555
open
5656
}
57+
sendRollRequestToDOM(req);
58+
return req;
59+
}
60+
async sendMessage(request, title, html, character, whisper, play_sound, source, attributes, description, attack_rolls, roll_info, damage_rolls, total_damages, open) {
61+
const req = sendMessageToDOM(request, title, html, character, whisper, play_sound, source, attributes, description, attack_rolls, roll_info, damage_rolls, total_damages, open);
5762
console.log("Sending message: ", req);
5863
chrome.runtime.sendMessage(req, (resp) => beyond20SendMessageFailure(character, resp));
59-
sendRollRequestToDOM(req);
6064
}
6165
displayError(message) {
6266
alertify.error(message);

src/generic-site/renderer.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class GenericDisplayer {
2424
total_damages,
2525
open
2626
}
27-
sendCustomEvent("RenderedRoll", [req]);
27+
forwardMessageToDOM(req);
2828
}
2929

3030
displayError(message) {

0 commit comments

Comments
 (0)