forked from Roll20/roll20-api-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChatTurnManager.js
251 lines (217 loc) · 7.09 KB
/
ChatTurnManager.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
/**
* ChatTurnManager v0.0.2
*
* Script to simplify Turn Order Management, and move it into chat.
* Commands:
*
* !turns-begin / !turns-start
* Sort the Turn Counter numerically descending, and add a turn counter to the
* top of the order
*
* !turns-clear
* Clear the turn order. NOTE: THERE IS NO CONFIRMATION.
*
* !turns-down <n> [--<before|after> prefix] name
* Add an item to the list that counts down from n. By default this is added
* to the current end of the order. If --before or --after is provided, the
* argument is used as a prefix search for a name to put the item before or
* after.
*
* !turns-up <n> [--<before|after> prefix] name
* Add an item to the list that counts up from n. By default this is added
* to the current end of the order. If --before or --after is provided, the
* argument is used as a prefix search for a name to put the item before or
* after.
*
* !turns-clean
* Remove all elements with a counter of 0.
*/
var ChatTurnManager;
var ChatTurnManager =
ChatTurnManager ||
(() => {
"use strict";
const version = "0.0.2";
const counterName = "Round Counter";
const getTurns = () => {
let turns = Campaign().get("turnorder");
if (turns === "") turns = [];
else turns = JSON.parse(turns);
for (let i = 0; i < turns.length; i++) {
turns[i]["pr"] = parseFloat(turns[i]["pr"]);
if (isNaN(turns[i]["pr"])) turns[i]["pr"] = 0;
}
return turns;
};
const setTurns = (turns) => {
Campaign().set("turnorder", JSON.stringify(turns));
};
const playerName = (playerID) => {
const player = getObj("player", playerID);
if (!player) return player;
return player.get("_displayname");
};
const whisperToID = (id, msg) => {
const name = id === "GM" ? id : playerName(id);
if (!name) return;
sendChat("ChatTurnManager", `/w "${name}" ${msg}`);
};
const itemName = (item) => {
if (item.id === "-1") return item.custom;
const g = getObj("graphic", item.id);
if (!g) return null;
const name = g.get("name");
if (name) return name;
const char = getObj("character", g.get("represents"));
if (!char) return null;
return char.get("name");
};
const findPrefixIndex = (turns, prefix) =>
turns.findIndex((t) => {
const name = itemName(t);
if (!name) return false;
return name.toLowerCase().startsWith(prefix);
});
const addWithFormula = (msg, isGM, playerID, formula) => {
const parts = msg.split(/\s+/);
parts.shift();
const newItem = { id: "-1", pr: parseFloat(parts.shift()), formula };
if (!isGM) newItem.p = true;
let position = null;
let search = null;
if (parts[0].startsWith("--")) {
position = parts.shift().substring(2);
search = parts.shift().toLowerCase();
}
newItem.custom = parts.join(" ");
let turns = getTurns();
let i = null;
if (search) {
i = findPrefixIndex(turns, search);
if (i == -1) {
i = null;
whisperToID(playerID, `could not find item prefix “${search}”. Putting “${newItem.custom}” at the end.`);
} else if (position === "after") i++;
}
if (i !== null) turns.splice(i, 0, newItem);
else turns.push(newItem);
setTurns(turns);
if (!isGM) {
const name = playerName(playerID) || "";
let pos = "";
if (i !== null) pos = ` in position ${i + 1}`;
whisperToID("GM", `Player (${name}) added turn item “${newItem.custom}${pos}”`);
}
};
const handleClear = (msg, isGM, playerID) => {
if (!isGM) {
whisperToID(playerID, "Only the GM can clear turn data.");
return;
}
const turns = Campaign().get("turnorder");
setTurns([]);
log(`ChatTurnManager: CLEARING: ${turns}`);
whisperToID("GM", `Turns cleared. To restore, run <code>!turns-load ${turns}</code>`);
};
const handleLoad = (msg, isGM, playerID) => {
if (!isGM) {
whisperToID(playerID, "Only the GM can load turn data.");
return;
}
Campaign().set("turnorder", msg.split(/\s+/, 2)[1]);
};
const handleAppend = (msg, isGM, playerID) => {
if (!isGM) {
whisperToID(playerID, "Only the GM can append turn data.");
return;
}
try {
const data = JSON.parse(msg.split(/\s+/, 2)[1]);
turns = getTurns();
setTurns(turns.concat(data));
} catch (e) {
whisperToID(playerID, `ERROR appending data: '${e.message}'`);
}
};
const handleClean = (msg) => {
let turns = getTurns();
turns = _.filter(turns, (t) => t.pr <= 0);
setTurns(turns);
};
const handleBegin = (msg, isGM) => {
if (!isGM) {
whisperToID(playerID, "Only the GM can start the counter.");
return;
}
let turns = getTurns();
turns = _.filter(turns, (t) => t.custom !== counterName);
turns = _.sortBy(turns, (t) => -t.pr);
turns.unshift({ id: "-1", custom: counterName, pr: 1, formula: "+1" });
setTurns(turns);
};
const handleUp = (msg, isGM, playerID) => {
addWithFormula(msg, isGM, playerID, "+1");
};
const handleDown = (msg, isGM, playerID) => {
addWithFormula(msg, isGM, playerID, "-1");
};
const handleRemove = (msg, isGM, playerID) => {
const parts = msg.split(/\s+/, 2);
const prefix = parts[1];
if (!prefix) {
whisperToID(playerID, `missing item to remove!`);
return;
}
const turns = getTurns();
const i = findPrefixIndex(turns, prefix);
if (i === -1) {
whisperToID(playerID, `Cannot find prefix “${prefix}” to remove.`);
return;
}
if (isGM || turns[i].p) {
turns.splice(i, 1);
setTurns(turns);
return;
}
const name = itemName(turns[i]) || "that item";
whisperToID(playerID, `You do not have permission to remove ${name}. Please ask the GM to do it.`);
};
const handlers = {
handleClear,
handleLoad,
handleAppend,
handleClean,
handleBegin,
handleUp,
handleDown,
handleRemove,
handleStart: handleBegin,
handleRm: handleRemove,
};
const handleMessage = (msg) => {
if (msg.type != "api" || !msg.content.startsWith("!turns")) return;
const cmd = msg.content.split(/\s+/)[0].substring(7);
const handler = handlers[`handle${cmd.charAt(0).toUpperCase()}${cmd.slice(1)}`];
if (handler) {
handler(msg.content, playerIsGM(msg.playerid), msg.playerid);
return;
}
log(`ChatTurnManager: unknown cmd: ${cmd}`);
whisperToID(playerID, `Unknown command: ${cmd}`);
};
const registerHandlers = () => {
on("chat:message", handleMessage);
};
const notifyStart = () => {
log(`ChatTurnManager ${version} Loading.`);
};
return {
notifyStart: notifyStart,
registerHandlers: registerHandlers,
};
})();
on("ready", () => {
"use strict";
ChatTurnManager.notifyStart();
ChatTurnManager.registerHandlers();
});