Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 36 additions & 8 deletions CombatTracker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1198,7 +1198,7 @@ function ct_add_token(token,persist=true,disablerolling=false, adv=false, dis=fa

hp=$("<div class='hp'/>");
let hp_input = $("<input class='hp'>");
if(token.isPlayer()){
if(window.DM && token.isPlayer() && !window.EXPERIMENTAL_SETTINGS['sheetPCSync'] ){
hp_input.prop("disabled", true);
}
hp_input.val(token.hp);
Expand Down Expand Up @@ -1251,9 +1251,21 @@ function ct_add_token(token,persist=true,disablerolling=false, adv=false, dis=fa
if ($(this).val().trim().startsWith("+") || $(this).val().trim().startsWith("-")) {
$(this).val(Math.max(0, parseInt(token.hp) + parseInt($(this).val())));
}

old.find(".hp").val($(this).val().trim());

const newHP = $(this).val().trim();
old.find(".hp").val(newHP);
//also update character sheet if synced style
if(window.DM && token.options.statBlock) {
const customStatBlock = window.JOURNAL.notes[token.options.statBlock].text;
const pcLinkedURL = $(customStatBlock).find('.custom-pc-sheet.custom-stat.custom-pc-sheet-linked').text();
if(pcLinkedURL) {
const characterid = parseInt(pcLinkedURL.split("/").pop(),10);
if(characterid) {
console.log("Attempt NPC update", pcLinkedURL);
DDBApi.putCharacterHP(characterid, parseInt(newHP,10))
}
}
}

if(window.all_token_objects[token.options.id] != undefined){
window.all_token_objects[token.options.id].hp = $(this).val();
}
Expand Down Expand Up @@ -1287,10 +1299,26 @@ function ct_add_token(token,persist=true,disablerolling=false, adv=false, dis=fa
});
}
else {
hp.off('click.message').on('click.message', function(){
showTempMessage('Player HP must be adjusted on the character sheet.')
})
hp_input.keydown(function(e) { if (e.keyCode == '13') token.update_from_page(); e.preventDefault(); }); // DISABLE WITHOUT MAKING IT LOOK UGLY
if(window.DM && window.EXPERIMENTAL_SETTINGS['sheetPCSync']) {
hp_input.css("pointer-events", "auto");
hp_input.change(function(e) {
let selector = "div[data-id='" + token.options.id + "']";
let old = $("#tokens").find(selector);
const characterid = token.options.id.split("/").pop();
if ($(this).val().trim().startsWith("+") || $(this).val().trim().startsWith("-")) {
$(this).val(Math.max(0, parseInt(token.hp) + parseInt($(this).val())));
}
console.log("Update player", characterid, old.find(".hp"), old.find(".hp")?.val());
DDBApi.putCharacterHP(characterid, parseInt($(this).val().trim(),10))
});
hp_input.click(function(e) {
$(e.target).select();
});
} else {
hp.off('click.message').on('click.message', function(){
showTempMessage('Player HP must be adjusted on the character sheet.')
hp_input.keydown(function(e) { if (e.keyCode == '13') token.update_from_page(); e.preventDefault(); }); // DISABLE WITHOUT MAKING IT LOOK UGLY
})}
maxhp_input.keydown(function(e) { if (e.keyCode == '13') token.update_from_page(); e.preventDefault(); });
}

Expand Down
24 changes: 23 additions & 1 deletion DDBApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -256,7 +256,29 @@ class DDBApi {
return await DDBApi.fetchCharacterDetails(characterIds);
}


static async putCharacterHP(characterId, hp) {
//have to fetch first: use tmp first to reduce or for addition beyond max
const prev = (await DDBApi.fetchCharacterDetails([characterId]))?.[0];
const snd = { characterId, temporaryHitPoints: 0, removedHitPoints: 0 };
//heuristic: use tmp points first in a damage
if(prev?.hitPointInfo) {
if(hp > prev.hitPointInfo.current) { //heal
snd.removedHitPoints = prev.hitPointInfo.maximum - hp - prev.hitPointInfo.temp;
} else if(hp < prev?.hitPointInfo.current) { //damage
const tmp = hp - prev.hitPointInfo.maximum;
if(tmp > 0) {
snd.temporaryHitPoints = tmp;
} else if(tmp < 0) {
snd.removedHitPoints = -tmp;
} else {
return;
}
} else { //nothing
return;
}
return DDBApi.putJsonWithToken(`https://character-service.dndbeyond.com/character/v5/life/hp/damage-taken`, snd);
} //else silent fail
}

static async fetchCharacterDetails(characterIds) {
if (!Array.isArray(characterIds) || characterIds.length === 0) {
Expand Down
3 changes: 2 additions & 1 deletion Journal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3814,7 +3814,8 @@ class JournalManager{
{ title: 'HP Roll', inline: 'b', classes: 'custom-hp-roll custom-stat' },
{ title: 'Initiative', inline: 'b', classes: 'custom-initiative custom-stat' },
{ title: 'CR', inline: 'b', classes: 'custom-challenge-rating custom-stat' },
{ title: 'Custom PC Sheet Link', inline: 'b', classes: 'custom-pc-sheet custom-stat' },
{ title: 'Custom PC Sheet Link', inline: 'b', classes: 'custom-pc-sheet custom-stat' },
{ title: 'Custom PC Sheet Link (synced)', inline: 'b', classes: 'custom-pc-sheet custom-stat custom-pc-sheet-linked' },
{ title: 'AboveVTT Slash Command Roll Button', inline: 'span', classes: 'abovevtt-slash-command-journal custom-stat' }
]}
],
Expand Down
13 changes: 13 additions & 0 deletions Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,6 +788,19 @@ function avtt_settings() {
},
class: 'ui'
})
settings.push(
{
name: "sheetPCSync",
label: "Sync PC Sheets (DM Only, requires reload)",
type: "toggle",
options: [
{ value: true, label: "Disabled", description: `By default AVTT does not attempt to sync hit point updates back to character sheets` },
{ value: false, label: "Enabled", description: `Syncing hit point counts back to characters can be inconsistent!` }
],
defaultValue: false,
class: 'ui'
})




Expand Down