-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathobserveGameLog.js
131 lines (108 loc) · 5.61 KB
/
observeGameLog.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
var isObservingGameLog = false;
var gameLogObserver = null;
function observeGameLog() {
if (isObservingGameLog) return;
gameLogObserver = new MutationObserver(mutations => {
mutations.forEach(mutation => {
if (mutation.type === 'childList' && mutation.addedNodes.length > 0) {
const addedLi = mutation.addedNodes[0];
if (addedLi.tagName === 'LI') {
const diceMessageElement = addedLi.querySelector('[class*="-Pending-ref"]');
if (!diceMessageElement) {
const roll = extractDataFromLi(addedLi);
const rollTarget = roll.rollTarget.toLowerCase();
if (!(rollTarget === 'to: self') && !(rollTarget === 'to: dm') && !(roll.diceRoll === '')) {
let diceRoll = roll.diceRoll;
let color = "<color=\"white\">";
let firstNumber, secondNumber, selectedNumber;
if (diceRoll.includes('d20')) { // determine if it crit or crit failed
let diceResultExpression = roll.diceResultMath;
if (diceRoll.includes('kh1') || diceRoll.includes('kl1')) { // Check if diceRoll is using Adv or DisAdv
const numbersInParentheses = diceResultExpression.match(/\((\d+),\s*(\d+)\)/);
if (numbersInParentheses) { //Get both numbers
firstNumber = parseInt(numbersInParentheses[1], 10);
secondNumber = parseInt(numbersInParentheses[2], 10);
if (diceRoll.includes('kh1')) { //determine which to keep
selectedNumber = Math.max(firstNumber, secondNumber);
} else {
selectedNumber = Math.min(firstNumber, secondNumber);
}
}
} else {
let firstNumberString = diceResultExpression.split(' ')[0]; // Split the string by space and take the first element
selectedNumber = parseInt(firstNumberString, 10); // Convert the string to a number
}
if (selectedNumber === 20) {
color = "<color=\"green\">"; // Sets color to Green if a natural 20 is rolled
} else if (selectedNumber === 1) {
color = "<color=\"red\">"; // Sets color to Red if a natural 1 is rolled
}
}
TS.symbiote.sendNotification(roll.character, "<align=\"center\">" + roll.rollAction + " " + roll.rollType +
"\n<size=200%>" + color + roll.diceResultTotal + "</color>\n<size=90%>" + roll.diceRoll + " (" + roll.diceResultMath + ")");
console.log(roll);
}
}
}
}
});
});
const gameLogElement = document.querySelector('[class*="GameLogEntries"]');
if (gameLogElement) {
gameLogObserver.observe(gameLogElement, {childList: true});
isObservingGameLog = true;
} else {
console.error('GameLogEntries not found');
}
}
function stopObservingGameLog() {
if (gameLogObserver) {
gameLogObserver.disconnect();
isObservingGameLog = false;
}
}
function checkAndObserveGameLog() {
const gameLogElement = document.querySelector('[class*="GameLogEntries"]');
if (gameLogElement) {
observeGameLog();
} else {
stopObservingGameLog();
}
}
function capitalizeWords(str) {
return str.replace(/\b\w+/g, word => word.charAt(0).toUpperCase() + word.slice(1).toLowerCase());
}
function extractDataFromLi(liElement) {
// Dice Roll
const diceRollElement = liElement.querySelector('[class*="Line-Notation"] span');
const diceRoll = diceRollElement ? diceRollElement.textContent : '';
// Dice Result Math
const diceResultMathElement = liElement.querySelector('[class*="Line-Breakdown"] span');
const diceResultMath = diceResultMathElement ? diceResultMathElement.textContent : '';
// Dice Result Total
const diceResultTotalElement = liElement.querySelector('[class*="TotalContainer"] span');
const diceResultTotal = diceResultTotalElement ? diceResultTotalElement.textContent : '';
// Character
const characterElement = liElement.querySelector('[class*="-Sender"]');
const character = characterElement ? characterElement.textContent : '';
// Roll Action
const rollActionElement = liElement.querySelector('[class*="-Action"]');
const rollAction = rollActionElement ? rollActionElement.textContent : '';
// Roll Type
const rollTypeElement = liElement.querySelector('[class*="-RollType"]');
const rollType = rollTypeElement ? capitalizeWords(rollTypeElement.textContent) : '';
// Roll Target
const rollTargetElement = liElement.querySelector('[class*="-Target-ref"]');
const rollTarget = rollTargetElement ? capitalizeWords(rollTargetElement.textContent) : '';
return {
diceRoll,
diceResultMath,
diceResultTotal,
character,
rollAction,
rollType,
rollTarget
};
}
// Call checkAndObserveGameLog every 5 seconds
setInterval(checkAndObserveGameLog, 5000);