forked from STBaf/NotYourTurn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotYourTurn.js
389 lines (346 loc) · 14.5 KB
/
NotYourTurn.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/*
* NotYourTurn
* Author: STB#9841 / Cris#6864
*/
import {registerSettings} from "./src/settings.js";
import {checkCombat, whisperGM, sockets, storeAllPositions, setTokenPositionOld, setTokenPositionNew, undoMovement} from "./src/misc.js";
new Date();
let timer = 0;
export var duplicateCheck = false;
let NYTTcontrolledTokens = [];
let dialogWait = false;
let GMwait = false;
let count = 0;
let warningTimer = 0;
let warningPeriod = 1000;
let NYTTokenPositionMap = new Map();
Hooks.on('NotYourTurn',async(data) => {
if (game.user.isGM == false) return;
let nonCombat;
if (data.nonCombat != undefined) {
if (data.nonCombat == true) nonCombat = true;
else if (data.nonCombat == false) nonCombat = false;
else if (data.nonCombat == 'toggle') nonCombat = !game.settings.get('NotYourTurn','nonCombat');
await game.settings.set('NotYourTurn','nonCombat',nonCombat);
ui.controls.controls.find(controls => controls.name == "token").tools.find(tools => tools.name == "blockMovement").active = nonCombat;
ui.controls.render();
storeAllPositions(NYTTokenPositionMap);
}
let combat;
if (data.combat != undefined) {
if (data.combat == true) combat = true;
else if (data.combat == false) combat = false;
else if (data.combat == 'toggle') combat = !game.settings.get('NotYourTurn','enable');
await game.settings.set('NotYourTurn','enable',combat);
ui.controls.controls.find(controls => controls.name == "token").tools.find(tools => tools.name == "enableNotYourTurn").active = combat;
ui.controls.render();
storeAllPositions(NYTTokenPositionMap);
}
});
Hooks.on('updateSetting', async(setting, value, diff, key) => {
if ((setting.key == 'NotYourTurn.nonCombat' || setting.key == 'NotYourTurn.enable') && setting.value == true) {
storeAllPositions(NYTTokenPositionMap);
}
});
Hooks.once('init', function(){
registerSettings(); //in ./src/settings.js
});
Hooks.once('ready', ()=>{
timer = Date.now();
sockets(NYTTokenPositionMap);
});
Hooks.on("canvasReady",(canvas) => {
NYTTcontrolledTokens = [];
NYTTokenPositionMap.clear();
// While on canvasReady in V10 X/Y of token was already filled with correct value, in V11 it is at CanvasReady still 0/0. So we need to track position later when set, seem to exist on first refreshToken.
// We can use the refreshToken in V11 globally since coords still the old before movement while in V10 already the new position (which would move token to actual position on Undo instead of back)
// Overall: V10 and V11 act different in token positions
if (game.version.startsWith("10"))
{
storeAllPositions(NYTTokenPositionMap, canvas);
}
if (game.version.startsWith("11"))
{
Hooks.on('refreshToken', OnRefreshTokenV11);
}
let tokens = canvas.tokens.children[0].children;
for (let i=0; i<tokens.length; i++)
{
if (tokens[i].isOwner)
{
NYTTcontrolledTokens.push(tokens[i].id);
}
}
});
//Register control button
Hooks.on("getSceneControlButtons", async(controls) => {
if (game.user.isGM) {
let tokenButton = controls.find(b => b.name == "token")
if (tokenButton) {
tokenButton.tools.push(
{
name: "enableNotYourTurn",
title: game.i18n.localize("NotYourTurn.Enable"),
icon: "fas fa-fist-raised",
toggle: true,
active: game.settings.get('NotYourTurn','enable'),
visible: game.user.isGM,
onClick: (value) => {
setEnable(value);
}
},
{
name: "blockMovement",
title: game.i18n.localize("NotYourTurn.ControlBtn"),
icon: "fas fa-lock",
toggle: true,
active: game.settings.get('NotYourTurn','nonCombat'),
visible: game.user.isGM,
onClick: (value) => {
setNonCombat(value);
}
}
);
}
}
});
async function setEnable(value){
await game.settings.set('NotYourTurn','enable',value);
storeAllPositions(NYTTokenPositionMap);
Hooks.call("NotYourTurn",{enable:value});
}
async function setNonCombat(value){
await game.settings.set('NotYourTurn','nonCombat',value);
storeAllPositions(NYTTokenPositionMap);
Hooks.call("NotYourTurn",{nonCombat:value});
}
//Register the token position
Hooks.on('controlToken', (token,controlled)=>{
if (controlled && token.isOwner) {
NYTTokenPositionMap.set(token.id, {x:token.x,y:token.y});
for (let i=0; i<NYTTcontrolledTokens.length; i++)
if (NYTTcontrolledTokens[i] == token.id)
return;
let length = NYTTcontrolledTokens.length+1;
for (let i=0; i<length; i++){
if (NYTTcontrolledTokens[i] == undefined){
NYTTcontrolledTokens[i] = token.id;
return;
}
}
}
else {
for (let i=0; i<NYTTcontrolledTokens.length; i++){
if (NYTTcontrolledTokens[i] == token.id){
NYTTcontrolledTokens.splice(i,1);
return;
}
}
}
});
function OnRefreshTokenV11(token)
{
if (token.controlled || token.isOwner) {
NYTTokenPositionMap.set(token.id, {x:token.x,y:token.y});
}
}
Hooks.on('updateToken',(a,b,c,d,e)=>{
const updateData = b;
const userId = d;
//Check if movement has been updated
if (updateData.x == undefined && updateData.y == undefined) {
return;
}
//To prevent the dialog from appearing multiple times, set a timer
if (duplicateCheck == true)
{
return;
}
//Check if client controls the token
if (userId != game.userId) {
return;
}
//Check if there is combat, or if nonCombat block is on
if (checkCombat() == false && game.settings.get('NotYourTurn','nonCombat') == false) { return };
if (checkCombat() && game.settings.get('NotYourTurn','enable') == false) { return };
//make sure the next part only happens once, even if you have multiple tokens selected
count++;
if (count < NYTTcontrolledTokens.length) { return };
count = 0;
duplicateCheck = true;
blockMovement(updateData);
});
async function blockMovement(data){
//Get the token shift
let token = canvas.tokens.children[0].children.find(p => p.id == data._id);
let movementShift = {
x: isNaN(data.x) ? 0 : data.x-token.x,
y: isNaN(data.y) ? 0 : data.y-token.y
};
let counter = 0;
let tokens = [];
//Add controlled tokens to the combatants array, except the token whose turn it is, or tokens that are not in combat is nonCombat is true
for (let i=0; i<NYTTcontrolledTokens.length; i++){
let token = canvas.tokens.children[0].children.find(p => p.id == NYTTcontrolledTokens[i]);
let location = {x:token.x+movementShift.x, y:token.y+movementShift.y};
if (checkCombat() && dialogWait == false && game.settings.get('NotYourTurn','AlwaysBlock') == false){
const combatTokenId = game.combat.combatant.token.id;
if (combatTokenId == NYTTcontrolledTokens[i] && token.isOwner){
NYTTokenPositionMap.set(token.id, location);
continue;
}
let isCombatant = game.combat.combatants.find(p => p.token.id == NYTTcontrolledTokens[i]);
if (isCombatant == undefined && game.settings.get('NotYourTurn','nonCombat') == false && token.isOwner){
NYTTokenPositionMap.set(token.id, location);
continue;
}
}
const tokenName = token.name;
tokens[counter] = {id: NYTTcontrolledTokens[i], name: tokenName, location, locationOld: NYTTokenPositionMap.get(token.id)};
counter++;
}
//If there are no more tokens, return
if (tokens.length == 0) {
duplicateCheck = false;
return;
}
//If the dialog box is open, prevent user from moving other tokens
if (dialogWait || GMwait){
await setTokenPositionOld(tokens, NYTTokenPositionMap);
duplicateCheck = false;
return;
}
//Get the block setting, depending on the setting of the user role
let role = game.user.role;
let blockSett = 0;
if (role == 1) blockSett = game.settings.get("NotYourTurn","BlockPlayer");
else if (role == 2) blockSett = game.settings.get("NotYourTurn","BlockTrusted");
else if (role == 3) blockSett = game.settings.get("NotYourTurn","BlockAssistant");
else if (role == 4) blockSett = game.settings.get("NotYourTurn","BlockGM");
if (blockSett == 0) return;
//Check if autoblock applies, which will automatically force the token back to its original position
if (blockSett == 3){
await setTokenPositionOld(tokens, NYTTokenPositionMap);
if (Date.now()-warningTimer > warningPeriod) {
ui.notifications.warn(game.i18n.localize("NotYourTurn.UI_Warning"));
warningTimer = Date.now();
}
duplicateCheck = false;
}
//Check if 'warning only' is set for the turn block function, if so, continue movement and give warning
else if (blockSett == 1){
let names = "";
for (let i=0; i<tokens.length; i++){
let token = canvas.tokens.children[0].children.find(p => p.id == tokens[i].id);
setTokenPositionNew(tokens, NYTTokenPositionMap);
names += "'" + token.name + "'";
if (i+2 == tokens.length) names += game.i18n.localize("NotYourTurn.And");
else if (i+1 == tokens.length) names += " ";
else names += ", ";
}
if (Date.now()-warningTimer > warningPeriod) {
ui.notifications.warn(game.i18n.localize("NotYourTurn.UI_Warning"));
if (game.settings.get("NotYourTurn","ChatMessages")==true && role < 3)
whisperGM(names + game.i18n.localize("NotYourTurn.MovementWhisper"));
warningTimer = Date.now();
}
duplicateCheck = false;
}
//In all other cases, create a dialog box
else {
//Create a dialog, with buttons based on the current situation
let applyChanges = 0;
let buttons = {
//Undo button
Undo: {
label: game.i18n.localize("NotYourTurn.Dialog_UndoBtn"),
callback: () => applyChanges = 0
}
}
//Check if the role of the user. If applicable, add ignore button
if (game.settings.get("NotYourTurn","IgnoreButton")<role){
buttons.Ignore = {
label: game.i18n.localize("NotYourTurn.Dialog_IgnoreBtn"),
callback: () => applyChanges = 1
}
}
//Check if the user is player, add request button if enabled
if (game.settings.get("NotYourTurn","RequestButton")==true && role <4){
buttons.Request = {
label: game.i18n.localize("NotYourTurn.Dialog_RequestBtn"),
callback: () => applyChanges = 2
}
}
dialogWait = true;
let d = new Dialog({
title: game.i18n.localize("NotYourTurn.Dialog_Title"),
content: game.i18n.localize("NotYourTurn.Dialog_Text")+ '<br><br>',
buttons,
default: "Undo",
close: html => {
//If 'Undo' is pressed, move token back to previous position
if (applyChanges == 0){ //undo
undoMovement(tokens, NYTTokenPositionMap);
}
//If 'Ignore' is pressed, continue movement
else if (applyChanges == 1) { //ignore
let names = "";
for (let i=0; i<tokens.length; i++){
setTokenPositionNew(tokens, NYTTokenPositionMap);
let token = canvas.tokens.children[0].children.find(p => p.id == tokens[i].id);
names += "'" + token.name + "'";
if (i+2 == tokens.length) names += " and ";
else if (i+1 == tokens.length) names += " ";
else names += ", ";
}
if (game.settings.get("NotYourTurn","ChatMessages")==true && role < 3)
whisperGM(names + game.i18n.localize("NotYourTurn.MovementWhisper"));
duplicateCheck = false;
dialogWait = false;
}
else if (applyChanges == 2) { //request movement
let users = game.users.contents;
//Request movement from GM, then apply movement (GM can undo this)
for (let i=0; i < users.length; i++)
if (users[i].role > 2) {
if (users[i].viewedScene == canvas.scene.id){
GMwait = true;
duplicateCheck = false;
dialogWait = false;
let payload = {
"msgType": "requestMovement",
"sender": game.userId,
"receiver": users[i].id,
"tokens": tokens,
"scene": {
id: canvas.scene.id,
name: canvas.scene.name
}
};
game.socket.emit(`module.NotYourTurn`, payload);
}
else {
ui.notifications.warn(game.i18n.localize("NotYourTurn.UI_GMnotOnScene"));
GMwait = false;
undoMovement(tokens, NYTTokenPositionMap);
}
break;
}
}
}
});
d.render(true);
}
}
export function setDuplicateCheck(value){
duplicateCheck = value;
}
export function setDialogWait(value){
dialogWait = value;
}
export function setGMwait(value){
GMwait = value;
}
export function setTimer(value){
timer = value;
}