-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIsGM.js
100 lines (85 loc) · 3.43 KB
/
IsGM.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
// Github: https://github.com/shdwjk/Roll20API/blob/master/IsGM/IsGM.js
// By: The Aaron, Arcane Scriptomancer
// Contact: https://app.roll20.net/users/104025/the-aaron
var IsGMModule = IsGMModule || (function() {
'use strict';
var version = '0.7.2',
lastUpdate = 1490707497,
schemaVersion = 0.6,
active = true,
reset_password = "swordfish",
hasPlayerIsGM = ("undefined" !== typeof playerIsGM && _.isFunction(playerIsGM)),
checkInstall = function() {
var players = findObjs({_type:"player"});
if( ! _.has(state,'IsGM') || ! _.has(state.IsGM,'version') || state.IsGM.version !== schemaVersion ) {
log(' > Updating Schema to v'+schemaVersion+' <');
state.IsGM={
version: schemaVersion,
gms: [],
players: [],
unknown: []
};
}
state.IsGM.unknown=_.difference(
_.pluck(players,'id'),
state.IsGM.gms,
state.IsGM.players
);
active = (state.IsGM.unknown.length>0);
},
isGM = (hasPlayerIsGM ? playerIsGM : function(id) {
return _.contains(state.IsGM.gms,id);
}),
handleMessages = function(msg) {
var player, tokenized, who;
if(msg.type !== "api") {
if(active && msg.playerid !== 'API') {
if(_.contains(state.IsGM.unknown, msg.playerid)) {
player=getObj('player',msg.playerid);
if("" === player.get('speakingas') || 'player|'+msg.playerid === player.get('speakingas')) {
if(msg.who === player.get('_displayname')) {
state.IsGM.players.push(msg.playerid);
} else {
state.IsGM.gms.push(msg.playerid);
sendChat('IsGM','/w gm '+player.get('_displayname')+' is now flagged as a GM.');
}
state.IsGM.unknown=_.without(state.IsGM.unknown,msg.playerid);
active = (state.IsGM.unknown.length>0);
}
}
}
} else {
tokenized = msg.content.split(/\s+/);
switch(tokenized[0]) {
case '!reset-isgm':
if(isGM(msg.playerid) || (tokenized.length>1 && tokenized[1] === reset_password)) {
delete state.IsGM;
checkInstall();
sendChat('IsGM','/w gm IsGM data reset.');
} else {
who=(getObj('player',msg.playerid)||{get:()=>'API'}).get('_displayname');
sendChat('IsGM','/w "'+who+'" ('+who+') Only GMs may reset the IsGM data.'+
'If you are a GM you can reset by specifying the reset password from'+
'the top of the IsGM script as an argument to !reset-isgm');
}
break;
}
}
},
registerEventHandlers = function() {
if(! hasPlayerIsGM ) {
on('chat:message',handleMessages);
}
};
return {
CheckInstall: checkInstall,
RegisterEventHandlers: registerEventHandlers,
IsGM: isGM
};
}());
on('ready',function() {
'use strict';
IsGMModule.CheckInstall();
IsGMModule.RegisterEventHandlers();
});
var isGM = isGM || IsGMModule.IsGM;