Skip to content

Commit

Permalink
Merge pull request #2856 from Azmoria/Fix---issue-with-player-conditi…
Browse files Browse the repository at this point in the history
…ons-not-always-updating-correctly

Fix - issue with player token data sometimes not changing / syncing to DDB data on load
  • Loading branch information
Azmoria authored Feb 1, 2025
2 parents e0dab5b + 9e34bcd commit f0f49a1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 36 deletions.
66 changes: 36 additions & 30 deletions CoreFunctions.js
Original file line number Diff line number Diff line change
Expand Up @@ -970,44 +970,50 @@ function update_pc_with_data(playerId, data) {
...data,
lastSynchronized: Date.now()
}
if (window.DM) {
if (!window.PC_TOKENS_NEEDING_UPDATES.includes(playerId)) {
window.PC_TOKENS_NEEDING_UPDATES.push(playerId);
}
debounce_pc_token_update();

if (!window.PC_TOKENS_NEEDING_UPDATES.includes(playerId)) {
window.PC_TOKENS_NEEDING_UPDATES.push(playerId);
}
debounce_pc_token_update();
}

const debounce_pc_token_update = mydebounce(() => {
if (window.DM) {
window.PC_TOKENS_NEEDING_UPDATES.forEach((playerId) => {
const pc = find_pc_by_player_id(playerId, false);
let token = window.TOKEN_OBJECTS[pc?.sheet];
if (token) {
let currentImage = token.options.imgsrc;
token.hp = pc.hitPointInfo.current;
token.options = {
...token.options,
...pc,
imgsrc: (token.options.alternativeImages?.length == 0) ? pc.image : currentImage,
id: pc.sheet // pc.id is DDB characterId, but we use the sheet as an id for tokens
};

window.PC_TOKENS_NEEDING_UPDATES.forEach((playerId) => {
const pc = find_pc_by_player_id(playerId, false);
let token = window.TOKEN_OBJECTS[pc?.sheet];
if (token) {
let currentImage = token.options.imgsrc;
token.hp = pc.hitPointInfo.current;
token.options = {
...token.options,
...pc,
imgsrc: (token.options.alternativeImages?.length == 0) ? pc.image : currentImage,
id: pc.sheet // pc.id is DDB characterId, but we use the sheet as an id for tokens
};
if (window.DM) {
token.place_sync_persist(); // not sure if this is overkill
}
token = window.all_token_objects[pc?.sheet] //for the combat tracker and cross scene syncing/tokens - we want to update this even if the token isn't on the current map
if(token){
let currentImage = token.options.imgsrc;
token.options = {
...token.options,
...pc,
imgsrc: (token.options.alternativeImages?.length == 0) ? pc.image : currentImage,
id: pc.sheet // pc.id is DDB characterId, but we use the sheet as an id for tokens
};
}
});
else{
token.place();
}
}
token = window.all_token_objects[pc?.sheet] //for the combat tracker and cross scene syncing/tokens - we want to update this even if the token isn't on the current map
if(token){
let currentImage = token.options.imgsrc;
token.options = {
...token.options,
...pc,
imgsrc: (token.options.alternativeImages?.length == 0) ? pc.image : currentImage,
id: pc.sheet // pc.id is DDB characterId, but we use the sheet as an id for tokens
};
}
});
if (window.DM) {
update_pc_token_rows();
window.PC_TOKENS_NEEDING_UPDATES = [];
}
window.PC_TOKENS_NEEDING_UPDATES = [];

},50);

function update_pc_with_api_call(playerId) {
Expand Down
20 changes: 15 additions & 5 deletions MessageBroker.js
Original file line number Diff line number Diff line change
Expand Up @@ -1710,11 +1710,7 @@ class MessageBroker {

}
else if(data.left){
// SOLO PLAYER. PUNTO UNICO DI CREAZIONE DEI TOKEN

if (window.DM) {
console.log("ATTENZIONEEEEEEEEEEEEEEEEEEE ATTENZIONEEEEEEEEEEEEEEEEEEE");
}

let t = new Token(data);
if(isNaN(parseFloat(t.options.left)) || isNaN(parseInt(t.options.top))){ // prevent errors with NaN positioned tokens - delete them as catch all.
t.options.deleteableByPlayers = true;
Expand All @@ -1728,6 +1724,20 @@ class MessageBroker {
t.sync = mydebounce(function(e) { // VA IN FUNZIONE SOLO SE IL TOKEN NON ESISTE GIA
window.MB.sendMessage('custom/myVTT/token', t.options);
}, 300);
if(t.isPlayer()){
const pc = find_pc_by_player_id(data.id, false);
let token = window.TOKEN_OBJECTS[data.id]
if (token) {
let currentImage = token.options.imgsrc;
token.hp = pc.hitPointInfo.current;
token.options = {
...token.options,
...pc,
imgsrc: (token.options.alternativeImages?.length == 0) ? pc.image : currentImage,
id: pc.sheet // pc.id is DDB characterId, but we use the sheet as an id for tokens
};
}
}
t.place();

let playerTokenId = $(`.token[data-id*='${window.PLAYER_ID}']`).attr("data-id");
Expand Down
6 changes: 5 additions & 1 deletion Token.js
Original file line number Diff line number Diff line change
Expand Up @@ -1548,7 +1548,11 @@ class Token {
this.addCondition("Inspiration")
}
} else{
array_remove_index_by_value(this.options.custom_conditions, "Inspiration");
this.removeCondition("Inspiration");
}

if(this.isPlayer()){
this.options.conditions = find_pc_by_player_id(this.options.id).conditions
}

const conditions = this.conditions;
Expand Down

0 comments on commit f0f49a1

Please sign in to comment.