-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
65 lines (56 loc) · 2.23 KB
/
main.ts
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
import OBR from "@owlbear-rodeo/sdk";
const ID = "com.example.action-tracker";
function broadcastCounterUpdate(color: string, value: number) {
if (OBR && OBR.broadcast) {
console.log("Broadcasting counter update:", color, value);
OBR.broadcast.sendMessage("counter-update", { color, value });
} else {
console.error("OBR.broadcast is not available");
}
}
function updateCounter(color: string, value: number) {
const counterElement = document.querySelector(`.counter[data-color="${color}"]`);
const currentValue = parseInt(counterElement.textContent, 10) || 0;
const newValue = currentValue + value;
counterElement.textContent = newValue > 0 ? newValue.toString() : "";
broadcastCounterUpdate(color, newValue);
saveCounterValue(color, newValue);
}
function saveCounterValue(color: string, value: number) {
OBR.room.setMetadata({ [`${ID}/${color}-counter`]: value });
}
function loadCounterValues() {
OBR.room.getMetadata().then((metadata) => {
const counterElements = document.querySelectorAll(".counter");
counterElements.forEach((counterElement) => {
const color = counterElement.getAttribute("data-color");
const savedValue = metadata[`${ID}/${color}-counter`] || 0;
counterElement.textContent = savedValue > 0 ? savedValue.toString() : "";
});
});
}
function handleCounterUpdate(event: any) {
const { color, value } = event.data;
const counterElement = document.querySelector(`.counter[data-color="${color}"]`);
counterElement.textContent = value > 0 ? value.toString() : "";
}
document.querySelectorAll(".plus").forEach((plusElement) => {
plusElement.addEventListener("click", () => {
const color = plusElement.getAttribute("data-color");
updateCounter(color, 1);
});
});
document.querySelectorAll(".minus").forEach((minusElement) => {
minusElement.addEventListener("click", () => {
const color = minusElement.getAttribute("data-color");
const counterElement = document.querySelector(`.counter[data-color="${color}"]`);
const currentValue = parseInt(counterElement.textContent, 10) || 0;
if (currentValue > 0) {
updateCounter(color, -1);
}
});
});
OBR.onReady(() => {
loadCounterValues();
OBR.broadcast.onMessage("counter-update", handleCounterUpdate);
});