-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathready-check-reloaded.js
187 lines (168 loc) · 5.95 KB
/
ready-check-reloaded.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
import { registerSettings } from "./scripts/settings.js";
import ReadyCheckApp from "./scripts/ReadyCheckApp.js";
let rca = new ReadyCheckApp();
Hooks.once("init", () => {
registerSettings();
createSocketHandler();
});
Hooks.once("ready", async() => {
// If there's an active ready check, display it
const checkIsActive = game.settings.get('ready-check-reloaded','checkIsActive');
await game.user.setFlag('ready-check-reloaded','isReady', false);
const isReady = game.user.flags['ready-check-reloaded'].isReady;
if(checkIsActive){
openReadyCheckApp();
const socketData = {
user: game.user,
action: "UPDATE_STATUS",
isReady: isReady
};
game.socket.emit('module.ready-check-reloaded', socketData);
}
})
Hooks.on('renderPlayerList', async function(){
// Add controls to player window
const controls = `
<div id="ready-check-reloaded-controls">
<button type="button" id="ready-check-reloaded-toggle" title="Toggle Ready Status" aria-label="Toggle Ready Status"><i class="fas fa-hourglass-half"></i></button>
</div>`;
$("#players").append(controls);
if(game.user.isGM){
const startButton = `<button type="button" id="ready-check-reloaded-start" title="Start Ready Check" aria-label="Start Ready Check"><i class="fas fa-check-to-slot"></i></button>`;
$("#ready-check-reloaded-controls").prepend(startButton);
}
// Add status indicators to player window
game.users.contents.forEach(u => {
const isReady = u.getFlag("ready-check-reloaded", "isReady");
const userId = u._id;
const playersWindowRow = $(`#players #player-list li[data-user-id="${userId}"]`);
if(isReady){
playersWindowRow.append(`<i class="fas fa-check ready-check-reloaded-status ready" title="Ready"></i>`);
} else {
playersWindowRow.append(`<i class="fas fa-times ready-check-reloaded-status not-ready" title="Not Ready"></i>`);
}
});
// Activate controls listeners
activateListeners();
});
function createSocketHandler(){
game.socket.on('module.ready-check-reloaded', async (data) => {
/*
data: {
fromUser: string
action: "START_CHECK" "END_CHECK" or "UPDATE_STATUS"
isReady: true or false
}
*/
if(data.action === 'START_CHECK'){
recieveReadyCheck();
}
else if (data.action === 'END_CHECK'){
closeReadyCheckApp();
}
else if (data.action === 'UPDATE_STATUS'){
recieveStatusUpdate(data);
} else {
console.warn(`Ready Set Go: Reloaded | Instruction type {${action}} not recognized.`)
}
});
}
function activateListeners(){
$('#ready-check-reloaded-start').click(async (event) => {
event.preventDefault();
startReadyCheck();
});
$('#ready-check-reloaded-toggle').click(async (event) => {
event.preventDefault();
toggleReadyStatus();
});
};
async function startReadyCheck(){
if(!game.user.isGM) return;
await game.user.setFlag('ready-check-reloaded','isReady', false);
await game.users.contents.filter(u => !u.active).forEach(async u => { await u.setFlag('ready-check-reloaded','isReady', false)});
await game.settings.set('ready-check-reloaded','checkIsActive', true);
openReadyCheckApp();
playReadyCheckAlert();
const socketData = {
user: game.user,
action: "START_CHECK"
};
game.socket.emit('module.ready-check-reloaded', socketData);
}
async function recieveReadyCheck(){
await game.user.setFlag('ready-check-reloaded','isReady', false);
openReadyCheckApp();
const socketData = {
user: game.user,
action: "UPDATE_STATUS",
isReady: false
};
game.socket.emit('module.ready-check-reloaded', socketData);
ui.players.render();
}
export async function toggleReadyStatus(){
const isReady = !game.user.flags['ready-check-reloaded'].isReady;
const checkIsActive = game.settings.get('ready-check-reloaded','checkIsActive');
await game.user.setFlag('ready-check-reloaded','isReady', isReady);
if(checkIsActive){
const alertSound = game.settings.get('ready-check-reloaded','responseAlertSoundPath');
playResponseAlert(alertSound, isReady);
openReadyCheckApp();
} else {
sendChatMessage(game.user, isReady);
}
const socketData = {
user: game.user,
action: "UPDATE_STATUS",
isReady: isReady
};
game.socket.emit('module.ready-check-reloaded', socketData);
ui.players.render();
}
function recieveStatusUpdate(data){
const checkIsActive = game.settings.get('ready-check-reloaded','checkIsActive');
if(checkIsActive){
openReadyCheckApp();
}
ui.players.render();
}
function openReadyCheckApp(){
if(rca == null ){
rca = new ReadyCheckApp().render(true);
} else {
rca.render(true);
}
}
async function closeReadyCheckApp(){
if(rca != null && rca.rendered){
await rca.close();
}
rca = null;
}
function sendChatMessage(user, isReady){
const sendMessage = game.settings.get("ready-check-reloaded", "showChatMessagesForUserUpdates");
if(!sendMessage) return;
const status = isReady ? "ready" : "not ready";
const content = `${user.name} is ${status}.`;
ChatMessage.create({speaker:{alias: "Ready Set Go: Reloaded"}, content: content});
}
function playReadyCheckAlert(){
const playAlert = game.settings.get("ready-check-reloaded", "playAlertForCheck");
if (!playAlert) return;
const alertSound = game.settings.get("ready-check-reloaded", "checkAlertSoundPath");
if(!alertSound){
AudioHelper.play({src: "modules/ready-check-reloaded/sounds/notification.mp3", volume: 1, autoplay: true, loop: false}, true);
} else{
AudioHelper.play({src: alertSound, volume: 1, autoplay: true, loop: false}, true);
}
}
function playResponseAlert(alertSound, isReady){
const playAlert = game.settings.get("ready-check-reloaded", "playAlertForResponse");
if (!playAlert || !isReady) return;
if(!alertSound){
AudioHelper.play({src: "modules/ready-check-reloaded/sounds/notification-2.mp3", volume: 1, autoplay: true, loop: false}, true);
} else{
AudioHelper.play({src: alertSound, volume: 1, autoplay: true, loop: false}, true);
}
}