-
Notifications
You must be signed in to change notification settings - Fork 2
/
extension.js
109 lines (88 loc) · 2.77 KB
/
extension.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
'use strict';
let nodecg;
let elapsedTime;
let remainingTime;
let isCooldownActive;
let passengers;
let dayTotal;
let threshold;
let duration;
let cdTimer = null;
function Train(extensionApi) {
nodecg = extensionApi;
// These properties are transient and are not persisted
elapsedTime = nodecg.Replicant('elapsedTime', {defaultValue: 0, persistent: false});
remainingTime = nodecg.Replicant('remainingTime', {defaultValue: 0, persistent: false});
isCooldownActive = nodecg.Replicant('isCooldownActive', {defaultValue: false, persistent: false});
// These properties are persisted to disk
passengers = nodecg.Replicant('passengers', {defaultValue: 0});
dayTotal = nodecg.Replicant('dayTotal', {defaultValue: 0});
threshold = nodecg.Replicant('threshold', {defaultValue: 10});
duration = nodecg.Replicant('duration', {defaultValue: 300});
nodecg.listenFor('startCooldown', this.startCooldown.bind(this));
nodecg.listenFor('endCooldown', this.endCooldown.bind(this));
nodecg.listenFor('resetCooldown', this.resetCooldown.bind(this));
// temporary workaround for some bundles until I figure out how to make this better
nodecg.listenFor('getPassengers', (data, cb) => {
cb(passengers.value);
});
nodecg.listenFor('getDayTotal', (data, cb) => {
cb(dayTotal.value);
});
}
Train.prototype.startCooldown = function () {
_killTimer(); // Kill any existing cooldown timer
elapsedTime.value = 0;
remainingTime.value = duration.value;
isCooldownActive.value = true;
cdTimer = setInterval(this.tickCooldown.bind(this), 1000);
nodecg.sendMessage('cooldownStart');
};
Train.prototype.tickCooldown = function () {
elapsedTime.value++;
remainingTime.value--;
nodecg.sendMessage('cooldownTick', {
remainingTime: remainingTime.value,
elapsedTime: elapsedTime.value
});
if (remainingTime.value <= 0) {
remainingTime.value = 0; // force to zero if we somehow went negative
this.endCooldown();
}
};
Train.prototype.resetCooldown = function () {
elapsedTime.value = 0;
remainingTime.value = duration.value;
};
Train.prototype.endCooldown = function () {
_killTimer();
passengers.value = 0;
isCooldownActive.value = false;
nodecg.sendMessage('cooldownEnd');
};
Train.prototype.addPassenger = function () {
passengers.value++;
dayTotal.value++;
const train = {
passengers: passengers.value,
dayTotal: dayTotal.value,
threshold: threshold.value,
isHype: passengers.value >= threshold.value
};
if (train.isHype && nodecg.bundleConfig.resetAfterThreshold) {
passengers.value = 0;
this.endCooldown();
} else if (nodecg.bundleConfig.autoStartCooldown) {
this.startCooldown();
}
return train;
};
function _killTimer() {
if (cdTimer !== null) {
clearInterval(cdTimer);
cdTimer = null;
}
}
module.exports = function (extensionApi) {
return new Train(extensionApi);
};