forked from joske2865/AMQ-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathamqDiceRollerUI.user.js
391 lines (359 loc) · 11.9 KB
/
amqDiceRollerUI.user.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
390
391
// ==UserScript==
// @name AMQ Dice Roller UI
// @namespace https://github.com/TheJoseph98
// @version 1.1.4
// @description Adds a window where you can roll dice
// @author TheJoseph98
// @match https://animemusicquiz.com/*
// @grant none
// @require https://raw.githubusercontent.com/TheJoseph98/AMQ-Scripts/master/common/amqScriptInfo.js
// @require https://raw.githubusercontent.com/TheJoseph98/AMQ-Scripts/master/common/amqWindows.js
// @updateURL https://github.com/TheJoseph98/AMQ-Scripts/raw/master/amqDiceRollerUI.user.js
// ==/UserScript==
// don't load on login page
if (document.getElementById("startPage")) return;
// Wait until the LOADING... screen is hidden and load script
let loadInterval = setInterval(() => {
if (document.getElementById("loadingScreen").classList.contains("hidden")) {
setup();
clearInterval(loadInterval);
}
}, 500);
let diceRolls = {};
let diceWindow;
let diceManagerWindow;
function createDiceWindow() {
diceWindow = new AMQWindow({
id: "diceWindow",
title: "Dice",
width: 200,
height: 225,
zIndex: 1055,
draggable: true
});
diceWindow.addPanel({
width: 1.0,
height: 50,
id: "diceSelectContainer"
});
diceWindow.addPanel({
width: 1.0,
height: 50,
position: {
x: 0,
y: 50
},
id: "diceRollButtonContainer"
});
diceWindow.addPanel({
width: 1.0,
height: 50,
position: {
x: 0,
y: 100
},
id: "diceChoiceContainer"
});
diceWindow.panels[0].panel.append($(`<select id="diceSelect"></select>`));
for (let key in diceRolls) {
$("#diceSelect").append($(`<option>${key}</option>`).attr("value", key));
}
diceWindow.panels[1].panel
.append($(`<button class="btn btn-primary" type="button">Roll</button>`)
.click(function () {
rollDice($("#diceSelect").val());
})
)
.append($(`<button class="btn btn-default" type="button">Edit</button>`)
.click(function () {
if (diceManagerWindow.isVisible()) {
diceManagerWindow.close();
}
else {
diceManagerWindow.open();
}
})
)
diceWindow.panels[2].panel.append($(`<span id="diceChoice"></span>`));
let oldWidth = $("#qpOptionContainer").width();
$("#qpOptionContainer").width(oldWidth + 35);
$("#qpOptionContainer > div").append($(`<div id="qpDiceRoll" class="clickAble qpOption"><i aria-hidden="true" class="fa fa-cube qpMenuItem"></i></div>`)
.click(function () {
if(diceWindow.isVisible()) {
diceWindow.close();
}
else {
diceWindow.open();
}
})
.popover({
placement: "bottom",
content: "Dice Roller",
trigger: "hover"
})
);
}
function createDiceManagerWindow() {
diceManagerWindow = new AMQWindow({
id: "diceManagerWindow",
title: "Dice Editor",
width: 500,
height: 350,
minWidth: 500,
minHeight: 350,
draggable: true,
resizable: true,
zIndex: 1052
});
diceManagerWindow.addPanel({
width: 200,
height: 1.0,
id: "diceKeysManager"
});
diceManagerWindow.addPanel({
width: "calc(100% - 200px)",
height: 50,
position: {
x: 200,
y: 0
},
id: "diceValuesManager"
});
diceManagerWindow.addPanel({
width: "calc(100% - 200px)",
height: "calc(100% - 50px)",
position: {
x: 200,
y: 50
},
scrollable: {
x: false,
y: true
}
});
diceManagerWindow.panels[0].panel
.append($(`<div class="diceManagerInputContainer"></div>`)
.append($(`<select id="diceManagerSelect"></select>`)
.change(function () {
displayValues($(this).val());
})
)
.append($(`<button class="btn btn-default" type="button">Remove</button>`)
.click(function () {
let selectedKey = $("#diceManagerSelect").val();
delete diceRolls[selectedKey];
$(`#diceManagerSelect > option`).filter((index, elem) => $(elem).attr("value") === selectedKey).remove();
$(`#diceSelect > option`).filter((index, elem) => $(elem).attr("value") === selectedKey).remove();
displayValues($("#diceManagerSelect").val());
saveDice();
})
)
)
.append($(`<div class="diceManagerInputContainer"></div>`)
.append($(`<input id="diceManagerKeyInput" type="text">`))
.append($(`<button type="button" class="btn btn-primary">Add</button>`)
.click(function () {
let newKey = $("#diceManagerKeyInput").val();
diceRolls[newKey] = [];
$("#diceSelect").append($(`<option>${newKey}</option>`).attr("value", newKey));
$("#diceManagerSelect").append($(`<option>${newKey}</option>`).attr("value", newKey));
$("#diceManagerSelect").val(newKey);
displayValues(newKey);
saveDice();
})
)
.append($(`<button type="button" class="btn btn-default">Rename</button>`)
.click(function () {
let oldKey = $("#diceManagerSelect").val();
let newKey = $("#diceManagerKeyInput").val();
if (oldKey === newKey) {
return;
}
diceRolls[newKey] = diceRolls[oldKey];
delete diceRolls[oldKey];
$(`#diceManagerSelect > option`).filter((index, elem) => $(elem).attr("value") === oldKey).attr("value", newKey).text(newKey);
$(`#diceSelect > option`).filter((index, elem) => $(elem).attr("value") === oldKey).attr("value", newKey).text(newKey);
displayValues(newKey);
saveDice();
})
)
)
for (let key in diceRolls) {
$("#diceManagerSelect").append($(`<option>${key}</option>`).attr("value", key));
}
diceManagerWindow.panels[1].panel
.append($(`<div id="diceValueInputContainer"></div>`)
.append($(`<input id="diceManagerValueInput" type="text">`))
.append($(`<button type="button" class="btn btn-primary">Add</button>`)
.click(function () {
let newValue = $("#diceManagerValueInput").val();
let selectedKey = $("#diceManagerSelect").val();
if (selectedKey !== null) {
diceRolls[selectedKey].push(newValue);
displayValue(newValue);
saveDice();
}
})
)
)
displayValues($("#diceManagerSelect").val());
}
function rollDice(key) {
if (key === null) {
$("#diceChoice").text("");
return;
}
if (diceRolls[key].length === 0) {
$("#diceChoice").text("");
return;
}
let randomIdx = Math.floor(Math.random() * diceRolls[key].length);
let randomChoice = diceRolls[key][randomIdx];
$("#diceChoice").text(randomChoice);
}
function clearValues() {
diceManagerWindow.panels[2].panel.children().remove();
}
function displayValues(key) {
clearValues();
if (key === null) {
return;
}
for (let value of diceRolls[key]) {
displayValue(value);
}
}
function displayValue(newValue) {
diceManagerWindow.panels[2].panel
.append($(`<div class="diceManagerValueContainer"></div>`)
.append($(`<span></span>`)
.text(newValue)
)
.append($(`<button type="button" class="btn btn-default">Remove</button>`)
.click(function () {
let selectedKey = $("#diceManagerSelect").val();
let index = diceRolls[selectedKey].indexOf(newValue);
if (index !== -1) diceRolls[selectedKey].splice(index, 1);
$(this).parent().remove();
saveDice();
})
)
)
}
function saveDice() {
localStorage.setItem("amqDice", JSON.stringify(diceRolls));
}
function loadDice() {
let savedDice = localStorage.getItem("amqDice");
if (savedDice !== null) {
diceRolls = JSON.parse(savedDice);
}
}
function setup() {
loadDice();
createDiceManagerWindow();
createDiceWindow();
$(".modal").on("show.bs.modal", () => {
diceWindow.setZIndex(1025);
diceManagerWindow.setZIndex(1022);
});
$(".modal").on("hidden.bs.modal", () => {
diceWindow.setZIndex(1055);
diceManagerWindow.setZIndex(1052);
});
$(".slCheckbox label").hover(() => {
diceWindow.setZIndex(1025);
diceManagerWindow.setZIndex(1022);
}, () => {
diceWindow.setZIndex(1055);
diceManagerWindow.setZIndex(1052);
});
AMQ_addScriptData({
name: "Dice Roller UI",
author: "TheJoseph98",
description: `
<p>Adds a window where you can select a dice and roll a random value associated with that dice</p>
<p>This window can be opened by clicking the cube icon at the top right while in quiz (there is no dice icon available, blame Egerod)</p>
<a href="https://i.imgur.com/e0ZmrQY.png" target="_blank"><img src="https://i.imgur.com/e0ZmrQY.png" /></a>
<a href="https://i.imgur.com/xElzT2s.png" target="_blank"><img src="https://i.imgur.com/xElzT2s.png" /></a>
`
});
AMQ_addStyle(`
#qpDiceRoll {
width: 30px;
margin-right: 5px;
}
#diceWindow .customWindowPanel {
border-bottom: 1px solid #6d6d6d;
}
#diceSelectContainer {
line-height: 50px;
text-align: center;
}
#diceSelect {
color: black;
}
#diceRollButtonContainer {
line-height: 50px;
text-align: center;
}
#diceRollButtonContainer > button {
margin: 0px 5px;
}
#diceChoiceContainer {
display: table;
text-align: center;
}
#diceChoice {
display: table-cell;
vertical-align: middle;
font-size: 16px;
}
#diceKeysManager {
border-right: 1px solid #6d6d6d;
}
#diceValuesManager {
border-bottom: 1px solid #6d6d6d;
}
.diceManagerInputContainer {
margin: 10px 0px 20px 0px;
text-align: center;
}
.diceManagerInputContainer > button {
width: 66px;
padding: 6px;
margin: 5px 5px;
}
#diceManagerSelect {
color: black;
width: 170px;
margin: 5px 0px;
}
#diceManagerKeyInput {
color: black;
width: 170px;
margin: 5px;
}
#diceValueInputContainer {
text-align: center;
padding: 8px;
}
#diceManagerValueInput {
color: black;
}
#diceValueInputContainer > * {
margin: 0px 6px;
}
.diceManagerValueContainer {
height: 31px;
margin: 10px 10px 0px 10px;
}
.diceManagerValueContainer > span {
font-size: 22px;
}
.diceManagerValueContainer > button {
float: right;
padding: 6px;
}
`);
}